Skip to content

Commit e6132d5

Browse files
cwstrykervirtuald
authored andcommitted
Changed GenericProfiledPIDController to TProfiledPIDController follow Python naming convention. Added Generic annotations and a Protocol to profiledpidcommand.py.
1 parent 0cfa3b4 commit e6132d5

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

commands2/profiledpidcommand.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
# the WPILib BSD license file in the root directory of this project.
66
#
77

8-
from typing import Any, Callable, Union
9-
10-
from .command import Command
11-
from .subsystem import Subsystem
8+
from typing import Any, Callable, Generic, Union
129

1310
from wpimath.controller import ProfiledPIDController, ProfiledPIDControllerRadians
1411
from wpimath.trajectory import TrapezoidProfile, TrapezoidProfileRadians
1512

13+
from .command import Command
14+
from .subsystem import Subsystem
15+
from .typing import TProfiledPIDController, UseOutputFunction
16+
1617

17-
class ProfiledPIDCommand(Command):
18+
class ProfiledPIDCommand(Command, Generic[TProfiledPIDController]):
1819
"""A command that controls an output with a :class:`.ProfiledPIDController`. Runs forever by default -
1920
to add exit conditions and/or other behavior, subclass this class. The controller calculation and
2021
output are performed synchronously in the command's execute() method.
@@ -24,10 +25,10 @@ class ProfiledPIDCommand(Command):
2425

2526
def __init__(
2627
self,
27-
controller,
28+
controller: TProfiledPIDController,
2829
measurementSource: Callable[[], float],
2930
goalSource: Union[float, Callable[[], float]],
30-
useOutput: Callable[[float, Any], Any],
31+
useOutput: UseOutputFunction,
3132
*requirements: Subsystem,
3233
):
3334
"""Creates a new ProfiledPIDCommand, which controls the given output with a ProfiledPIDController. Goal
@@ -40,14 +41,15 @@ def __init__(
4041
:param requirements: the subsystems required by this command
4142
"""
4243

44+
super().__init__()
4345
if isinstance(controller, ProfiledPIDController):
4446
self._stateCls = TrapezoidProfile.State
4547
elif isinstance(controller, ProfiledPIDControllerRadians):
4648
self._stateCls = TrapezoidProfileRadians.State
4749
else:
4850
raise ValueError(f"unknown controller type {controller!r}")
4951

50-
self._controller = controller
52+
self._controller: TProfiledPIDController = controller
5153
self._useOutput = useOutput
5254
self._measurement = measurementSource
5355
if isinstance(goalSource, (float, int)):

commands2/profiledpidsubsystem.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
from wpimath.trajectory import TrapezoidProfile
88

99
from .subsystem import Subsystem
10-
from .typing import GenericProfiledPIDController
10+
from .typing import TProfiledPIDController
1111

1212

13-
class ProfiledPIDSubsystem(Subsystem, Generic[GenericProfiledPIDController]):
13+
class ProfiledPIDSubsystem(Subsystem, Generic[TProfiledPIDController]):
1414
"""
1515
A subsystem that uses a :class:`wpimath.controller.ProfiledPIDController`
1616
or :class:`wpimath.controller.ProfiledPIDControllerRadians` to
@@ -20,12 +20,12 @@ class ProfiledPIDSubsystem(Subsystem, Generic[GenericProfiledPIDController]):
2020

2121
def __init__(
2222
self,
23-
controller: GenericProfiledPIDController,
23+
controller: TProfiledPIDController,
2424
initial_position: float = 0,
2525
):
2626
"""Creates a new PIDSubsystem."""
2727
super().__init__()
28-
self._controller: GenericProfiledPIDController = controller
28+
self._controller: TProfiledPIDController = controller
2929
self._enabled = False
3030
self.setGoal(initial_position)
3131

@@ -39,7 +39,7 @@ def periodic(self):
3939

4040
def getController(
4141
self,
42-
) -> GenericProfiledPIDController:
42+
) -> TProfiledPIDController:
4343
"""Returns the controller"""
4444
return self._controller
4545

commands2/typing.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
from typing import TypeVar
1+
from typing import Protocol, TypeVar
22

33
from wpimath.controller import ProfiledPIDController, ProfiledPIDControllerRadians
4+
from wpimath.trajectory import TrapezoidProfile, TrapezoidProfileRadians
45

5-
GenericProfiledPIDController = TypeVar(
6-
"GenericProfiledPIDController", ProfiledPIDControllerRadians, ProfiledPIDController
6+
TProfiledPIDController = TypeVar(
7+
"TProfiledPIDController", ProfiledPIDControllerRadians, ProfiledPIDController
78
)
9+
TTrapezoidProfileState = TypeVar(
10+
"TTrapezoidProfileState",
11+
TrapezoidProfileRadians.State,
12+
TrapezoidProfile.State,
13+
)
14+
15+
16+
class UseOutputFunction(Protocol):
17+
18+
def __init__(self): ...
19+
20+
def __call__(self, t: float, u: TTrapezoidProfileState) -> None: ...
21+
22+
def accept(self, t: float, u: TTrapezoidProfileState) -> None: ...

0 commit comments

Comments
 (0)