From 492d6cd2d4edb8057c423a5a3dfd456714fe1678 Mon Sep 17 00:00:00 2001 From: Barry-Williams <69337381+Barry-Williams@users.noreply.github.com> Date: Fri, 28 Oct 2022 15:29:56 +1030 Subject: [PATCH] pybricks.robotics.DriveBase: Add basic example. --- doc/main/robotics.rst | 9 +++++++++ examples/pup/robotics/drivebase_basics.py | 24 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 examples/pup/robotics/drivebase_basics.py diff --git a/doc/main/robotics.rst b/doc/main/robotics.rst index 36dd24e..68ab5ab 100644 --- a/doc/main/robotics.rst +++ b/doc/main/robotics.rst @@ -104,3 +104,12 @@ .. autoattribute:: pybricks.robotics.DriveBase.heading_control :annotation: + +Examples +------------------- + +Driving straight and turning in place +********************************************** + +.. literalinclude:: + ../../examples/pup/robotics/drivebase_basics.py diff --git a/examples/pup/robotics/drivebase_basics.py b/examples/pup/robotics/drivebase_basics.py new file mode 100644 index 0000000..ddb4eb2 --- /dev/null +++ b/examples/pup/robotics/drivebase_basics.py @@ -0,0 +1,24 @@ +from pybricks.pupdevices import Motor +from pybricks.parameters import Port, Direction +from pybricks.robotics import DriveBase + +# Initialize both motors. In this example, the motor on the +# left must turn counterclockwise to make the robot go forward. +left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE) +right_motor = Motor(Port.B) + +# Initialize the drive base. In this example, the wheel diameter is 56mm. +# The distance between the two wheel-ground contact points is 112mm. +drive_base = DriveBase(left_motor, right_motor, wheel_diameter=56, axle_track=112) + +# Drive forward by 500mm (half a meter). +drive_base.straight(500) + +# Turn around clockwise (180 degrees) +drive_base.turn(180) + +# Drive forward again to drive back. +drive_base.straight(500) + +# Turn around counterclockwise. +drive_base.turn(-180)