所以我有使用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()
,它不,它只是向右轉,居中,並來回。
在self.turnLeft()之後不應該有'time.sleep(5) '在'redBall'中?因爲現在,當您調用turnLeft時,循環立即繼續,並再次調用turnRight。這意味着它沒有時間做左轉。 – 2012-04-04 13:28:37
哈哈,這真的很無聊,但它是真實的,現在它正在工作,你可以把它放在正確的答案,所以我可以驗證它。 – Tsunaze 2012-04-04 13:44:42
沒問題,我添加了幾乎相同內容的答案。 – 2012-04-04 13:52:02