snippets/pup: add PFMotor examples

This commit is contained in:
Laurens Valk
2020-08-11 12:09:36 +02:00
parent bc2ec92634
commit e9735b2db0
2 changed files with 45 additions and 0 deletions
+20
View File
@@ -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()
+25
View File
@@ -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()