Files
pybricks-api/examples/pup/motor/motor_until_stalled.py
T
Laurens Valk b0ffb24a2e examples: import snippets from pybricks-projects
These code samples are rendered in the docs, so we include them here.

For the git history of these snippets, see:
https://github.com/pybricks/pybricks-projects/commits/f4914069aaacfd9ab1e0dd6f05524f62cfc56b29/snippets
2020-11-04 10:17:33 +01:00

27 lines
917 B
Python

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)