def code(): rightArm = Motor('A') rightArm.start(50) wait_for_seconds(2) rightArm.stop()
print("Skiing...") # Original value is 6. # I set this to 3 due to space constraints. n_pushes = 3 for ii in range(0, n_pushes): # Now, we move the arm motors back to position 0 sequentially. # This doesn't affect the movement, but looks a bit clumsy. # An alternative to this could be using coroutines to # make them run "parallel", as in the drum_solo project. motor_left_arm.run_to_position(0, direction='shortest path') motor_right_arm.run_to_position(0, direction='shortest path') motors_arms.move(85, unit='degrees') motor_right_arm.start() t_wait = 0.1 wait_for_seconds(t_wait) motor_right_arm.stop() motor_left_arm.run_to_position(0, direction='shortest path') motor_right_arm.run_to_position(0, direction='shortest path') app.play_sound('Skid') print("DONE!") # %% print("-" * 15 + " Execution ended " + "-" * 15 + "\n")
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')
print("Steering...") motor_steer.run_to_position(POSITION, speed=35) print("DONE!") # %% [markdown] # The tank speed is given by `SPEED`. It should have a value between `-100` and `100`. # * A negative value will move the tank forward. # * A positive value will move the tank backwards. # # Recommended value is `-50` # %% SPEED = -50 print("Moving...") motor_power.start(SPEED) print("DONE!") # %% [markdown] # # Configure the patrolling # We will move the turret constantly. It will go from left to right and from # right to left. When an obstacle is detected, the turret will go back to the # initial position and "fire". # # # # ## Define distance function # As part of the program, we need to continuously check if the # measured distance is less than 10 cm. # %%