|
| 1 | +# Copyright (c) FIRST and other WPILib contributors. |
| 2 | +# Open Source Software; you can modify and/or share it under the terms of |
| 3 | +# the WPILib BSD license file in the root directory of this project. |
| 4 | +from __future__ import annotations |
| 5 | +from typing import Set, Callable, Union, List |
| 6 | + |
| 7 | +from wpimath.controller import ( |
| 8 | + HolonomicDriveController, |
| 9 | + PIDController, |
| 10 | + ProfiledPIDControllerRadians, |
| 11 | + SimpleMotorFeedforwardMeters, |
| 12 | +) |
| 13 | +from wpimath.geometry import Pose2d, Rotation2d |
| 14 | +from wpimath.kinematics import ( |
| 15 | + ChassisSpeeds, |
| 16 | + MecanumDriveKinematics, |
| 17 | + MecanumDriveWheelSpeeds, |
| 18 | +) |
| 19 | +from wpimath.trajectory import Trajectory |
| 20 | +from wpilib import Timer |
| 21 | + |
| 22 | +from .subsystem import Subsystem |
| 23 | +from .command import Command |
| 24 | + |
| 25 | + |
| 26 | +class MecanumControllerCommand(Command): |
| 27 | + """ |
| 28 | + A command that uses two PID controllers (PIDController) and a ProfiledPIDController |
| 29 | + (ProfiledPIDController) to follow a trajectory Trajectory with a mecanum drive. |
| 30 | +
|
| 31 | + The command handles trajectory-following, Velocity PID calculations, and feedforwards |
| 32 | + internally. This is intended to be a more-or-less "complete solution" that can be used by teams |
| 33 | + without a great deal of controls expertise. |
| 34 | +
|
| 35 | + Advanced teams seeking more flexibility (for example, those who wish to use the onboard PID |
| 36 | + functionality of a "smart" motor controller) may use the secondary constructor that omits the PID |
| 37 | + and feedforward functionality, returning only the raw wheel speeds from the PID controllers. |
| 38 | +
|
| 39 | + The robot angle controller does not follow the angle given by the trajectory but rather goes |
| 40 | + to the angle given in the final state of the trajectory. |
| 41 | + """ |
| 42 | + |
| 43 | + __FRONT_LEFT_INDEX = 0 |
| 44 | + __REAR_LEFT_INDEX = 1 |
| 45 | + __FRONT_RIGHT_INDEX = 2 |
| 46 | + __REAR_RIGHT_INDEX = 3 |
| 47 | + |
| 48 | + def __init__( |
| 49 | + self, |
| 50 | + trajectory: Trajectory, |
| 51 | + pose: Callable[[], Pose2d], |
| 52 | + kinematics: MecanumDriveKinematics, |
| 53 | + xController: PIDController, |
| 54 | + yController: PIDController, |
| 55 | + thetaController: ProfiledPIDControllerRadians, |
| 56 | + # rotationSupplier: Callable[[], Rotation2d], |
| 57 | + maxWheelVelocityMetersPerSecond: float, |
| 58 | + outputConsumer: Callable[[Union[List[float], MecanumDriveWheelSpeeds]], None], |
| 59 | + *requirements: Subsystem, |
| 60 | + feedforward: SimpleMotorFeedforwardMeters | None = None, |
| 61 | + chassisWheelPIDControllers: List[PIDController] | None = None, |
| 62 | + currentWheelSpeedsSupplier: Callable[[], MecanumDriveWheelSpeeds] | None = None, |
| 63 | + ): |
| 64 | + super().__init__() |
| 65 | + |
| 66 | + self.trajectory: Trajectory = trajectory |
| 67 | + self.pose = pose |
| 68 | + self.kinematics = kinematics |
| 69 | + self.controller = HolonomicDriveController( |
| 70 | + xController, yController, thetaController |
| 71 | + ) |
| 72 | + # self.rotationSupplier = rotationSupplier |
| 73 | + self.maxWheelVelocityMetersPerSecond = maxWheelVelocityMetersPerSecond |
| 74 | + self.outputConsumer = outputConsumer |
| 75 | + |
| 76 | + # Member to track time of last loop execution |
| 77 | + self.prevTime = 0 |
| 78 | + self.usePID = False |
| 79 | + |
| 80 | + # Optional requirements checks. If any one of the feedforward, pidcontrollers, or wheelspeedsuppliers |
| 81 | + # are not None, then all should be non-null. Raise RuntimeError if they are. The List of PID controllers |
| 82 | + # should be equal to each corner of the robot being commanded. |
| 83 | + if ( |
| 84 | + feedforward |
| 85 | + or chassisWheelPIDControllers |
| 86 | + or currentWheelSpeedsSupplier is not None |
| 87 | + ): |
| 88 | + if ( |
| 89 | + feedforward |
| 90 | + or chassisWheelPIDControllers |
| 91 | + or currentWheelSpeedsSupplier is None |
| 92 | + ): |
| 93 | + 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} " |
| 96 | + ) |
| 97 | + |
| 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 | + ] |
| 115 | + |
| 116 | + if currentWheelSpeedsSupplier is None: |
| 117 | + raise RuntimeError( |
| 118 | + f"Failed to instantiate MecanumControllerCommand, no WheelSpeedSupplierProvided." |
| 119 | + ) |
| 120 | + |
| 121 | + self.currentWheelSpeeds = currentWheelSpeedsSupplier |
| 122 | + |
| 123 | + if feedforward is None: |
| 124 | + raise RuntimeError( |
| 125 | + f"Failed to instantiate MecanumControllerCommand, no SimpleMotorFeedforwardMeters supplied." |
| 126 | + ) |
| 127 | + |
| 128 | + self.feedforward: SimpleMotorFeedforwardMeters = feedforward |
| 129 | + |
| 130 | + # All the optional requirements verify, set usePid flag to True |
| 131 | + self.usePID = True |
| 132 | + |
| 133 | + # Set the requirements for the command |
| 134 | + self.addRequirements(*requirements) |
| 135 | + |
| 136 | + self.timer = Timer() |
| 137 | + |
| 138 | + def initialize(self): |
| 139 | + initialState: Trajectory.State = self.trajectory.sample(0) |
| 140 | + initialXVelocity = initialState.velocity * initialState.pose.rotation().cos() |
| 141 | + initialYVelocity = initialState.velocity * initialState.pose.rotation().sin() |
| 142 | + self.m_prevSpeeds = self.kinematics.toWheelSpeeds( |
| 143 | + ChassisSpeeds(initialXVelocity, initialYVelocity, 0.0) |
| 144 | + ) |
| 145 | + self.timer.restart() |
| 146 | + self.prevTime = 0 |
| 147 | + |
| 148 | + 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() |
| 157 | + ) |
| 158 | + targetWheelSpeeds = self.kinematics.toWheelSpeeds(targetChassisSpeeds) |
| 159 | + targetWheelSpeeds.desaturate(self.maxWheelVelocityMetersPerSecond) |
| 160 | + frontLeftSpeedSetpoint = targetWheelSpeeds.frontLeft |
| 161 | + rearLeftSpeedSetpoint = targetWheelSpeeds.rearLeft |
| 162 | + frontRightSpeedSetpoint = targetWheelSpeeds.frontRight |
| 163 | + rearRightSpeedSetpoint = targetWheelSpeeds.rearRight |
| 164 | + |
| 165 | + if not self.usePID: |
| 166 | + self.outputConsumer( |
| 167 | + MecanumDriveWheelSpeeds( |
| 168 | + frontLeftSpeedSetpoint, |
| 169 | + frontRightSpeedSetpoint, |
| 170 | + rearLeftSpeedSetpoint, |
| 171 | + rearRightSpeedSetpoint, |
| 172 | + ) |
| 173 | + ) |
| 174 | + else: |
| 175 | + frontLeftFeedforward = self.feedforward.calculate( |
| 176 | + frontLeftSpeedSetpoint, |
| 177 | + (frontLeftSpeedSetpoint - self.m_prevSpeeds.frontLeft) / dt, |
| 178 | + ) |
| 179 | + rearLeftFeedforward = self.feedforward.calculate( |
| 180 | + rearLeftSpeedSetpoint, |
| 181 | + (rearLeftSpeedSetpoint - self.m_prevSpeeds.rearLeft) / dt, |
| 182 | + ) |
| 183 | + frontRightFeedforward = self.feedforward.calculate( |
| 184 | + frontRightSpeedSetpoint, |
| 185 | + (frontRightSpeedSetpoint - self.m_prevSpeeds.frontRight) / dt, |
| 186 | + ) |
| 187 | + rearRightFeedforward = self.feedforward.calculate( |
| 188 | + rearRightSpeedSetpoint, |
| 189 | + (rearRightSpeedSetpoint - self.m_prevSpeeds.rearRight) / dt, |
| 190 | + ) |
| 191 | + frontLeftOutput = frontLeftFeedforward + self.frontLeftController.calculate( |
| 192 | + self.currentWheelSpeeds().frontLeft, |
| 193 | + frontLeftSpeedSetpoint, |
| 194 | + ) |
| 195 | + rearLeftOutput = rearLeftFeedforward + self.rearLeftController.calculate( |
| 196 | + self.currentWheelSpeeds().rearLeft, |
| 197 | + rearLeftSpeedSetpoint, |
| 198 | + ) |
| 199 | + frontRightOutput = ( |
| 200 | + frontRightFeedforward |
| 201 | + + self.frontRightController.calculate( |
| 202 | + self.currentWheelSpeeds().frontRight, |
| 203 | + frontRightSpeedSetpoint, |
| 204 | + ) |
| 205 | + ) |
| 206 | + rearRightOutput = rearRightFeedforward + self.rearRightController.calculate( |
| 207 | + self.currentWheelSpeeds().rearRight, |
| 208 | + rearRightSpeedSetpoint, |
| 209 | + ) |
| 210 | + self.outputConsumer( |
| 211 | + frontLeftOutput, frontRightOutput, rearLeftOutput, rearRightOutput |
| 212 | + ) |
| 213 | + |
| 214 | + # Store current call information for next call |
| 215 | + self.prevTime = curTime |
| 216 | + self.m_prevSpeeds = targetWheelSpeeds |
| 217 | + |
| 218 | + def end(self, interrupted): |
| 219 | + self.timer.stop() |
| 220 | + |
| 221 | + def isFinished(self): |
| 222 | + return self.timer.hasElapsed(self.trajectory.totalTime()) |
0 commit comments