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