Skip to content

Commit 604af69

Browse files
cwstrykervirtuald
authored andcommitted
Added more docstrings to ProfiledPIDController. Created and used TypeAliases (FloatSupplier & FloatOrFloatSupplier). Updated UseOutputFucntion protocol based on testing.
1 parent 910783c commit 604af69

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

commands2/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from . import button
44
from . import cmd
5+
from . import typing
56

67
from .commandscheduler import CommandScheduler
78
from .conditionalcommand import ConditionalCommand

commands2/profiledpidcommand.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
# the WPILib BSD license file in the root directory of this project.
66
#
77

8-
from typing import Any, Callable, Generic, Union
8+
from typing import Any, Generic
99

1010
from wpimath.controller import ProfiledPIDController, ProfiledPIDControllerRadians
1111
from wpimath.trajectory import TrapezoidProfile, TrapezoidProfileRadians
1212

1313
from .command import Command
1414
from .subsystem import Subsystem
15-
from .typing import TProfiledPIDController, UseOutputFunction
15+
from .typing import (
16+
FloatOrFloatSupplier,
17+
FloatSupplier,
18+
TProfiledPIDController,
19+
UseOutputFunction,
20+
)
1621

1722

1823
class ProfiledPIDCommand(Command, Generic[TProfiledPIDController]):
@@ -26,8 +31,8 @@ class ProfiledPIDCommand(Command, Generic[TProfiledPIDController]):
2631
def __init__(
2732
self,
2833
controller: TProfiledPIDController,
29-
measurementSource: Callable[[], float],
30-
goalSource: Union[float, Callable[[], float]],
34+
measurementSource: FloatSupplier,
35+
goalSource: FloatOrFloatSupplier,
3136
useOutput: UseOutputFunction,
3237
*requirements: Subsystem,
3338
):

commands2/profiledpidsubsystem.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ def __init__(
2525
controller: TProfiledPIDController,
2626
initial_position: float = 0,
2727
):
28-
"""Creates a new PIDSubsystem."""
28+
"""
29+
Creates a new Profiled PID Subsystem using the provided PID Controller
30+
31+
:param controller: the controller that controls the output
32+
:param initial_position: the initial value of the process variable
33+
34+
"""
2935
super().__init__()
3036
self._controller: TProfiledPIDController = controller
3137
self._enabled = False
@@ -46,15 +52,11 @@ def getController(
4652
return self._controller
4753

4854
def setGoal(self, goal):
49-
"""
50-
Sets the goal state for the subsystem.
51-
"""
55+
"""Sets the goal state for the subsystem."""
5256
self._controller.setGoal(goal)
5357

5458
def useOutput(self, output: float, setpoint: TTrapezoidProfileState):
55-
"""
56-
Uses the output from the controller object.
57-
"""
59+
"""Uses the output from the controller object."""
5860
raise NotImplementedError(f"{self.__class__} must implement useOutput")
5961

6062
def getMeasurement(self) -> float:
@@ -75,7 +77,5 @@ def disable(self):
7577
self.useOutput(0, TrapezoidProfile.State())
7678

7779
def isEnabled(self) -> bool:
78-
"""
79-
Returns whether the controller is enabled.
80-
"""
80+
"""Returns whether the controller is enabled."""
8181
return self._enabled

commands2/typing.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
from typing import Protocol, TypeVar
1+
from typing import Callable, Protocol, TypeVar, Union
22

3+
from typing_extensions import TypeAlias
34
from wpimath.controller import ProfiledPIDController, ProfiledPIDControllerRadians
45
from wpimath.trajectory import TrapezoidProfile, TrapezoidProfileRadians
56

7+
# Generic Types
68
TProfiledPIDController = TypeVar(
79
"TProfiledPIDController", ProfiledPIDControllerRadians, ProfiledPIDController
810
)
@@ -13,10 +15,16 @@
1315
)
1416

1517

18+
# Protocols - Structural Typing
1619
class UseOutputFunction(Protocol):
1720

18-
def __init__(self): ...
21+
def __init__(self, *args, **kwargs) -> None: ...
1922

2023
def __call__(self, t: float, u: TTrapezoidProfileState) -> None: ...
2124

2225
def accept(self, t: float, u: TTrapezoidProfileState) -> None: ...
26+
27+
28+
# Type Aliases
29+
FloatSupplier: TypeAlias = Callable[[], float]
30+
FloatOrFloatSupplier: TypeAlias = Union[float, Callable[[], float]]

0 commit comments

Comments
 (0)