Skip to content

Commit 889ce00

Browse files
author
NewtonCrosby
committed
Added requested PR changes and fixed Mypy errors
1 parent 9c7ed12 commit 889ce00

File tree

1 file changed

+71
-77
lines changed

1 file changed

+71
-77
lines changed

commands2/mecanumcontrollercommand.py

Lines changed: 71 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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-
from typing import Set, Callable, Union, List
5+
from typing import Set, Callable, Union, List, Optional
66

77
from wpimath.controller import (
88
HolonomicDriveController,
@@ -40,11 +40,6 @@ class MecanumControllerCommand(Command):
4040
to the angle given in the final state of the trajectory.
4141
"""
4242

43-
__FRONT_LEFT_INDEX = 0
44-
__REAR_LEFT_INDEX = 1
45-
__FRONT_RIGHT_INDEX = 2
46-
__REAR_RIGHT_INDEX = 3
47-
4843
def __init__(
4944
self,
5045
trajectory: Trajectory,
@@ -55,115 +50,108 @@ def __init__(
5550
thetaController: ProfiledPIDControllerRadians,
5651
# rotationSupplier: Callable[[], Rotation2d],
5752
maxWheelVelocityMetersPerSecond: float,
58-
outputConsumer: Callable[[Union[List[float], MecanumDriveWheelSpeeds]], None],
53+
outputConsumer: Callable[[MecanumDriveWheelSpeeds], None],
5954
*requirements: Subsystem,
60-
feedforward: SimpleMotorFeedforwardMeters | None = None,
61-
chassisWheelPIDControllers: List[PIDController] | None = None,
62-
currentWheelSpeedsSupplier: Callable[[], MecanumDriveWheelSpeeds] | None = None,
55+
feedforward: Optional[SimpleMotorFeedforwardMeters] = None,
56+
frontLeftController: Optional[PIDController] = None,
57+
rearLeftController: Optional[PIDController] = None,
58+
frontRightController: Optional[PIDController] = None,
59+
rearRightController: Optional[PIDController] = None,
60+
currentWheelSpeedsSupplier: Optional[
61+
Callable[[], MecanumDriveWheelSpeeds]
62+
] = None,
6363
):
6464
super().__init__()
6565

66-
self.trajectory: Trajectory = trajectory
67-
self.pose = pose
68-
self.kinematics = kinematics
69-
self.controller = HolonomicDriveController(
66+
self._trajectory: Trajectory = trajectory
67+
self._pose = pose
68+
self._kinematics = kinematics
69+
self._controller = HolonomicDriveController(
7070
xController, yController, thetaController
7171
)
7272
# self.rotationSupplier = rotationSupplier
73-
self.maxWheelVelocityMetersPerSecond = maxWheelVelocityMetersPerSecond
74-
self.outputConsumer = outputConsumer
73+
self._maxWheelVelocityMetersPerSecond = maxWheelVelocityMetersPerSecond
74+
self._outputConsumer = outputConsumer
7575

7676
# Member to track time of last loop execution
77-
self.prevTime = 0
78-
self.usePID = False
77+
self._prevTime = 0
78+
self._usePID = False
7979

8080
# Optional requirements checks. If any one of the feedforward, pidcontrollers, or wheelspeedsuppliers
8181
# are not None, then all should be non-null. Raise RuntimeError if they are. The List of PID controllers
8282
# should be equal to each corner of the robot being commanded.
8383
if (
8484
feedforward
85-
or chassisWheelPIDControllers
85+
or frontLeftController
86+
or rearLeftController
87+
or frontRightController
88+
or rearRightController
8689
or currentWheelSpeedsSupplier is not None
8790
):
8891
if (
8992
feedforward
90-
or chassisWheelPIDControllers
93+
or frontLeftController
94+
or rearLeftController
95+
or frontRightController
96+
or rearRightController
9197
or currentWheelSpeedsSupplier is None
9298
):
9399
raise RuntimeError(
94-
f"Failed to instantiate MecanumControllerCommand, one of the arguments passed in was None: \n \
95-
\t Feedforward: {feedforward} - chassisWheelPIDControllers: {chassisWheelPIDControllers} - wheelSpeedSupplier: {currentWheelSpeedsSupplier} "
100+
f"Failed to instantiate MecanumControllerCommand, one of the arguments passed in was None "
96101
)
97102

98-
# check the length of the PID controllers
99-
if len(chassisWheelPIDControllers != 4):
100-
raise RuntimeError(
101-
f"Failed to instantiate MecanumControllerCommand, not enough PID controllers provided.\n \
102-
\t Required: 4 - Provided: {len(chassisWheelPIDControllers)}"
103-
)
104-
105-
self.frontLeftController = chassisWheelPIDControllers[
106-
self.__FRONT_LEFT_INDEX
107-
]
108-
self.rearLeftController = chassisWheelPIDControllers[self.__REAR_LEFT_INDEX]
109-
self.frontRightController = chassisWheelPIDControllers[
110-
self.__FRONT_RIGHT_INDEX
111-
]
112-
self.rearRightController = chassisWheelPIDControllers[
113-
self.__REAR_RIGHT_INDEX
114-
]
103+
self._frontLeftController = frontLeftController
104+
self._rearLeftController = rearLeftController
105+
self._frontRightController = frontRightController
106+
self._rearRightController = rearRightController
115107

116108
if currentWheelSpeedsSupplier is None:
117109
raise RuntimeError(
118110
f"Failed to instantiate MecanumControllerCommand, no WheelSpeedSupplierProvided."
119111
)
120112

121-
self.currentWheelSpeeds = currentWheelSpeedsSupplier
113+
self._currentWheelSpeeds = currentWheelSpeedsSupplier
122114

123115
if feedforward is None:
124116
raise RuntimeError(
125117
f"Failed to instantiate MecanumControllerCommand, no SimpleMotorFeedforwardMeters supplied."
126118
)
127119

128-
self.feedforward: SimpleMotorFeedforwardMeters = feedforward
120+
self._feedforward = feedforward
129121

130122
# All the optional requirements verify, set usePid flag to True
131-
self.usePID = True
123+
self._usePID = True
132124

133125
# Set the requirements for the command
134126
self.addRequirements(*requirements)
135127

136-
self.timer = Timer()
128+
self._timer = Timer()
137129

138130
def initialize(self):
139-
initialState: Trajectory.State = self.trajectory.sample(0)
131+
initialState = self._trajectory.sample(0)
140132
initialXVelocity = initialState.velocity * initialState.pose.rotation().cos()
141133
initialYVelocity = initialState.velocity * initialState.pose.rotation().sin()
142-
self.m_prevSpeeds = self.kinematics.toWheelSpeeds(
134+
self.m_prevSpeeds = self._kinematics.toWheelSpeeds(
143135
ChassisSpeeds(initialXVelocity, initialYVelocity, 0.0)
144136
)
145-
self.timer.restart()
146-
self.prevTime = 0
137+
self._timer.restart()
147138

148139
def execute(self):
149-
curTime = self.timer.get()
150-
dt = curTime - self.prevTime
151-
desiredState: Trajectory.State = self.trajectory.sample(curTime)
152-
targetChassisSpeeds = self.controller.calculate(
153-
self.pose(),
154-
desiredState,
155-
desiredState.pose.rotation()
156-
# self.pose.get(), desiredState, self.rotationSupplier.get()
140+
curTime = self._timer.get()
141+
dt = curTime - self._prevTime
142+
desiredState = self._trajectory.sample(curTime)
143+
targetChassisSpeeds = self._controller.calculate(
144+
self._pose(), desiredState, desiredState.pose.rotation()
157145
)
158-
targetWheelSpeeds = self.kinematics.toWheelSpeeds(targetChassisSpeeds)
159-
targetWheelSpeeds.desaturate(self.maxWheelVelocityMetersPerSecond)
146+
targetWheelSpeeds = self._kinematics.toWheelSpeeds(targetChassisSpeeds)
147+
targetWheelSpeeds.desaturate(self._maxWheelVelocityMetersPerSecond)
160148
frontLeftSpeedSetpoint = targetWheelSpeeds.frontLeft
161149
rearLeftSpeedSetpoint = targetWheelSpeeds.rearLeft
162150
frontRightSpeedSetpoint = targetWheelSpeeds.frontRight
163151
rearRightSpeedSetpoint = targetWheelSpeeds.rearRight
164152

165-
if not self.usePID:
166-
self.outputConsumer(
153+
if not self._usePID:
154+
self._outputConsumer(
167155
MecanumDriveWheelSpeeds(
168156
frontLeftSpeedSetpoint,
169157
frontRightSpeedSetpoint,
@@ -172,51 +160,57 @@ def execute(self):
172160
)
173161
)
174162
else:
175-
frontLeftFeedforward = self.feedforward.calculate(
163+
frontLeftFeedforward = self._feedforward.calculate(
176164
frontLeftSpeedSetpoint,
177165
(frontLeftSpeedSetpoint - self.m_prevSpeeds.frontLeft) / dt,
178166
)
179-
rearLeftFeedforward = self.feedforward.calculate(
167+
rearLeftFeedforward = self._feedforward.calculate(
180168
rearLeftSpeedSetpoint,
181169
(rearLeftSpeedSetpoint - self.m_prevSpeeds.rearLeft) / dt,
182170
)
183-
frontRightFeedforward = self.feedforward.calculate(
171+
frontRightFeedforward = self._feedforward.calculate(
184172
frontRightSpeedSetpoint,
185173
(frontRightSpeedSetpoint - self.m_prevSpeeds.frontRight) / dt,
186174
)
187-
rearRightFeedforward = self.feedforward.calculate(
175+
rearRightFeedforward = self._feedforward.calculate(
188176
rearRightSpeedSetpoint,
189177
(rearRightSpeedSetpoint - self.m_prevSpeeds.rearRight) / dt,
190178
)
191-
frontLeftOutput = frontLeftFeedforward + self.frontLeftController.calculate(
192-
self.currentWheelSpeeds().frontLeft,
193-
frontLeftSpeedSetpoint,
179+
frontLeftOutput = (
180+
frontLeftFeedforward
181+
+ self._frontLeftController.calculate(
182+
self._currentWheelSpeeds().frontLeft,
183+
frontLeftSpeedSetpoint,
184+
)
194185
)
195-
rearLeftOutput = rearLeftFeedforward + self.rearLeftController.calculate(
196-
self.currentWheelSpeeds().rearLeft,
186+
rearLeftOutput = rearLeftFeedforward + self._rearLeftController.calculate(
187+
self._currentWheelSpeeds().rearLeft,
197188
rearLeftSpeedSetpoint,
198189
)
199190
frontRightOutput = (
200191
frontRightFeedforward
201-
+ self.frontRightController.calculate(
202-
self.currentWheelSpeeds().frontRight,
192+
+ self._frontRightController.calculate(
193+
self._currentWheelSpeeds().frontRight,
203194
frontRightSpeedSetpoint,
204195
)
205196
)
206-
rearRightOutput = rearRightFeedforward + self.rearRightController.calculate(
207-
self.currentWheelSpeeds().rearRight,
208-
rearRightSpeedSetpoint,
197+
rearRightOutput = (
198+
rearRightFeedforward
199+
+ self._rearRightController.calculate(
200+
self._currentWheelSpeeds().rearRight,
201+
rearRightSpeedSetpoint,
202+
)
209203
)
210-
self.outputConsumer(
204+
self._outputConsumer(
211205
frontLeftOutput, frontRightOutput, rearLeftOutput, rearRightOutput
212206
)
213207

214208
# Store current call information for next call
215-
self.prevTime = curTime
209+
self._prevTime = curTime
216210
self.m_prevSpeeds = targetWheelSpeeds
217211

218212
def end(self, interrupted):
219-
self.timer.stop()
213+
self._timer.stop()
220214

221215
def isFinished(self):
222-
return self.timer.hasElapsed(self.trajectory.totalTime())
216+
return self._timer.hasElapsed(self._trajectory.totalTime())

0 commit comments

Comments
 (0)