WPILib’s coordinate system is NWU. See the picture below. Positive X is forward, positive Y is to the left, and positive rotation is CCW.

I don’t need to go any further than your kinematics to see that you have this incorrect.
public static final SwerveDriveKinematics kDriveKinematics =
new SwerveDriveKinematics(
new Translation2d(kWheelBase / 2, -kTrackWidth / 2),
new Translation2d(-kWheelBase / 2, -kTrackWidth / 2),
new Translation2d(kWheelBase / 2, kTrackWidth / 2),
new Translation2d(-kWheelBase / 2, kTrackWidth / 2));
Tracking this through your code, the order is FL, FR, BL, BR. The signs on your wheel translations are incorrect. They should be like this:

Once you get the kinematics set up properly, you will notice that you’re not driving the robot in the right direction. DriverMap
and TwoJoyStickDriverMap
is driving the robot sideways (robot X is forward/backward, joystick X is side-to-side.)
drivetrain.driveCommand(
this::getSwerveXSpeed, this::getSwerveYSpeed, this::getSwerveRot));
@Override
public double getSwerveXSpeed() {
return -controller.getAxis(Axis.AXIS_LEFT_X) * MaxSwerveConstants.kMaxSpeedMetersPerSecond;
}
@Override
public double getSwerveYSpeed() {
return controller.getAxis(Axis.AXIS_LEFT_Y) * MaxSwerveConstants.kMaxSpeedMetersPerSecond;
}
Most likely you’ll find there are other workarounds in the code that made it appear to be working, even though the robot is driving sideways. I would make the changes above, and then do the following:
- Verify that your drive motors spin forward when you apply positive output.
- Verify that your encoders read positive when you turn the wheel azimuth CCW.
- Verify that your azimuth motors rotate CCW when positive out put is applied.
- Verify that your gyro rotation is positive when you rotate the robot CCW.
- Verify that your azimuth is consistent:
a) The units need to be consistent (rotations, degrees, or radians)
b) The range needs to be consistent ([-180°, 180°], [0°, 360°], [-Pi, Pi] radians, [0, 2Pi] radians, [-.5, .5] rotations, or [0, 1] rotation.