def __move_reg(self, degrees, sensor_degrees): t = Thread(target=self.read_sensors) start_l, start_r = (self.lm_left.degrees, self.lm_right.degrees) distance_l, distance_r = 0, 0 while (distance_l < degrees or distance_r < degrees): speed_l, speed_r = self.__reg() self.lm_left.on(speed_l, brake=True) self.lm_right.on(speed_r, brake=True) ok = self.__check_button() if (ok is False): self.lm_left.stop() self.lm_right.stop() return False elif (ok is None): debug_print("none") self.lm_left.stop() self.lm_right.stop() return None if ((distance_l >= sensor_degrees or distance_r >= sensor_degrees) and not t.isAlive()): t.start() distance_l = abs(start_l - self.lm_left.degrees) distance_r = abs(start_r - self.lm_right.degrees) t.join() return True
def __check_button(self): timeout = time.time() + self.regulation_tick while (time.time() <= timeout): # check for touch sensor if (self.touch_left.is_pressed or self.touch_right.is_pressed): return False if (self.btn.left or self.btn.right): debug_print("pressed") return None time.sleep(0.01) return True
def Main(): map_var = Map(Ghost_mapping_type.none) io = IO() logic = Logic(map_var) button = Button() saver = Map_saver(map_var, button) saver.wait_for_load() print("READY") motors = { Moves.fwd: lambda: io.go_forward(), Moves.left: lambda: io.go_left(), Moves.right: lambda: io.go_right(), Moves.back: lambda: io.go_back() } mapping = { Moves.fwd: lambda: map_var.go_forward(), Moves.left: lambda: map_var.go_left(), Moves.right: lambda: map_var.go_right(), Moves.back: lambda: map_var.go_back() } motors[Moves.fwd]() mapping[Moves.fwd]() while (True): clr_sensor = io.directions_free() us_sensor = io.ghost_distance() map_var.write_sensor_values(clr_sensor, us_sensor) debug_print(map_var) move = logic.get_next_move() debug_print(map_var) switch[move]() if button.any(): saver.wait_for_load() ok = motors[move]() if (ok is False): io.after_crash() map_var.rotation += int(move) continue elif (ok is None): saver.wait_for_load() continue mapping[move]() saver.save_map()
def sensors_test(): io = IO() for _ in range(8): io.read_sensors() debug_print("us", io.ultrasonic_sensor_values) debug_print("color", io.color_sensor_values)
def touch_sensor_test(): io = IO() val = False while True: debug_print(io.go_forward()) time.sleep(1)
def after_crash(self): debug_print("crash") self.__move(self.after_crash_degrees, self.move_speed) self.read_sensors()
def load_map(self): debug_print("loaded") debug_print(self.map) with open("map.json", "r+") as f: self.map.load_map_data([[Map_tile(item) for item in col] for col in json.load(f)])
def save_map(self): debug_print("saved") with open("map.json", "w") as f: json.dump(self.map.map, f, default=lambda obj: obj.value if obj.value != 5 else 1)