The following is a pretty normal control loop for robot control (or other embedded devices). There are probably some syntactical errors as I&#39;m writing this without a working python interpreter available at this second, but it should give you the correct gist to do what you said you were trying to do.<br>
<br>You may want something like:<br>#robotcontrol.py<br>import time<br><br>class OperationalStep(object):<br>&nbsp;&nbsp; def __init__(self,func,interval=0):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.func = func<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.interval = interval<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.lastRun = getTime()<br>
<br>&nbsp;&nbsp; def run():<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;If the step hasn&#39;t been run recently enough, do so&quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if self.interval + self.lastRun &lt; getTime():<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.func()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.lastRun = getTime()<br>
<br>def unknownEvent():<br>&nbsp; print( &quot;This is an undefined event code&quot; )<br><br>events = collections.defaultdict(unknownEvent)<br><br>def getTime():<br>&nbsp;&nbsp; &quot;&quot;&quot;returns time&quot;&quot;&quot;<br>&nbsp; return time.clock()<br>
<br>def registerEventHandler(eventCode,handler):<br>&nbsp;&nbsp; &quot;&quot;&quot;When event eventCode is seen, run handler&quot;&quot;&quot;<br>&nbsp;&nbsp; global events<br>&nbsp;&nbsp; events[eventCode] = handler<br><br>def registerOperationalStep(step,delayAtLeast=0):<br>
&nbsp;&nbsp; &quot;&quot;&quot;Registers a step that will be taken in the program no more often than delayAtLeast often&quot;&quot;&quot;<br>&nbsp;&nbsp; global tasks<br>&nbsp;&nbsp; t = OperationalStep(step,delayAtLeast)<br>&nbsp;&nbsp; tasks.append(t)<br><br>
eventQueue = []<br>def addEvent(ev):<br>&nbsp;&nbsp; global eventQueue<br>&nbsp;&nbsp; eventQueue.append(ev)<br><br>def robotLoop():<br>&nbsp; &quot;&quot;&quot;runs each task, handling events as possible&quot;&quot;&quot;<br>&nbsp;&nbsp; global eventQueue,tasks<br>
&nbsp;&nbsp; while True:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for task in tasks:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #process events<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for ev in eventQueue:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; events[ev]()<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #run next task<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; task.run()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br><br>All of this would be put in its own module, say robotcontrol, that would then be used thusly in a complete separate file:<br>
<br>#myrobot.py<br>import robotcontrol<br>from madeupRobotApi import *<br>from time import sleep<br><br><br>def checkBump():<br>&nbsp;&nbsp; &quot;&quot;&quot;Checks to see if the robot bump bar is pushed in&quot;&quot;&quot;<br>&nbsp;&nbsp; if bump.pressed():<br>
&nbsp;&nbsp;&nbsp;&nbsp; robotcontrol.addEvent(BUMP_EVENT)<br><br>def handleSonarData():<br>&nbsp;&nbsp; &quot;&quot;&quot;gets data from the sonar&quot;&quot;&quot;<br>&nbsp;&nbsp; data = sonar.read()<br>&nbsp;&nbsp; print (data)<br><br>def handleCoreMeltdown():<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;turns off the core&quot;&quot;&quot;<br>
&nbsp;&nbsp;&nbsp; powercore.off()<br><br>def turnLeft():<br>&nbsp;&nbsp; &quot;&quot;turns the robot 90 degrees left&quot;<br>&nbsp;&nbsp; lwheels.run(-10)<br>&nbsp;&nbsp; rwheels.run(10)<br>&nbsp;&nbsp; sleep(10)<br>&nbsp;&nbsp; lwheels.run(0)<br>&nbsp;&nbsp; rwheels.run(0)<br>&nbsp;&nbsp; checkBump()<br>
<br>def turnRight():<br>&nbsp;&nbsp; &quot;&quot;turns the robot 90 degrees right&quot;<br>&nbsp;&nbsp; lwheels.run(10)<br>&nbsp;&nbsp; rwheels.run(-10)<br>&nbsp;&nbsp; sleep(10)<br>&nbsp;&nbsp; lwheels.run(0)<br>&nbsp;&nbsp; rwheels.run(0)<br>&nbsp;&nbsp; checkBump()<br><br>def goForward(speed=10,t=10):<br>
&nbsp; &quot;&quot;&quot;goes forward at speed for time t&quot;&quot;&quot;<br>&nbsp; lwheels.run(speed)<br>&nbsp; rwheels.run(speed)<br>&nbsp; sleep(t)<br>&nbsp; lwheels.run(0)<br>&nbsp; rwheels.run(0)<br>&nbsp; checkBump()<br><br>def goForwardALittle()<br>
&nbsp; goForward(10,1)<br>&nbsp; checkBump()<br><br>def checkSensors()<br>&nbsp; if sonar.ready():<br>&nbsp;&nbsp;&nbsp; robotcontrol.addEvent(SONAR_EVENT)<br>&nbsp; if core.tooHot():<br>&nbsp;&nbsp;&nbsp; robotcontrol.addEvent(OHNO_EVENT)<br><br>direction = &quot;RIGHT&quot;<br>
<br>def decideNewDirection():<br>&nbsp;&nbsp; global direction<br>&nbsp;&nbsp; if sonar.data == &quot;heavy&quot;:<br>&nbsp;&nbsp;&nbsp;&nbsp; direction = &quot;LEFT&quot;<br>&nbsp;&nbsp; else:<br>&nbsp;&nbsp;&nbsp;&nbsp; direction = &quot;RIGHT&quot; <br><br>def handleBump()<br>&nbsp;&nbsp; &quot;&quot;&quot;back up upon bump&quot;&quot;&quot;<br>
&nbsp;&nbsp; if bump.pressed():<br>&nbsp;&nbsp;&nbsp;&nbsp; goForward(speed=-10,t=10)<br><br>def turn():<br>&nbsp;&nbsp; &quot;&quot;&quot;turns the direction specified in direction&quot;&quot;&quot;<br>&nbsp;&nbsp; global direction<br>&nbsp;&nbsp; if direction == &quot;LEFT&quot;:<br>
&nbsp;&nbsp;&nbsp;&nbsp; turnLeft()<br>&nbsp;&nbsp; else:<br>&nbsp;&nbsp;&nbsp;&nbsp; turnRight()<br>&nbsp;&nbsp; <br><br>def robotSetup():<br>&nbsp;&nbsp; &quot;&quot;&quot;sets all the event handlers and defines the operational program&quot;&quot;&quot;<br><br>&nbsp;&nbsp; #these will happen as things generate events<br>
&nbsp;&nbsp; registerEventHandler(SONAR_EVENT,handleSonarData)<br>&nbsp;&nbsp; registerEventHandler(OHNO_EVENT,handleCoreMeltdown)<br>&nbsp;&nbsp; registerEventHandler(BUMP_EVENT,handleBump)<br><br>&nbsp;&nbsp; #robot control scheme<br>&nbsp;&nbsp; registerOperationalStep(checkSensors,10)<br>
&nbsp;&nbsp; registerOperationalStep(decideNewDirection)<br>&nbsp;&nbsp; registerOperationalStep(turn)<br>&nbsp;&nbsp; registerOperationalStep(goForwardALittle)<br>&nbsp;&nbsp; registerOperationalStep(decideNewDirection)<br>&nbsp;&nbsp; registerOperationalStep(turn)<br>&nbsp;&nbsp; <br>
&nbsp;&nbsp; <br>if &quot;__name__&quot;==__main__:<br>&nbsp; robotSetup()<br>&nbsp; robotcontrol.robotLoop()<br>&nbsp;&nbsp; <br>-- <br>Michael Langford<br>404-386-0495<br><a href="http://www.RowdyLabs.com">http://www.RowdyLabs.com</a><br>&nbsp; <br><br><br>