Trying to have a infinit loop in "go forward", which would stop when "kill the motors" is called

 

def heard(data):
    data = msg_i01_ear_recognized.data[0]
    
    if (data == "go forward"):
          forwardServo.moveTo(60)
          x = 1
          while True:
              i01.setHandSpeed("left", 1.0, 1.0, 1.0, 1.0, 1.0, 1.0)
              i01.setHandSpeed("right", 1.0, 1.0, 1.0, 1.0, 1.0, 1.0)
              i01.setArmSpeed("right", 0.95, 0.95, 0.95, 0.85)
              i01.setArmSpeed("left", 0.95, 0.95, 0.95, 0.85)
              i01.setHeadSpeed(0.75, 0.75)
              i01.moveHead(70,79,85,85,65)
              i01.moveArm("left",5,90,23,10)
              i01.moveArm("right",15,90,30,10)
              i01.moveHand("left",92,33,37,71,66,25)
              i01.moveHand("right",81,66,82,60,105,113)
              i01.moveTorso(75,97,90)
              sleep(2)
              i01.moveHead(79,100,85,85,65)
              i01.moveArm("left",15,84,36,15)
              i01.moveArm("right",5,82,22,20)
              i01.moveHand("left",92,33,37,71,66,25)
              i01.moveHand("right",81,66,82,60,105,113)
              i01.moveTorso(124,83,90)
              sleep(2)
              x += 1
        

    if (data == "kill the motor"):
          forwardServo.moveTo(93)
          relax()
          break

 

In this exemple I made the "break" function is out of the loop, and I have been reading stuff, but I'm getting tired and I implore the good Elves of MyRobotLab for help.

kwatters

9 years 6 months ago

I think something like this is what you want.  
Here I create a Thread class called  the "WalkingThread" 
It has a method called "run"  that is where we put the loop.
I added a parameter called "this.running"  if that is false, the loop breaks out and the thread exits.
 
In the "heard" method, the "go forward" command starts the thread.  the "kill the motor" command tells the thread to stop.  (There is probably a better way to stop/kill the thread, when I have a chance to test this out, I'll clean that up.)
 
[[home/kwatters/walkingThread.py]]