pg.key.set_repeat() # game loop while self._running: # handle events for event in pg.event.get(): self.on_event(event) # update game state self.on_loop() # render or flush outputs self.on_render() self.on_cleanup() def keyUpdate(kold, knew): res = [] for i in range(len(knew)): if kold[i] != knew[i]: res.append(pg.key.name(i) + ['u', 'd'][knew[i]]) return res if __name__ == "__main__": xb = Xbee(SRV_IP, SRV_PT) xb.connect() theApp = App(xb.sk) theApp.on_execute() xb.close()
class Serialcom(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.shutdown_flag = threading.Event() self.motor = Motorcontroller() self.buzzer = Buzzer() self.xbee = Xbee() self.decoder = Decoder() self.servo1 = 96 self.servo2 = 75 self.joycalc = Joystick() self.motor.setServo1(self.servo1) self.motor.setServo2(self.servo2) self.lastSavedTime = 0 def run(self): print('Thread #%s started' % self.ident) self.motor.timeout(1) while not self.shutdown_flag.is_set(): rcvdata = self.xbee.read() self.decoder.decode(rcvdata) self.motor.recalCommand() currenttime = time.time() if currenttime - self.lastSavedTime > 1.0: self.lastSavedTime = time.time() self.xbee.sendBat(self.decoder.getRfrating()) if self.decoder.getStatus() and self.decoder.checkCRC(): if self.decoder.getJoyStickPB1() == 0: self.motor.EmergyStop() self.buzzer.beep(300) elif self.decoder.getJoystickM1() > 248 and self.decoder.getJoystickM2() > 248: self.joycalc.calculateReg(255) self.motor.Motor1MC2(255 - self.joycalc.cor1) self.motor.Motor2MC2(255 - self.joycalc.cor2) elif (abs(self.decoder.getJoystickM1() - self.decoder.getJoystickM2()) <= 3) and (self.decoder.getJoystickM1() > 50): self.joycalc.calculateReg(self.decoder.getJoystickM1()) self.motor.Motor1MC2(self.decoder.getJoystickM1() - self.joycalc.cor1) self.motor.Motor2MC2(self.decoder.getJoystickM1() - self.joycalc.cor2) #print "drive forward without full speed" else: self.motor.Motor1MC2(self.decoder.getJoystickM1()) self.motor.Motor2MC2(self.decoder.getJoystickM2()) #print "other speeds" if self.decoder.getJoystickPB2() == 0: self.servo1 = 96 self.motor.setServo1(self.servo1) self.buzzer.beep(300) elif self.decoder.getJoystickVRX2() > 1000: if(self.servo1 > 0): self.servo1 = self.servo1 - 1 self.motor.setServo1(self.servo1) elif self.decoder.getJoystickVRX2() < 24: if(self.servo1 < 180): self.servo1 = self.servo1 + 1 self.motor.setServo1(self.servo1) if self.decoder.getJoystickPB2() == 0: self.servo2 = 75 self.motor.setServo2(self.servo2) elif self.decoder.joystick_VRY2 > 1000: if(self.servo2 > 0): self.servo2 = self.servo2 - 1 self.motor.setServo2(self.servo2) elif self.decoder.getJoystickVRY2() < 24: if(self.servo2 < 180): self.servo2 = self.servo2 + 1 self.motor.setServo2(self.servo2) time.sleep(0.001) # ... Clean shutdown code here ... self.xbee.close() self.motor.close() print('Thread #%s stopped' % self.ident)