def __init__(self, gps_rate=None): # initialize objects self._gui = gui.GUI() self._buttons = buttons.Buttons(on_green=self.green_callback, on_blue=self.blue_callback, on_red=self.red_callback) self.rs = RobotState() print("The first state in the state machine is: %s" % rs.state) self._gps = gps.GPS()
def __init__(self, gps_rate=None): # initialize objects self.stop_blink = threading.Event() self._gui = gui.GUI() self._buttons = buttons.Buttons(on_green=self.green_callback, on_blue=self.blue_callback, on_red=self.red_callback) self.rs = RobotState() print("The first state in the state machine is: %s" % self.rs.state) print("Mac Address: " + gma()) self._gps = gpsCode.GPS()
class MainApp(): def __init__(self, gps_rate=None): # initialize objects self.stop_blink = threading.Event() self._gui = gui.GUI() self._buttons = buttons.Buttons(on_green=self.green_callback, on_blue=self.blue_callback, on_red=self.red_callback) self.rs = RobotState() print("The first state in the state machine is: %s" % self.rs.state) print("Mac Address: " + gma()) self._gps = gpsCode.GPS() def start(self, gps_rate=None): # User login #self.user_name = self._gui.waitForLogin() # < blocking tempUser = gma() self.user_name = "picker_" + tempUser.replace(':', '') print("User: "******"wss://lcas.lincoln.ac.uk/car/ws", user_name=self.user_name, update_orders_cb=self.update_orders_cb) # setup the main gui window self._gui.setupMainWindow() self._gui.setUser("User: "******"Welcome to Call A Robot.") # start gps thread self._gps.start() # start ws thread self._ws.start() if gps_rate is None: ## we want gps readings as soon as they arrive self._gps.set_callback(self._ws.send_gps) # start gui thread (tkinter only runs on the main thread :-( ) self._gui.loopMainWindow() # < blocking else: ## we want gps readings at a certain rate seconds = 1. / float(gps_rate) while self._gps.has_more_data(): lat, lon, epx, epy, ts = self._gps.get_latest_data() self._ws.send_gps(lat, lon, epx, epy, ts) time.sleep(seconds) def stop(self): self._gps.stop() self._ws.stop() self._buttons.cleanup() # this receives updated state for the current user def update_orders_cb(self, state): if state == "ACCEPT": if self.rs.state == "CALLED": print("A Robot is on the way.") self._gui.setDescription("A Robot is on the way") self.rs.accepted_robot() elif state == "INIT": if self.rs.state == "CALLED": print("Robot Has Been Cancelled.") self._gui.setDescription("Robot Has Been Cancelled.") self._buttons.setGreenLed(False) self._gui.setGreenButton(False) self._buttons.setRedLed(False) self._gui.setRedButton(False) self.rs.cancel_robot() elif self.rs.state == "ACCEPTED": print("Robot Has Been Cancelled.") self._gui.setDescription("Robot Has Been Cancelled.") self._buttons.setGreenLed(False) self._gui.setGreenButton(False) self._buttons.setRedLed(False) self._gui.setRedButton(False) self.rs.cancel_accept() elif self.rs.state == "ARRIVED": print("Robot Load Has Been Cancelled.") self._gui.setDescription("Robot Load Has Been Cancelled.") self.stop_blink.set() self._buttons.setBlueLed(False) self._gui.setBlueButton(False) self._buttons.setRedLed(False) self._gui.setRedButton(False) self.rs.cancel_load() time.sleep(1) self._gui.setDescription("Welcome to Call A Robot.") elif state == "ARRIVED": if self.rs.state == "ACCEPTED": self._buttons.setGreenLed(False) self._gui.setGreenButton(False) print("Robot has arrived.") print( "Please load the tray on the robot\n then press the blue button." ) self._gui.setDescription( "Please load the tray on the robot\n then press the blue button." ) self.blink_thr = threading.Thread(target=self.blue_blink) self.blink_thr.start() self.rs.robot_arrived() elif state == "LOADED": if self.rs.state == "LOADED": self.stop_blink.set() # update internal state #self.rs.state = state def green_callback(self, channel): print("Green button pressed") if (self.rs.state == "INIT"): print("Calling Robot.") self._gui.setDescription("Calling Robot.") self._buttons.setGreenLed(True) self._gui.setGreenButton(True) self._ws.call_robot() self.rs.call_robot() self.stop_blink.clear() # y = threading.Thread(target=check_arrived) # y.start() else: print("A Robot has already been called.") self._gui.setDescription("A Robot has already been called.") time.sleep(1) self._gui.setDescription("Welcome to Call A Robot.") def red_callback(self, channel): print("Red button pressed") if (self.rs.state == "CALLED"): print("Cancelling...") self._gui.setDescription("Cancelling...") self._buttons.setRedLed(True) self._gui.setRedButton(True) self._ws.cancel_robot() self.rs.cancel_robot() time.sleep(1) self._gui.setDescription("Robot Sucessfully Cancelled.") self._buttons.setGreenLed(False) self._gui.setGreenButton(False) self._buttons.setRedLed(False) self._gui.setRedButton(False) time.sleep(1) self._gui.setDescription("Welcome to Call A Robot.") elif (self.rs.state == "ACCEPTED"): print("Cancelling...") self._gui.setDescription("Cancelling...") self._buttons.setRedLed(True) self._gui.setRedButton(True) self._ws.cancel_robot() self.rs.cancel_accept() time.sleep(1) self._gui.setDescription("Robot Sucessfully Cancelled.") self._buttons.setGreenLed(False) self._gui.setGreenButton(False) self._buttons.setRedLed(False) self._gui.setRedButton(False) time.sleep(1) self._gui.setDescription("Welcome to Call A Robot.") elif (self.rs.state == "ARRIVED"): print("Cancelling...") self._gui.setDescription("Cancelling...") self._buttons.setRedLed(True) self._gui.setRedButton(True) self._ws.cancel_robot() self.rs.cancel_robot() time.sleep(1) self._gui.setDescription("Robot Sucessfully Cancelled.") self._buttons.setGreenLed(False) self._gui.setGreenButton(False) self._buttons.setRedLed(False) self._gui.setRedButton(False) time.sleep(1) self._gui.setDescription("Welcome to Call A Robot.") else: print("No incoming robots to cancel.") self._gui.setDescription("No incoming robots to cancel.") time.sleep(1) self._gui.setDescription("Welcome to Call A Robot.") def blue_callback(self, channel): if (self.rs.state == "ARRIVED"): self._gui.setDescription( "Thank you the robot will now drive away.") self._ws.set_loaded() self.rs.robot_loaded() time.sleep(2) self.rs.user_reset() self._ws.set_init() self._gui.setDescription("Welcome to Call A Robot.") else: print("No robots to load.") self._gui.setDescription("No robots to load.") time.sleep(1) self._gui.setDescription("Welcome to Call A Robot.") def blue_blink(self): while not (self.stop_blink.is_set()): self._buttons.setBlueLed(True) self._gui.setBlueButton(True) time.sleep(0.5) self._buttons.setBlueLed(False) self._gui.setBlueButton(False) time.sleep(0.5)
def main(): #Main program print("Welcome to Call A Robot") rs = RobotState() print("The first state in the state machine is: %s" % rs.state) def green_callback(channel): print("Green button pressed") if (rs.state == "INIT"): print("Calling Robot.") GPIO.output(greenLED, True) rs.call_robot() #Websocket connection for robot call. #ws.send(json.dumps({'method':'call', 'user': '******'})) print("A Robot is on the way.") #arrival testing sleep(5) robot_arrived_callback() else: print("A Robot has already been called.") def red_callback(channel): print("Red button pressed") if (rs.state == "CALLED"): print("Cancelling...") GPIO.output(redLED, True) rs.cancel_robot() #Websocket Connection for robot cancel. #ws.send(json.dumps({'method':'cancel', 'user': '******'})) sleep(1) print("Robot Successfully Cancelled.") GPIO.output(greenLED, False) GPIO.output(redLED, False) elif (rs.state == "ARRIVED"): print("Cancelling...") GPIO.output(redLED, True) rs.cancel_load() #Websocket Connection for robot cancel. #ws.send(json.dumps({'method':'cancel', 'user': '******'})) sleep(1) print("Robot Load Successfully Cancelled.") GPIO.output(blueLED, False) GPIO.output(redLED, False) else: print("No incoming robots to cancel.") def blue_callback(channel): if (rs.state == "ARRIVED"): #Websocket Connection for robot loaded. #ws.send(json.dumps({'method':'set_state', 'user': '******', 'state': 'LOADED'})) rs.robot_loaded() else: print("No robots to load.") def robot_arrived_callback(): rs.robot_arrived() GPIO.output(greenLED, False) print("Robot has arrived.") print( "Please load the tray on the robot then press the blue button." ) while (rs.state == "ARRIVED"): GPIO.output(blueLED, True) sleep(0.5) GPIO.output(blueLED, False) sleep(0.5) if GPIO.event_detected(blueButton): print("Blue button pressed") if GPIO.event_detected(redButton): print("Red button pressed") GPIO.add_event_detect(greenButton, GPIO.FALLING, callback=green_callback, bouncetime=200) GPIO.add_event_detect(redButton, GPIO.FALLING, callback=red_callback, bouncetime=1100) GPIO.add_event_detect(blueButton, GPIO.FALLING, callback=blue_callback, bouncetime=200) pause()
def main(): #Main program print("Welcome to Call A Robot") label_text.set("Welcome to Call A Robot.") rs = RobotState() print("The first state in the state machine is: %s" % rs.state) print("Logged in: " + user) user_text.set("User: "******"Green button pressed") if (rs.state == "INIT"): print("Calling Robot.") label_text.set("Calling Robot.") GPIO.output(greenLED, True) gbutton.configure(bg="green") rs.call_robot() #Websocket connection for robot call. ws.send(json.dumps({'method': 'call', 'user': user})) print("A Robot is on the way.") label_text.set("A Robot is on the way") y = threading.Thread(target=check_arrived) y.start() else: print("A Robot has already been called.") label_text.set("A Robot has already been called.") sleep(1) label_text.set("A Robot is on the way") def red_callback(channel): print("Red button pressed") if (rs.state == "CALLED"): print("Cancelling...") label_text.set("Cancelling...") GPIO.output(redLED, True) rbutton.configure(bg="red") rs.cancel_robot() #Websocket Connection for robot cancel. ws.send(json.dumps({'method': 'cancel', 'user': user})) sleep(1) print("Robot Successfully Cancelled.") label_text.set("Robot Successfully Cancelled.") GPIO.output(greenLED, False) gbutton.configure(bg="white") GPIO.output(redLED, False) rbutton.configure(bg="white") sleep(1) label_text.set("Welcome to Call A Robot.") elif (rs.state == "ARRIVED"): print("Cancelling...") label_text.set("Cancelling...") GPIO.output(redLED, True) rbutton.configure(bg="red") rs.cancel_load() #Websocket Connection for robot cancel. ws.send(json.dumps({'method': 'cancel', 'user': user})) sleep(1) print("Robot Load Successfully Cancelled.") label_text.set("Robot Successfully Cancelled.") GPIO.output(blueLED, False) bbutton.configure(bg="white") GPIO.output(redLED, False) rbutton.configure(bg="white") sleep(1) label_text.set("Welcome to Call A Robot.") else: print("No incoming robots to cancel.") label_text.set("No incoming robots to cancel.") sleep(1) label_text.set("Welcome to Call A Robot.") def blue_callback(channel): if (rs.state == "ARRIVED"): label_text.set("Thank you the robot will now drive away.") #Websocket Connection for robot loaded. ws.send( json.dumps({ 'method': 'set_state', 'user': user, 'state': 'LOADED' })) rs.robot_loaded() sleep(2) label_text.set("Welcome to Call A Robot.") ws.send( json.dumps({ 'method': 'set_state', 'user': user, 'state': 'INIT' })) rs.user_reset() else: print("No robots to load.") label_text.set("No robots to load.") sleep(1) label_text.set("Welcome to Call A Robot.") def robot_arrived_callback(): rs.robot_arrived() GPIO.output(greenLED, False) gbutton.configure(bg="white") print("Robot has arrived.") print( "Please load the tray on the robot then press the blue button." ) label_text.set( "Please load the tray on the robot then press the blue button." ) x = threading.Thread(target=blue_blink) x.start() def blue_blink(): while (rs.state == "ARRIVED"): bbutton.configure(bg="blue") GPIO.output(blueLED, True) sleep(0.5) bbutton.configure(bg="white") GPIO.output(blueLED, False) sleep(0.5) def check_arrived(): # Robot arrival ws.send(json.dumps({'method': 'get_states', 'user': user})) while rs.state == "CALLED": sleep(5) jsondata = ws.recv() data = loads(jsondata) states = data['states'] for key in states: if key == user: print(states[key]) if states[key] == "ARRIVED": print("Robot Arrived") robot_arrived_callback() # if GPIO.event_detected(blueButton): # print("Blue button pressed") # elif GPIO.event_detected(redButton): # print("Red button pressed") GPIO.add_event_detect(greenButton, GPIO.RISING, callback=green_callback, bouncetime=1100) GPIO.add_event_detect(redButton, GPIO.FALLING, callback=red_callback, bouncetime=1100) GPIO.add_event_detect(blueButton, GPIO.FALLING, callback=blue_callback, bouncetime=1100) root.focus_force() #getPositionData(gpsd) root.mainloop()