def main(): print("--------------------------------------------") print(" Beacon seeking") print("--------------------------------------------") ev3.Sound.speak("Beacon seeking") robot = robo.Snatch3r() try: while True: found = seek_beacon(robot) # d: 5. Save the result of the seek_beacon function (a bool), then use that value to only say "Found the # beacon" if the return value is True. (i.e. don't say "Found the beacon" if the attempts was cancelled.) if found: ev3.Sound.speak("Found the beacon") command = input( "Hit enter to seek the beacon again or enter q to quit: ") if command == "q": break except: traceback.print_exc() ev3.Sound.speak("Error") print("Goodbye!") ev3.Sound.speak("Goodbye").wait()
def main(): print("--------------------------------------------") print(" Pixy display") print(" Press the touch sensor to exit") print("--------------------------------------------") ev3.Sound.speak("Pixy display").wait() print("Press the touch sensor to exit this program.") # Done 2 # Then connect to the pc using the connect_to_pc method. mqtt_client = com.MqttClient() robot = robo.Snatch3r() mqtt_client.connect_to_pc() robot.pixy.mode = "SIG1" while not robot.touch_sensor.is_pressed: # Done 3 # x = robot.pixy.value(1) y = robot.pixy.value(2) width = robot.pixy.value(3) height = robot.pixy.value(4) print("(X, Y)=({}, {}) Width={} Height={}".format(x, y, width, height)) # Done 4 # If you open m2_pc_pixy_display you can see the parameters for that method [x, y, width, height] mqtt_client.send_message('on_rectangle_update', [x,y,width,height]) time.sleep(0.25) print("Goodbye!") ev3.Sound.speak("Goodbye").wait() mqtt_client.close()
def main(): print("--------------------------------------------") print(" Color tracking") print("--------------------------------------------") ev3.Sound.speak("Color tracking").wait() print("Press the touch sensor to exit this program.") # This code assumes you have setup the pixy object on the Snatch3r class. # Add the pixy property to that class if you have not done so already. robot = robo.Snatch3r() robot.pixy.mode = "SIG1" turn_speed = 100 while not robot.touch_sensor.is_pressed: # TODO: 2. Read the Pixy values for x and y # Print the values for x and y # TODO: 3. Use the x value to turn the robot # If the Pixy x value is less than 150 turn left (-turn_speed, turn_speed) # If the Pixy x value is greater than 170 turn right (turn_speed, -turn_speed) # If the Pixy x value is between 150 and 170 stop the robot # Continuously track the color until the touch sensor is pressed to end the program. time.sleep(0.25) print("Goodbye!") ev3.Sound.speak("Goodbye").wait()
def main(): print("--------------------------------------------") print(" Beep at hands") print("--------------------------------------------") ev3.Sound.speak("Beep at hands") print("Press the touch sensor to exit this program.") robot = robo.Snatch3r() # Note, it is assumed that you have a touch_sensor property on the Snatch3r class. # Presumably you added this in the digital_inputs unit, if not add it now so that # the code below works to monitor the touch_sensor. while not robot.touch_sensor.is_pressed: # d: 2. Implement the module as described in the opening comment block. # It is recommended that you add to your Snatch3r class's constructor the ir_sensor, as shown # self.ir_sensor = ev3.InfraredSensor() # assert self.ir_sensor # Then here you can use a command like robot.ir_sensor.proximity if robot.ir_sensor.proximity < 10: ev3.Sound.beep() time.sleep(1.5) print(robot.ir_sensor.proximity) time.sleep(0.1) # TODO: 3. Call over a TA or instructor to sign your team's checkoff sheet. # # Observations you should make, the instance variable robot.ir_sensor.proximity is always updating with a distance. print("Goodbye!") ev3.Sound.speak("Goodbye").wait()
def main(): print("--------------------------------------------") print(" Drive polygon") print("--------------------------------------------") ev3.Sound.speak("Drive polygon").wait() robot = robo.Snatch3r() while True: speed_deg_per_second = int(input("Speed (0 to 900 dps): ")) if speed_deg_per_second == 0: break sides = int(input("Number of sides: ")) # Tip for later, try a negative value for Number of sides: to drive CW around the polygon instead of CCW. if sides == 0: break turn_amount = 360 / sides edge_length_in = int(input("Length of each edge (inches): ")) if edge_length_in == 0: break # TODO: 2. Individually implement the code here to use your drive_inches and turn_degrees library methods to # drive a polygon with the correct number of sides. (Hint: You will add 3 lines of code. What are they?). # TODO: 3. Call over a TA or instructor to sign your team's checkoff sheet and do a code review. # You are done with the Motors unit! # # Observations you should make, by making library functions you can make this program in only 3 lines of code. print("Goodbye!") ev3.Sound.speak("Goodbye").wait()
def main(): # -------------------------------------------------------------- # We have already implemented this module for you. # There are no TODOs in the code. Do NOT modify it. # You are not allowed to make any changes to this code. # -------------------------------------------------------------- print("--------------------------------------------") print(" Drive inches") print("--------------------------------------------") ev3.Sound.speak("Drive inches").wait() robot = robo.Snatch3r() while True: speed_deg_per_second = int(input("Speed (0 to 900 dps): ")) if speed_deg_per_second == 0: break inches_target = int(input("Distance (inches): ")) if inches_target == 0: break robot.drive_inches(inches_target, speed_deg_per_second) ev3.Sound.beep().wait() # Fun little beep print("Goodbye!") ev3.Sound.speak("Goodbye").wait()
def main(): print("--------------------------------------------") print(" Drive to the color") print(" Up button goes to Red") print(" Down button goes to Blue") print(" Left button goes to Black") print(" Right button goes to White") print("--------------------------------------------") ev3.Sound.speak("Drive to the color").wait() print("Press Back to exit this program.") robot = robo.Snatch3r() dc = DataContainer() # For our standard shutdown button. btn = ev3.Button() # Done 2 Uncomment the lines below to setup event handlers for these buttons. btn.on_up = lambda state: drive_to_color(state, robot, ev3.ColorSensor.COLOR_RED) btn.on_down = lambda state: drive_to_color(state, robot, ev3.ColorSensor.COLOR_BLUE) btn.on_left = lambda state: drive_to_color(state, robot, ev3.ColorSensor.COLOR_BLACK) btn.on_right = lambda state: drive_to_color(state, robot, ev3.ColorSensor.COLOR_WHITE) btn.on_backspace = lambda state: handle_shutdown(state, dc) while dc.running: btn.process() time.sleep(0.01) print("Goodbye!") ev3.Sound.speak("Goodbye").wait()
def main(): # -------------------------------------------------------------- # We have already implemented this module for you. # There are no TODOs in the code. Do NOT modify it. # You are not allowed to make any changes to this file. # -------------------------------------------------------------- print("--------------------------------------------") print(" Arm movement via library") print("--------------------------------------------") ev3.Sound.speak("Arm movement via library").wait() robot = robo.Snatch3r() while True: command_to_run = input( "Enter c (for calibrate), u (for up), d (for down), or q (for quit): " ) if command_to_run == 'c': print("Calibrate the arm") robot.arm_calibration() elif command_to_run == 'u': print("Move the arm to the up position") robot.arm_up() elif command_to_run == 'd': print("Move the arm to the down position") robot.arm_down() elif command_to_run == 'q': break else: print(command_to_run, "is not a known command. Please enter a valid choice.") ev3.Sound.speak("Goodbye").wait()
def main(): print("--------------------------------------------") print(" Beep at blue") print("--------------------------------------------") ev3.Sound.speak("Beep at blue").wait() print("Press the touch sensor to exit this program.") robot = robo.Snatch3r() robot.pixy.mode = "SIG1" while not robot.touch_sensor.is_pressed: # Done 2 # It is recommended that you add to your Snatch3r class's constructor the pixy object, as shown # self.pixy = ev3.Sensor(driver_name="pixy-lego") # assert self.pixy # Then here you can use a command like width = robot.pixy.value(3) x = robot.pixy.value(1) y = robot.pixy.value(2) width = robot.pixy.value(3) height = robot.pixy.value(4) print(x, y, width, height) if width > 0: ev3.Sound.beep() print(x, y, width, height) time.sleep(1) print("Goodbye!") ev3.Sound.speak("Goodbye").wait()
def main(): robot = robo.Snatch3r() mqtt_client = com.MqttClient(robot) mqtt_client.connect_to_pc() # mqtt_client.connect_to_pc("35.194.247.175") # Off campus IP address of a GCP broker robot.loop_forever( ) # Calls a function that has a while True: loop within it to avoid letting the program end.
def main(): print("--------------------------------------------") print(" Beacon pickup") print("--------------------------------------------") ev3.Sound.speak("Beacon pickup").wait() ##################################################### # There are no TODOs in this code. # Your only edits will be in the Snatch3r class. ##################################################### robot = robo.Snatch3r() try: while True: found_beacon = robot.seek_beacon() if found_beacon: ev3.Sound.speak("I got the beacon") robot.arm_up() time.sleep(1) robot.arm_down() command = input( "Hit enter to seek the beacon again or enter q to quit: ") if command == "q": break except: traceback.print_exc() ev3.Sound.speak("Error") print("Goodbye!") ev3.Sound.speak("Goodbye").wait()
def main(): print("--------------------------------------------") print("IR Remote") print(" - Use IR remote channel 1 to drive around") print(" - Use IR remote channel 2 to for the arm") print(" - Press the Back button on EV3 to exit") print("--------------------------------------------") ev3.Sound.speak("I R Remote") ev3.Leds.all_off() # Turn the leds off robot = robo.Snatch3r() dc = DataContainer() # TODO: 4. Add the necessary IR handler callbacks as per the instructions above. # Remote control channel 1 is for driving the crawler tracks around (none of these functions exist yet below). # Remote control channel 2 is for moving the arm up and down (all of these functions already exist below). # For our standard shutdown button. btn = ev3.Button() btn.on_backspace = lambda state: handle_shutdown(state, dc) robot.arm_calibration() # Start with an arm calibration in this program. while dc.running: # TODO: 5. Process the RemoteControl objects. btn.process() time.sleep(0.01) # TODO: 2. Have everyone talk about this problem together then pick one member to modify libs/robot_controller1.py # as necessary to implement the method below as per the instructions in the opening doc string. Once the code has # been tested and shown to work, then have that person commit their work. All other team members need to do a # VCS --> Update project... # Once the library is implemented any team member should be able to run his code as stated in todo3. robot.shutdown()
def main(): delegate = robo.Snatch3r() ev3.Sound.speak("Let's play the Hot and Cold Game!") mqtt = com.MqttClient(delegate) delegate.set_mqtt(mqtt) mqtt.connect_to_pc() mqtt.send_message('draw_circle_from_robot') time.sleep(0.05) delegate.loop_forever()
def receiving_messages_from_pc(): """ Example of connecting from an EV3 to the PC and sending commands. This does not necessarily show best practice, just showing syntax.""" robot = robo.Snatch3r() mqtt_client = com.MqttClient(robot) mqtt_client.connect_to_pc() robot.loop_forever( ) # Avoids letting the robot finish until some "end" command. # Pretend like the Snatch3r class has these methods. The other end can say "shutdown" def loop_forever(self): self.running = True while self.running: time.sleep(0.1) # Do nothing until the robot does a shutdown. def shutdown(self): ev3.Sound.speak("Goodbye").wait() self.running = False
def main(): print("--------------------------------------------") print(" Follow a line") print("--------------------------------------------") ev3.Sound.speak("Follow a line").wait() # TODO: 4: After running the code set the default white and black levels to a better initial guess. # Once you have the values hardcoded to resonable numbers here you don't really need the w and b commands below. white_level = 50 black_level = 40 robot = robo.Snatch3r() while True: command_to_run = input( "Enter w (white), b (black), f (follow), or q (for quit): ") if command_to_run == 'w': print("Calibrate the white light level") # TODO: 2. Read the reflected_light_intensity property of the color sensor and set white_level to that value # As discussed in the prior module, it is recommended that you've added to your Snatch3r class's constructor # the color_sensor, as shown: # self.color_sensor = ev3.ColorSensor() # assert self.color_sensor # Then here you can use a command like robot.color_sensor.reflected_light_intensity print("New white level is {}.".format(white_level)) elif command_to_run == 'b': print("Calibrate the black light level") # TODO: 3. Read the reflected_light_intensity property of the color sensor and set black_level print("New black level is {}.".format(black_level)) elif command_to_run == 'f': print("Follow the line until the touch sensor is pressed.") follow_the_line(robot, white_level, black_level) elif command_to_run == 'q': break else: print(command_to_run, "is not a known command. Please enter a valid choice.") print("Goodbye!") ev3.Sound.speak("Goodbye").wait()
def main(): print("--------------------------------------------") print(" Color tracking") print("--------------------------------------------") ev3.Sound.speak("Color tracking").wait() print("Press the touch sensor to exit this program.") # This code assumes you have setup the pixy object on the Snatch3r class. # Add the pixy property to that class if you have not done so already. robot = robo.Snatch3r() robot.pixy.mode = "SIG1" turn_speed = 120 while not robot.touch_sensor.is_pressed: # Done 1 x = robot.pixy.value(1) y = robot.pixy.value(2) print("(X,Y)=({},{})".format(x,y)) if x <150: robot.turn_degrees(-90,turn_speed) if x > 170: robot.turn_degrees(90,turn_speed) else: robot.shutdown() # TODO: 3. Use the x value to turn the robot # If the Pixy x value is less than 150 turn left (-turn_speed, turn_speed) # If the Pixy x value is greater than 170 turn right (turn_speed, -turn_speed) # If the Pixy x value is between 150 and 170 stop the robot # Continuously track the color until the touch sensor is pressed to end the program. time.sleep(0.25) print("Goodbye!") ev3.Sound.speak("Goodbye").wait()
def __init__(self): self.robot = robo.Snatch3r() self.mqtt_client = None # To be set later.