def main(): """ This code, which must run on the ROBOT: 1. Constructs a robot, an MQTT SENDER, and a DELEGATE to respond to messages FROM the LAPTOP sent TO the ROBOT via MQTT. 2. Stays in an infinite loop while a listener (for MQTT messages) runs in the background, "delegating" work to the "delegate". """ robot = rb.RoseBot() delegate = DelegateForRobotCode(robot) mqtt_sender = mqtt.MqttClient(delegate) delegate.set_mqtt_sender(mqtt_sender) number = robot_number.get_robot_number() mqtt_sender.connect_to_mqtt_to_talk_to_laptop(lego_robot_number=number) time.sleep(1) # To let the connection process complete print() print("Starting to listen for messages. The delegate responds to them.") print() # Stay in an infinite loop while the listener (for MQTT messages) # runs in the background: while True: time.sleep(0.01) if delegate.is_time_to_quit: break
def print_beacon_sensor_readings(seconds_between_readings): robot = rb.RoseBot() # Fresh RoseBot so that sensors do not conflict. print() print("--------------------------------------------------") print("Testing the Beacon Sensor of a robot") print("--------------------------------------------------") print() print("This function displays readings,") print("once per {:5.2f} second,".format(seconds_between_readings)) print("from the Beacon (the stand-alone remote-control thing).") print() print("While this test is running, try turning the Beacon") print("on, then moving it to various places in front") print("of the robot, as well as places out of the Infrared") print("Sensors field of vision. Then try turning the Beacon off.") print() print("Stop this test by pressing Control-C when desired.") input("Press the ENTER key when ready to start getting readings.") try: while True: message = "Distance (inches), heading (sort of degrees): {:3} {:3}" print( message.format(robot.beacon_sensor.get_distance(), robot.beacon_sensor.get_heading())) time.sleep(seconds_between_readings) except KeyboardInterrupt: print() print("OK, you just did a keyboard interrupt (Control-C).") print("No worries. The program will keep running from here.")
def print_color_sensor_readings(seconds_between_readings): robot = rb.RoseBot() # Fresh RoseBot so that sensors do not conflict. print() print("--------------------------------------------------") print("Testing the Color Sensor of a robot") print("--------------------------------------------------") print() print("This function displays readings,") print("once per {:5.2f} second,".format(seconds_between_readings)) print("from the downward-facing physical Color Sensor.") print() print("While this test is running, try moving the robot around,") print("placing the physical Color Sensor on different colors.") print() print("Stop this test by pressing Control-C when desired.") input("Press the ENTER key when ready to start getting readings.") try: while True: print("As name, number, reflectance: {:8} {:1} {:3}".format( robot.color_sensor.get_color_as_name(), robot.color_sensor.get_color_as_number(), robot.color_sensor.get_reflected_light_intensity())) time.sleep(seconds_between_readings) except KeyboardInterrupt: print() print("OK, you just did a keyboard interrupt (Control-C).") print("No worries. The program will keep running from here.")
def print_infrared_proximity_readings(seconds_between_readings): robot = rb.RoseBot() # Fresh RoseBot so that sensors do not conflict. print() print("--------------------------------------------------") print("Testing the Infrared Proximity Sensor of a robot") print("--------------------------------------------------") print() print("This function displays readings,") print("once per {:5.2f} second,".format(seconds_between_readings)) print("from the forward-facing physical Infrared Proximity") print("Sensor (the thing on the front of the claw).") print() print("While this test is running, try putting your hand") print("different distances from that physical Infrared sensor.") print() print("Stop this test by pressing Control-C when desired.") input("Press the ENTER key when ready to start getting readings.") try: while True: print("Distance in inches: {:5.2f}".format( robot.infrared_proximity_sensor.get_distance_in_inches())) time.sleep(seconds_between_readings) except KeyboardInterrupt: print() print("OK, you just did a keyboard interrupt (Control-C).") print("No worries. The program will keep running from here.")
def print_touch_sensor_readings(seconds_between_readings): robot = rb.RoseBot() # Fresh RoseBot so that sensors do not conflict. print() print("--------------------------------------------------") print("Testing the Touch Sensor of a robot") print("--------------------------------------------------") print() print("This function displays readings,") print("once per {:5.2f} second,".format(seconds_between_readings)) print("from the physical Touch Sensor that is underneath") print("the physical motor at the top of the Arm and Claw.") print() print("While this test is running, try pressing and releasing") print("that physical Touch Sensor to see its readings.") print("") print("Stop this test by pressing Control-C when desired.") input("Press the ENTER key when ready to start getting readings.") try: while True: print("Pressed (True or False)? Value (0 or 1)?: {:5} {}".format( str(robot.touch_sensor.is_pressed()), robot.touch_sensor.get_reading())) time.sleep(seconds_between_readings) except KeyboardInterrupt: print() print("OK, you just did a keyboard interrupt (Control-C).") print("No worries. The program will keep running from here.")
def make_sounds(): try: robot = rb.RoseBot() print() print("--------------------------------------------------") print("Demonstrating how to make sounds.") print("--------------------------------------------------") print() print("Remember: Leave time between any sound-making,") print("else sounds may be clipped. If sounds are") print("clipped, you may have to reboot the robot.") print() print("Beep 5 times.") for k in range(5): robot.sound.beep() time.sleep(0.2) print() print("Play a fun song.") robot.sound.play_vader_song() print() print("Play 5 tones.") for tone in range(100, 350, 50): duration = 50 # milliseconds robot.sound.play_tone(tone, duration) print() print("Play a 6-tone sequence.") robot.sound.play_tone_sequence([(100, 200, 10), (150, 50, 50), (200, 100, 10), (250, 1000, 20), (300, 50, 5), (350, 50, 10)]) print() print("Play a WAV file.") robot.sound.play_lego_wav_file() print() print("Speak. Speaking may not work if you make") print("other sounds before speaking.") print("After speaking, other sounds may fail.") print("Leave plenty of time for the speech") print("to happen, and use SHORT phrases.") robot.sound.speak("Greetings, Earthlings.") time.sleep(3) except KeyboardInterrupt: print() print("OK, you just did a keyboard interrupt (Control-C).") print("No worries. The program will keep running from here.")
def run_test_raise_and_lower(): """ Tests the raise and lower methods of the ArmAndClaw class. """ print() print("------------------------------------------") print("Testing the raise and lower methods") print(" of the ArmAndClaw class.") print("------------------------------------------") print() # Start with a fresh (uncalibrated) ArmAndClaw. robot = rb.RoseBot() try: print("Press the ENTER key when ready for the robot") print("to go ** DOWN ** all the way.") print("Since it has not yet been calibrated, it will first") print("go UP, then DOWN, to calibrate.") print(" If it does NOT do that and seems to be stuck,") input(" press Control-C to force this test to stop.") # --------------------------------------------------------------------- # TODO: 5. Replace the pass statement below with a call to # the lower_arm method on the arm_and_claw of the given RoseBot. # Then print a simple message like "Lowered!" # --------------------------------------------------------------------- pass print("Press the ENTER key when ready for the robot") print("to go ** UP ** all the way.") print(" If it does NOT do that and seems to be stuck,") input(" press Control-C to force this test to stop.") # --------------------------------------------------------------------- # TODO: 6. Replace the pass statement below with a call to # the raise_arm method on the arm_and_claw of the given RoseBot. # Then print a simple message like "Raised!" # --------------------------------------------------------------------- pass # --------------------------------------------------------------------- # TODO: 7. Replace the pass statement below with a call to # the lower_arm method on the arm_and_claw of the given RoseBot. # Then print a simple message like "Lowered!" # --------------------------------------------------------------------- pass except KeyboardInterrupt: print() print("OK, you just did a keyboard interrupt (Control-C).") print("No worries. The program will keep running from here.")
def run_test_drive_system(): """ Test the DRIVE SYSTEM of a Snatch3r robot. """ print() print("--------------------------------------------------") print("Testing the DRIVE SYSTEM of a robot") print("--------------------------------------------------") # ------------------------------------------------------------------------- # TODO: 4. With your instructor, construct a robot, that is, # a rosebot.RoseBot() object. # ------------------------------------------------------------------------- # SOLUTION CODE: Delete later. robot = rosebot.RoseBot()
def main(): """ Tests the BrickButtons of a Snatch3r robot. """ print() print("--------------------------------------------------") print("Testing the BrickButtons of a robot") print("--------------------------------------------------") # ------------------------------------------------------------------------- # TODO: 3. The following constructs a RoseBot object, then sends it as an # argument to the TEST functions. In those TEST functions, you will # access the RoseBot object's brick_buttons instance variable to make # the physical buttons on the EV3 Brick do things. # Change this _TODO_ to DONE after you understand the following code. # ------------------------------------------------------------------------- robot = rb.RoseBot() run_test_brick_buttons(robot)
def main(): """ Tests the DriveSystem of a Snatch3r robot. """ print() print("--------------------------------------------------") print("Testing the DriveSystem class of a RoseBot") print("--------------------------------------------------") # ------------------------------------------------------------------------- # TODO: 3. The following constructs a RoseBot object, then sends it as an # argument to the TEST functions. In those TEST functions, you will access # the RoseBot object's drive_system instance variable to make the robot # move. Change this _TODO_ to DONE after you understand the following code. # ------------------------------------------------------------------------- robot = rb.RoseBot() run_test_go_stop(robot) run_test_go_straight_for_seconds(robot)
def main(): """ Tests the Leds of a Snatch3r robot. """ print() print("--------------------------------------------------") print("Testing the Leds of a robot") print("--------------------------------------------------") # ------------------------------------------------------------------------- # TODO: 3. The following constructs a RoseBot object, then sends it as an # argument to the TEST functions. In those TEST functions, you will # access the RoseBot object's leds instance variable to make # the physical LEDs turn on and off with various colors. # Change this _TODO_ to DONE after you understand the following code. # ------------------------------------------------------------------------- robot = rb.RoseBot() run_test_leds_on_off(robot) run_test_leds_colors(robot)
def run_test_move_arm_to_position(): """ Tests the move_arm_to_position method of the ArmAndClaw class. """ print() print("-----------------------------------------=-") print("Testing the move_arm_to_position method") print(" of the ArmAndClaw class.") print("-------------------------------------------") print() # Start with a fresh (uncalibrated) ArmAndClaw. robot = rb.RoseBot() # Positions to which to move the Arm, one at a time. positions = [1200, 200, 1000, 2000, 1000, 500, 14 * 360, 0] print() print("READ the above tests to know what movement to expect") print("in the following tests.") try: for k in range(len(positions)): print() print("Press the ENTER key when ready for the robot") print("to move to position {}.".format(positions[k])) print("Since it has not yet been calibrated, it will first") print( "go UP, then DOWN, to calibrate, then to the above position.") print(" If it does NOT do that and seems to be stuck,") input(" press Control-C to force this test to stop.") # ----------------------------------------------------------------- # TODO: 8. Replace the pass statement below with a call to # the move_arm_to_position method on the arm_and_claw # of the given RoseBot, sending it positions[k]. # ----------------------------------------------------------------- pass # SOLUTION CODE: Delete from the project given to students. robot.arm_and_claw.move_arm_to_position(positions[k]) except KeyboardInterrupt: print() print("OK, you just did a keyboard interrupt (Control-C).") print("No worries. The program will keep running from here.")
def main(): """ Tests the TouchSensor of a Snatch3r robot. """ print() print("--------------------------------------------------") print("Testing the TOUCH SENSOR of a robot") print("--------------------------------------------------") # ------------------------------------------------------------------------- # TODO: 3. The following constructs a TouchSensor object, # then sends it as an argument to the TEST functions. In those TEST # functions, you will access the methods of the TouchSensor object. # Change this _TODO_ to DONE after you understand the following code. # ------------------------------------------------------------------------- robot = rb.RoseBot() run_test_get_reading(robot) run_test_is_pressed(robot) run_test_wait_until_pressed(robot) run_test_wait_until_released(robot)
def main(): """ Tests the ArmAndClaw of a Snatch3r robot. """ print() print("--------------------------------------------------") print("Testing the ArmAndClaw class of a RoseBot") print("--------------------------------------------------") # ------------------------------------------------------------------------- # TODO: 3. The following constructs a RoseBot object, then sends it as an # argument to the first TEST function. In that TEST functions, you will # access the RoseBot object's arm_and_claw instance variable to make # the physical Arm (and associated Claw) move. The subsequent test # functions construct their own RoseBot, thus starting "fresh" # (i.e., with an uncalibrated ArmAndClaw). # Change this _TODO_ to DONE after you understand the following code. # ------------------------------------------------------------------------- robot = rb.RoseBot() run_test_calibrate(robot) run_test_raise_and_lower() run_test_move_arm_to_position()
def print_camera_readings(seconds_between_readings): robot = rb.RoseBot() # Fresh RoseBot so that sensors do not conflict. print() print("--------------------------------------------------") print("Testing the Camera of a robot") print("--------------------------------------------------") print() print("This function displays readings,") print("once per {:5.2f} second,".format(seconds_between_readings)) print("from the Camera.") print() print("Before running this test, train your Camera") print("on a colored object to get a good color model.") print("Also, make sure the Camera is NOT") print("in Arduino mode.") print() print("While this test is running, try putting the object") print("that you used for training in front of the physical") print("camera, then slowly moving it to various places") print("to the left/right and up/down, as well as") print("places out of the Camera's field of vision.") print() print("Stop this test by pressing Control-C when desired.") input("Press the ENTER key when ready to start getting readings.") try: while True: blob = robot.camera.get_biggest_blob() print(blob) time.sleep(seconds_between_readings) except KeyboardInterrupt: print() print("OK, you just did a keyboard interrupt (Control-C).") print("No worries. The program will keep running from here.")
def print_remote_control_readings(seconds_between_readings): robot = rb.RoseBot() # Fresh RoseBot so that sensors do not conflict. print() print("--------------------------------------------------") print("Testing the Remote Control Sensor of a robot") print("--------------------------------------------------") print() print("This function displays readings,") print("once per {:5.2f} second,".format(seconds_between_readings)) print("from the Remote Control (the stand-alone thing).") print() print("While this test is running, try pressing the") print("Remote Control's four buttons (one at a time)") print("while pointing the Remote Control toward the front") print("of the robot (and elsewhere too).") print() print("Also try the red switch as its four settings.") print("When no button is pressed, nothing will be printed.") print() print("Stop this test by pressing Control-C when desired.") input("Press the ENTER key when ready to start getting readings.") try: while True: for red_switch in [1, 2, 3, 4]: for button in ["red_up", "red_down", "blue_up", "blue_down"]: if robot.remote_control.is_pressed(red_switch, button): msg = "Button {} with red switch at {} is pressed." print(msg.format(button, red_switch)) time.sleep(seconds_between_readings) except KeyboardInterrupt: print() print("OK, you just did a keyboard interrupt (Control-C).") print("No worries. The program will keep running from here.")