From e9735b2db0abf3ef004ed2620a93d3ac4d24d617 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Tue, 11 Aug 2020 12:00:03 +0200 Subject: [PATCH] snippets/pup: add PFMotor examples --- snippets/pup/motor_pf/motor_pf_basics.py | 20 +++++++++++++++++++ snippets/pup/motor_pf/motor_pf_pwm.py | 25 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 snippets/pup/motor_pf/motor_pf_basics.py create mode 100644 snippets/pup/motor_pf/motor_pf_pwm.py diff --git a/snippets/pup/motor_pf/motor_pf_basics.py b/snippets/pup/motor_pf/motor_pf_basics.py new file mode 100644 index 0000000..25558d3 --- /dev/null +++ b/snippets/pup/motor_pf/motor_pf_basics.py @@ -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() diff --git a/snippets/pup/motor_pf/motor_pf_pwm.py b/snippets/pup/motor_pf/motor_pf_pwm.py new file mode 100644 index 0000000..65c5bb0 --- /dev/null +++ b/snippets/pup/motor_pf/motor_pf_pwm.py @@ -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()