async def on_start(vm, stack): ms_hub.status_light.on("black") rotate_hub_display_to_value("3") blinking_frames = [hub.Image(frame) for frame in blinking_animation] vm.system.display.show(blinking_frames, clear=False, delay=round(1000 / 8), loop=True, fade=1) left_hand = Motor("B") right_hand = Motor("F") # Calibrate left_hand.run_to_position(0) right_hand.run_to_position(0) vm.system.sound.play("/extra_files/Humming") for i in range(2): right_hand.run_for_degrees(-100, 50) right_hand.run_for_degrees(100, 50) left_hand.run_for_degrees(100, 50) left_hand.run_for_degrees(-100, 50) left_turn = MotorPair("B", "E") right_turn = MotorPair("A", "F") vm.system.sound.play("/extra_files/Humming") for i in range(2): right_turn.move(100, "degrees", steering=100, speed=50) right_turn.move(-100, "degrees", steering=100, speed=50) left_turn.move(100, "degrees", steering=-100, speed=50) left_turn.move(-100, "degrees", steering=-100, speed=50) wheels = MotorPair("A", "E") wheels.move(14 * math.pi, "cm", steering=100, speed=80) wheels.move(14 * math.pi, "cm", steering=-100, speed=80) right_hand.run_to_position(270, direction="counterclockwise") wait_for_seconds(1) right_hand.run_for_seconds(1, 50) await vm.system.sound.play_async("/extra_files/Tadaa") wait_for_seconds(1) right_hand.run_to_position(0, direction="counterclockwise")
# driving with Tricky wheel_radius = 2.8 pen_radius = 8.6 axis_radius = 5.6 line_length = 5 # Create your objects here. hub = MSHub() # Write your program here. hub.speaker.beep() motor = Motor('C') motor.run_to_position(165) def pen_up(): motor.run_for_degrees(-90) def pen_down(): motor.run_for_degrees(90) motor_pair = MotorPair('B', 'A') motor_pair.set_default_speed(20) motor_pair.set_motor_rotation(wheel_radius * 2 * math.pi, 'cm') pen_down()
# # Configure motors # %% print("Configuring motors...") motor_left_arm = Motor('B') # Left arm motor_right_arm = Motor('F') # Right arm motors_arms = MotorPair('B', 'F') motors_wheels = MotorPair('A', 'E') print("DONE!") # %% [markdown] # # Set arm motors to starting position # %% print("Setting arm motors to position 0...") motor_left_arm.run_to_position(0) motor_right_arm.run_to_position(0) print("DONE!") # %% [markdown] # # Overwrite `Timer` # %% print("Overwriting timer...") class Timer(): """ Replacement Timer class that allows using floats (i.e., seconds with a decimal place). """ def __init__(self):
motor_right_arm = Motor('F') # Right arm motor_left_arm.set_default_speed(40) motor_right_arm.set_default_speed(-40) motors_arms = MotorPair('B', 'F') print("DONE!") # %% [markdown] # # Set arm motors to starting position # This isn't part of the original program, but I think it is good practice to make sure motors always start in the same state. # # %% print("Setting arm motors to position 0...") motor_left_arm.run_to_position(0) motor_right_arm.run_to_position(0) print("DONE!") # %% [markdown] # # Move shopper Charlie # # So far, we've used `move` to move the wheel motors. # However, when using `move`, the program will not continue until the specified value is reached. # This is a problem, since we want to move the wheels *and* the arms at the same time. # Therefore, we will use `start` and `stop`. This comes at the cost of being unable to # define a stopping condition based on the motors alone (e.g., distance, time). # We have to stop them at certain point of the program (as we do here) or based on a # sensory input. # # I wonder if there's a way to perform async execution using the vanilla (Micro)Python.
class AutonomousCar: def __init__(self, settings): self.settings = settings self.go_ahead = True self.turn = False self.turn_angle = 30 if bool(random.getrandbits(1)): self.turn_angle *= -1 self.initiate_objects() if settings['run_test']: self.test_function() else: self.calibrate() self.the_loop() def initiate_objects(self): self.hub = MSHub() self.hub.status_light.on('orange') self.motor_front = Motor(self.settings['motor_front_port']) self.motor_rear = Motor(self.settings['motor_rear_port']) self.dist_sens = DistanceSensor(self.settings['dist_sens_port']) def calibrate(self): self.motor_front.run_to_position(self.settings['motor_front_angle']) def test_function(self): self.hub.light_matrix.show_image('PACMAN') sounds_and_colors = [(65, "blue"), (70, "azure"), (60, "cyan")] for i in sounds_and_colors: self.hub.speaker.beep(i[0], 0.3) self.hub.status_light.on(i[1]) self.hub.light_matrix.off() def end_turn(self): self.turn = False self.motor_front.run_for_degrees(self.turn_angle * -1) def the_loop(self): self.hub.light_matrix.write('GO') self.hub.status_light.on('green') loop_time_diff = 0 loop_start_time = time.time() turn_time_diff = 0 turn_start_time = None while loop_time_diff < self.settings['how_long_run']: how_far = self.dist_sens.get_distance_cm() rear_speed = self.settings.get('motor_rear_speed') if self.turn: self.hub.status_light.on('yellow') turn_time_control = time.time() turn_time_diff = turn_time_control - turn_start_time if isinstance( how_far, int ) and how_far <= self.settings['dist_threshold_low']: self.go_ahead = False self.end_turn() elif turn_time_diff >= self.settings['turn_in_seconds']: self.end_turn() elif self.go_ahead: self.hub.status_light.on('green') rear_speed = rear_speed * -1 if isinstance( how_far, int ) and how_far <= self.settings['dist_threshold_low']: self.go_ahead = False else: self.hub.status_light.on('blue') if isinstance( how_far, int ) and how_far >= self.settings['dist_threshold_high']: self.turn = True turn_start_time = time.time() self.motor_front.run_for_degrees(self.turn_angle) self.go_ahead = True self.motor_rear.start(rear_speed) wait_for_seconds(0.25) loop_time_control = time.time() loop_time_diff = loop_time_control - loop_start_time self.motor_rear.stop() self.calibrate() self.hub.light_matrix.write('STOP')
# # Configure motors # %% print("Configuring motors...") motor_left_arm = Motor('B') # Left arm motor_right_arm = Motor('F') # Right arm motors_arms = MotorPair('B', 'F') motors_wheels = MotorPair('A', 'E') print("DONE!") # %% [markdown] # # Set arm motors to starting position # %% print("Setting arm motors to position 0...") motor_left_arm.run_to_position(0) motor_right_arm.run_to_position(0) print("DONE!") # %% [markdown] # # Configure sensor # %% print("Configuring sensors...") color_sensor = ColorSensor('C') distance_sensor = DistanceSensor('D') distance_sensor.light_up_all() print("DONE!") # %% [markdown]
# %% [markdown] # # Configure motors # %% print("Configuring motors...") motor_steer = Motor('A') # Front wheels (for steering) motor_power = Motor('B') # Back wheels (for moving) print("DONE!") # %% [markdown] # # Set steering motor to starting position # %% print("Setting steering motor to position 0...") motor_steer.run_to_position(45, speed=100) motor_steer.run_to_position(0, speed=100) print("DONE!") # %% [markdown] # # Move MVP # %% print("Steering...") motor_steer.run_to_position(50, speed=35) print("DONE!") # %% print("Moving...") motor_power.set_default_speed(80) motor_power.run_for_rotations(-16)
# %% print("Turning center button off...") hub.status_light.on('black') print("DONE!") # %% [markdown] # # Set arm motors to starting position # In the original Scratch program, there's a `Charlie - Calibrate` block. I don't know exactly how the calibration is done, but in the end I think that it just sets the motor to position 0. # Notice that moving motors to a specific position needs to be done individually (and sequentially). In other words, we cannot run a `MotorPair` to a position, only one motor at a time. # # %% print("Setting arm motors to position 0...") motor_b = Motor('B') # Left arm motor_f = Motor('F') # Right arm motor_b.run_to_position(0) motor_f.run_to_position(0) print("DONE!") # %% [markdown] # # Configure motors # %% print("Configuring motors...") motors_movement = MotorPair('A', 'E') motors_movement.set_default_speed(80) print("DONE!") # %% [markdown] # # Program color reactions
# %% print("Configuring motors...") motor_left_arm = Motor('B') # Left arm motor_right_arm = Motor('F') # Right arm motors_arms = MotorPair('B', 'F') motor_left_arm.set_default_speed(-100) motor_right_arm.set_default_speed(100) print("DONE!") # %% [markdown] # # Set arm motors to starting position # %% print("Setting arm motors to position 0...") motor_left_arm.run_to_position(0) motor_right_arm.run_to_position(0) print("DONE!") # %% [markdown] # # Make Charlie push himself around # If Charlie doesn't move properly, try: # * adjusting the wheels on his poles # * playing with the waiting time between movement of the right arm (`t_wait`) # * putting him on another surface # %% print("Skiing...") # Original value is 6. # I set this to 3 due to space constraints.
# hub.status_light.on('black') hub.led(0, 0, 0) # %% [markdown] # # Initialize motors # %% print("Configuring motors...") motor_steer = Motor('A') # Front wheels (for steering) motor_power = Motor('C') # Back wheels (for moving) motor_turret = Motor('B') # Turrent spinning # %% print("Setting motors to position 0...") motor_steer.run_to_position(45, speed=100) motor_steer.run_to_position(0, speed=100) motor_turret.run_to_position(0, speed=75) print("DONE!") # %% [markdown] # # Initialize distance sensor # %% print("Initializing distance sensor...") distance_sensor = DistanceSensor('D') print("DONE!") # %% [markdown] # # Put the AAT MS5 in motion
# %% print("Displaying happy face...") hub.light_matrix.show_image('HAPPY') print("DONE!") # %% [markdown] # # Set arm motors to starting position # In the original Scratch program, there's a `Charlie - Calibrate` block. I don't know exactly how the calibration is done, but in the end I think that it just sets the motor to position 0. # Notice that moving motors to a specific position needs to be done individually (and sequentially). In other words, we cannot run a `MotorPair` to a position, only one motor at a time. # # %% print("Setting arm motors to position 0...") motor_b = Motor('B') # Left arm motor_f = Motor('F') # Right arm motor_b.run_to_position(0) motor_f.run_to_position(0) print("DONE!") # %% [markdown] # # Dance time! # %% print("Dancing steps 1...") app.start_sound('Party Blower') for ii in range(0, 2): motor_f.run_for_degrees(-100) motor_f.run_for_degrees(100)