20
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pepperハッカソン 音の方向にPepperが動いてくれる

Posted at

SoundLoation.png

今日のハッカソンの、音の方向にPepperが動いてくれるBoxを忘れないうちにメモ。各パラメータは以下。

soundLocparam.png
moveTowardParampng.png
「Wait3」boxは、60秒。「Wait4」boxは3秒で設定しました。

「Move Toward」boxにinputを一つ追加。(inputの付近で右クリックで、AddInputすれば画面でます。)
inputxy.png
そして、最後に「Move Toward」boxをダブルクリックして、

頭に

import math

を追加。そして

def onInput_inputxy(self, p):
    self.lock.acquire()
    try:
        self.test = p[0]
        self.shouldRun = True
        self.updateMovement()

    finally:
        self.lock.release()

を追加して

def updateMovement(self):の
        #x = self.getParameter("X")
        #y = self.getParameter("Y")
        x = math.cos(self.test)
        y = math.sin(self.test)

の書き換えをすれば完了です。

全体のソースは以下に。ただ時間内に動かす目的だけに作ったのでご了承ください。

import time
import math
import threading

class MyClass(GeneratedClass):
def init(self):
GeneratedClass.init(self, False)

def onLoad(self):
    self.motion = ALProxy("ALMotion")
    self.shouldRun = False
    self.x = 0
    self.y = 0
    self.theta = 0
    self.timer = None
    self.lock = threading.RLock()

def onUnload(self):
    self.lock.acquire()
    try:
        self.shouldRun = False
        if self.timer:
            self.timer.cancel()
            self.timer = None

        self.x = 0
        self.y = 0
        self.theta = 0
        self.motion.moveToward(0, 0, 0)
        self.motion.waitUntilMoveIsFinished()
    finally:
        self.lock.release()

def onInput_onStop(self):
    self.lock.acquire()
    try:
        self.onUnload()
        self.onStopped()
    finally:
        self.lock.release()

def onInput_onStart(self):
    self.lock.acquire()
    try:
        self.shouldRun = True
        self.updateMovement()
    finally:
        self.lock.release()

def onInput_inputxy(self, p):
    self.lock.acquire()
    try:
        self.test = p[0]
        self.shouldRun = True
        self.updateMovement()

    finally:
        self.lock.release()

def updateMovement(self):
    self.lock.acquire()
    try:
        if self.timer:
            self.timer.cancel()
            self.timer = None
        enableArms = self.getParameter("Arms movement enabled")
        self.motion.setMoveArmsEnabled(enableArms, enableArms)
        #x = self.getParameter("X")
        #y = self.getParameter("Y")
        x = math.cos(self.test)
        y = math.sin(self.test)
        theta = self.getParameter("Theta")
        period = self.getParameter("Period of direction update (s)")
        epsilon = 0.0001
        dx = math.fabs(x - self.x)
        dy = math.fabs(y - self.y)
        dt = math.fabs((theta - self.theta))
        if(dx > epsilon or dy > epsilon or dt > epsilon):
            self.x=x
            self.y=y
            self.theta=theta
            self.motion.moveToward(self.x, self.y, self.theta)

        if self.shouldRun:
            self.timer = threading.Timer(period, self.updateMovement)
            self.timer.start()
    finally:
        self.lock.release()

以上

20
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?