from pybricks.pupdevices import Motor
from pybricks.parameters import Port
from pybricks.tools import wait

# Initialize a motor on port A.
example_motor = Motor(Port.A)

# Make the motor run clockwise at 500 degrees per second.
example_motor.run(500)

# Wait for three seconds.
wait(3000)

# Make the motor run counterclockwise at 500 degrees per second.
example_motor.run(-500)

# Wait for three seconds.
wait(3000)
steer.reset_angle((right_end - left_end) / 2)
steer.run_target(speed=200, target_angle=0, wait=False)

# Now we can start driving!
while True:
    # Check which buttons are pressed.
    pressed = remote.buttons.pressed()

    # Choose the steer angle based on the left controls.
    steer_angle = 0
    if Button.LEFT_PLUS in pressed:
        steer_angle -= 75
    if Button.LEFT_MINUS in pressed:
        steer_angle += 75

    # Steer to the selected angle.
    steer.run_target(500, steer_angle, wait=False)

    # Choose the drive speed based on the right controls.
    drive_speed = 0
    if Button.RIGHT_PLUS in pressed:
        drive_speed += 1000
    if Button.RIGHT_MINUS in pressed:
        drive_speed -= 1000

    # Apply the selected speed.
    front.run(drive_speed)

    # Wait.
    wait(10)
Example #3
0
from pybricks.pupdevices import Motor
from pybricks.parameters import Port
from pybricks.tools import wait

# Initialize a motor on port A.
example_motor = Motor(Port.A)

# Start moving at 300 degrees per second.
example_motor.run(300)

# Display the angle and speed 50 times.
for i in range(100):

    # Read the angle (degrees) and speed (degrees per second).
    angle = example_motor.angle()
    speed = example_motor.speed()

    # Print the values.
    print(angle, speed)

    # Wait some time so we can read what is displayed.
    wait(200)
        steer_angle -= max_steering
    if Button.LEFT_MINUS in pressed:
        steer_angle += max_steering

    # Steer to the selected angle.
    steer_motor.run_target(1000, steer_angle, wait=False)

    # Choose the drive speed based on the right controls.
    drive_speed = 0
    if Button.RIGHT_PLUS in pressed:
        drive_speed += 1000
    if Button.RIGHT_MINUS in pressed:
        drive_speed -= 1000

    # Apply the selected speed.
    drive_motor1.run(drive_speed)
    drive_motor2.run(drive_speed)

    # Button for differential lock
    if Button.CENTER in pressed:
        # Stop the drive motors
        drive_motor1.stop()
        drive_motor2.stop()

        # Run lock motor for half a second.
        remote.light.on(Color.RED)
        lock_motor.dc(LOCK_POWER if locked else -LOCK_POWER)
        wait(500)
        lock_motor.stop()

        # Update lock state.
# Hand-Controlled Grabber:
# press the Force Sensor to grab objects,
# and release the Force Sensor to let go.

from pybricks.hubs import PrimeHub
from pybricks.pupdevices import ForceSensor, Motor
from pybricks.parameters import Port

# Configure the Hub, the Force Sensor and the Motor
hub = PrimeHub()
force_sensor = ForceSensor(Port.E)
motor = Motor(Port.A)

while True:
    # Grab when the Force Sensor is pressed
    if force_sensor.pressed():
        motor.run(speed=-1000)

    # else let go
    else:
        motor.run_until_stalled(speed=1000)
motor = Motor(Port.A)
ultrasonic_sensor = UltrasonicSensor(Port.C)

# Detect object.
motor.run_target(500, 0)
distance = ultrasonic_sensor.distance()
assert distance < 100, "Expected < 100 mm, got {0}.".format(distance)

# Move object away.
motor.run_target(500, 180)
distance = ultrasonic_sensor.distance()
assert distance > 100, "Expected > 100 mm, got {0}.".format(distance)

# Prepare fast detection.
motor.reset_angle(0)
motor.run(700)
DETECTIONS = 5

# Wait for given number of detections.
for i in range(DETECTIONS):
    # Wait for object to be detected.
    while ultrasonic_sensor.distance() > 100:
        pass

    # Wait for object to move away.
    angle_detected = motor.angle()
    while motor.angle() < angle_detected + 180:
        pass

# Assert that we have made as many turns.
rotations = round(motor.angle() / 360)
from pybricks.pupdevices import Motor
from pybricks.parameters import Port, Direction
from pybricks.tools import wait

# Initialize a motor on port A with the positive direction as counterclockwise.
# Also specify one gear train with a 12-tooth and a 36-tooth gear. The 12-tooth
# gear is attached to the motor axle. The 36-tooth gear is at the output axle.
geared_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE, [12, 36])

# Make the output axle run at 100 degrees per second. The motor speed
# is automatically increased to compensate for the gears.
geared_motor.run(100)

# Wait for three seconds.
wait(3000)
Example #8
0
class ExplorationRover():
    """Control the Mars Exploration Rover."""

    # In this robot, we want to detect red and cyan/teal "mars rocks".
    ROCK_COLORS = (Color.RED, Color.CYAN)

    # These are the gears between the motor and the rear wheels.
    REAR_GEARS = (8, 24, 12, 20)

    # An animation of heart icons of varying brightness, giving a heart beat.
    HEART_BEAT = [
        Icon.HEART * i / 100
        for i in list(range(0, 100, 4)) + list(range(100, 0, -4))
    ]

    def __init__(self):
        # Initialize each motor with the correct direction and gearing.
        self.left_rear_wheels = Motor(Port.E, Direction.CLOCKWISE,
                                      self.REAR_GEARS)
        self.right_rear_wheels = Motor(Port.F, Direction.COUNTERCLOCKWISE,
                                       self.REAR_GEARS)
        self.left_front_wheel = Motor(Port.C, Direction.COUNTERCLOCKWISE, None)
        self.right_front_wheel = Motor(Port.D, Direction.CLOCKWISE, None)

        # Initialize sensors, named after the Perseverance Mars Rover instruments.
        self.mast_cam = UltrasonicSensor(Port.B)
        self.sherloc = ColorSensor(Port.A)

        # Initialize the hub and start the animation
        self.hub = InventorHub()
        self.hub.display.animate(self.HEART_BEAT, 30)

    def calibrate(self):
        # First, measure the color of the floor. This makes it easy to explicitly
        # ignore the floor later. This is useful if your floor has a wood color,
        # which can appear red or yellow to the sensor.
        self.floor_color = self.sherloc.hsv()

        # Set up all the colors we want to distinguish, including no color at all.
        self.sherloc.detectable_colors(self.ROCK_COLORS +
                                       (self.floor_color, None))

    def drive(self, speed, steering, time=None):
        # Drive the robot at a given speed and steering for a given amount of time.
        # Speed and steering is expressed as degrees per second of the wheels.
        self.left_rear_wheels.run(speed + steering)
        self.left_front_wheel.run(speed + steering)
        self.right_rear_wheels.run(speed - steering)
        self.right_front_wheel.run(speed - steering)

        # If the user specified a time, wait for this duration and then stop.
        if time is not None:
            wait(time)
            self.stop()

    def stop(self):
        # Stops all the wheels.
        self.left_rear_wheels.stop()
        self.left_front_wheel.stop()
        self.right_rear_wheels.stop()
        self.right_front_wheel.stop()

    def scan_rock(self, time):
        # Stops the robot and moves the scan arm.
        self.stop()
        self.left_front_wheel.run(100)

        # During the given duration, scan for rocks.
        watch = StopWatch()
        while watch.time() < time:

            # If a rock color is detected, display it and make a sound.
            if self.sherloc.color() in self.ROCK_COLORS:
                self.hub.display.image(Icon.CIRCLE)
                self.hub.speaker.beep()
            else:
                self.hub.display.off()

            wait(10)

        # Turn the arm motor and restore the heartbeat animation again.
        self.hub.display.animate(self.HEART_BEAT, 30)
        self.left_front_wheel.stop()
        continue

    # Read the key.
    key = stdin.read(1)
    print("You pressed:", key)

    # If the keys 1, 4, or 7 are pressed, steer left
    # If the keys 3, 6, or 9 are pressed, steer right
    # Otherwise steer to the middle
    if key in ('1', '4', '7'):
        steer_angle = -90
    elif key in ('3', '6', '9'):
        steer_angle = 90
    else:
        steer_angle = 0

    steer.run_target(200, steer_angle, wait=False, then=Stop.COAST)

    # If the keys 7, 8, or 9 are pressed, go forward
    # If the keys 1, 2, or 3 are pressed, go backward
    # Otherwise stop driving
    if key in ('7', '8', '9'):
        sign = 1
    elif key in ('1', '2', '3'):
        sign = -1
    else:
        sign = 0

    front.run(sign * 800)
    rear.run(sign * 800)