eacousineau

ALProxy.wait() and stop() workaround

Jul 11th, 2011
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. # NOTE: This isn't working so well with multithreading
  2.  
  3. from naoqi import *
  4. import time
  5. from threading import Thread, Event
  6.  
  7. naoIp = "0.0.0.0"
  8. naoPort = 9559
  9.  
  10. motion = ALProxy("ALMotion", naoIp, naoPort)
  11.  
  12. def testBlocking():
  13.     mId = motion.post.angleInterpolation(["LHand"], [[0, 1, 0]], [[1, 3, 7]], True)
  14.     event = Event()
  15.     def waitAndKill():
  16.         time.sleep(2)
  17.         print "Killing"
  18.         motion.stop(mId)
  19.         event.set()
  20.     def waitUntilDone():
  21.         while motion.isRunning(mId):
  22.             # This is extremely costly...
  23.             pass
  24.         event.set()
  25.     # If true, will show wait functionality. Set to false to test basics.
  26.     useThread = True
  27.     if useThread:
  28.         # This will use ALProxy.wait(), which will not kill task until it is finished
  29.         useWait = False
  30.         print "Waiting..."
  31.         killThread = Thread()
  32.         killThread.run = waitAndKill
  33.         killThread.start()
  34.         if useWait:
  35.             motion.wait(mId, 0)
  36.         else:
  37.             monitorThread = Thread()
  38.             monitorThread.run = waitUntilDone
  39.             monitorThread.start()
  40.             event.wait()
  41.             # For reuse: event.clear()
  42.         print "Done"
  43.     else:
  44.         waitAndKill()
  45.  
  46. testBlocking()
Advertisement
Add Comment
Please, Sign In to add comment