Swerve drives straight but chassis turns

My team has been having an issue with our swerve drive where our robot can drive in a straight line, but the chassis itself is turning. From a straight line from point A to point B, the drive is straight, but the robot itself is now at a slight angle. It’s small when we drive slow, but it becomes noticeable when going at fast speeds. We have no idea what could be causing this but part of me believes it’s something with our field oriented.
Our pigeon is mounted on the very center of our robot, although it’s mounted in a way that x is on the y axis and y is on the x. We are using the MK4 L2 swerve modules and they are calibrated fully from the CANcoders.
We used the Team 364 swerve template for the whole swerve drive and our specific code is here on GitHub. Everything swerve related is in the files named Swerve. It’s also set up a bit in constants and is controlled off of robot container.
We have no idea if it’s mechanical or programming, any advice will help.


Have you checked your tread or weight distribution? This can result from one module not driving as hard as the others because of bad tread or less weight. It can also be cause from your modules not being properly offset, so that is something else you can check.

A problem that is most likely exaggerated for most teams this year is not driving “perfectly” straight, and some fancy physics occur where the robot tilts towards the heavier side. For many teams this season, the heavier side is the back (where arm/elevator mounts typically are), meaning that if the robot is not facing perfectly straight it will err towards the back.

This is why it happened for my team, at least. If your module offsets are all correct, no motors are bad, and your tread is good, then it’s not a problem to stress about.

1 Like

That does make sense. I’ll look into it thank you. With our robot, weight distribution could 100% be wonky.

Less likely, but also check that your joystick (or whatever) controlling robot rotation is properly zeroed, especially as a function of the drive direction controller.

1 Like

I think one of our robot’s wheels is higher than the others because our frame wasn’t assembled perfectly, so our robot doesn’t drive in a perfect straight line without a little manual correction.

This is not the only solution to your problem, but using your gyro and some code, you can add some drift correction to help your robot drive in a straight line.

Steps for the code:

  • Make a PID controller responsible for turning your robot to keep it from drifting. A ProfiledPIDController would work for this. You may already have one set up for autonomous.
  • Record the last robot angle after turning (whenever the turn value is above a threshold like 0.05)
  • Using the PID controller’s calculate method, the setpoint will be the last robot angle after turning, and feed the current robot angle into the method as well. You get your correction turn value returned.
  • Use that value to turn whenever you only want to strafe or move forward. Not when you want to tutrn. You can add that value to your Z/turn value before you create your ChassisSpeeds

Tune your PID constants correctly. It depends on how much you drift. You may also have to negate the returned value of the calculate method from what I recall when I did this. You should also avoid doing any correction if your robot is too far off from a threshold like 10 degrees. You might not want to correct the robot’s angle if it gets hit hard by another robot.

Note that this will probably mess up odometry.

Another way is to make sure each of the wheels actually move at the same max speed by measuring position or velocity. If one is slightly slower, you could cap the max speed or scale it down using a constant. A PID controller keeping each wheel speed the same may work the best, but I am uncertain of how to do that.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.