pybricks.robotics: Add typing.

This commit is contained in:
Laurens Valk
2022-06-10 10:05:12 +02:00
parent 31256d70c1
commit fb3446ad0d
2 changed files with 81 additions and 85 deletions
+81 -46
View File
@@ -3,10 +3,12 @@
"""Robotics module for the Pybricks API."""
from ._common import Control as _Control
from ._common import Control, Motor, Number
from .parameters import Stop as _Stop
from typing import Tuple, Optional, overload
class DriveBase:
"""A robotic vehicle with two powered wheels and an optional support
@@ -25,7 +27,7 @@ class DriveBase:
"""
distance_control = _Control()
distance_control = Control()
"""The traveled distance and drive speed are controlled by a PID
controller. You can use this attribute to change its settings.
See the :ref:`motor control <settings>` attribute for an overview of
@@ -33,7 +35,7 @@ class DriveBase:
functionality, but the settings apply to every millimeter driven by the
drive base, instead of degrees turned by one motor."""
heading_control = _Control()
heading_control = Control()
"""The robot turn angle and turn rate are controlled by a PID
controller. You can use this attribute to change its settings.
See the :ref:`motor control <settings>` attribute for an overview of
@@ -42,7 +44,13 @@ class DriveBase:
whole drive base (viewed from the top) instead of degrees turned by one
motor."""
def __init__(self, left_motor, right_motor, wheel_diameter, axle_track):
def __init__(
self,
left_motor: Motor,
right_motor: Motor,
wheel_diameter: Number,
axle_track: Number,
):
"""DriveBase(left_motor, right_motor, wheel_diameter, axle_track)
Arguments:
@@ -50,105 +58,132 @@ class DriveBase:
The motor that drives the left wheel.
right_motor (Motor):
The motor that drives the right wheel.
wheel_diameter (:ref:`dimension`): Diameter of the wheels.
axle_track (:ref:`dimension`): Distance between the points where
wheel_diameter (Number, mm): Diameter of the wheels.
axle_track (Number, mm): Distance between the points where
both wheels touch the ground.
"""
def drive(self, speed, turn_rate):
"""Starts driving at the specified speed and turn rate. Both values are
def drive(self, speed: Number, turn_rate: Number) -> None:
"""drive(speed, turn_rate)
Starts driving at the specified speed and turn rate. Both values are
measured at the center point between the wheels of the robot.
Arguments:
speed (:ref:`linspeed`): Speed of the robot.
turn_rate (:ref:`speed`): Turn rate of the robot.
speed (Number, mm/s): Speed of the robot.
turn_rate (Number, deg/s): Turn rate of the robot.
"""
pass
def stop(self):
"""Stops the robot by letting the motors spin freely."""
def stop(self) -> None:
"""stop()
Stops the robot by letting the motors spin freely."""
pass
def distance(self):
"""Gets the estimated driven distance.
def distance(self) -> int:
"""distance() -> int: mm
Gets the estimated driven distance.
Returns:
:ref:`distance`: Driven distance since last reset.
Driven distance since last reset.
"""
pass
def angle(self):
"""Gets the estimated rotation angle of the drive base.
def angle(self) -> int:
"""angle() -> int: deg
Gets the estimated rotation angle of the drive base.
Returns:
:ref:`angle`: Accumulated angle since last reset.
Accumulated angle since last reset.
"""
pass
def state(self):
"""Gets the state of the robot.
def state(self) -> Tuple[int, int, int, int]:
"""state() -> Tuple[int, int, int, int]
This returns the current :meth:`.distance`, the drive speed, the
:meth:`.angle`, and the turn rate.
Gets the state of the robot.
:returns: Distance, drive speed, angle, turn rate
:rtype: (:ref:`distance`, :ref:`linspeed`, :ref:`angle`, :ref:`speed`)
Returns:
Tuple of distance, drive speed, angle, and turn rate of the robot.
"""
pass
def reset(self):
"""Resets the estimated driven distance and angle to 0."""
def reset(self) -> None:
"""reset()
Resets the estimated driven distance and angle to 0."""
pass
@overload
def settings(
self, straight_speed, straight_acceleration, turn_rate, turn_acceleration
):
"""Configures the speed and acceleration used
self,
straight_speed: Optional[Number],
straight_acceleration: Optional[Number],
turn_rate: Optional[Number],
turn_acceleration: Optional[Number],
) -> None:
...
@overload
def settings(self) -> Tuple[int, int, int, int]:
...
def settings(self, *args):
"""settings(straight_speed, straight_acceleration, turn_rate, turn_acceleration)
settings() -> Tuple[int, int, int, int]
Configures the speed and acceleration used
by :meth:`.straight`, :meth:`.turn`, and :meth:`.curve`.
If you give no arguments, this returns the current values as a tuple.
You can only change the settings while the robot is stopped. This is
either before you begin driving or after you call :meth:`.stop`.
Arguments:
straight_speed (:ref:`linspeed`): Straight-line speed of the robot.
straight_acceleration (:ref:`linacceleration`): Straight-line
straight_speed (Number, mm/s): Straight-line speed of the robot.
straight_acceleration (Number, mm/s²): Straight-line
acceleration and deceleration of the robot.
turn_rate (:ref:`speed`): Turn rate of the robot.
turn_acceleration (:ref:`acceleration`): Angular acceleration and
turn_rate (Number, deg/s): Turn rate of the robot.
turn_acceleration (Number, deg/s²): Angular acceleration and
deceleration of the robot.
"""
pass
def straight(self, distance, then=_Stop.HOLD, wait=True):
"""Drives straight for a given distance and then stops.
def straight(self, distance, then=_Stop.HOLD, wait=True) -> None:
"""straight(distance, then=Stop.HOLD, wait=True)
Drives straight for a given distance and then stops.
Arguments:
distance (:ref:`distance`): Distance to travel
distance (Number, mm): Distance to travel
then (Stop): What to do after coming to a standstill.
wait (bool): Wait for the maneuver to complete before continuing
with the rest of the program.
"""
pass
def turn(self, angle, then=_Stop.HOLD, wait=True):
"""Turns in place by a given angle and then stops.
def turn(self, angle, then=_Stop.HOLD, wait=True) -> None:
"""turn(angle, then=Stop.HOLD, wait=True)
Turns in place by a given angle and then stops.
Arguments:
angle (:ref:`angle`): Angle of the turn.
angle (Number, deg): Angle of the turn.
then (Stop): What to do after coming to a standstill.
wait (bool): Wait for the maneuver to complete before continuing
with the rest of the program.
"""
pass
def curve(self, radius, angle, then=_Stop.HOLD, wait=True):
"""Drives an arc along a circle of a given radius, by a given angle.
def curve(self, radius, angle, then=_Stop.HOLD, wait=True) -> None:
"""curve(radius, angle, then=Stop.HOLD, wait=True)
Drives an arc along a circle of a given radius, by a given angle.
Arguments:
radius (:ref:`dimension`): Radius of the circle.
angle (:ref:`angle`): Angle along the circle.
radius (Number, mm): Radius of the circle.
angle (Number, deg): Angle along the circle.
then (Stop): What to do after coming to a standstill.
wait (bool): Wait for the maneuver to complete before continuing
with the rest of the program.
-39
View File
@@ -1,39 +0,0 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2020-2021 The Pybricks Authors
from typing import Optional, Tuple, overload, Union
from ._common import Control, Motor
from .parameters import Stop
class DriveBase:
distance_control: Control
heading_control: Control
def __init__(
self,
left_motor: Motor,
right_motor: Motor,
wheel_diameter: Union[int, float],
axle_track: Union[int, float],
): ...
def drive(self, speed: int, turn_rate: int) -> None: ...
def stop(self) -> None: ...
def distance(self) -> int: ...
def angle(self) -> int: ...
def state(self) -> Tuple[int, int, int, int]: ...
def reset(self) -> None: ...
@overload
def settings(self) -> Tuple[int, int, int, int]: ...
@overload
def settings(
self,
straight_speed: Optional[int],
straight_acceleration: Optional[int],
turn_rate: Optional[int],
turn_acceleration: Optional[int],
) -> None: ...
def straight(self, distance: int) -> None: ...
def turn(self, angle: int) -> None: ...
def curve(
self, radius: int, angle: int, then: Stop = Stop.HOLD, wait: bool = True
) -> None: ...