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

![SoundLoation.png](https://qiita-image-store.s3.amazonaws.com/0/58907/33eea609-a0a4-f895-ba91-fab3fddd05f7.png)

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

![soundLocparam.png](https://qiita-image-store.s3.amazonaws.com/0/58907/5dcb703b-4b3e-4e3a-99c4-e3fb3f88ccbc.png)
![moveTowardParampng.png](https://qiita-image-store.s3.amazonaws.com/0/58907/8faaf4c5-a894-b50f-6e6b-6aee33a16222.png)
「Wait3」boxは、60秒。「Wait4」boxは3秒で設定しました。

「Move Toward」boxにinputを一つ追加。(inputの付近で右クリックで、AddInputすれば画面でます。)
![inputxy.png](https://qiita-image-store.s3.amazonaws.com/0/58907/87fa8219-6e8b-bc54-bedf-d0ff6839723b.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()

以上