而循环和Python与NAO

时间:2012-04-04 13:00:18

标签: python while-loop nao-robot

所以我有一个Python代码,我试图让它与NAO(Aldebaran Robotics)合作

import time
class MyClass(GeneratedClass):
    def __init__(self):
        GeneratedClass.__init__(self)
        self.motion = ALProxy("ALMotion")
        self.maxTour = 3
        self.reponse = False

    def onLoad(self):
        #~ puts code for box initialization here
        self.tournerDroite()
        time.sleep(5)
        #detect ball
        self.tournerCentre()
        time.sleep(5)
         #detect ball
        self.turnLeft()
         #detect ball
        #self.notInCenter()
        #self.redBall()
        pass

    def onUnload(self):
        #~ puts code for box cleanup here
        pass

    def onInput_onStart(self, ):
        #~ self.onStopped() #~ activate output of the box
        pass

    def onInput_onStop(self):
        self.onUnload() #~ it is recommanded to call onUnload of this box in a onStop method, as the code written in onUnload is used to stop the box as well
        pass

    def turnRight(self):
        self.motion.setStiffnesses("HeadYaw", 0)
        self.motion.setAngles("HeadYaw", -0.5, 0.05)
        self.motion.setStiffnesses("HeadYaw", 1)
        pass
    def turnLeft(self):
        self.motion.setStiffnesses("HeadYaw", 0)
        self.motion.setAngles("HeadYaw", 0.5, 0.05)
        self.motion.setStiffnesses("HeadYaw", 1)
        pass
    def turnCenter(self):
        self.motion.setStiffnesses("HeadYaw", 0)
        self.motion.setAngles("HeadYaw", 0, 0.05)
        self.motion.setStiffnesses("HeadYaw", 1)
        pass

    def notInCenter(self):
        if(self.motion.getAngles("HeadYaw", True) != 0):
            self.turnCenter()
            return True
        else:
            return False
        pass

    def redBall(self):
        while self.reponse == False:
            self.turnRight()
            time.sleep(5)
            #detect ball

            self.turnCenter()
            time.sleep(5)
             #detect ball
            self.turnLeft()
             #detect ball
        pass

问题是在onLoad()中,机器人转向右转,然后是中心,然后是左转,但是当我使用redBall()时,它没有,它只是右转和中心,来回走动。

2 个答案:

答案 0 :(得分:0)

time.sleep(5)中的self.turnLeft()之后应该redBall

当你调用turnLeft时,循环立即继续,它再次调用turnRight。这意味着它没有时间向左转弯。这就是为什么它做右转和中转但没有左转。

答案 1 :(得分:0)

好像你正在以反向方式使用setStiffnesses:0取消电机的电源,1设置电源。

所以:         self.motion.setStiffnesses(“HeadYaw”,0)         self.motion.setAngles(“HeadYaw”, - 0.5,5.05)         self.motion.setStiffnesses(“HeadYaw”,1)

正在移除电源,然后转头,但什么都不做,因为没有电源,然后设置电源=>没有