From 8bca5eb6e4e29a751d34ebe13b016217293371d7 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Fri, 21 Apr 2023 10:32:57 +0200 Subject: [PATCH] pybricks.robotics: Add GyroDriveBase. --- CHANGELOG.md | 1 + doc/common/extensions/requirements-static.py | 8 ++-- doc/main/robotics.rst | 39 +++++++++++++++++++- examples/pup/robotics/drivebase_basics.py | 4 +- jedi/tests/test_complete_import.py | 4 +- src/pybricks/robotics.py | 6 +++ 6 files changed, 51 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4806e72..809f60e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ state and change its settings. - Added `rotation`, `orientation`, `ready`, `stationary` and `settings` methods to `IMU` class. +- Added `GyroDriveBase` class to `pybricks.robotics`. ### Changed - Change implementation status of `IMU.heading` and `IMU.reset_heading`. They diff --git a/doc/common/extensions/requirements-static.py b/doc/common/extensions/requirements-static.py index 755e0d4..991dd3c 100644 --- a/doc/common/extensions/requirements-static.py +++ b/doc/common/extensions/requirements-static.py @@ -24,10 +24,10 @@ FEATURES_LARGE = FEATURES_MEDIUM | set() HUB_FEATURES = { "movehub": {"movehub"} | FEATURES_SMALL, "cityhub": {"cityhub"} | FEATURES_MEDIUM, - "technichub": {"technichub"} | FEATURES_MEDIUM, - "primehub": {"primehub", "inventorhub", "light-matrix"} | FEATURES_LARGE, - "inventorhub": {"primehub", "inventorhub", "light-matrix"} | FEATURES_LARGE, - "essentialhub": {"essentialhub"} | FEATURES_LARGE, + "technichub": {"technichub", "gyro"} | FEATURES_MEDIUM, + "primehub": {"primehub", "inventorhub", "light-matrix", "gyro"} | FEATURES_LARGE, + "inventorhub": {"primehub", "inventorhub", "light-matrix", "gyro"} | FEATURES_LARGE, + "essentialhub": {"essentialhub", "gyro"} | FEATURES_LARGE, } diff --git a/doc/main/robotics.rst b/doc/main/robotics.rst index d60b9c0..98c93ae 100644 --- a/doc/main/robotics.rst +++ b/doc/main/robotics.rst @@ -1,11 +1,10 @@ -.. pybricks-requirements:: - :mod:`robotics ` -- Robotics and drive bases =============================================================== .. automodule:: pybricks.robotics :no-members: +.. pybricks-requirements:: .. autoclass:: pybricks.robotics.DriveBase :no-members: @@ -113,11 +112,47 @@ The :meth:`done` and :meth:`stalled` methods have been moved. + +.. pybricks-requirements:: gyro + +.. class:: GyroDriveBase + + This class works just like the :class:`DriveBase`, but it uses the hub's + built-in gyroscope to drive straight and turn more accurately. + + If your hub is not mounted flat in your robot, make sure to specify + the ``top_side`` and ``front_side`` parameters when you initialize the + :class:`PrimeHub() `, + :class:`InventorHub() `, + :class:`EssentialHub() `, or + :class:`TechnicHub() `. This way your robot + knows which rotation to measure when turning. + + The gyro in each hub is a bit different, which can cause it to be a few + degrees off for big turns, or many small turns in the same + direction. For example, you may need to use + :meth:`turn(357) ` or + :meth:`turn(362) ` + on your robot to make a full turn. + + By default, this class tries to maintain the robot's position after a move + completes. This means the wheels will spin if you pick the robot up, in an + effort to maintain its heading angle. To avoid this, you can choose + ``then=Stop.COAST`` in your last + :meth:`straight `, + :meth:`turn `, or + :meth:`curve ` command. + Examples ------------------- Driving straight and turning in place ********************************************** +The following program shows the basics of driving and turning. + +To use the built-in gyro, just replace the two occurences of +:class:`DriveBase` with :class:`GyroDriveBase`. + .. literalinclude:: ../../examples/pup/robotics/drivebase_basics.py diff --git a/examples/pup/robotics/drivebase_basics.py b/examples/pup/robotics/drivebase_basics.py index ddb4eb2..66e189f 100644 --- a/examples/pup/robotics/drivebase_basics.py +++ b/examples/pup/robotics/drivebase_basics.py @@ -14,10 +14,10 @@ drive_base = DriveBase(left_motor, right_motor, wheel_diameter=56, axle_track=11 # Drive forward by 500mm (half a meter). drive_base.straight(500) -# Turn around clockwise (180 degrees) +# Turn around clockwise by 180 degrees. drive_base.turn(180) -# Drive forward again to drive back. +# Drive forward again to get back to the start. drive_base.straight(500) # Turn around counterclockwise. diff --git a/jedi/tests/test_complete_import.py b/jedi/tests/test_complete_import.py index 40af678..e69a424 100644 --- a/jedi/tests/test_complete_import.py +++ b/jedi/tests/test_complete_import.py @@ -159,9 +159,7 @@ def test_from_pybricks_pupdevices_import(): def test_from_pybricks_robotics_import(): code = "from pybricks.robotics import " completions: list[CompletionItem] = json.loads(complete(code, 1, len(code) + 1)) - assert [c["insertText"] for c in completions] == [ - "DriveBase", - ] + assert [c["insertText"] for c in completions] == ["DriveBase", "GyroDriveBase"] def test_from_pybricks_tools_import(): diff --git a/src/pybricks/robotics.py b/src/pybricks/robotics.py index 15d1aef..65ef209 100644 --- a/src/pybricks/robotics.py +++ b/src/pybricks/robotics.py @@ -222,6 +222,12 @@ class DriveBase: """ +class GyroDriveBase(DriveBase): + """A robotic vehicle with two powered wheels and an optional support + wheel or caster. It measures the heading using the hub's built-in gyroscope, + which can make turning and driving straight more accurate.""" + + # HACK: hide from jedi if TYPE_CHECKING: del Motor