Skip to content

Commit 314703e

Browse files
committed
Factored in the edits.
1 parent 916ade3 commit 314703e

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

commands2/pidcommand.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# the WPILib BSD license file in the root directory of this project.
44
from __future__ import annotations
55

6-
from typing import Any, Callable
6+
from typing import Any, Callable, Union
77

88
from .command import Command
99
from .subsystem import Subsystem
@@ -16,15 +16,13 @@ class PIDCommand(Command):
1616
A command that controls an output with a PIDController. Runs forever by default - to add
1717
exit conditions and/or other behavior, subclass this class. The controller calculation and output
1818
are performed synchronously in the command's execute() method.
19-
20-
This class is provided by the NewCommands VendorDep
2119
"""
2220

2321
def __init__(
2422
self,
2523
controller: PIDController,
2624
measurementSource: Callable[[], float],
27-
setpoint: float,
25+
setpoint: Union[Callable[[], float], float],
2826
useOutput: Callable[[float], Any],
2927
*requirements: Subsystem,
3028
):
@@ -38,34 +36,32 @@ def __init__(
3836
:param requirements: the subsystems required by this command
3937
"""
4038
super().__init__()
41-
if controller is None:
42-
raise ValueError("controller must not be None")
43-
if measurementSource is None:
44-
raise ValueError("measurementSource must not be None")
45-
if setpoint is None:
46-
raise ValueError("setpointSource must not be None")
47-
if useOutput is None:
48-
raise ValueError("useOutput must not be None")
4939

50-
self.controller = controller
51-
self.useOutput = useOutput
52-
self.measurement = measurementSource
53-
self.setpoint = setpoint
54-
self.requirements.addAll(set(requirements))
40+
self._controller = controller
41+
self._useOutput = useOutput
42+
self._measurement = measurementSource
43+
44+
if isinstance(setpoint, (float, int)):
45+
setpoint = lambda: setpoint
46+
47+
self._setpoint = setpoint
48+
self.addRequirements(*requirements)
5549

5650
def initialize(self):
57-
self.controller.reset()
51+
self._controller.reset()
5852

5953
def execute(self):
60-
self.useOutput(self.controller.calculate(self.measurement(), self.setpoint))
54+
self._useOutput(
55+
self._controller.calculate(self._measurement(), self._setpoint())
56+
)
6157

6258
def end(self, interrupted):
63-
self.useOutput(0)
59+
self._useOutput(0)
6460

6561
def getController(self):
6662
"""
6763
Returns the PIDController used by the command.
6864
6965
:return: The PIDController
7066
"""
71-
return self.controller
67+
return self._controller

0 commit comments

Comments
 (0)