示例#1
0
Hardware Module: 1

Description: Verifies motor readings such as angle, absolute angle, and speed.
"""

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):
示例#2
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)