|
| 1 | +package org.frc6423.frc2025.subsystems.swerve; |
| 2 | + |
| 3 | +import static org.frc6423.frc2025.Constants.kTickSpeed; |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | + |
| 7 | +import org.frc6423.frc2025.subsystems.swerve.module.Module; |
| 8 | +import org.frc6423.frc2025.util.swerveUtil.SwerveConfig; |
| 9 | + |
| 10 | +import edu.wpi.first.math.estimator.SwerveDrivePoseEstimator; |
| 11 | +import edu.wpi.first.math.geometry.Pose2d; |
| 12 | +import edu.wpi.first.math.geometry.Rotation2d; |
| 13 | +import edu.wpi.first.math.kinematics.ChassisSpeeds; |
| 14 | +import edu.wpi.first.math.kinematics.SwerveDriveKinematics; |
| 15 | +import edu.wpi.first.math.kinematics.SwerveModulePosition; |
| 16 | +import edu.wpi.first.math.kinematics.SwerveModuleState; |
| 17 | +import edu.wpi.first.math.system.plant.DCMotor; |
| 18 | +import edu.wpi.first.wpilibj.DriverStation; |
| 19 | +import edu.wpi.first.wpilibj2.command.SubsystemBase; |
| 20 | + |
| 21 | +public class SwerveSubsystem extends SubsystemBase { |
| 22 | + private final SwerveConfig m_config; |
| 23 | + |
| 24 | + private final Module[] m_modules; |
| 25 | + |
| 26 | + private SwerveDriveKinematics m_kinematics; |
| 27 | + private SwerveDrivePoseEstimator m_poseEstimator; |
| 28 | + |
| 29 | + public SwerveSubsystem(SwerveConfig config) { |
| 30 | + // Create modules |
| 31 | + var moduleConfigs = config.getModuleConfigs(); |
| 32 | + m_modules = new Module[moduleConfigs.length]; |
| 33 | + Arrays.stream(moduleConfigs) |
| 34 | + .forEach((c) -> m_modules[c.kIndex] = new Module(c)); |
| 35 | + |
| 36 | + // Create math objects |
| 37 | + m_kinematics = new SwerveDriveKinematics(config.getModuleLocs()); |
| 38 | + m_poseEstimator = new SwerveDrivePoseEstimator(m_kinematics, getHeading(), getModulePoses(), new Pose2d()); |
| 39 | + |
| 40 | + m_config = config; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void periodic() { |
| 45 | + |
| 46 | + // Stop module input when driverstation is disabled |
| 47 | + if (DriverStation.isDisabled()) { |
| 48 | + for (Module module : m_modules) { |
| 49 | + module.stop(); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Runs swerve at desired robot relative velocity |
| 56 | + * |
| 57 | + * @param velocity desired velocity |
| 58 | + * @param openloopEnabled enable or disable open loop voltage control |
| 59 | + */ |
| 60 | + public void runVelocities(ChassisSpeeds velocity, boolean openloopEnabled) { |
| 61 | + velocity = ChassisSpeeds.discretize(velocity, kTickSpeed); |
| 62 | + |
| 63 | + SwerveModuleState[] desiredStates = m_kinematics.toSwerveModuleStates(velocity); |
| 64 | + SwerveDriveKinematics.desaturateWheelSpeeds(desiredStates, m_config.getMaxLinearSpeedMetersPerSec()); |
| 65 | + |
| 66 | + for(int i = 0; i < desiredStates.length; i++) { |
| 67 | + if (openloopEnabled) { |
| 68 | + ChassisSpeeds currentVelocities = getVelocitiesRobotRelative(); |
| 69 | + boolean focEnabled = |
| 70 | + Math.sqrt( |
| 71 | + Math.pow(currentVelocities.vxMetersPerSecond, 2) |
| 72 | + + Math.pow(currentVelocities.vyMetersPerSecond, 2)) // converts linear velocity components to linear velocity |
| 73 | + < 1 * m_config.getMaxLinearSpeedMetersPerSec(); |
| 74 | + |
| 75 | + // Converts desired motor velocity into input voltage |
| 76 | + // OmegaRadsPerSec/(KvRadsPerVolt) |
| 77 | + double driveVoltage = desiredStates[i].speedMetersPerSecond / DCMotor.getKrakenX60(1).KvRadPerSecPerVolt; |
| 78 | + m_modules[i].runSetpointOpenloop(driveVoltage, desiredStates[i].angle, focEnabled); |
| 79 | + } else { |
| 80 | + m_modules[i].runSetpoint(desiredStates[i]); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** Gets current robot velocity (robot relative) */ |
| 86 | + public ChassisSpeeds getVelocitiesRobotRelative() { |
| 87 | + return m_kinematics.toChassisSpeeds(getModuleStates()); |
| 88 | + } |
| 89 | + |
| 90 | + /** Gets current robot heading */ |
| 91 | + public Rotation2d getHeading() { |
| 92 | + return new Rotation2d(); |
| 93 | + } |
| 94 | + |
| 95 | + /** Gets current robot field pose */ |
| 96 | + public Pose2d getPose() { |
| 97 | + return m_poseEstimator.getEstimatedPosition(); |
| 98 | + } |
| 99 | + |
| 100 | + /** Returns an array of module field positions */ |
| 101 | + public SwerveModulePosition[] getModulePoses() { |
| 102 | + return Arrays.stream(m_modules) |
| 103 | + .map(Module::getModulePose) |
| 104 | + .toArray(SwerveModulePosition[]::new); |
| 105 | + } |
| 106 | + |
| 107 | + /** Returns an array of module states */ |
| 108 | + public SwerveModuleState[] getModuleStates() { |
| 109 | + return Arrays.stream(m_modules) |
| 110 | + .map(Module::getModuleState) |
| 111 | + .toArray(SwerveModuleState[]::new); |
| 112 | + } |
| 113 | +} |
0 commit comments