Skip to content

Commit b40c398

Browse files
committed
pybricks.common.Motor: Add Model() class instance.
This is mainly used to debug the motor model, and to quickly select the feedback parameters. This is useful when adding or configuring new motor types. Reading the estimated speed can also be useful in advanced applications where the user builds their own PID controller. We may choose to hide this method from the documentation before the release.
1 parent 13612bd commit b40c398

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
### Added
88
- Documented ``integral_deadzone`` in ``Control.pid()``.
9+
- Documented ``Motor.model``. This can be used to view the estimated motor
10+
state and change its settings.
911

1012
## 3.2.0 - 2022-12-20
1113

doc/main/pupdevices/motor.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ Motors with rotation sensors
9393
The :meth:`done`, :meth:`stalled` and :meth:`load` methods have been
9494
moved.
9595

96+
.. pybricks-requirements:: pybricks-common-control
97+
98+
.. automethod:: pybricks.pupdevices.Motor.model.state
99+
100+
.. pybricks-requirements:: pybricks-common-control
101+
102+
.. automethod:: pybricks.pupdevices.Motor.model.settings
103+
96104
Initialization examples
97105
-----------------------
98106

src/pybricks/_common.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,52 @@ def stall_tolerances(self, speed, time):
304304
"""
305305

306306

307+
class Model:
308+
"""Class to interact with motor state observer and settings."""
309+
310+
def state(self) -> Tuple[float, float, float, bool]:
311+
"""state() -> Tuple[float, float, float, bool]
312+
313+
Gets the estimated angle, speed, current, and stall state of the motor,
314+
using a simulation model that mimics the real motor.
315+
These estimates are updated faster than the real measurements,
316+
which can be useful when building your own PID controllers.
317+
318+
For most applications it is better to used the *measured*
319+
:meth:`angle <pybricks.pupdevices.Motor.angle>`,
320+
:meth:`speed <pybricks.pupdevices.Motor.speed>`,
321+
:meth:`load <pybricks.pupdevices.Motor.load>`, and
322+
:meth:`stall <pybricks.pupdevices.Motor.stalled>` state instead.
323+
324+
Returns:
325+
Tuple with the estimated angle (deg), speed (deg/s), current (mA),
326+
and stall state (``True`` or ``False``).
327+
"""
328+
329+
@overload
330+
def settings(self, values: tuple) -> None:
331+
...
332+
333+
@overload
334+
def settings(self) -> tuple:
335+
...
336+
337+
def settings(self, speed, time):
338+
"""settings(values)
339+
settings() -> Tuple
340+
341+
Gets or sets model settings as a tuple of integers. If no arguments are
342+
given, this will return the current values. This method is mainly used
343+
to debug the motor model class. Changing these settings should not be
344+
needed in user programs.
345+
346+
.. _model settings: https://docs.pybricks.com/projects/pbio/en/latest/struct__pbio__observer__settings__t.html
347+
348+
Arguments:
349+
values (Tuple): Tuple with `model settings`_.
350+
"""
351+
352+
307353
class Motor(DCMotor):
308354
"""Generic class to control motors with built-in rotation sensors."""
309355

@@ -313,6 +359,9 @@ class Motor(DCMotor):
313359
``control`` attribute of the motor. See :ref:`control` for an overview
314360
of available methods."""
315361

362+
model = Model()
363+
"""Model representing the observer that estimates the motor state."""
364+
316365
def __init__(
317366
self,
318367
port: Port,

0 commit comments

Comments
 (0)