Пример #1
0
front = Motor(Port.A, Direction.COUNTERCLOCKWISE)

# 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.
Пример #2
0
drive_motor1 = Motor(Port.A)
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()
Пример #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)
Пример #4
0
# Basic trajectories starting from zero. It would be nice to compute them
# here in the loop so we can also include random tests, vary acceleration, etc.
trajectories = [
    ((500, 90), (0, 244, 244, 488, 0, 45, 45, 90, 0, 367, 1500, -1500)),
    ((1000, 720), (0, 666, 720, 1386, 0, 333, 387, 720, 0, 1000, 1500, -1500)),
    ((2000, 720), (0, 666, 720, 1386, 0, 333, 387, 720, 0, 1000, 1500, -1500)),
    ((10, 720), (0, 6, 72006, 72012, 0, 0, 720, 720, 0, 10, 1500, -1500)),
    ((-500, 360), (0, 333, 721, 1054, 0, -83, -277, -360, 0, -500, -1500,
                   1500)),
    ((500, -360), (0, 333, 721, 1054, 0, -83, -277, -360, 0, -500, -1500,
                   1500)),
    ((-500, -360), (0, 333, 721, 1054, 0, 83, 277, 360, 0, 500, 1500, -1500)),
    ((500, 1), (0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1500, -1500)),
    ((500, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)),
]

# Assert that all trajectories are correct.
for (speed, angle), trajectory in trajectories:
    # Start from 0.
    motor.reset_angle(0)

    # Initiate run_target command, stopping immediately.
    motor.run_angle(speed, angle, wait=False)
    result = motor.control.trajectory()
    motor.stop()

    # Compare generated trajectory to saved trajectory.
    assert trajectory == result, "Bad trajectory: {0} for {1}".format(
        result, (speed, angle))
Пример #5
0
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()
assert -5 <= motor.angle() <= 5, "Unexpected absolute motor angle."

# Test absolute angle for sign change by putting it at +90 degrees clockwise.
motor.run_target(500, 90)
assert 85 <= motor.angle() <= 95, "Unable to put motor in +90 position."
Пример #6
0
from pybricks.hubs import TechnicHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Port, Stop
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)
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)
Пример #8
0
from pybricks.hubs import InventorHub
from pybricks.pupdevices import Motor, ColorSensor
from pybricks.parameters import Port, Direction
from pybricks.tools import wait, StopWatch
from pybricks.geometry import Axis

# Initialize motors.
left = Motor(Port.C, Direction.COUNTERCLOCKWISE)
right = Motor(Port.D)
left.reset_angle(0)
right.reset_angle(0)

# Initialize hub and color sensor.
# You can also use the TechnicHub and/or the ColorDistanceSensor
# instead.
hub = InventorHub()
sensor = ColorSensor(Port.A)

# Initialize position buffer for speed calculation.
DT = 5
window = int(300 / DT)
buf = [0] * window
idx = 0
angle = 0
DRIVE_SPEED = 300
PAUSE = 5000

# Timer to generate reference position.
watch = StopWatch()

while True:
Пример #9
0
from pybricks.parameters import Port, Stop
from pybricks.tools import wait
from pybricks.experimental import getchar

hub = TechnicHub()

drive = Motor(Port.D)
steer = Motor(Port.B)

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

steer.reset_angle()
steer.run_target(SPEED_STEER, 0, then=Stop.BRAKE)

while True:
    c = getchar()
    if c == ord('q'):
        drive.dc(SPEED_DRIVE)
        wait(TIME_DRIVE)
    elif c == ord('a'):
        drive.dc(-SPEED_DRIVE)
        wait(TIME_DRIVE)
    elif c == ord('o'):
        if steer.angle() > -MAX_STEER:
            steer.run_angle(SPEED_STEER, -STEP_STEER, then=Stop.BRAKE)
    elif c == ord('p'):
        if steer.angle() < MAX_STEER:
Пример #10
0
from pybricks.hubs import MoveHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Port, Stop, Color
from pybricks.tools import wait

# Initialize the arm motor
arm = Motor(Port.D)
arm.run_until_stalled(-100, duty_limit=30)
arm.reset_angle(0)

# Initialize the belt motor
belt = Motor(Port.B)
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)
Пример #11
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)
Пример #12
0
from pybricks.pupdevices import Motor
from pybricks.parameters import Port

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

# Reset the angle to 0.
example_motor.reset_angle(0)

# Reset the angle to 1234.
example_motor.reset_angle(1234)

# Reset the angle to the absolute angle.
# This is only supported on motors that have
# an absolute encoder. For other motors, this
# will raise an error.
example_motor.reset_angle()