def color_collision_protocol(color_sensor_tuple): leds.set_color("LEFT", "RED") leds.set_color("RIGHT", "RED") if color_sensor_tuple[0]: stop_both() move_back(15, TIME) turn_left(-30, TIME) turn_right(30, TIME) elif color_sensor_tuple[2]: stop_both() move_back(15, TIME) turn_right(-30, TIME) turn_left(30, TIME) elif color_sensor_tuple[1]: move_back(15, 1) if random.randint(1, 2) == 1: turn_left(-10, TIME) turn_right(10, TIME) else: turn_right(-10, TIME) turn_left(10, TIME) leds.set_color("LEFT", "GREEN") leds.set_color("RIGHT", "GREEN")
def touch_collision_protocol(touch_sensor_tuple): stop_both() move_back(15, TIME) if touch_sensor_tuple[0]: turn_left(30, TIME) turn_right(-30, TIME) elif touch_sensor_tuple[1]: turn_left(-30, TIME) turn_right(30, TIME)
def ultrasonic_collision_protocol(): stop_both() leds.set_color("LEFT", "RED") leds.set_color("RIGHT", "RED") move_back(15, TIME) if random.randint(1, 2) == 1: turn_left(-30, TIME) turn_right(30, TIME) else: turn_left(30, TIME) turn_right(-30, TIME) leds.set_color("LEFT", "GREEN") leds.set_color("RIGHT", "GREEN")
def detect_lakes(color_sensor_tuple, lakes): color_sensor_firing = [False, False, False] has_time_elapsed = time() - globalStart >= FIND_LAKES_TIMEOUT global lakes_found, find_lakes_duration if has_time_elapsed: lakes_found = True find_lakes_duration = floor(time() - globalStart) for index, color in enumerate(color_sensor_tuple): if color in lakes: color_sensor_firing[index] = True stop_both() if color == RED and not color_found[RED] and not has_time_elapsed: process_lake(RED, "Red") if color == YELLOW and not color_found[YELLOW] and not has_time_elapsed: process_lake(YELLOW, "Yellow") if color == BLUE and not color_found[BLUE] and not has_time_elapsed: process_lake(BLUE, "Blue") if color == GREEN and not color_found[GREEN] and not has_time_elapsed: process_lake(GREEN, "Green") color_collision_protocol(color_sensor_firing) ultrasonic_back_collision_protocol()
def ultrasonic_back_collision_protocol(): if us_back.value() / 10 >= 4: stop_both() sleep(0.4) move_both_for_seconds(30, TIME)
find_lakes_duration = sys.maxsize push_bricks_duration = sys.maxsize # start timers globalStart = time() while mission_ongoing(): color_collision_protocol(detect_line(BORDER_COLOR)) ultrasonic_back_collision_protocol() detect_lakes(detect_color(), lakes_to_find) move_both(SPEED) push_bricks() detect_touch() # mission completed (or timeout exceeded) stop_both() s.speak("Exploration finished") s.speak("Let us celebrate") celebrate("sing") s.speak("Now let me park") park_rover(BORDER_COLOR) stop_both() create_mission_report() s.speak("Mission report generated") s.speak("Mission") arb.write_to_socket(sock_out, False)