Skip to content

Commit d8384c7

Browse files
author
NewtonCrosby
committed
Fixed Mypy errors and added PR comments.
1 parent b645b6b commit d8384c7

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

commands2/pidsubsystem.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ def __init__(self, controller: PIDController, initial_position: float = 0.0):
1515
:param controller: The PIDController to use.
1616
:param initial_position: The initial setpoint of the subsystem.
1717
"""
18-
self.m_controller: PIDController = controller
18+
self._controller = controller
1919
self.setSetpoint(initial_position)
20-
self.addChild("PID Controller", self.m_controller)
21-
self.m_enabled = False
20+
self.addChild("PID Controller", self._controller)
21+
self._enabled = False
2222

2323
def periodic(self):
2424
"""
2525
Executes the PID control logic during each periodic update.
2626
2727
This method is called synchronously from the subsystem's periodic() method.
2828
"""
29-
if self.m_enabled:
29+
if self._enabled:
3030
self.useOutput(
31-
self.m_controller.calculate(self.getMeasurement()), self.getSetpoint()
31+
self._controller.calculate(self.getMeasurement()), self.getSetpoint()
3232
)
3333

3434
def getController(self) -> PIDController:
@@ -37,23 +37,23 @@ def getController(self) -> PIDController:
3737
3838
:return: The PIDController.
3939
"""
40-
return self.m_controller
40+
return self._controller
4141

4242
def setSetpoint(self, setpoint: float):
4343
"""
4444
Sets the setpoint for the subsystem.
4545
4646
:param setpoint: The setpoint for the subsystem.
4747
"""
48-
self.m_controller.setSetpoint(setpoint)
48+
self._controller.setSetpoint(setpoint)
4949

5050
def getSetpoint(self) -> float:
5151
"""
5252
Returns the current setpoint of the subsystem.
5353
5454
:return: The current setpoint.
5555
"""
56-
return self.m_controller.getSetpoint()
56+
return self._controller.getSetpoint()
5757

5858
def useOutput(self, output: float, setpoint: float):
5959
"""
@@ -74,12 +74,12 @@ def getMeasurement(self) -> float:
7474

7575
def enable(self):
7676
"""Enables the PID control. Resets the controller."""
77-
self.m_enabled = True
78-
self.m_controller.reset()
77+
self._enabled = True
78+
self._controller.reset()
7979

8080
def disable(self):
8181
"""Disables the PID control. Sets output to zero."""
82-
self.m_enabled = False
82+
self._enabled = False
8383
self.useOutput(0, 0)
8484

8585
def isEnabled(self) -> bool:
@@ -88,4 +88,4 @@ def isEnabled(self) -> bool:
8888
8989
:return: Whether the controller is enabled.
9090
"""
91-
return self.m_enabled
91+
return self._enabled

commands2/trapezoidprofilesubsystem.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,77 @@
22
# Open Source Software; you can modify and/or share it under the terms of
33
# the WPILib BSD license file in the root directory of this project.
44
from __future__ import annotations
5-
5+
from typing import Union
66
from .subsystem import Subsystem
77
from wpimath.trajectory import TrapezoidProfile
88

9+
910
class TrapezoidProfileSubsystem(Subsystem):
1011
"""A subsystem that generates and runs trapezoidal motion profiles automatically. The user specifies
1112
how to use the current state of the motion profile by overriding the `useState` method.
1213
1314
This class is provided by the NewCommands VendorDep
1415
"""
16+
1517
def __init__(
16-
self, constraints: TrapezoidProfile.Constraints,
17-
initial_position: float,
18-
period: float = 0.02
19-
):
18+
self,
19+
constraints: TrapezoidProfile.Constraints,
20+
initial_position: float,
21+
period: float = 0.02,
22+
):
2023
"""
2124
Creates a new TrapezoidProfileSubsystem.
2225
2326
:param constraints: The constraints (maximum velocity and acceleration) for the profiles.
2427
:param initial_position: The initial position of the controlled mechanism when the subsystem is constructed.
2528
:param period: The period of the main robot loop, in seconds.
2629
"""
27-
self.profile = TrapezoidProfile(constraints)
28-
self.state = TrapezoidProfile.State(initial_position, 0)
30+
self._profile = TrapezoidProfile(constraints)
31+
self._state = TrapezoidProfile.State(initial_position, 0)
2932
self.setGoal(initial_position)
30-
self.period = period
31-
self.enabled = True
33+
self._period = period
34+
self._enabled = True
3235

3336
def periodic(self):
3437
"""
3538
Executes the TrapezoidProfileSubsystem logic during each periodic update.
3639
3740
This method is called synchronously from the subsystem's periodic() method.
3841
"""
39-
self.state = self.profile.calculate(self.period, self.goal, self.state)
40-
if self.enabled:
41-
self.useState(self.state)
42+
self._state = self._profile.calculate(self._period, self.goal, self._state)
43+
if self._enabled:
44+
self.useState(self._state)
4245

43-
def setGoal(self, goal: TrapezoidProfile.State):
46+
def __setGoal(self, goal: TrapezoidProfile.State):
4447
"""
4548
Sets the goal state for the subsystem.
4649
4750
:param goal: The goal state for the subsystem's motion profile.
4851
"""
4952
self.goal = goal
5053

51-
def setGoal(self, goal: float):
54+
def setGoal(self, goal: Union[TrapezoidProfile.State, float]):
5255
"""
5356
Sets the goal state for the subsystem. Goal velocity assumed to be zero.
5457
55-
:param goal: The goal position for the subsystem's motion profile.
58+
:param goal: The goal position for the subsystem's motion profile. The goal
59+
can either be a `TrapezoidProfile.State` or `float`. If float is provided,
60+
the assumed velocity for the goal will be 0.
5661
"""
57-
self.setGoal(TrapezoidProfile.State(goal, 0))
62+
newGoal: TrapezoidProfile.State = goal
63+
# If we got a float, instantiate the state
64+
if isinstance(goal, float):
65+
newGoal = TrapezoidProfile.State(goal, 0)
66+
67+
self.__setGoal(newGoal)
5868

5969
def enable(self):
6070
"""Enable the TrapezoidProfileSubsystem's output."""
61-
self.enabled = True
71+
self._enabled = True
6272

6373
def disable(self):
6474
"""Disable the TrapezoidProfileSubsystem's output."""
65-
self.enabled = False
75+
self._enabled = False
6676

6777
def useState(self, state: TrapezoidProfile.State):
6878
"""

0 commit comments

Comments
 (0)