def main(): global gRobotList gMaxRobotNum = 1 # max number of robots to control comm = RobotComm(gMaxRobotNum) comm.start() #print 'Bluetooth starts' gRobotList = comm.robotList root = tk.Tk() #root world_handle = VirtualHamsterWorld() t_update_world = threading.Thread(name='update_world', target=world_handle.update_virtual_world) t_update_world.daemon = True t_update_world.start() gui_handle = GUIpart(root, world_handle.vworld) gui_handle.draw_virtual_world() # this method runs in main thread world_handle.set_numref(gui_handle.numref) root.mainloop() comm.stop() comm.join()
def main(): maxRobotNum = 1 comm = RobotComm(maxRobotNum) comm.start() robotList = comm.robotList action = RobotActionThread(robotList) action.start() root = tk.Tk() root.geometry('1000x500') gui = GUI(root, action) action.line_limit = gui.lines root.mainloop() comm.stop() comm.join() return
def main(): # instantiate COMM object gMaxRobotNum = 1 # max number of robots to control comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' robotList = comm.robotList # settings # grid_map, all_paths, success_paths, best_path = find_best_path(global_rows, global_cols, global_start, global_end, global_blocks) root = tk.Tk() # gui_handle.draw_virtual_world() # this method runs in main thread # gui_handle = GridGraphDisplay(frame=root, blocks=global_blocks, grid_map=grid_map, all_path=all_paths, success_paths=success_paths, start=global_start, end=global_end, best_path=best_path) behaviors = RobotBehaviorThread(robotList, global_orientation) behaviors.setDaemon(True) behaviors.start() root.mainloop() comm.stop() comm.join() return
def main(): q = Queue.Queue() comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' global gRobotList gRobotList = comm.robotList frame = tk.Tk() t_handle = BehaviorThreads() gui = GUI(frame, t_handle) #sm = StateMachine('Main FSM', q) # tm = MyMachine(gui, sm) frame.mainloop() comm.stop() comm.join() print("terminated!")
def main(): width = input("Enter graph width, from 1-10: ") height = input("Enter graph height, from 1-10: ") start_col = input("Enter starting column, from 0-" + str(width - 1) + ": ") start_row = input("Enter starting row, from 0-" + str(height - 1) + ": ") if (width > 10 or height > 10 or start_row >= height or start_col >= width): print "Out of bounds" return graph = GridGraph() # grid dimension graph.set_grid_rows(width) graph.set_grid_cols(height) # origin of grid is (0, 0) lower left corner # graph.obs_list = ([1,1],) # in case of one obs. COMMA # graph.set_start('0-0') # graph.obs_list = ("2-3", "1-1", "2-1", "2-2") # graph.set_goal('1-2') # graph.set_start('0-0') # graph.obs_list = ("2-1", "1-2") # graph.set_goal('2-2') graph.make_grid() graph.connect_nodes() graph.compute_node_locations() search = KnightSearch(start_row, start_col, width, height) order = search.solve_tour() # kt = KnightSearch(8, 8) # kt.tour(1, [], (0,0)) # kt.print_board() gMaxRobotNum = 1 # max number of robots to control comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' robotList = comm.robotList behaviors = RobotBehaviorThread(robotList) path = list(behaviors.make_plan(order, width)) behaviors.path_to_plan() behaviors.start() root = tk.Tk() graph_display = GridGraphDisplay(root, graph, path, behaviors) graph_display.display_graph() root.mainloop() comm.stop() comm.join() return
def main(argv=None): global m comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' m = tk.Tk() #root drawQueue = Queue.Queue(0) #creating the virtual appearance of the robot canvas_width = 700 # half width canvas_height = 380 # half height rCanvas = tk.Canvas(m, bg="white", width=canvas_width*2, height=canvas_height*2) vrobot = virtual_robot() pi4 = 3.1415 / 4 #----- initialize position and angle of robot -----# vrobot.set_robot_a_pos(-pi4*2, 230, 0) joystick = Joystick(comm, m, rCanvas, vrobot) # visual elements of the virtual robot poly_points = [0,0,0,0,0,0,0,0] joystick.vrobot.poly_id = rCanvas.create_polygon(poly_points, fill='blue') #robot joystick.vrobot.prox_l_id = rCanvas.create_line(0,0,0,0, fill="red") #prox sensors ---- here joystick.vrobot.prox_r_id = rCanvas.create_line(0,0,0,0, fill="red") joystick.vrobot.floor_l_id = rCanvas.create_oval(0,0,0,0, outline="white", fill="white") #floor sensors joystick.vrobot.floor_r_id = rCanvas.create_oval(0,0,0,0, outline="white", fill="white") time.sleep(1) update_vrobot_thread = threading.Thread(target=joystick.update_virtual_robot) update_vrobot_thread.daemon = True update_vrobot_thread.start() #create the virtual worlds that contains the virtual robot, pass the joystick to reuse the move forward, stop, etc. functions # joystick should then be renamed to 'set of functions used to move robot forwards, stop robot, etc.' b/c joystick functionality is not used vWorld = virtual_world(drawQueue, joystick, joystick.vrobot, rCanvas, canvas_width, canvas_height) rectA = [-260, -20, -220, 20] vWorld.add_obstacle(rectA) draw_world_thread = threading.Thread(target=draw_virtual_world, args=(vWorld, joystick)) draw_world_thread.daemon = True draw_world_thread.start() gui = VirtualWorldGui(vWorld, m) gui.drawGrid() gui.drawMap() rCanvas.after(200, gui.updateCanvas, drawQueue) m.mainloop() for robot in joystick.gRobotList: robot.reset() comm.stop() comm.join()
def start(): comm = RobotComm(1) comm.start() RobotThread(comm.robotList, None).start() frame = tk.Tk() frame.mainloop() comm.stop() comm.join()
def main(argv=None): # instantiate COMM object comm = RobotComm(1, -50) #maxRobot = 1, minRSSI = -50 if comm.start(): print 'Communication starts' else: print 'Error: communication' return # instanciate Robot robotList = comm.get_robotList() global gFrame gFrame = tk.Tk() gFrame.geometry('600x500') #gFrame.focus_set() #gFrame.bind('<KeyPress>', joystick) gRobotDraw = draw.RobotDraw(gFrame, tk) # create behaviors using set global gBehaviors gBehaviors = {} # gBehaviors[0] = color.Behavior("color", robotList) # gBehaviors[1] = sound.Behavior("sound", robotList) # gBehaviors[2] = motion.Behavior("motion", robotList, 10) # gBehaviors[3] = proxy.Behavior("proxy", robotList, 85, 0.01, gRobotDraw.get_queue()) gBehaviors[0] = scanning.Behavior("scanning", robotList, 16.0, gRobotDraw.get_queue()) # start behavior threads using list behavior_threads = [] for i in range(0, len(gBehaviors)): gBehaviors[i].set_bQuit(False) behavior_threads.append(threading.Thread(target=gBehaviors[i].behavior_loop, name = gBehaviors[i].get_name())) for thread in behavior_threads: thread.daemon = True thread.start() gRobotDraw.start() gFrame.mainloop() for behavior in behavior_threads: print "joining... ", behavior.getName() behavior.join() print behavior.getName(), "joined!" for robot in robotList: robot.reset() comm.stop() comm.join() print("terminated!")
def main(argv=None): global m global comm comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' m = tk.Tk() #root drawQueue = Queue.Queue(0) #creating tje virtual appearance of the robot canvas_width = 700 # half width canvas_height = 380 # half height rCanvas = tk.Canvas(m, bg="white", width=canvas_width*2, height=canvas_height*2) joystick = Joystick(comm, m, rCanvas, robot1_config) # visual elements of the virtual robot poly_points = [0,0,0,0,0,0,0,0] joystick.vrobot.poly_id = rCanvas.create_polygon(poly_points, fill='blue') #robot joystick.vrobot.prox_l_id = rCanvas.create_line(0,0,0,0, fill="red") #prox sensors joystick.vrobot.prox_r_id = rCanvas.create_line(0,0,0,0, fill="red") joystick.vrobot.floor_l_id = rCanvas.create_oval(0,0,0,0, outline="white", fill="white") #floor sensors joystick.vrobot.floor_r_id = rCanvas.create_oval(0,0,0,0, outline="white", fill="white") time.sleep(1) update_vrobot_thread = threading.Thread(target=joystick.update_virtual_robot) update_vrobot_thread.daemon = True update_vrobot_thread.start() #create the virtual worlds that contains the virtual robot vWorld = virtual_world(drawQueue, joystick.vrobot, rCanvas, canvas_width, canvas_height) boxes = [[0, 0, 40, 100] ,[40, 80, 140, 120] ,[120, -80, 200, 0] ,[0, -80, 40, -180] ,[40, -180, 300, -220] ,[300, -180, 340, 200] ,[300, 200, 0, 240]] for box in boxes: vWorld.add_obstacle(box) draw_world_thread = threading.Thread(target=draw_virtual_world, args=(vWorld, joystick)) draw_world_thread.daemon = True draw_world_thread.start() gui = VirtualWorldGui(vWorld, joystick, m) rCanvas.after(200, gui.updateCanvas, drawQueue) m.mainloop() for robot in joystick.gRobotList: robot.reset() comm.stop() comm.join()
def main(argv=None): gMaxRobotNum = 2 # max number of robots to control comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' robotList = comm.robotList robot_handle = Robots(robotList) m = tk.Tk() #root gui = UI(m, robot_handle) print("I GOT HERE") m.mainloop() comm.stop() comm.join()
def main(argv=None): gMaxRobotNum = 1 # max number of robots to control comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' robotList = comm.robotList robot_handle = Robots(robotList) m = tk.Tk() #m is tk.Tk() and root gui = UI(m, robot_handle) #m is tk.Tk() and root m.mainloop() # stop the program from ending. mainloop() is in tkinterface comm.stop() comm.join()
def main(): max_robot_num = 1 # max number of robots to control comm = RobotComm(max_robot_num) comm.start() print 'Bluetooth starts' robotList = comm.robotList root = tk.Tk() t_handle = BehaviorThreads(robotList) gui = GUI(root, t_handle) root.mainloop() comm.stop() comm.join()
def main(): global gRobotList comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' gRobotList = comm.robotList #if don't want to connect to real robot #gRobotList = [] m = tk.Tk() #root client = ThreadedVirtualWorld(m) m.mainloop() comm.stop() comm.join()
def main(): global gRobotList gMaxRobotNum = 1 comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' gRobotList = comm.robotList frame = tk.Tk() frame.mainloop() comm.stop() comm.join() print("terminated!")
def main(): gMaxRobotNum = 1 # max number of robots to control comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' robot_list = comm.robotList behaviors = RobotBehavior(robot_list) frame = tk.Tk() GUI(frame, behaviors) frame.mainloop() comm.stop() comm.join() return
def main(argv=None): paths = getPaths(1000, 600, 500, 200) drawList.append((500, 200)) drawList.append((1500, 200)) drawList.append((500, 800)) drawList.append((1500, 800)) # instantiate COMM object comm = RobotComm(2, -50) #maxRobot = 1, minRSSI = -50 if comm.start(): print 'Communication starts' else: print 'Error: communication' return # instantiate Robot robotList = comm.get_robotList() # create behaviors using set global gBehaviors gBehaviors = {} # start behavior threads using list behavior_threads = [] behavior_threads.append(threading.Thread(target=command, args=(paths, robotList))) for thread in behavior_threads: thread.daemon = True thread.start() # start the cv thread main_thread() for behavior in behavior_threads: print "joining... ", behavior.getName() behavior.join() print behavior.getName(), "joined!" for robot in robotList: robot.reset() comm.stop() comm.join() print("terminated!")
def main(): # instantiate COMM object gMaxRobotNum = 1; # max number of robots to control comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' robotList = comm.robotList behaviors = RobotBehaviorThread(robotList) behaviors.start() frame = tk.Tk() GUI(frame, behaviors) frame.mainloop() comm.stop() comm.join() print("terminated!")
def main(argv=None): gMaxRobotNum = 1 # max number of robots to control comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' robotList = comm.robotList robot_handle = Robots(robotList) m = tk.Tk() #root gui = UI(m, robot_handle) b2 = tk.Button(m, text='Exit') b2.pack(side='left') b2.bind('<Button-2>', UI.stopProg) m.mainloop() comm.stop() comm.join()
def main(): graph = GridGraph() # grid dimension graph.set_grid_rows(4) graph.set_grid_cols(3) # origin of grid is (0, 0) lower left corner # graph.obs_list = ([1,1],) # in case of one obs. COMMA graph.set_start('0-2') graph.obs_list = ("2-3", "1-1", "2-1", "2-2") graph.set_goal('3-2') # graph.set_start('0-0') # graph.obs_list = ("2-1", "1-2") # graph.set_goal('2-2') graph.make_grid() graph.connect_nodes() graph.compute_node_locations() bfs = BFS(graph.nodes) path = bfs.bfs_shortest_path(graph.get_start_node(), graph.get_goal_node()) gMaxRobotNum = 1 # max number of robots to control comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' robotList = comm.robotList behaviors = RobotBehaviorThread(robotList) behaviors.make_plan(path) behaviors.start() root = tk.Tk() graph_display = GridGraphDisplay(root, graph, behaviors) graph_display.display_graph(path, graph.get_start_node(), graph.get_goal_node()) comm.stop() comm.join() return
def main(): max_robot_num = 1 # max number of robots to control comm = RobotComm(max_robot_num) comm.start() print 'Bluetooth starts' robotList = comm.robotList root = tk.Tk() # t_handle = BehaviorThreads(robotList) # gui = GUI(root, t_handle) mThread = threading.Thread(name="main thread", target=mainThread, args=[robotList]) mThread.daemon = True mThread.start() root.mainloop() comm.stop() comm.join()
def main(argv=None): gMaxRobotNum = 1 # max number of robots to control comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' robotList = comm.robotList robot_moves = RobotMoves(robotList) m = tk.Tk() #root gui = UI(m, robot_moves) # start a watcher thread display = SensorDisplayThread(gui.canvas, robotList) display.setDaemon(True) display.start() m.mainloop() comm.stop() comm.join()
def main(argv=None): # instantiate COMM object comm = RobotComm(1, -50) #maxRobot = 1, minRSSI = -50 if comm.start(): print 'Communication starts' else: print 'Error: communication' return # instantiate Robot robotList = comm.get_robotList() # instantiate global variables gFrame and gBehavior settings.init() settings.gFrame = tk.Tk() settings.gFrame.geometry('600x500') gRobotDraw = draw.RobotDraw(settings.gFrame, tk) # create behaviors settings.gBehaviors[0] = scanning.Behavior("scanning", robotList, 4.0, gRobotDraw.get_queue()) settings.gBehaviors[1] = collision.Behavior("collision", robotList, -50) gRobotDraw.start() settings.gFrame.mainloop() for behavior in behavior_threads: print "joining... ", behavior.getName() behavior.join() print behavior.getName(), "joined!" for robot in robotList: robot.reset() comm.stop() comm.join() print("terminated!")
def main(): grid = [4,4] obs=[] a = input("Enter an obs or enter 0") while (a != 0): obs.append(a) a = input("Enter an obs or enter 0") # start_node = str(input("Start Node (x-x):")) # end_node = str(input("End Node (x-x):")) start_node = '1-1' start_int = [1,1] end_node = '4-4' end_int = [4,4] graph, nodes_location = graphgen.main(grid,obs) print 'graph:', graph frame = tk.Tk() frame.title('Simple Graph Display') frame.geometry("400x400") display = tk_simple_graph_display.SimpleGraphDisplay(frame, graph, nodes_location, start_node, end_node) bfs = bfs_engine.BFS(graph) p = bfs.bfs_shortest_path(start_node, end_node) l = display.highlight_path(p) gMaxRobotNum = 1; # max number of robots to control comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' robotList = comm.robotList movem = movements.move(l, start_int, end_int,robotList) movem.start() frame.mainloop() comm.stop() comm.join() print("terminated!")
def start(periodicFunc): global robotList, func func = periodicFunc comm = RobotComm(1) comm.start() print("Bluetooth running...") robotList = comm.robotList window = tk.Tk() canvas = tk.Canvas(window, bg="white", width = 100, height = 100) canvas.pack() button = tk.Button(window, text="Exit") button.pack() button.bind("<Button-1>", lambda _: shutdown(window)) robotThread = Thread(target = runRobot) robotThread.start() window.mainloop() comm.stop() comm.join()
def main(): global gRobotList gMaxRobotNum = 0 # max number of robots to control comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' gRobotList = comm.robotList root = tk.Tk() #root root.geometry = "630x880" world_handle = VirtualWorld(root) t_update_world = threading.Thread(name='update_world', target=world_handle.update_virtual_world) t_update_world.daemon = True t_update_world.start() gui_handle = GUI(root, world_handle.vworld, world_handle.stopProg) gui_handle.draw_virtual_world() # this method runs in main thread root.mainloop() comm.stop() comm.join()
def main(argv=None): global m comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' m = tk.Tk() #root drawQueue = Queue.Queue(0) #creating the virtual appearance of the robot canvas_width = 700 # half width canvas_height = 380 # half height rCanvas = tk.Canvas(m, bg="white", width=canvas_width * 2, height=canvas_height * 2) vrobot = virtual_robot() pi4 = 3.1415 / 4 #----- initialize position and angle of robot -----# vrobot.set_robot_a_pos(-pi4 * 2, 230, 0) joystick = Joystick(comm, m, rCanvas, vrobot) # visual elements of the virtual robot poly_points = [0, 0, 0, 0, 0, 0, 0, 0] joystick.vrobot.poly_id = rCanvas.create_polygon(poly_points, fill='blue') #robot joystick.vrobot.prox_l_id = rCanvas.create_line( 0, 0, 0, 0, fill="red") #prox sensors ---- here joystick.vrobot.prox_r_id = rCanvas.create_line(0, 0, 0, 0, fill="red") joystick.vrobot.floor_l_id = rCanvas.create_oval( 0, 0, 0, 0, outline="white", fill="white") #floor sensors joystick.vrobot.floor_r_id = rCanvas.create_oval(0, 0, 0, 0, outline="white", fill="white") time.sleep(1) update_vrobot_thread = threading.Thread( target=joystick.update_virtual_robot) update_vrobot_thread.daemon = True update_vrobot_thread.start() #create the virtual worlds that contains the virtual robot, pass the joystick to reuse the move forward, stop, etc. functions # joystick should then be renamed to 'set of functions used to move robot forwards, stop robot, etc.' b/c joystick functionality is not used vWorld = virtual_world(drawQueue, joystick, joystick.vrobot, rCanvas, canvas_width, canvas_height) #objects in the world rectF = [0, -50, 40, 50] rectE = [-100, -180, 0, -140] rectD = [-140, -180, -100, -80] rectC = [-100, 140, 0, 180] rectB = [-140, 80, -100, 180] rectA = [-260, -20, -220, 20] vWorld.add_obstacle(rectF) vWorld.add_obstacle(rectE) vWorld.add_obstacle(rectD) vWorld.add_obstacle(rectC) vWorld.add_obstacle(rectB) vWorld.add_obstacle(rectA) draw_world_thread = threading.Thread(target=draw_virtual_world, args=(vWorld, joystick)) draw_world_thread.daemon = True draw_world_thread.start() gui = VirtualWorldGui(vWorld, m) # #---- create a new thread to update the physical robot # update_probot_thread = threading.Thread(target=gui.nextStep) # update_probot_thread.daemon = True # update_probot_thread.start() gui.drawGrid() gui.drawMap() rCanvas.after(200, gui.updateCanvas, drawQueue) m.mainloop() #---------------- Automatically scan for box (not needed for assignment) --------------# # start a state machine and set initial state (unnecessary) # set up event queue # define dispatcher # define handler for robot in joystick.gRobotList: robot.reset() comm.stop() comm.join()
class WebServer: def __init__(self): self.comm = None self.server = None self.s_thread = None self.ul_thread = None self.robot_list_raw = [] self.robot_list = {} self.robot_num = 0 self.max_robot_num = 1 # Complete maximum that the API can handle is 8 self.frame = tk.Tk() self.status = tk.Text(self.frame) self.status.pack() btn_exit = tk.Button(self.frame, text="Stop and Exit", command=self.stop_server) btn_exit.pack() self.init_server() def init_server(self): self.comm = RobotComm(self.max_robot_num) self.server = Flask(__name__) CORS(self.server) @self.server.route('/') def ws_status(): return jsonify({ 'online': True, 'robots': self.format_list() }) @self.server.route('/post/<robot_id>/<path:cmd_path>') def ws_raw_update(robot_id, cmd_path): cmds = cmd_path.split('/') if robot_id not in self.robot_list or not self.robot_list[robot_id].active: return jsonify({'online': True, 'success': False, 'error': 'robot_not_found', 'cmd_path': cmds}) if cmds[0] == 'l_motor': self.set_wheel(0, int(cmds[1]), robot_id=robot_id) elif cmds[0] == 'r_motor': self.set_wheel(1, int(cmds[1]), robot_id=robot_id) elif cmds[0] == 'a_motor': self.set_wheel(0, int(cmds[1]), robot_id=robot_id) self.set_wheel(1, int(cmds[1]), robot_id=robot_id) elif cmds[0] == 'e_stop': self.set_wheel(0, 0, robot_id=robot_id) self.set_wheel(1, 0, robot_id=robot_id) elif cmds[0] == 't_tone': self.set_musical_note(int(cmds[1]), robot_id=robot_id) else: return jsonify({'online': True, 'success': False, 'error': 'cmd_not_found', 'cmd_path': cmds}) return jsonify({ 'online': True, 'success': True, 'robots': self.format_list(), 'cmd_path': cmds }) self.s_thread = Thread( name='web server', target=self.server.run, kwargs={'debug': True, 'use_reloader': False} ) self.s_thread.setDaemon(True) self.ul_thread = Thread(name='list watcher', target=self.update_robot_list) self.ul_thread.setDaemon(True) def format_list(self): new_list = dict() for r in self.robot_list: new_list[r] = self.robot_list[r].to_json() return new_list def start_server(self): self.log_message('Starting BLE communication thread...') self.comm.start() self.log_message('Starting web server thread...') self.s_thread.start() self.log_message('Starting robot list watching thread...') self.ul_thread.start() self.robot_list_raw = self.comm.robotList self.log_message('Starting GUI...') self.log_message('You may now connect up to ' + str(self.max_robot_num) + ' robot(s).') self.frame.mainloop() def log_message(self, log): self.status.insert(tk.END, '[' + str(time.time()) + '] ' + log + '\n') def stop_server(self): self.log_message('Killing the server now..') self.comm.stop() self.comm.join() sys.exit() def set_wheel(self, motor_num, speed, robot_num = None, robot_id = None): r_id = robot_id if robot_id else self.get_robot_id(robot_num) if motor_num == RobotEnum.LEFT_SIDE: self.robot_list[r_id].left_motor = speed elif motor_num == RobotEnum.RIGHT_SIDE: self.robot_list[r_id].right_motor = speed def set_musical_note(self, note, robot_num = None, robot_id = None): r_id = robot_id if robot_id else self.get_robot_id(robot_num) self.robot_list[r_id].musical_note = note def set_led(self, led_num, led_color, robot_num = None, robot_id = None): r_id = robot_id if robot_id else self.get_robot_id(robot_num) if led_num == RobotEnum.LEFT_SIDE: self.robot_list[r_id].left_led = led_color elif led_num == RobotEnum.RIGHT_SIDE: self.robot_list[r_id].right_led = led_color def get_wheel(self, motor_num, robot_num = None, robot_id = None): r_id = robot_id if robot_id else self.get_robot_id(robot_num) if motor_num == RobotEnum.LEFT_SIDE: return self.robot_list[r_id].left_motor elif motor_num == RobotEnum.RIGHT_SIDE: return self.robot_list[r_id].right_motor else: return 0 def get_led(self, led_num, robot_num = None, robot_id = None): r_id = robot_id if robot_id else self.get_robot_id(robot_num) if led_num == RobotEnum.LEFT_SIDE: return self.robot_list[r_id].left_led elif led_num == RobotEnum.RIGHT_SIDE: return self.robot_list[r_id].right_led else: return 0 def get_proximity(self, prox_num, robot_num = None, robot_id = None): r_id = robot_id if robot_id else self.get_robot_id(robot_num) return self.robot_list[r_id]._robot.get_proximity(prox_num) def get_floor(self, floor_num, robot_num = None, robot_id = None): r_id = robot_id if robot_id else self.get_robot_id(robot_num) return self.robot_list[r_id]._robot.get_floor(floor_num) def get_robot_id(self, robot_num): return str(id(self.robot_list[robot_num])) def update_robot_list(self): while True: if self.robot_num != len(self.robot_list_raw): logging.info('[new change to robot list]') self.log_message('list watcher: new number of robots connected (' + str(len(self.robot_list_raw)) + ')') robot_visited = set() robot_all = set() # Recalculate everything for robot in self.robot_list_raw: # NOTE: the robot id is based on the # robot object in the communication list robot_visited.add(str(id(robot))) if str(id(robot)) not in self.robot_list: self.robot_list[str(id(robot))] = RobotBehaviorState(robot) for robot in self.robot_list: robot_all.add(self.robot_list[robot].get_id()) robot_inactive = robot_all - robot_visited for r_id in robot_inactive: self.robot_list[r_id].active = False self.robot_num = len(self.robot_list_raw) time.sleep(0.1)
def start(periodicFunc, mapClickCallback, imageClickCallback): global robotList, func func = periodicFunc comm = RobotComm(1) comm.start() print("Bluetooth running...") robotList = comm.robotList window = tk.Tk() image = np.zeros((1080, 960, 3), dtype=np.uint8) image = Image.fromarray(image) image = ImageTk.PhotoImage(image) imagePanel = tk.Label(image=image) imagePanel.image = image imagePanel.bind("<Button-1>", imageClickCallback) imagePanel.pack(side="top", padx=10, pady=10) def _updateImage(img): img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = Image.fromarray(img) img = ImageTk.PhotoImage(img) imagePanel.configure(image=img) imagePanel.image = img image2 = np.zeros((1080, 960, 3), dtype=np.uint8) image2 = Image.fromarray(image2) image2 = ImageTk.PhotoImage(image2) imagePanel2 = tk.Label(image=image2, borderwidth=2, relief="solid") imagePanel2.image = image2 imagePanel2.bind("<Button-1>", mapClickCallback) imagePanel2.pack(side="bottom", padx=10, pady=10) def _updateImage2(img): img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = Image.fromarray(img) img = ImageTk.PhotoImage(img) imagePanel2.configure(image=img) imagePanel2.image = img global updateImageFunc updateImageFunc = _updateImage global updateImage2Func updateImage2Func = _updateImage2 button = tk.Button(window, text="Exit") button.pack() button.bind("<Button-1>", lambda _: shutdown(window)) robotThread = Thread(target=runRobot) robotThread.start() window.mainloop() comm.stop() comm.join()
def main(argv=None): global m comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' m = tk.Tk() #root drawQueue = Queue.Queue(0) #creating the virtual appearance of the robot canvas_width = 700 # half width canvas_height = 380 # half height rCanvas = tk.Canvas(m, bg="white", width=canvas_width*2, height=canvas_height*2) vrobot = virtual_robot() pi4 = 3.1415 / 4 #----- initialize position and angle of robot -----# vrobot.set_robot_a_pos(-pi4*2, 230, 0) joystick = Joystick(comm, m, rCanvas, vrobot) # visual elements of the virtual robot poly_points = [0,0,0,0,0,0,0,0] joystick.vrobot.poly_id = rCanvas.create_polygon(poly_points, fill='blue') #robot joystick.vrobot.prox_l_id = rCanvas.create_line(0,0,0,0, fill="red") #prox sensors ---- here joystick.vrobot.prox_r_id = rCanvas.create_line(0,0,0,0, fill="red") joystick.vrobot.floor_l_id = rCanvas.create_oval(0,0,0,0, outline="white", fill="white") #floor sensors joystick.vrobot.floor_r_id = rCanvas.create_oval(0,0,0,0, outline="white", fill="white") time.sleep(1) update_vrobot_thread = threading.Thread(target=joystick.update_virtual_robot) update_vrobot_thread.daemon = True update_vrobot_thread.start() #create the virtual worlds that contains the virtual robot, pass the joystick to reuse the move forward, stop, etc. functions # joystick should then be renamed to 'set of functions used to move robot forwards, stop robot, etc.' b/c joystick functionality is not used vWorld = virtual_world(drawQueue, joystick, joystick.vrobot, rCanvas, canvas_width, canvas_height) #objects in the world rectF = [0, -50, 40, 50] rectE = [-100, -180, 0, -140] rectD = [-140, -180, -100, -80] rectC = [-100, 140, 0, 180] rectB = [-140, 80, -100, 180] rectA = [-260, -20, -220, 20] vWorld.add_obstacle(rectF) vWorld.add_obstacle(rectE) vWorld.add_obstacle(rectD) vWorld.add_obstacle(rectC) vWorld.add_obstacle(rectB) vWorld.add_obstacle(rectA) draw_world_thread = threading.Thread(target=draw_virtual_world, args=(vWorld, joystick)) draw_world_thread.daemon = True draw_world_thread.start() gui = VirtualWorldGui(vWorld, m) # #---- create a new thread to update the physical robot # update_probot_thread = threading.Thread(target=gui.nextStep) # update_probot_thread.daemon = True # update_probot_thread.start() gui.drawGrid() gui.drawMap() rCanvas.after(200, gui.updateCanvas, drawQueue) m.mainloop() #---------------- Automatically scan for box (not needed for assignment) --------------# # start a state machine and set initial state (unnecessary) # set up event queue # define dispatcher # define handler for robot in joystick.gRobotList: robot.reset() comm.stop() comm.join()
def main(): global gRobotList, gQuit global gCanvas, frame, gHamsterBox global display_thread, monitor_thread, dispatch_thread global gBeepQueue, gWheelQueue, drawQueue global vWorld comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth started' gRobotList = comm.robotList monitor_thread = False dispatch_thread = False display_thread = False # create UI frame = tk.Tk() canvas_width = 700 # half width canvas_height = 380 # half height gCanvas = tk.Canvas(frame, bg="white", width=canvas_width * 2, height=canvas_height * 2) draw_track() vrobot = virtual_robot() pi4 = 3.1415 / 4 # robot starting positions vrobot.set_robot_a_pos(pi4 * 2, -520, 340) # keyboard input joystick = Joystick(comm, frame, gCanvas, vrobot) poly_points = [0, 0, 0, 0, 0, 0, 0, 0] joystick.vrobot.poly_id = gCanvas.create_polygon(poly_points, fill='blue') #robot joystick.vrobot.prox_l_id = gCanvas.create_line( 0, 0, 0, 0, fill="red") #prox sensors ---- here joystick.vrobot.prox_r_id = gCanvas.create_line(0, 0, 0, 0, fill="red") joystick.vrobot.floor_l_id = gCanvas.create_oval( 0, 0, 0, 0, outline="white", fill="white") #floor sensors joystick.vrobot.floor_r_id = gCanvas.create_oval(0, 0, 0, 0, outline="white", fill="white") time.sleep(1) update_vrobot_thread = threading.Thread( target=joystick.update_virtual_robot) update_vrobot_thread.daemon = True update_vrobot_thread.start() # virtual world UI drawQueue = Queue.Queue(0) vWorld = virtual_world(drawQueue, joystick, vrobot, gCanvas, canvas_width, canvas_height) landmark = [-500, 220, -460, 180] vWorld.add_obstacle(landmark) draw_world_thread = threading.Thread(target=draw_virtual_world, args=(vWorld, )) draw_world_thread.daemon = True draw_world_thread.start() gui = VirtualWorldGui(vWorld, frame) gui.drawGrid() gui.drawMap() gCanvas.after(200, gui.updateCanvas, drawQueue) frame.mainloop() gQuit = True for robot in gRobotList: robot.reset() time.sleep(1.0) comm.stop() comm.join() print 'Terminated'
def main(): global gRobotList, gQuit global gCanvas, frame, gHamsterBox global display_thread, monitor_thread, dispatch_thread global gBeepQueue, gWheelQueue, drawQueue global vWorld comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth started' gRobotList = comm.robotList monitor_thread = False dispatch_thread= False display_thread = False # create UI frame = tk.Tk() canvas_width = 700 # half width canvas_height = 380 # half height gCanvas = tk.Canvas(frame, bg="white", width=canvas_width*2, height=canvas_height*2) draw_track() vrobot = virtual_robot() pi4 = 3.1415 / 4 # robot starting positions vrobot.set_robot_a_pos(pi4*2, -520, 340) # keyboard input joystick = Joystick(comm, frame, gCanvas, vrobot) poly_points = [0,0,0,0,0,0,0,0] joystick.vrobot.poly_id = gCanvas.create_polygon(poly_points, fill='blue') #robot joystick.vrobot.prox_l_id = gCanvas.create_line(0,0,0,0, fill="red") #prox sensors ---- here joystick.vrobot.prox_r_id = gCanvas.create_line(0,0,0,0, fill="red") joystick.vrobot.floor_l_id = gCanvas.create_oval(0,0,0,0, outline="white", fill="white") #floor sensors joystick.vrobot.floor_r_id = gCanvas.create_oval(0,0,0,0, outline="white", fill="white") time.sleep(1) update_vrobot_thread = threading.Thread(target=joystick.update_virtual_robot) update_vrobot_thread.daemon = True update_vrobot_thread.start() # virtual world UI drawQueue = Queue.Queue(0) vWorld = virtual_world(drawQueue, joystick, vrobot, gCanvas, canvas_width, canvas_height) landmark = [-500, 220, -460, 180] vWorld.add_obstacle(landmark) draw_world_thread = threading.Thread(target=draw_virtual_world, args=(vWorld,)) draw_world_thread.daemon = True draw_world_thread.start() gui = VirtualWorldGui(vWorld, frame) gui.drawGrid() gui.drawMap() gCanvas.after(200, gui.updateCanvas, drawQueue) frame.mainloop() gQuit = True for robot in gRobotList: robot.reset() time.sleep(1.0) comm.stop() comm.join() print 'Terminated'
def main(): comm = RobotComm(gVars.gMaxRobotNum,-70) #maxRobot, minRSSI comm.start() print 'Bluetooth starts' #instanciate robot list and GUI root gVars.grobotList = comm.get_robotList() gVars.m = tk.Tk() #root drawQueue = Queue.Queue(0) #creating the GUI canvas canvas_width = 700 # half width canvas_height = 300 # half height rCanvas = tk.Canvas(gVars.m, bg="white", width=canvas_width*2, height=canvas_height*2) #create the prisoner vitual robot prisoner = pris_bot.Prisoner(comm, rCanvas) # visual elements of the virtual robot poly_points = [0,0,0,0,0,0,0,0] prisoner.vrobot.poly_id = rCanvas.create_polygon(poly_points, fill='orange') #robot prisoner.vrobot.prox_l_id = rCanvas.create_line(0,0,0,0, fill="red") #prox sensors prisoner.vrobot.prox_r_id = rCanvas.create_line(0,0,0,0, fill="red") prisoner.vrobot.floor_l_id = rCanvas.create_oval(0,0,0,0, outline="white", fill="white") #floor sensors prisoner.vrobot.floor_r_id = rCanvas.create_oval(0,0,0,0, outline="white", fill="white") update_pvrobot_thread = threading.Thread(target=prisoner.update_virtual_robot) update_pvrobot_thread.daemon = True update_pvrobot_thread.start() #create the prisoner vitual robot guard = guard_bot.Guard(comm, rCanvas) # visual elements of the virtual robot poly_points = [0,0,0,0,0,0,0,0] guard.vrobot.poly_id = rCanvas.create_polygon(poly_points, fill='green') #robot guard.vrobot.prox_l_id = rCanvas.create_line(0,0,0,0, fill="red") #prox sensors guard.vrobot.prox_r_id = rCanvas.create_line(0,0,0,0, fill="red") guard.vrobot.floor_l_id = rCanvas.create_oval(0,0,0,0, outline="white", fill="white") #floor sensors guard.vrobot.floor_r_id = rCanvas.create_oval(0,0,0,0, outline="white", fill="white") update_gvrobot_thread = threading.Thread(target=guard.update_virtual_robot) update_gvrobot_thread.daemon = True update_gvrobot_thread.start() #create the virtual worlds that contains the virtual robot vWorld = draw.virtual_world(drawQueue, prisoner.vrobot, guard.vrobot, rCanvas, canvas_width, canvas_height) #objects in the world rect_slider = [-150, 160, -60, 120] rect_l1 = [-150, 0, -110, 120] #left 1 rect_l2 = [-112, -100, -110, -5] #left 2 (paper flap) rect_b1 = [-250, -105, 265, -100] #back wall rect_r1 = [260, -100, 265, 200] #right 1 rect_f1 = [-110, 100, -40, 105] #front 1 rect_f2 = [40, 100, 260, 105] #front 2 rect_lh = [-250, -100, -245, 210] #left hallway wall rect_rh = [-150, 160, -145, 210] vWorld.add_obstacle(rect_l1) vWorld.add_obstacle(rect_l2) vWorld.add_obstacle(rect_b1) vWorld.add_obstacle(rect_r1) vWorld.add_obstacle(rect_f1) vWorld.add_obstacle(rect_f2) vWorld.add_obstacle(rect_lh) vWorld.add_obstacle(rect_rh) vWorld.add_obstacle(rect_slider) #create boundary for decoy box starting position vWorld.add_boundary([50,0,170,80]) #start thread to draw the virtual world draw_world_thread = threading.Thread(target=GUI.draw_virtual_world, args=(vWorld, prisoner, guard)) draw_world_thread.daemon = True draw_world_thread.start() graphics = GUI.VirtualWorldGui(vWorld, gVars.m) #create FSM for guard guard_fsm = guard_FSM.EventFsm() print "Guard: Created the guard state machine. \n" guard_FSM.build_states(guard_fsm, vWorld, guard) guard_fsm.set_start("init") guard_fsm.set_current("init") #start thread to monitor events in guard FSM guard_event_thread = threading.Thread(target=guard_FSM.monitor_events, args=(guard_fsm,)) guard_event_thread.daemon = True guard_event_thread.start() #start thread to run guard FSM guard_fsm_thread = threading.Thread(target=guard_fsm.run) guard_fsm_thread.daemon = True guard_fsm_thread.start() #create FSM for prisoner pris_fsm = pris_FSM.EventFsm() print "Prisoner: Ceated the prisoner state machine. \n" pris_FSM.build_states(pris_fsm, vWorld, prisoner, guard_fsm) pris_fsm.set_start("init") pris_fsm.set_current("init") #start thread to monitor events in prisoner FSM pris_event_thread = threading.Thread(target=pris_FSM.monitor_events, args =(pris_fsm, prisoner, guard_fsm)) pris_event_thread.daemon = True pris_event_thread.start() #start thread to run prisoner FSM pris_fsm_thread = threading.Thread(target=pris_fsm.run) pris_fsm_thread.daemon = True pris_fsm_thread.start() rCanvas.after(200, graphics.updateCanvas, drawQueue) gVars.m.mainloop() print "Cleaning up" #reset robot list and join active threads for robot in gVars.grobotList: robot.reset() comm.stop() comm.join() if pris_event_thread.isAlive(): pris_event_thread.join(.5) if pris_fsm_thread.isAlive(): pris_fsm_thread.join(.5) if guard_event_thread.isAlive(): guard_event_thread.join(.5) if guard_fsm_thread.isAlive(): guard_fsm_thread.join(.5) if draw_world_thread.isAlive(): draw_world_thread.join(.5) print "Goodbye."
class PathFollower(): def __init__(self): gMaxRobotNum = 1 # max number of robots to control self.comm = RobotComm(gMaxRobotNum) self.comm.start() print 'Bluetooth starts' robotList = self.comm.robotList self.robotList = robotList self.go = False self.done = False return def run(self, path): robot = None #0:N, 1:E, 2:S, 3:W currentDir = 0 while not self.done: for robot in self.robotList: if robot and self.go: ############################################# # START OF YOUR WORKING AREA!!! ############################################# speed = 40 # robot.set_wheel(0, 0) # robot.set_wheel(1,0) robot.set_wheel(0, speed) robot.set_wheel(1, speed) left = robot.get_floor(0) right = robot.get_floor(1) # print("Left: " + str(left)+"\t"+"Right: "+str(right)) if right < 75 and left < 75: #intersection?? robot.set_wheel(0, speed) robot.set_wheel(1, speed) time.sleep(0.2) robot.set_wheel(0, 0) robot.set_wheel(1, 0) left = robot.get_floor(0) right = robot.get_floor(1) if (True): nextDir = path[0] del path[0] robot.set_musical_note(40) time.sleep(0.1) robot.set_musical_note(0) tTime = 0.6 #straight if nextDir == currentDir: print("GOING straight") #right elif nextDir == currentDir + 1 or ( nextDir == 0 and currentDir == 3): print("TURNING RIGHT") robot.set_wheel(0, 50) robot.set_wheel(1, -50) time.sleep(tTime) #left elif nextDir == currentDir - 1 or ( nextDir == 3 and currentDir == 0): print("TURNING LEFT") robot.set_wheel(0, -50) robot.set_wheel(1, 50) time.sleep(tTime) elif nextDir == (currentDir + 2) % 4: print("TURNING AROUND???") robot.set_wheel(0, 50) robot.set_wheel(1, -50) time.sleep(2 * tTime) currentDir = nextDir elif (right - left) > 15: robot.set_wheel(1, speed) robot.set_wheel(0, 0) # print("Turning Left") time.sleep(0.03) elif (left - right) > 15: robot.set_wheel(0, speed) robot.set_wheel(1, 0) # print("Turning Right") time.sleep(0.03) def runPath(self, path): self.go = True thread = threading.Thread(name="Run Thread", target=self.run, args=[path]) thread.daemon = True thread.start() def exit(self): self.go = False self.done = True self.comm.stop() self.comm.join() print("terminated!") sys.exit(0)
def main(argv=None): global m global comm comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' m = tk.Tk() #root drawQueue = Queue.Queue(0) #creating tje virtual appearance of the robot canvas_width = 700 # half width canvas_height = 380 # half height rCanvas = tk.Canvas(m, bg="white", width=canvas_width * 2, height=canvas_height * 2) joystick = Joystick(comm, m, rCanvas, robot1_config) # visual elements of the virtual robot poly_points = [0, 0, 0, 0, 0, 0, 0, 0] joystick.vrobot.poly_id = rCanvas.create_polygon(poly_points, fill='blue') #robot joystick.vrobot.prox_l_id = rCanvas.create_line(0, 0, 0, 0, fill="red") #prox sensors joystick.vrobot.prox_r_id = rCanvas.create_line(0, 0, 0, 0, fill="red") joystick.vrobot.floor_l_id = rCanvas.create_oval( 0, 0, 0, 0, outline="white", fill="white") #floor sensors joystick.vrobot.floor_r_id = rCanvas.create_oval(0, 0, 0, 0, outline="white", fill="white") time.sleep(1) update_vrobot_thread = threading.Thread( target=joystick.update_virtual_robot) update_vrobot_thread.daemon = True update_vrobot_thread.start() #create the virtual worlds that contains the virtual robot vWorld = virtual_world(drawQueue, joystick.vrobot, rCanvas, canvas_width, canvas_height) boxes = [[0, 0, 40, 100], [40, 80, 140, 120], [120, -80, 200, 0], [0, -80, 40, -180], [40, -180, 300, -220], [300, -180, 340, 200], [300, 200, 0, 240]] for box in boxes: vWorld.add_obstacle(box) draw_world_thread = threading.Thread(target=draw_virtual_world, args=(vWorld, joystick)) draw_world_thread.daemon = True draw_world_thread.start() gui = VirtualWorldGui(vWorld, joystick, m) rCanvas.after(200, gui.updateCanvas, drawQueue) m.mainloop() for robot in joystick.gRobotList: robot.reset() comm.stop() comm.join()
def main(argv=None): global m, comm global sleepTime sleepTime = 0.01 comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' m = tk.Tk() #root drawQueue = Queue.Queue(0) global lastMoveDirection lastMoveDirection = "NORTH" canvas_width = 500 #original: 700 # half width canvas_height = 200 # original 380half height rCanvas = tk.Canvas(m, bg="black", width=canvas_width*2, height=canvas_height*2) global joystick joystick = Joystick(comm, m, rCanvas) # visual elements of the virtual robots pacman_points = [0,0,0,0,0,0,0,0] blinky_points = [120,120,120,120,120,120,120,120] inky_points =[-120,-120,-120,-120,-120,-120,-120,-120] poly_points = [pacman_points, blinky_points, inky_points] colors = ['yellow', 'red', 'cyan'] for agentIndex in range(3): joystick.vrobots[agentIndex].poly_id = rCanvas.create_polygon(poly_points[agentIndex], fill=colors[agentIndex]) #robots, pacman, blinky(top right), inky(bottom left) joystick.vrobots[agentIndex].prox_l_id = rCanvas.create_line(0,0,0,0, fill="red") #prox sensors joystick.vrobots[agentIndex].prox_r_id = rCanvas.create_line(0,0,0,0, fill="red") joystick.vrobots[agentIndex].floor_l_id = rCanvas.create_oval(0,0,0,0, outline="white", fill="white") #floor sensors joystick.vrobots[agentIndex].floor_r_id = rCanvas.create_oval(0,0,0,0, outline="white", fill="white") time.sleep(1) #create the virtual worlds that contains the virtual robot vWorld = virtual_world(drawQueue, joystick.vrobots, rCanvas, canvas_width, canvas_height) ''' objects in the world ''' rectangles = [] pellets = [] super_pellets = [] # pacman map/walls rectangles.append([-150, -150, 150, 150]) #overall square rectangles.append([-90, 90, -30, 30]) rectangles.append([90, 90, 30, 30]) rectangles.append([-90, -90, -30, -30]) rectangles.append([90, -90, 30, -30]) #pacman pellets/capsules/food for index_x in range(BOARD_SIZE): for index_y in range(BOARD_SIZE): x_position = -120 + (60*index_x) y_position = -120 + (60*index_y) if (index_x == 0 and index_y == 4) or (index_x == 4 and index_y == 0): new_super_pellet = [x_position, y_position] super_pellets.append(new_super_pellet) elif not ((index_x == 0 and index_y == 0) or (index_x ==2 and index_y == 2) or (index_x ==4 and index_y ==4) or (index_x == 1 and index_y == 1) or (index_x == 3 and index_y == 1) or (index_x == 1 and index_y == 3) or (index_x==3 and index_y==3)): new_pellet = [x_position, y_position] pellets.append(new_pellet) vWorld.add_score_label([175,-175]) for pill in pellets: vWorld.add_pellet(pill) for super_pill in super_pellets: vWorld.add_super_pellet(super_pill) for rect in rectangles: vWorld.add_obstacle(rect) draw_world_thread = threading.Thread(target=draw_virtual_world, args=(vWorld, joystick)) draw_world_thread.daemon = True draw_world_thread.start() gui = VirtualWorldGui(vWorld, m) gui.drawGrid() gui.drawMap() rCanvas.after(200, gui.updateCanvas, drawQueue) m.mainloop() for robot in joystick.gRobotList: robot.reset() comm.stop() comm.join()
def main(): global gRobotList, gQuit global gCanvas, frame, gHamsterBox global monitor_thread, dispatch_thread global gBeepQueue, gWheelQueue, drawQueue global vWorld global joystick comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth started' gRobotList = comm.robotList monitor_thread = False dispatch_thread= False # create UI: two separate Tkinter windows # 1. frame = course track display # 2. gFrame = localization scanning display frame = tk.Tk() canvas_width = 700 # half width canvas_height = 380 # half height gCanvas = tk.Canvas(frame, bg="white", width=canvas_width*2, height=canvas_height*2) draw_track() settings.init() settings.gFrame = tk.Tk() settings.gFrame.geometry('600x500') gRobotDraw = draw.RobotDraw(settings.gFrame, tk) # create scanning behavior settings.gBehaviors[0] = scanning.Behavior("scanning", gRobotList, 4.0, gRobotDraw.get_queue()) gRobotDraw.start() # create 2 virtual robot data objects vrobot = [] joystick = [] keyBindings = [] for robot_i in range(gMaxRobotNum): vrobot.append ( virtual_robot() ) pi4 = 3.1415 / 4 # robot starting positions vrobot[robot_i].set_robot_a_pos(pi4*2, -520 + robot_i * 40, +340 - robot_i * 80) # keyboard input if robot_i == 0: keyBindings = ['w','s','a','d','x'] elif robot_i == 1: keyBindings = ['i','k','j','l',','] joystick.append( Joystick(comm, frame, gCanvas, vrobot[robot_i], robot_i, keyBindings) ) poly_points = [0,0,0,0,0,0,0,0] joystick[robot_i].vrobot.poly_id = gCanvas.create_polygon(poly_points, fill='blue') #robot joystick[robot_i].vrobot.prox_l_id = gCanvas.create_line(0,0,0,0, fill="red") #prox sensors ---- here joystick[robot_i].vrobot.prox_r_id = gCanvas.create_line(0,0,0,0, fill="red") joystick[robot_i].vrobot.floor_l_id = gCanvas.create_oval(0,0,0,0, outline="white", fill="white") #floor sensors joystick[robot_i].vrobot.floor_r_id = gCanvas.create_oval(0,0,0,0, outline="white", fill="white") time.sleep(1) update_vrobot_thread = threading.Thread(target=joystick[robot_i].update_virtual_robot) update_vrobot_thread.daemon = True update_vrobot_thread.start() # virtual world UI drawQueue = Queue.Queue(0) vWorld = virtual_world(drawQueue, joystick[0], vrobot[0], gCanvas, canvas_width, canvas_height) landmark = [-500, 220, -460, 180] vWorld.add_obstacle(landmark) draw_world_thread = threading.Thread(target=draw_virtual_world, args=(vWorld,)) draw_world_thread.daemon = True draw_world_thread.start() gui = VirtualWorldGui(vWorld, frame) gui.drawGrid() gui.drawMap() gCanvas.after(200, gui.updateCanvas, drawQueue) frame.mainloop() gQuit = True for robot in gRobotList: robot.reset() time.sleep(1.0) comm.stop() comm.join() print 'Terminated'
def main(argv=None): global m comm = RobotComm(gMaxRobotNum) comm.start() print 'Bluetooth starts' m = tk.Tk() #root drawQueue = Queue.Queue(0) #creating the virtual appearance of the robot canvas_width = 700 # half width canvas_height = 380 # half height rCanvas = tk.Canvas(m, bg="white", width=canvas_width * 2, height=canvas_height * 2) vrobot = virtual_robot() pi4 = 3.1415 / 4 #----- initialize position and angle of robot -----# vrobot.set_robot_a_pos(-pi4 * 2, 230, 0) joystick = Joystick(comm, m, rCanvas, vrobot) # visual elements of the virtual robot poly_points = [0, 0, 0, 0, 0, 0, 0, 0] joystick.vrobot.poly_id = rCanvas.create_polygon(poly_points, fill='blue') #robot joystick.vrobot.prox_l_id = rCanvas.create_line( 0, 0, 0, 0, fill="red") #prox sensors ---- here joystick.vrobot.prox_r_id = rCanvas.create_line(0, 0, 0, 0, fill="red") joystick.vrobot.floor_l_id = rCanvas.create_oval( 0, 0, 0, 0, outline="white", fill="white") #floor sensors joystick.vrobot.floor_r_id = rCanvas.create_oval(0, 0, 0, 0, outline="white", fill="white") time.sleep(1) update_vrobot_thread = threading.Thread( target=joystick.update_virtual_robot) update_vrobot_thread.daemon = True update_vrobot_thread.start() #create the virtual worlds that contains the virtual robot, pass the joystick to reuse the move forward, stop, etc. functions # joystick should then be renamed to 'set of functions used to move robot forwards, stop robot, etc.' b/c joystick functionality is not used vWorld = virtual_world(drawQueue, joystick, joystick.vrobot, rCanvas, canvas_width, canvas_height) rectA = [-260, -20, -220, 20] vWorld.add_obstacle(rectA) draw_world_thread = threading.Thread(target=draw_virtual_world, args=(vWorld, joystick)) draw_world_thread.daemon = True draw_world_thread.start() gui = VirtualWorldGui(vWorld, m) gui.drawGrid() gui.drawMap() rCanvas.after(200, gui.updateCanvas, drawQueue) m.mainloop() for robot in joystick.gRobotList: robot.reset() comm.stop() comm.join()