Skip to content

Commit

Permalink
pybricks.robotics: Add GyroDriveBase.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurensvalk committed Apr 21, 2023
1 parent 95a12a7 commit 8bca5eb
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions doc/common/extensions/requirements-static.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
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,
}


Expand Down
39 changes: 37 additions & 2 deletions doc/main/robotics.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
.. pybricks-requirements::

:mod:`robotics <pybricks.robotics>` -- Robotics and drive bases
===============================================================

.. automodule:: pybricks.robotics
:no-members:

.. pybricks-requirements::

.. autoclass:: pybricks.robotics.DriveBase
:no-members:
Expand Down Expand Up @@ -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() <pybricks.hubs.PrimeHub>`,
:class:`InventorHub() <pybricks.hubs.PrimeHub>`,
:class:`EssentialHub() <pybricks.hubs.EssentialHub>`, or
:class:`TechnicHub() <pybricks.hubs.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) <pybricks.robotics.DriveBase.turn>` or
:meth:`turn(362) <pybricks.robotics.DriveBase.turn>`
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 <pybricks.robotics.DriveBase.straight>`,
:meth:`turn <pybricks.robotics.DriveBase.turn>`, or
:meth:`curve <pybricks.robotics.DriveBase.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
4 changes: 2 additions & 2 deletions examples/pup/robotics/drivebase_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
# 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.
Expand Down
4 changes: 1 addition & 3 deletions jedi/tests/test_complete_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
6 changes: 6 additions & 0 deletions src/pybricks/robotics.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ def stalled(self) -> bool:
"""


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
Expand Down

0 comments on commit 8bca5eb

Please sign in to comment.