mirror of
https://github.com/pybricks/pybricks-projects.git
synced 2026-09-15 02:52:41 +00:00
snippets/pup: add PFMotor examples
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
from pybricks.pupdevices import ColorDistanceSensor, PFMotor
|
||||
from pybricks.parameters import Port, Color
|
||||
from pybricks.tools import wait
|
||||
|
||||
# Initialize the sensor.
|
||||
sensor = ColorDistanceSensor(Port.B)
|
||||
|
||||
# Initialize a motor on channel 1, on the red output.
|
||||
motor = PFMotor(sensor, 1, Color.RED)
|
||||
|
||||
# Rotate and then stop.
|
||||
motor.dc(100)
|
||||
wait(1000)
|
||||
motor.stop()
|
||||
wait(1000)
|
||||
|
||||
# Rotate the other way at half speed, and then stop.
|
||||
motor.dc(-50)
|
||||
wait(1000)
|
||||
motor.stop()
|
||||
@@ -0,0 +1,25 @@
|
||||
from pybricks.pupdevices import ColorDistanceSensor, PFMotor
|
||||
from pybricks.parameters import Port, Color, Direction
|
||||
from pybricks.tools import wait
|
||||
|
||||
# Initialize the sensor.
|
||||
sensor = ColorDistanceSensor(Port.B)
|
||||
|
||||
# You can use multiple motors on different channels.
|
||||
arm = PFMotor(sensor, 1, Color.BLUE)
|
||||
wheel = PFMotor(sensor, 4, Color.RED, Direction.COUNTERCLOCKWISE)
|
||||
|
||||
# Accelerate both motors. Only these values are available.
|
||||
# Other values will be rounded down to the nearest match.
|
||||
for duty in [15, 30, 45, 60, 75, 90, 100]:
|
||||
arm.dc(duty)
|
||||
wheel.dc(duty)
|
||||
wait(1000)
|
||||
|
||||
# To make the signal more reliable, there is a short
|
||||
# pause between commands. So, they change speed and
|
||||
# stop at a slightly different time.
|
||||
|
||||
# Brake both motors.
|
||||
arm.brake()
|
||||
wheel.brake()
|
||||
Reference in New Issue
Block a user