Example #1
0
    (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
    # how this is currently defined. For now, 1 degree both ways will do.
    assert abs(motor.angle() - expected) <= 1, "{0} != {1}".format(
Example #2
0
motor.run_target(500, 90)
assert 85 <= motor.angle() <= 95, "Unable to put motor in +90 position."
motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
assert -85 >= motor.angle() >= -95, "Unexpected angle after CCW init."

# Test DC positive direction signs.
for direction in (Direction.CLOCKWISE, Direction.COUNTERCLOCKWISE, "default"):

    # Initialize the motor with given sign.
    if direction == "default":
        motor = Motor(Port.A)
    else:
        motor = Motor(Port.A, direction)

    old_angle = motor.angle()
    motor.dc(100)
    wait(1000)
    assert motor.angle() > old_angle + 90
    motor.dc(0)
    wait(500)

# The motor is now in positive orientation. Test DC forward.
old_angle = motor.angle()
motor.dc(50)
wait(1000)
assert motor.angle() > old_angle + 45
motor.dc(0)
wait(500)

# The motor is now in positive orientation. Test DC backward.
old_angle = motor.angle()
Example #3
0
from pybricks.parameters import Port
from pybricks.tools import wait

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

# Run at 500 deg/s and then stop by coasting.
print("Demo of run")
example_motor.run(500)
wait(1500)
example_motor.stop()
wait(1500)

# Run at 70% duty cycle ("power") and then stop by coasting.
print("Demo of dc")
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
Example #4
0
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)
    elif c == ord('r') or c == ord('0'):
        mSteer.run_target(SPEED_STEER, 0, then=Stop.BRAKE)
    else:
        mDrive.stop()
Example #5
0
    # Get motor position.
    position = (left.angle() + right.angle()) / 2

    # Calculate motor speed.
    speed = (position - buf[idx]) / (window * DT) * 1000
    buf[idx] = position
    idx = (idx + 1) % window

    # Calculate reference position, which just grows linearly with
    # time.
    reference = -max(watch.time() - PAUSE, 0) / 1000 * DRIVE_SPEED

    # Calculate duty cycle.
    diff = position - reference
    duty = 0.018 * rate + 19 * angle + 0.45 * diff + 0.16 * speed

    # Account for battery level and type.
    duty *= 7200 / hub.battery.voltage()

    # Calculate steering.
    reflection = sensor.reflection()
    steering = (reflection - 28) * 0.6

    # Apply duty cycle for balancing and steering.
    left.dc(duty + steering)
    right.dc(duty - steering)

    # Wait some time.
    wait(DT)
Example #6
0
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:
            steer.run_angle(SPEED_STEER, STEP_STEER, then=Stop.BRAKE)
    elif c == ord('r') or c == ord('0'):
        steer.run_target(SPEED_STEER, 0, then=Stop.BRAKE)
    else:
        drive.stop()