|
| 1 | +# |
| 2 | +# Copyright (c) FIRST and other WPILib contributors. |
| 3 | +# Open Source Software; you can modify and/or share it under the terms of |
| 4 | +# the WPILib BSD license file in the root directory of this project. |
| 5 | +# |
| 6 | + |
| 7 | +import math |
| 8 | +import wpilib |
| 9 | +import wpimath.kinematics |
| 10 | +import wpimath.geometry |
| 11 | +import wpimath.controller |
| 12 | +import wpimath.trajectory |
| 13 | + |
| 14 | +kWheelRadius = 0.0508 |
| 15 | +kEncoderResolution = 4096 |
| 16 | +kModuleMaxAngularVelocity = math.pi |
| 17 | +kModuleMaxAngularAcceleration = math.tau |
| 18 | + |
| 19 | + |
| 20 | +class SwerveModule: |
| 21 | + def __init__( |
| 22 | + self, |
| 23 | + driveMotorChannel: int, |
| 24 | + turningMotorChannel: int, |
| 25 | + driveEncoderChannelA: int, |
| 26 | + driveEncoderChannelB: int, |
| 27 | + turningEncoderChannelA: int, |
| 28 | + turningEncoderChannelB: int, |
| 29 | + ) -> None: |
| 30 | + """Constructs a SwerveModule with a drive motor, turning motor, drive encoder and turning encoder. |
| 31 | +
|
| 32 | + :param driveMotorChannel: PWM output for the drive motor. |
| 33 | + :param turningMotorChannel: PWM output for the turning motor. |
| 34 | + :param driveEncoderChannelA: DIO input for the drive encoder channel A |
| 35 | + :param driveEncoderChannelB: DIO input for the drive encoder channel B |
| 36 | + :param turningEncoderChannelA: DIO input for the turning encoder channel A |
| 37 | + :param turningEncoderChannelB: DIO input for the turning encoder channel B |
| 38 | + """ |
| 39 | + self.driveMotor = wpilib.PWMSparkMax(driveMotorChannel) |
| 40 | + self.turningMotor = wpilib.PWMSparkMax(turningMotorChannel) |
| 41 | + |
| 42 | + self.driveEncoder = wpilib.Encoder(driveEncoderChannelA, driveEncoderChannelB) |
| 43 | + self.turningEncoder = wpilib.Encoder( |
| 44 | + turningEncoderChannelA, turningEncoderChannelB |
| 45 | + ) |
| 46 | + |
| 47 | + # Gains are for example purposes only - must be determined for your own robot! |
| 48 | + self.drivePIDController = wpimath.controller.PIDController(1, 0, 0) |
| 49 | + |
| 50 | + # Gains are for example purposes only - must be determined for your own robot! |
| 51 | + self.turningPIDController = wpimath.controller.ProfiledPIDController( |
| 52 | + 1, |
| 53 | + 0, |
| 54 | + 0, |
| 55 | + wpimath.trajectory.TrapezoidProfile.Constraints( |
| 56 | + kModuleMaxAngularVelocity, |
| 57 | + kModuleMaxAngularAcceleration, |
| 58 | + ), |
| 59 | + ) |
| 60 | + |
| 61 | + # Gains are for example purposes only - must be determined for your own robot! |
| 62 | + self.driveFeedforward = wpimath.controller.SimpleMotorFeedforwardMeters(1, 3) |
| 63 | + self.turnFeedforward = wpimath.controller.SimpleMotorFeedforwardMeters(1, 0.5) |
| 64 | + |
| 65 | + # Set the distance per pulse for the drive encoder. We can simply use the |
| 66 | + # distance traveled for one rotation of the wheel divided by the encoder |
| 67 | + # resolution. |
| 68 | + self.driveEncoder.setDistancePerPulse( |
| 69 | + math.tau * kWheelRadius / kEncoderResolution |
| 70 | + ) |
| 71 | + |
| 72 | + # Set the distance (in this case, angle) in radians per pulse for the turning encoder. |
| 73 | + # This is the the angle through an entire rotation (2 * pi) divided by the |
| 74 | + # encoder resolution. |
| 75 | + self.turningEncoder.setDistancePerPulse(math.tau / kEncoderResolution) |
| 76 | + |
| 77 | + # Limit the PID Controller's input range between -pi and pi and set the input |
| 78 | + # to be continuous. |
| 79 | + self.turningPIDController.enableContinuousInput(-math.pi, math.pi) |
| 80 | + |
| 81 | + def getState(self) -> wpimath.kinematics.SwerveModuleState: |
| 82 | + """Returns the current state of the module. |
| 83 | +
|
| 84 | + :returns: The current state of the module. |
| 85 | + """ |
| 86 | + return wpimath.kinematics.SwerveModuleState( |
| 87 | + self.driveEncoder.getRate(), |
| 88 | + wpimath.geometry.Rotation2d(self.turningEncoder.getDistance()), |
| 89 | + ) |
| 90 | + |
| 91 | + def getPosition(self) -> wpimath.kinematics.SwerveModulePosition: |
| 92 | + """Returns the current position of the module. |
| 93 | +
|
| 94 | + :returns: The current position of the module. |
| 95 | + """ |
| 96 | + return wpimath.kinematics.SwerveModulePosition( |
| 97 | + self.driveEncoder.getRate(), |
| 98 | + wpimath.geometry.Rotation2d(self.turningEncoder.getDistance()), |
| 99 | + ) |
| 100 | + |
| 101 | + def setDesiredState( |
| 102 | + self, desiredState: wpimath.kinematics.SwerveModuleState |
| 103 | + ) -> None: |
| 104 | + """Sets the desired state for the module. |
| 105 | +
|
| 106 | + :param desiredState: Desired state with speed and angle. |
| 107 | + """ |
| 108 | + |
| 109 | + encoderRotation = wpimath.geometry.Rotation2d(self.turningEncoder.getDistance()) |
| 110 | + |
| 111 | + # Optimize the reference state to avoid spinning further than 90 degrees |
| 112 | + state = wpimath.kinematics.SwerveModuleState.optimize( |
| 113 | + desiredState, encoderRotation |
| 114 | + ) |
| 115 | + |
| 116 | + # Scale speed by cosine of angle error. This scales down movement perpendicular to the desired |
| 117 | + # direction of travel that can occur when modules change directions. This results in smoother |
| 118 | + # driving. |
| 119 | + state.speed *= (state.angle - encoderRotation).cos() |
| 120 | + |
| 121 | + # Calculate the drive output from the drive PID controller. |
| 122 | + driveOutput = self.drivePIDController.calculate( |
| 123 | + self.driveEncoder.getRate(), state.speed |
| 124 | + ) |
| 125 | + |
| 126 | + driveFeedforward = self.driveFeedforward.calculate(state.speed) |
| 127 | + |
| 128 | + # Calculate the turning motor output from the turning PID controller. |
| 129 | + turnOutput = self.turningPIDController.calculate( |
| 130 | + self.turningEncoder.getDistance(), state.angle.radians() |
| 131 | + ) |
| 132 | + |
| 133 | + turnFeedforward = self.turnFeedforward.calculate( |
| 134 | + self.turningPIDController.getSetpoint().velocity |
| 135 | + ) |
| 136 | + |
| 137 | + self.driveMotor.setVoltage(driveOutput + driveFeedforward) |
| 138 | + self.turningMotor.setVoltage(turnOutput + turnFeedforward) |
0 commit comments