While loop and Python with NAO
-
29-05-2021 - |
سؤال
So i've got a Code with Python and i'm trying to make it work with 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
The problem is that in onLoad()
, the robot turn is head Right, then Center, then Left but when i use the redBall()
, it doesn't, it just turn right and center, and go back and forth.
المحلول
There should be time.sleep(5)
after self.turnLeft()
in redBall
.
When you call turnLeft, the loop immediately continues and it calls turnRight again. That means it won't have time to do the turn to the left. That's why it does a right and center turn but no left turn.
نصائح أخرى
Seems like you're using setStiffnesses in an inverted way: 0 remove the power from the motor, and 1 set the power.
So: self.motion.setStiffnesses("HeadYaw", 0) self.motion.setAngles("HeadYaw", -0.5, 0.05) self.motion.setStiffnesses("HeadYaw", 1)
is removing the power, then turn head, but do nothing because there's no power, then set the power => nothing