You have some issues with the coordinate system. I highly recommend you read the coordinate system documentaiton.
Looking at your kinematics and your modules, I see some problems. Take a look at the Swerve drive kinematics documentation. Your comments are wrong on your kinematics. I put the actual module in parenthesis:
public static final SwerveDriveKinematics kinematics = new SwerveDriveKinematics(
new Translation2d(wheelBase / 2.0, trackWidth / 2.0), //FR (actually FL)
new Translation2d(wheelBase / 2.0, -trackWidth / 2.0), //BR (actually FR)
new Translation2d(-wheelBase / 2.0, trackWidth / 2.0), //FL (actually BL)
new Translation2d(-wheelBase / 2.0, -trackWidth / 2.0)); //BL (actually BR)
The order of your modules here has to match the order of your modules whenever you interact with kinematics. That means, when you call kinematics.toSwerveModuleStates()
, it will return an array in the same order [FL, FR, BL, BR]. However, in your Swerve
class you have them in a different order [FR, BR, FL, BL]:
mSwerveMods = new SwerveModule[] {
new SwerveModule(1, Constants.Swerve.Mod1.constants, Constants.Swerve.Mod1.invertedDrive, Constants.Swerve.Mod1.invertedSteer),
new SwerveModule(3, Constants.Swerve.Mod3.constants, Constants.Swerve.Mod3.invertedDrive, Constants.Swerve.Mod3.invertedSteer),
new SwerveModule(0, Constants.Swerve.Mod0.constants, Constants.Swerve.Mod0.invertedDrive, Constants.Swerve.Mod0.invertedSteer),
new SwerveModule(2, Constants.Swerve.Mod2.constants, Constants.Swerve.Mod2.invertedDrive, Constants.Swerve.Mod2.invertedSteer),
};
...
public void setModuleStates(SwerveModuleState[] desiredStates) {
SwerveDriveKinematics.desaturateWheelSpeeds(desiredStates, Constants.Swerve.maxSpeed);
for(SwerveModule mod : mSwerveMods){
mod.setDesiredAutoState(desiredStates[mod.moduleNumber]);
}
}
Your controller input is telling the robot to rotate backwards, take a look at the Joystick and controller coordinate system section of the documentation. It may appear like this is working because you swapped the modules around, but it’s not working. All three axes need to be inverted:
yAxis = -controller.getLeftY();
xAxis = -controller.getLeftX();
rAxis = controller.getRightX(); // Needs to be inverted
Finally, you are swapping the X and Y axis in your Swerve.drive()
method:
SwerveModuleState[] swerveModuleStates =
Constants.Swerve.kinematics.toSwerveModuleStates(
fieldRelative ? ChassisSpeeds.fromFieldRelativeSpeeds(
translation.getY(), // This should be X
translation.getX(), // This should be Y
rotation,
getGyro()
)
: new ChassisSpeeds(
translation.getY(), // This should be X
translation.getX(), // This should be Y
rotation)
);
In short, you’re driving your robot sideways and rotation backwards.