Пример #1
0
def code():
    rightArm = Motor('A')

    rightArm.start(50)
    wait_for_seconds(2)
    rightArm.stop()
Пример #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')
# This code is meant as a boilerplate for your remote control project.
# Connect to it using a steering wheel or app:
# https://play.google.com/store/apps/details?id=com.antonsmindstorms.mindstormsrc&hl=nl&gl=US

from projects.mpy_robot_tools.rc import (RCReceiver, L_STICK_HOR, L_STICK_VER,
                                         R_STICK_HOR, R_STICK_VER, L_TRIGGER,
                                         R_TRIGGER, SETTING1, SETTING2)
from projects.mpy_robot_tools.helpers import clamp_int
from mindstorms import MSHub, ColorSensor, Motor, DistanceSensor
from mindstorms.control import wait_for_seconds
import math

# Create your objects here.
rcv = RCReceiver(name="robot")
ma = Motor("A")
hub = MSHub()

# Write your program here.
while True:  # Forever
    if rcv.is_connected():
        speed, turn = rcv.controller_state(R_STICK_VER, R_STICK_HOR)
        if rcv.button_pressed(1):
            hub.speaker.beep()
        ma.start_at_power(clamp_int(speed))

    else:
        # Stop all motors when not connected
        ma.stop()
        print("Waiting for connection...")
        wait_for_seconds(0.3)  # async wait, better than sleep_ms
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")
Пример #5
0
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]
# Then, it will fire three blasters. Each blaster will come with a sound and an
# animation of the blaster moving in the hub.
#
# First, lets define the frames of the animation.

# %%
print("Defining animation frames...")

frames = [
    '00000:00000:00000:00000:00000', '00900:00000:00000:00000:00000',
    '00700:00900:00000:00000:00000', '00500:00700:00900:00000:00000',
    '00000:00500:00700:00900:00000', '00000:00000:00500:00700:00900',