Skip to content

Commit

Permalink
use 254 cheesy poofs swerve setpoint generator
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelLesirge committed Sep 14, 2024
1 parent 22308bc commit 681a861
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/main/java/frc/robot/subsystems/drive/Drive.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import edu.wpi.first.wpilibj2.command.sysid.SysIdRoutine;
import frc.robot.Constants;
import frc.robot.RobotState;
import frc.robot.utility.AllianceFlipUtil;
import frc.robot.utility.LocalADStarAK;
import frc.robot.utility.swerve254util.ModuleLimits;
import frc.robot.utility.swerve254util.SwerveSetpoint;
import frc.robot.utility.swerve254util.SwerveSetpointGenerator;
import java.util.Arrays;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
Expand All @@ -53,6 +57,9 @@ public class Drive extends SubsystemBase {
private Rotation2d rawGyroRotation = new Rotation2d();
private SwerveModulePosition[] lastModulePositions;

private SwerveSetpoint currentSetpoint;
private final SwerveSetpointGenerator setpointGenerator;

private Pose2d pose = new Pose2d();

private SwerveDrivePoseEstimator poseEstimator;
Expand Down Expand Up @@ -91,16 +98,26 @@ public Drive(

// --- Set up kinematics ---

kinematics =
new SwerveDriveKinematics(
modules().map(Module::getDistanceFromCenter).toArray(Translation2d[]::new));
Translation2d[] moduleTranslations =
modules().map(Module::getDistanceFromCenter).toArray(Translation2d[]::new);

kinematics = new SwerveDriveKinematics(moduleTranslations);

// --- Set up odometry ---

lastModulePositions = modules().map(Module::getPosition).toArray(SwerveModulePosition[]::new);
poseEstimator =
new SwerveDrivePoseEstimator(kinematics, rawGyroRotation, lastModulePositions, pose);

// --- Swerve Setpoint Generator ---

currentSetpoint =
new SwerveSetpoint(
new ChassisSpeeds(),
modules().map(Module::getSpeeds).toArray(SwerveModuleState[]::new));

setpointGenerator = new SwerveSetpointGenerator(kinematics, moduleTranslations);

// --- Start odometry threads ---

// Start threads (does nothing if no signals have been created)
Expand Down Expand Up @@ -148,9 +165,7 @@ public Drive(
null,
state -> Logger.recordOutput("Drive/SysIdState", state.toString())),
new SysIdRoutine.Mechanism(
(voltage) -> runCharacterization(voltage.in(Units.Volts)),
null,
this));
(voltage) -> runCharacterization(voltage.in(Units.Volts)), null, this));
}

// --- Robot Pose ---
Expand Down Expand Up @@ -324,11 +339,19 @@ public void setRobotSpeeds(ChassisSpeeds speeds, boolean fieldRelative) {
speeds, AllianceFlipUtil.apply(getPose().getRotation()));
}

speeds = ChassisSpeeds.discretize(speeds, Constants.LOOP_PERIOD_SECONDS);
ModuleLimits currentModuleLimits = RobotState.getInstance().getModuleLimits();
currentSetpoint =
setpointGenerator.generateSetpoint(
currentModuleLimits, currentSetpoint, speeds, Constants.LOOP_PERIOD_SECONDS);
SwerveDriveWheelStates cheesyWheelSpeeds =
new SwerveDriveWheelStates(currentSetpoint.moduleStates());
Logger.recordOutput("SwerveStates/254/CheesyPoofsDesiredWheelSpeeds", cheesyWheelSpeeds.states);

speeds = ChassisSpeeds.discretize(speeds, Constants.LOOP_PERIOD_SECONDS);
SwerveDriveWheelStates wheelSpeeds = kinematics.toWheelSpeeds(speeds);
Logger.recordOutput("SwerveStates/254/DefaultDesiredWheelSpeeds", wheelSpeeds.states);

setWheelSpeeds(wheelSpeeds);
setWheelSpeeds(DriveConstants.USE_254_SWERVE_SETPOINT ? cheesyWheelSpeeds : wheelSpeeds);
}

// --- Wheel States ---
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/frc/robot/subsystems/drive/DriveConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
public class DriveConstants {

public static final boolean USE_254_SWERVE_SETPOINT = true;

// --- Drive Config ---

public record DriveConfig(
Expand Down

0 comments on commit 681a861

Please sign in to comment.