Example #1
0
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")
Example #2
0
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')
        turret_generator.send(TURRET_ANGLE)

        wait_for_seconds(0.01)  # Small pause between steps.

    return None


# %% [markdown]
# After we have defined the turret movement, we can now make the AAT MS5 patrol until it finds those pesky Republic supporters!

# %%
TURRET_ANGLE = 40

print("Initializing turret with angle " + str(TURRET_ANGLE) + "...")
motor_turret.set_default_speed(10)
motor_turret.run_for_degrees(-TURRET_ANGLE)
print("DONE!")

print("Starting patrolling...")
move_turret()
print("DONE!")

# %% [markdown]
# Once it finds an enemy (i.e, it detects an obstacle), it will stop and center the turret.

# %%
print("Enemy detected! Attack!")
motor_power.stop()  # Stop the movement
motor_turret.run_to_position(0, speed=75)  # Center the turret

# %% [markdown]
Example #4
0
print("Configuring color sensor...")
color_sensor = ColorSensor('C')
print("DONE!")

# %% [markdown]
# # Hit ball

# %%
while (True):

    print("Waiting for color confirmation...")
    wait_until(color_sensor.get_color, equal_to, 'red')
    print("DONE!")

    print("Hitting ball...")
    motor_left_arm.run_for_degrees(-235)
    motor_left_arm.run_for_seconds(1)

    hub.light_matrix.show_image('HAPPY')
    motor_wheel.run_for_degrees(
        880)  # We can change this value to adjust for turn between shots.
    hub.light_matrix.off()

    print("DONE!")

# %% [markdown]
# Notice that since hitting the ball activity is inside an infinite loop,
# we will never reach the following line.

# %%
print("-" * 15 + " Execution ended " + "-" * 15 + "\n")
Example #5
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)

    motor_b.run_for_degrees(100)
    motor_b.run_for_degrees(-100)

print("DONE!")

# %%
print("Dancing steps 2...")

app.start_sound('Party Blower')

motors_af = MotorPair('A', 'F')
motors_be = MotorPair('B', 'E')
Example #6
0
distance_sensor = DistanceSensor('D')
distance_sensor.light_up_all()
print("DONE!")

# %% [markdown]
# # High five!

# %%
print("Waiting for high five intention...")
distance_sensor.wait_for_distance_closer_than(12, unit='cm')
distance_sensor.light_up_all(0)
print("DONE!")

# %%
print("Let's high five!")
motor_right_arm.run_for_degrees(-120)

timer = Timer()


def high_five_or_no_high_five():
    # Original threshold of speed is -10.
    # However, in my experience, -5 worked better.
    return (motor_right_arm.get_speed() < -5) or (timer.now() > 5)


# Wait for either a high five or for the timer to go past 5 s.
wait_until(high_five_or_no_high_five)

# If the timer exceeded 5, it means that there was no high five.
if timer.now() > 5: