Driving Straight

Hey, so as we get close to the season, there’s one thing we wanna understand how to do. We wanna understand how to drive straight using autonomous period. Using a PIDController for distance will not work since the two sides of the chassis may drive in a different speed. Using a PIDController for speed might work a little better though it will be hard to land in the distance we tried to go to. So how do we go into doing something like that?

Good luck in the season everybody!

If you have a gyro, you can run a PID on the angle so that if it turns one way or the other, it will increase/decrease the outputs on the sides to help it turn back.


When WPILib 2017 is released, take a look at the gyro example. It is a sample program to demonstrate how to use a gyro sensor to make a robot drive straight. The program uses a joystick to drive forwards and backwards while the gyro is used for direction keeping. You can replace the joystick in the example with the output from a PIDController to drive straight for a certain distance.

You can get to the Gyro example by:
File -> New -> Other -> WPILib Robot Java Development -> Example Robot Java Project -> Gyro

If you simply can’t wait that long, feel free to look at our AutoDrive command, which locks onto the current heading (yaw) from our navX when it starts and uses PID to maintain that heading as it drives. The same approach could be used for any gyro.

1 Like

You can also look at our library in which you will find a PidDrive class that provides a method named “setTarget”. It allows you to set the target distances in X or the Y direction. It also let you specified the “heading” you want to maintain relative to current heading. Don’t worry about it being a FTC project. Our library is shared between FTC and FRC, so the TrcLib portion of the library is platform independent. So it would be identical code between FTC and FRC.

We are trying to implement the PIDController to get our tank drive robot to drive straight in autonomous mode. I looked at the examples, but they really didn’t help me. Does anyone have a really simple example how to implement it on a command based robot with a NavX using the PIDController object? It could be driving by time or distance, I just want to get something we could start with at this point.

These examples don’t use the PIDController class, but they are simple examples of how to drive straight using a gyro:


This is another example, this one from team code, that uses the NavX in a command-based program. Similar to the Screensteps example above, the team rolled their own PID control math rather than using PIDController in order to avoid the threading complexity that the WPILib PIDController currently requires. For 2020, WPILib includes a new PIDController class that will be easier to use for this sort of thing, as it no longer runs in a separate thread.