class ZumiService(rpyc.Service): """ RPC server class # Note only methods that are _exposed_ are callable via rpc """ def __init__(self): self.myConnection = '' self.user = '' #init zumi instance self.zumi = Zumi() def on_connect(self, conn): """ method is automatically run at new rpc connections """ self.myConnection = conn print ('new connection') #print (conn._config['credentials']['subject']) #self.user = conn._config['credentials']['subject'][5][0][1] #if DEBUG: #print("new connection: ", # conn._config['endpoints'], "from user ", self.user) pass def on_disconnect(self, conn): """method is automatically run when rpc connection is terminated """ if DEBUG: print ("conn ended", conn._config['connid']) pass def exposed_ping(self): """simple ping for rpc connection testing """ if DEBUG: print ("ping") return 'pong' #----------------------------------------------------------------------------- # reproducing ZUMI API - see http://docs.robolink.com/zumi-library for details #----------------------------------------------------------------------------- # DRIVING def exposed_right_circle(self, speed=30, step=2): self.zumi.right_circle(speed, step) def exposed_hard_brake(self): self.zumi.hard_brake() def exposed_parallel_park(self,speed=15, step=1, delay=0.01): self.zumi.parallel_park(speed, step, delay) def exposed_circle(self,speed=30, step=2, direction=1, delay=0.02): self.zumi.circle(speed, step, direction, delay) def exposed_triangle(self,speed=40, seconds=1.5, direction=1): self.zumi.triangle(speed, seconds, direction) def exposed_go_straight(self,speed, desired_angle, max_speed=127): self.zumi.go_straight(speed, desired_angle, max_speed) def exposed_right_u_turn(self,speed=30, step=4, delay=0.02): self.zumi.right_u_turn(speed, step, delay) def exposed_figure_8(self,speed=30, step=3, delay=0.02): self.zumi.figure_8(speed, step, delay) def exposed_rectangle(self,speed=40, seconds=1.0, direction=1, ratio=2): self.zumi.rectangle(speed, seconds, direction, ratio) def exposed_reverse(self,speed=40, duration=1.0, desired_angle=123456): self.zumi.reverse(speed, duration, desired_angle) def exposed_left_u_turn(self,speed=30, step=4, delay=0.02): self.zumi.left_u_turn(speed, step, delay) def exposed_square(self,speed=40, seconds=1, direction=1): self.zumi.square(speed, seconds, direction) def exposed_square_left(self,speed=40, seconds=1.0): self.zumi.square_left(speed, seconds) def exposed_turn_left(self,desired_angle=90, duration=1.0): self.zumi.turn_left(desired_angle, duration) def exposed_j_turn(self,speed=80, step=4, delay=0.005): self.zumi.j_turn(speed, step, delay) def exposed_left_circle(self,speed=30, step=2): self.zumi.left_circle(speed, step) def exposed_forward(self,speed=40, duration=1.0, desired_angle=123456): self.zumi.forward(speed, duration, desired_angle) def exposed_turn_right(self,desired_angle=-90,duration=1.0): self.zumi.turn_right(desired_angle,duration) def exposed_square_right(self,speed=40, seconds=1.0): self.zumi.square_right(speed, seconds) def exposed_go_reverse(self, speed, desired_angle, max_speed=127): self.zumi.go_reverse(speed, desired_angle, max_speed) def exposed_line_follower(self, duration, left_thresh=100, right_thresh=100): self.zumi.line_follower(duration, left_thresh, right_thresh) #SENSORS def exposed_get_all_IR_data(self): return self.zumi.get_all_IR_data() def exposed_get_battery_voltage(self): return self.zumi.get_battery_voltage() #this is undocumented in the Zumi API def exposed_get_battery_percent(self): return self.zumi.get_battery_percent() def exposed_get_IR_data(self,ir_sensor_index): return self.zumi.get_IR_data(ir_sensor_index) # CAM - here we use picamera directly, avoiding the poor zumi interface def exposed_get_picture(self, resolution=(1024,768)): with picamera.PiCamera() as camera: camera.resolution = resolution camera.rotation = 180 output = np.empty((resolution[1],resolution[0],3),dtype=np.uint8) camera.capture(output , 'rgb') return output
class Drive: def __init__(self, _zumi=None): if _zumi is None: self.z = Zumi() else: self.z = _zumi self.z.mpu.calibrate_MPU() self.NORTH = 0 self.WEST = 90 self.EAST = -90 self.SOUTH = 180 self.heading = self.NORTH self.current_x = 0 self.current_y = 0 self.motor_speed = 10 self.ir_threshold = 125 # ----------------FUNCTIONS----------------- def turn(self, angle=91, speed=30, step=4, direction=-1, delay=0.01): direction = self.z.clamp(direction, -1, 1) init_ang_z = self.z.read_z_angle() for i in range(0, angle, step): self.z.go_straight(speed, init_ang_z + direction * i) time.sleep(delay) def cross_intersection(self): start = time.time() end = 0 while end < 0.4: end = time.time() - start self.z.go_straight(10, self.heading) def move_to_coordinate(self, desired_x, desired_y): dx = desired_x - self.current_x # Find out the difference in x dy = desired_y - self.current_y # Find out the difference in y if dx % 10 == 0 and dx != 5: if dx > 0: # If x is positive (going East) if not self.current_x == 0: self.cross_intersection() self.heading = self.EAST self.drive_block(dx) elif dx < 0: # If x is negative (going West) self.cross_intersection() self.heading = self.WEST self.drive_block(abs(dx)) self.current_x = desired_x if dy > 0: # If y is also positive (going North) self.cross_intersection() self.heading = self.NORTH self.drive_block(dy) elif dy < 0: # If y is negative (going South) self.cross_intersection() self.heading = self.SOUTH self.drive_block(abs(dy)) self.current_y = desired_y else: if dy > 0: # If y is also positive (going North) self.cross_intersection() self.heading = self.NORTH self.drive_block(dy) elif dy < 0: # If y is negative (going South) self.cross_intersection() self.heading = self.SOUTH self.drive_block(abs(dy)) self.current_y = desired_y if dx > 0: # If x is positive (going East) self.cross_intersection() self.heading = self.EAST self.drive_block(dx) elif dx < 0: # If x is negative (going West) if self.self.current_x != 0: self.cross_intersection() self.heading = self.WEST self.drive_block(abs(dx)) self.current_x = desired_x def park_left(self): self.turn(direction=1) self.z.forward(duration=0.5) def park_right(self): self.turn() self.z.forward(duration=0.5) def drive_block(self, x): left_on_white = False right_on_white = False right_switch = 0 left_switch = 0 while True: ir_readings = self.z.get_all_IR_data() bottom_right_ir = ir_readings[1] bottom_left_ir = ir_readings[3] front_left_ir = ir_readings[0] front_right_ir = ir_readings[5] if bottom_left_ir < self.ir_threshold: if not left_on_white: left_switch += 1 left_on_white = True else: left_on_white = False if bottom_right_ir < self.ir_threshold: if not right_on_white: right_switch += 1 right_on_white = True else: right_on_white = False if right_on_white and not left_on_white: self.heading -= 1 if left_on_white and not right_on_white: self.heading += 1 if right_switch == x or left_switch == x: break if front_left_ir < 70 or front_right_ir < 70: self.z.stop(0) continue #clear_output(wait=True) self.z.go_straight(self.motor_speed, self.heading) def run_demo(self, location): try: if location == "a": self.move_to_coordinate(10, 5) self.park_left() if location == "b": self.move_to_coordinate(15, 10) self.park_right() if location == "c": self.move_to_coordinate(25, 0) self.park_left() if location == "d": self.move_to_coordinate(5, 10) self.park_left() if location == "e": self.move_to_coordinate(15, 20) self.park_right() if location == "f": self.move_to_coordinate(20, 16) self.park_right() if location == "g": self.move_to_coordinate(0, 26) self.park_right() if location == "h": self.move_to_coordinate(10, 25) self.park_right() if location == "i": self.move_to_coordinate(26, 30) self.park_right() finally: self.z.stop()
class Route_new: def __init__(self, zumi = None): self.start_node = Point(0, 0) self.bigben = Point(5, 20) self.seattle = Point(25, 0) self.paris = Point(15, 10) self.NY = Point(10, 15) self.china = Point(25, 20) self.node1 = Point(10, 0) self.node2 = Point(20, 0) self.node3 = Point(30, 0) self.node4 = Point(0, 10) self.node5 = Point(10, 10) self.node6 = Point(20, 10) self.node7 = Point(30, 10) self.node8 = Point(0, 20) self.node9 = Point(10, 20) self.node10 = Point(20, 20) self.node11 = Point(30, 20) self.node12 = Point(0, 30) self.node13 = Point(10, 30) self.node14 = Point(30, 30) self.G = nx.Graph() self.generate_map() self.NORTH = 0 self.WEST = 90 self.EAST = -90 self.SOUTH = 180 self.heading = self.NORTH if zumi is None: # pass self.zumi = Zumi() self.motor_speed = 10 self.ir_threshold = 70 self.reverse = False def generate_map(self): # Directed Graph # G 그래프 만들기 (node 간의 edge가 존재하면 add_node 하고 add_edge 안해도 됨 self.G.add_edge(self.start_node, self.node1, distance=10) self.G.add_edge(self.start_node, self.node4, distance=10) self.G.add_edge(self.node1, self.node2, distance=10) self.G.add_edge(self.node1, self.node5, distance=10) self.G.add_edge(self.node2, self.seattle, distance=5) self.G.add_edge(self.node2, self.node6, distance=10) self.G.add_edge(self.seattle, self.node3, distance=5) self.G.add_edge(self.node3, self.node7, distance=10) self.G.add_edge(self.node4, self.node5, distance=10) self.G.add_edge(self.node4, self.node8, distance=10) self.G.add_edge(self.node5, self.NY, distance=5) self.G.add_edge(self.node5, self.paris, distance=5) self.G.add_edge(self.NY, self.node9, distance=5) self.G.add_edge(self.paris, self.node6, distance=5) self.G.add_edge(self.node6, self.node7, distance=10) self.G.add_edge(self.node6, self.node10, distance=10) self.G.add_edge(self.node7, self.node11, distance=10) self.G.add_edge(self.node8, self.bigben, distance=5) self.G.add_edge(self.node8, self.node12, distance=10) self.G.add_edge(self.bigben, self.node9, distance=5) self.G.add_edge(self.node9, self.node10, distance=10) self.G.add_edge(self.node9, self.node13, distance=10) self.G.add_edge(self.node10, self.china, distance=5) self.G.add_edge(self.china, self.node11, distance=5) self.G.add_edge(self.node11, self.node14, distance=10) self.G.add_edge(self.node12, self.node13, distance=10) self.G.add_edge(self.node13, self.node14, distance=20) def find_path(self, start, destination): start_point = start destination = destination # 연결 안 된 노드가 있을 경우를 방지 if nx.has_path(self.G, start_point, destination): path = nx.shortest_path(self.G, source=start_point, target=destination, weight='distance') else: print("No path!!") return if path is not None: print(path) return path def driving(self, start, destination): shortest_path = self.find_path(start, destination)[1:] current_node = start while len(shortest_path): print(shortest_path) next_node = shortest_path.pop(0) # found_obstacle = False found_obstacle = self.drive_to_nextnode(current_node,next_node) if found_obstacle: print("find obstacle : {}".format(found_obstacle)) self.go_back_to_node(found_obstacle) self.disconnect_route(current_node, next_node) self.driving(current_node, destination) return current_node = next_node self.zumi.stop() return def driving_without_reroute(self, start, destination): shortest_path = self.find_path(start, destination)[1:] current_node = start while len(shortest_path): print(shortest_path) next_node = shortest_path.pop(0) x = self.drive_to_nextnode(current_node,next_node) current_node = next_node self.zumi.stop() return def drive_to_nextnode(self, current, next): dx = next.x - current.x dy = next.y - current.y print("[{},{}]".format(dx, dy)) self.decide_turn_or_pass_intersection(dx, dy, current) # if max(abs(dx),abs(dy))%10: # result = self.drive_n_block(abs(dx + dy)+1) # else: result = self.drive_n_block(abs(dx+dy)) return result def decide_turn_or_pass_intersection(self, dx, dy, current): print("check turn or not") temp = current.x + current.y if self.reverse: self.reverse = False elif temp and temp % 10 == 0: self.cross_intersection() if dx > 0: new_heading = self.EAST elif dx < 0: new_heading = self.WEST elif dy > 0: new_heading = self.NORTH else: new_heading = self.SOUTH if not new_heading - 20 < self.heading < new_heading + 20: print("change heading") self.heading = new_heading def drive_n_block(self, n): print("drive n block") ir_readings = self.zumi.get_all_IR_data() left_on_white = False if ir_readings[3] > self.ir_threshold else True right_on_white = False if ir_readings[1] > self.ir_threshold else True right_switch = 0 left_switch = 0 while left_on_white or right_on_white: ir_readings = self.zumi.get_all_IR_data() if ir_readings[3] < self.ir_threshold: if not left_on_white: left_on_white = True else: left_on_white = False if ir_readings[1] < self.ir_threshold: if not right_on_white: right_on_white = True else: right_on_white = False self.adjust_driving(left_on_white, right_on_white) if ir_readings[0] < 70 or ir_readings[5] < 70: return max(right_switch, left_switch) self.zumi.go_straight(self.motor_speed, self.heading) while right_switch != n and left_switch != n: print("{},{}".format(left_switch, right_switch)) ir_readings = self.zumi.get_all_IR_data() if ir_readings[3] < self.ir_threshold: if not left_on_white: left_switch += 1 left_on_white = True else: left_on_white = False if ir_readings[1] < self.ir_threshold: if not right_on_white: right_switch += 1 right_on_white = True else: right_on_white = False self.adjust_driving(left_on_white, right_on_white) # detect obstacle if ir_readings[0] < 70 or ir_readings[5] < 70: return max(right_switch, left_switch) self.zumi.go_straight(self.motor_speed, self.heading) return False def adjust_driving(self, left_on_white, right_on_white, reverse=1): if right_on_white and not left_on_white: correction = -1 elif left_on_white and not right_on_white: correction = 1 else: return self.heading += correction*reverse def cross_intersection(self): print("cross road") start = time.time() end = 0 while end < 0.45: end = time.time()-start self.zumi.go_straight(10, self.heading) def go_back_to_node(self, n): self.reverse = True left_on_white = False right_on_white = False right_switch = 0 left_switch = 0 while right_switch != n and left_switch != n: print( "{},{}".format(left_switch, right_switch)) ir_readings = self.zumi.get_all_IR_data() if ir_readings[3] < self.ir_threshold: if not left_on_white: left_switch += 1 left_on_white = True else: left_on_white = False if ir_readings[1] < self.ir_threshold: if not right_on_white: right_switch += 1 right_on_white = True else: right_on_white = False self.adjust_driving(left_on_white, right_on_white, reverse=-1) self.zumi.go_reverse(self.motor_speed, self.heading) # while right_on_white or left_on_white: # ir_readings = self.zumi.get_all_IR_data() # # if ir_readings[3] < self.ir_threshold: # if not left_on_white: # left_switch += 1 # left_on_white = True # else: # left_on_white = False # # if ir_readings[1] < self.ir_threshold: # if not right_on_white: # right_switch += 1 # right_on_white = True # else: # right_on_white = False # # self.adjust_driving(left_on_white, right_on_white, reverse=-1) # # # detect obstacle # if ir_readings[0] < 70 or ir_readings[5] < 70: # return max(right_switch, left_switch) # # self.zumi.go_reverse(self.motor_speed, self.heading) time.sleep(0.4) print("done") def disconnect_route(self, current_node, next_node): # self.G.add_edge(current_node, next_node, distance=1000) self.G.remove_edge(current_node, next_node) def reset_map(self): self.G = nx.Graph() self.generate_map() def turn(self, angle=91, speed=30, step=4, direction=-1, delay=0.01): direction = self.zumi.clamp(direction,-1,1) init_ang_z = self.zumi.read_z_angle() for i in range(0, angle, step): self.zumi.go_straight(speed, init_ang_z+direction*i) time.sleep(delay) def park_left(self): self.turn(direction=1) self.zumi.forward(duration=0.5) def park_right(self): self.turn() self.zumi.forward(duration=0.5)
from zumi.zumi import Zumi from zumi.util.screen import Screen import time zumi = Zumi() speed = 30 threashold = 70 prev_state = -1 HEAD = zumi.read_z_angle() try: while True: ir_readings = zumi.get_all_IR_data() left = ir_readings[1] right = ir_readings[3] print(left, right) if left > threashold and right > threashold: print('1') zumi.control_motors(speed, speed, 0) prev_state = 0 elif left < threashold and right > threashold: print('2') zumi.control_motors(-int(speed/3), int(speed/3), 0) prev_state = 1 elif left > threashold and right < threashold: print('3') zumi.control_motors(int(speed/3), -int(speed/3), 0) prev_state = 2 else: