Exemplo n.º 1
0
class MVP:
    def __init__(self):
        self.steer_motor = Motor(Port.A)
        self.drive_motor = Motor(Port.B,
                                 positive_direction=Direction.COUNTERCLOCKWISE)

    def calibrate(self):
        self.steer_motor.run_target(speed=1000, target_angle=0)
Exemplo n.º 2
0
# Lower the acceleration so the car starts and stops realistically.
front.control.limits(acceleration=1000)

# Connect to the remote.
remote = Remote()

# Find the steering endpoint on the left and right.
# The middle is in between.
left_end = steer.run_until_stalled(-200, then=Stop.HOLD)
right_end = steer.run_until_stalled(200, then=Stop.HOLD)

# We are now at the right. Reset this angle to be half the difference.
# That puts zero in the middle.
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)
Exemplo n.º 3
0
# Lower the acceleration so the car starts and stops realistically.
front.control.limits(acceleration=1000)
rear.control.limits(acceleration=1000)

# Find the steering endpoint on the left and right. The difference
# between them is the total angle it takes to go from left to right.
# The middle is in between.
left_end = steer.run_until_stalled(-200, then=Stop.HOLD)
right_end = steer.run_until_stalled(200, then=Stop.HOLD)

# We are now at the right limit. We reset the motor angle to the
# limit value, so that the angle is 0 when the steering mechanism is
# centered.
limit = (right_end - left_end) // 2
steer.reset_angle(limit)
steer.run_target(speed=200, target_angle=0, then=Stop.COAST)


# Given a motor speed (deg/s) and a steering motor angle (deg), this
# function makes the car move at the desired speed and turn angle.
# The car keeps moving until you give another drive command.
def drive(drive_motor_speed, steer_angle):
    # Start running the drive motors
    front.run(drive_motor_speed)
    rear.run(drive_motor_speed)

    # Limit the steering value for safety, and then start the steer
    # motor.
    limited_angle = max(-limit, min(steer_angle, limit))
    steer.run_target(200, limited_angle, wait=False)
Exemplo n.º 4
0
SPEED = 500

# Color angle targets
angles = {
    "GREEN": 20,
    "BLUE": 110,
    "RED": 200,
    "YELLOW": 290,
    "BLACK": 250,
    "WHITE": 75,
    "NONE": 162,
}

# Verify saturated colors without calibration.
for name in ("GREEN", "BLUE", "RED", "YELLOW"):
    motor.run_target(SPEED, angles[name])
    detected = color_sensor.color()
    assert detected == Color[name], "Expected {0} but got {1}".format(
        Color[name], detected)

# Update all colors.
for name in angles.keys():
    motor.run_target(SPEED, angles[name])
    Color[name] = color_sensor.hsv()

# Set new colors as detectable colors.
color_sensor.detectable_colors([Color[key] for key in angles.keys()])

# Test all newly calibrated colors.
for name in angles.keys():
    motor.run_target(SPEED, angles[name])
Exemplo n.º 5
0
drive_motor2 = Motor(Port.B)

# Lower the acceleration so it starts and stops realistically.
drive_motor1.control.limits(acceleration=1000)
drive_motor2.control.limits(acceleration=1000)

# Find the steering endpoint on the left and right.
# The middle is in between.
steer_motor = Motor(Port.D, Direction.COUNTERCLOCKWISE)
left_end = steer_motor.run_until_stalled(-400)
right_end = steer_motor.run_until_stalled(400)

# Return to the center.
max_steering = (right_end - left_end) / 2
steer_motor.reset_angle(max_steering)
steer_motor.run_target(speed=200, target_angle=0)

# Initialize the differential as unlocked.
lock_motor = DCMotor(Port.C)
LOCK_POWER = 60
lock_motor.dc(-LOCK_POWER)
wait(600)
lock_motor.stop()
locked = True

# 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.
Exemplo n.º 6
0
    None,
    [1, 3],
    (1, 3),
    [1, 2, 3],
    [1, 28],
    [12, 36],
    [36, 12],
    [[1, 24], [12, 36]],
    [(1, 24), (12, 36)],
    [(1, 24), [12, 36]],
    [[57, 24], [12, 123456789, 1]],
]

# Initialize device to known position.
motor = Motor(Port.A, gears=None)
motor.run_target(500, 170)
wait(500)
motor.dc(0)
real_angle = motor.angle()

# Test expected result.
for gears in test_args:
    # Initialize motors with new settings.
    motor = Motor(Port.A, gears=gears)

    # Get expected and measured value.
    expected = real_angle / get_gear_ratio(gears)
    measured = motor.angle()

    # Allow at most one degree of error. We could be a bit more precise
    # here and also test the expected rounding direction, by investigating
Exemplo n.º 7
0
from pybricks.pupdevices import Motor, UltrasonicSensor
from pybricks.parameters import Port, Direction
from pybricks.tools import wait, StopWatch

# Initialize devices.
motor = Motor(Port.A)
motor = Motor(port=Port.A, positive_direction=Direction.CLOCKWISE, gears=[])
ultrasonic_sensor = UltrasonicSensor(Port.C)

# Assert initial values.
assert motor.speed() == 0, "Unexpected initial motor speed."
assert -180 <= motor.angle() <= 179, "Unexpected initial motor angle."

# Verify driving to zero.
for target in (-360, 0, 360):
    motor.run_target(500, target)
    assert ultrasonic_sensor.distance() < 100, "Unexpected distance result."

# Test angle reset.
for reset_value in (-98304, -360, -180, -1.234, 0, 0.1, 1, 178, 180, 13245687):
    motor.reset_angle(reset_value)
    assert motor.angle() == int(reset_value), "Incorrect angle reset"

# Test motion after reset. Also test kwarg.
motor.reset_angle(angle=180)
for target in (-180, 180):
    motor.run_target(500, target)
    assert ultrasonic_sensor.distance() < 100, "Unexpected reset result."

# Test reset to absolute value, given that sensor sees object at the top.
motor.reset_angle()
Exemplo n.º 8
0
# Initialize sensor to make sure we're in the expected mode.
color_sensor.color()
wait(500)

# Color angle targets
angles = {
    "GREEN": 20,
    "BLUE": 110,
    "RED": 200,
    "YELLOW": 290,
    "BLACK": 250,
    "WHITE": 75,
    "NONE": 162,
}

# Go to blue and assert that we really do see blue.
motor.run_target(SPEED, angles["BLUE"])
assert color_sensor.color() == Color["BLUE"]

# Read ambient light, causing a mode change.
color_sensor.ambient()
wait(3000)

# Go to red and read the color right away.
motor.run_target(SPEED, angles["RED"])
detected = color_sensor.color()

# With the built-in delay, the stale blue value should be gone, now giving red.
assert detected == Color["RED"], "Expected {0} but got {1}".format(Color["RED"], detected)
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)

# Please have a look at the previous example first. This example
# finds two endspoints and then makes the middle the zero point.

# The run_until_stalled gives us the angle at which it stalled.
# We want to know this value for both endpoints.
left_end = example_motor.run_until_stalled(-200, duty_limit=30)
right_end = example_motor.run_until_stalled(200, duty_limit=30)

# We have just moved to the rightmost endstop. So, we can reset
# this angle to be half the distance between the two endpoints.
# That way, the middle corresponds to 0 degrees.
example_motor.reset_angle((right_end - left_end) / 2)

# From now on we can simply run towards zero to reach the middle.
example_motor.run_target(200, 0)

wait(1000)
Exemplo n.º 10
0
example_motor.dc(50)
wait(1500)
example_motor.stop()
wait(1500)

# Run at 500 deg/s for two seconds.
print("Demo of run_time")
example_motor.run_time(500, 2000)
wait(1500)

# Run at 500 deg/s for 90 degrees.
print("Demo of run_angle")
example_motor.run_angle(500, 180)
wait(1500)

# Run at 500 deg/s back to the 0 angle
print("Demo of run_target to 0")
example_motor.run_target(500, 0)
wait(1500)

# Run at 500 deg/s back to the -90 angle
print("Demo of run_target to -90")
example_motor.run_target(500, -90)
wait(1500)

# Run at 500 deg/s until the motor stalls
print("Demo of run_until_stalled")
example_motor.run_until_stalled(500)
print("Done")
wait(1500)
Exemplo n.º 11
0
from pybricks.tools import wait
from pybricks.experimental import getchar

hub = TechnicHub()

mDrive = Motor(Port.D)
mSteer = Motor(Port.B)
mSteer.reset_angle()

SPEED_DRIVE = 100
TIME_DRIVE = 30
STEP_STEER = 15
SPEED_STEER = 720
MAX_STEER = 75

mSteer.run_target(SPEED_STEER, 0, then=Stop.BRAKE)

while True:
    c = getchar()
    if c == ord('q'):
        mDrive.dc(SPEED_DRIVE)
        wait(TIME_DRIVE)
    elif c == ord('a'):
        mDrive.dc(-SPEED_DRIVE)
        wait(TIME_DRIVE)
    elif c == ord('o'):
        if mSteer.angle() > -MAX_STEER:
            mSteer.run_angle(SPEED_STEER, -STEP_STEER, then=Stop.BRAKE)
    elif c == ord('p'):
        if mSteer.angle() < MAX_STEER:
            mSteer.run_angle(SPEED_STEER, STEP_STEER, then=Stop.BRAKE)
Exemplo n.º 12
0
Hardware Module: 1

Description: Verifies the distance values of the Ultrasonic Sensor.
It rotates the motor to place an obstacle in front of the sensor to test
distance values. Then it rotates quickly to verify faster readings.
"""

from pybricks.pupdevices import Motor, UltrasonicSensor
from pybricks.parameters import Port

# Initialize devices.
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):
Exemplo n.º 13
0
belt.run_until_stalled(-100, duty_limit=30)
belt.reset_angle(0)

# Component positions
FEET = 0
BELLY = 365
HEAD = 128
ARMS = 244
NECK = 521
BASE = 697

# Place all the elements
for element in (FEET, BELLY, ARMS, NECK, HEAD):

    # Go to the element
    belt.run_target(speed=200, target_angle=element)

    # Grab the element
    arm.run_time(speed=300, time=2000)

    # Lift the element by going back to nearly zero
    arm.run_target(speed=300, target_angle=55)

    # Go to the base
    belt.run_target(speed=200, target_angle=BASE)
    wait(500)

    # Put the element down
    arm.run_time(speed=300, time=2000)

    # Lift the arm back up
Exemplo n.º 14
0
from pybricks.pupdevices import Motor
from pybricks.parameters import Port

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

# We'll use a speed of 200 deg/s in all our commands.
speed = 200

# Run the motor in reverse until it hits a mechanical stop.
# The duty_limit=30 setting means that it will apply only 30%
# of the maximum torque against the mechanical stop. This way,
# you don't push against it with too much force.
example_motor.run_until_stalled(-speed, duty_limit=30)

# Reset the angle to 0. Now whenever the angle is 0, you know
# that it has reached the mechanical endpoint.
example_motor.reset_angle(0)

# Now make the motor go back and forth in a loop.
# This will now work the same regardless of the
# initial motor angle, because we always start
# from the mechanical endpoint.
for count in range(10):
    example_motor.run_target(speed, 180)
    example_motor.run_target(speed, 90)
Exemplo n.º 15
0
# Lower the acceleration so the car starts and stops realistically.
front.control.limits(acceleration=1000)
rear.control.limits(acceleration=1000)

# Find the steering endpoint on the left and right. The difference
# between them is the total angle it takes to go from left to right.
# The middle is in between.
left_end = steer.run_until_stalled(-200, then=Stop.HOLD)
right_end = steer.run_until_stalled(200, then=Stop.HOLD)

# We are now at the right. Reset this angle to be half the difference.
# That puts zero in the middle. From now on, running to 0 means to
# the middle.
steer.reset_angle((right_end - left_end) / 2)
steer.run_target(speed=200, target_angle=0, then=Stop.COAST)

# Now keep setting steering and driving based on keypad key:
#
#     <     ^     >
#      \    |    /
#       7   8   9
#
#  <--  4   5   6  -->
#
#       1   2   3
#      /    |    \
#     <     v     >

while True: