2012-04-04 135 views
0

所以我有使用Python守则,我要把它与NAO(Aldebaran Robotics公司)工作While循环和Python与NAO

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(),它不,它只是向右转,居中,并来回。

+0

在self.turnLeft()之后不应该有'time.sleep(5) '在'redBall'中?因为现在,当您调用turnLeft时,循环立即继续,并再次调用turnRight。这意味着它没有时间做左转。 – 2012-04-04 13:28:37

+0

哈哈,这真的很无聊,但它是真实的,现在它正在工作,你可以把它放在正确的答案,所以我可以验证它。 – Tsunaze 2012-04-04 13:44:42

+0

没问题,我添加了几乎相同内容的答案。 – 2012-04-04 13:52:02

回答

0

应该有time.sleep(5)self.turnLeft()redBall

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

0

似乎你正在以反向方式使用setStiffnesses:0消除电机的功率,1设置功率。

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

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