|
| 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 | + |
| 6 | +from typing import Callable, Union |
| 7 | +from wpimath.controller import PIDController, RamseteController, SimpleMotorFeedforwardMeters |
| 8 | +from wpimath.geometry import Pose2d |
| 9 | +from wpimath.kinematics import ChassisSpeeds, DifferentialDriveKinematics, DifferentialDriveWheelSpeeds |
| 10 | +from wpimath.trajectory import Trajectory |
| 11 | +from wpiutil import SendableBuilder |
| 12 | +from wpilib import Timer |
| 13 | + |
| 14 | + |
| 15 | +from .command import Command |
| 16 | +from .subsystem import Subsystem |
| 17 | + |
| 18 | +class RamseteCommand(Command): |
| 19 | + """ |
| 20 | + A command that uses a RAMSETE controller (RamseteController) to follow a trajectory |
| 21 | + (Trajectory) with a differential drive. |
| 22 | +
|
| 23 | + The command handles trajectory-following, PID calculations, and feedforwards internally. This |
| 24 | + is intended to be a more-or-less "complete solution" that can be used by teams without a great |
| 25 | + deal of controls expertise. |
| 26 | +
|
| 27 | + Advanced teams seeking more flexibility (for example, those who wish to use the onboard PID |
| 28 | + functionality of a "smart" motor controller) may use the secondary constructor that omits the PID |
| 29 | + and feedforward functionality, returning only the raw wheel speeds from the RAMSETE controller. |
| 30 | +
|
| 31 | + This class is provided by the NewCommands VendorDep |
| 32 | + """ |
| 33 | + |
| 34 | + def __init__( |
| 35 | + self, |
| 36 | + trajectory: Trajectory, |
| 37 | + pose: Callable[[], Pose2d], |
| 38 | + controller: RamseteController, |
| 39 | + kinematics: DifferentialDriveKinematics, |
| 40 | + outputVolts: Callable[[float, float], None], |
| 41 | + *requirements: Subsystem, |
| 42 | + feedforward: SimpleMotorFeedforwardMeters | None = None, |
| 43 | + leftController: PIDController | None = None, |
| 44 | + rightController: PIDController | None = None, |
| 45 | + wheelSpeeds: Callable[[], DifferentialDriveWheelSpeeds] | None = None, |
| 46 | + ): |
| 47 | + """Constructs a new RamseteCommand that, when executed, will follow the provided trajectory. PID |
| 48 | + control and feedforward are handled internally, and outputs are scaled -12 to 12 representing |
| 49 | + units of volts. |
| 50 | +
|
| 51 | + Note: The controller will *not* set the outputVolts to zero upon completion of the path - |
| 52 | + this is left to the user, since it is not appropriate for paths with nonstationary endstates. |
| 53 | +
|
| 54 | + :param trajectory: The trajectory to follow. |
| 55 | + :param pose: A function that supplies the robot pose - use one of the odometry classes to |
| 56 | + provide this. |
| 57 | + :param controller: The RAMSETE controller used to follow the trajectory. |
| 58 | + :param kinematics: The kinematics for the robot drivetrain. |
| 59 | + :param outputVolts: A function that consumes the computed left and right outputs (in volts) for |
| 60 | + the robot drive. |
| 61 | + :param requirements: The subsystems to require. |
| 62 | +
|
| 63 | + OPTIONAL PARAMETERS |
| 64 | + When the following optional parameters are provided, when executed, the RamseteCommand will follow |
| 65 | + provided trajectory. PID control and feedforward are handled internally, and the outputs are scaled |
| 66 | + from -12 to 12 representing units of volts. If any one of the optional parameters are provided, each |
| 67 | + other optional parameter becomes required. |
| 68 | + :param feedforward The feedforward to use for the drive |
| 69 | + :param leftController: The PIDController for the left side of the robot drive. |
| 70 | + :param rightController: The PIDController for the right side of the robot drive. |
| 71 | + :param wheelSpeeds: A function that supplies the speeds of the left and right sides of the robot |
| 72 | + drive. |
| 73 | + """ |
| 74 | + super().__init__() |
| 75 | + |
| 76 | + self.timer = Timer() |
| 77 | + |
| 78 | + # Store all the requirements that are required |
| 79 | + self.trajectory: Trajectory = trajectory |
| 80 | + self.pose = pose |
| 81 | + self.follower: RamseteController = controller |
| 82 | + self.kinematics = kinematics |
| 83 | + self.output: Callable[[float, float], None] = outputVolts |
| 84 | + self.leftController = None |
| 85 | + self.rightController = None |
| 86 | + self.feedforward = None |
| 87 | + self.wheelspeeds = None |
| 88 | + self.usePID = False |
| 89 | + # Optional requirements checks. If any one of the optionl parameters are not None, then all the optional |
| 90 | + # requirements become required. |
| 91 | + if feedforward or leftController or rightController or wheelSpeeds is not None: |
| 92 | + if feedforward or leftController or rightController or wheelSpeeds is None: |
| 93 | + raise RuntimeError(f'Failed to instantiate RamseteCommand, not all optional arguments were provided.\n \ |
| 94 | + Feedforward - {feedforward}, LeftController - {leftController}, RightController - {rightController}, WheelSpeeds - {wheelSpeeds} ' |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | + self.leftController: PIDController = leftController |
| 99 | + self.rightController: PIDController = rightController |
| 100 | + self.wheelspeeds: Callable[[], DifferentialDriveWheelSpeeds] = wheelSpeeds |
| 101 | + self.feedforward: SimpleMotorFeedforwardMeters = feedforward |
| 102 | + self.usePID = True |
| 103 | + self.prevSpeeds = DifferentialDriveWheelSpeeds() |
| 104 | + self.prevTime = -1.0 |
| 105 | + |
| 106 | + self.addRequirements(*requirements) |
| 107 | + |
| 108 | + def initialize(self): |
| 109 | + self.prevTime = -1 |
| 110 | + initial_state = self.trajectory.sample(0) |
| 111 | + initial_speeds = self.kinematics.toWheelSpeeds( |
| 112 | + ChassisSpeeds( initial_state.velocity, 0, initial_state.curvature * initial_state.velocity ) |
| 113 | + ) |
| 114 | + self.prevSpeeds = initial_speeds |
| 115 | + self.timer.restart() |
| 116 | + if self.usePID: |
| 117 | + self.leftController.reset() |
| 118 | + self.rightController.reset() |
| 119 | + |
| 120 | + def execute(self): |
| 121 | + cur_time = self.timer.get() |
| 122 | + dt = cur_time - self.prevTime |
| 123 | + |
| 124 | + if self.prevTime < 0: |
| 125 | + self.output(0.0, 0.0) |
| 126 | + self.prevTime = cur_time |
| 127 | + return |
| 128 | + |
| 129 | + target_wheel_speeds = self.kinematics.toWheelSpeeds( |
| 130 | + self.follower.calculate(self.pose(), self.trajectory.sample(cur_time)) |
| 131 | + ) |
| 132 | + |
| 133 | + left_speed_setpoint = target_wheel_speeds.left |
| 134 | + right_speed_setpoint = target_wheel_speeds.right |
| 135 | + |
| 136 | + if self.usePID: |
| 137 | + left_feedforward = self.feedforward.calculate( |
| 138 | + left_speed_setpoint, |
| 139 | + (left_speed_setpoint - self.prevSpeeds.left) / dt |
| 140 | + ) |
| 141 | + |
| 142 | + right_feedforward = self.feedforward.calculate( |
| 143 | + right_speed_setpoint, |
| 144 | + (right_speed_setpoint - self.prevSpeeds.right) / dt |
| 145 | + ) |
| 146 | + |
| 147 | + left_output = ( |
| 148 | + left_feedforward |
| 149 | + + self.leftController.calculate( |
| 150 | + self.wheelspeeds().left, |
| 151 | + left_speed_setpoint |
| 152 | + ) |
| 153 | + ) |
| 154 | + |
| 155 | + right_output = ( |
| 156 | + right_feedforward |
| 157 | + + self.rightController.calculate( |
| 158 | + self.wheelspeeds().right, |
| 159 | + right_speed_setpoint |
| 160 | + ) |
| 161 | + ) |
| 162 | + else: |
| 163 | + left_output = left_speed_setpoint |
| 164 | + right_output = right_speed_setpoint |
| 165 | + |
| 166 | + self.output(left_output, right_output) |
| 167 | + self.prevSpeeds = target_wheel_speeds |
| 168 | + self.prevTime = cur_time |
| 169 | + |
| 170 | + def end(self, interrupted): |
| 171 | + self.timer.stop() |
| 172 | + |
| 173 | + if interrupted: |
| 174 | + self.output(0.0, 0.0) |
| 175 | + |
| 176 | + def isFinished(self): |
| 177 | + return self.timer.hasElapsed(self.trajectory.totalTime()) |
| 178 | + |
| 179 | + def initSendable(self, builder: SendableBuilder): |
| 180 | + super().initSendable(builder) |
| 181 | + builder.addDoubleProperty("leftVelocity", lambda: self.prevSpeeds.left, None) |
| 182 | + builder.addDoubleProperty("rightVelocity", lambda: self.prevSpeeds.right, None) |
0 commit comments