Help in Driving Straight


Our robot does not drive straight with the current code we have. We are using different methods to make it drive straight with encoders and gyros. However, these methods are causing it to veer to one side.

public class AutonomousDrive extends CommandBase {

    double left = .5;
    double right = .475;

    public AutonomousDrive() {
        // Use requires() here to declare subsystem dependencies
        // eg. requires(chassis);

    // Called just before this Command runs the first time
    protected void initialize() {

    // Called repeatedly when this Command is scheduled to run
    protected void execute() {
        driveTrain.driveXboxTank(left, right);
        double rightEncoderCompensation = driveTrain.leftEncoder.getDistance() - driveTrain.rightEncoder.getDistance();
        double increaseFactor = rightEncoderCompensation/200.0;
        if (rightEncoderCompensation > 0)
            if (right >= 0.5) {
                left -= increaseFactor;
            } else {
                right += increaseFactor;
        } else if (rightEncoderCompensation < 0) {
            if (left >= 0.5) {
                right -= increaseFactor;
            } else {
                left += increaseFactor;

        /*if (driveTrain.gyro.getAngle() > 0.5) {
            if (right == 1.0) {
                left -= .0025;
            } else {
                right += .0025;
        } else if (driveTrain.gyro.getAngle() < -0.5) {
            if (left == 1.0) {
                right -= .0025;
            } else {
                left += .0025;

We were thinking maybe PID is the solution, but we have no idea on how to implement it.

How severely does it veer to one side?
Does it always veer to the same side?

What are the units of the getDistance methods? There would be a big difference between adjusting your turn if one wheel was ahead of the other by 0.5 feet vs 0.5 inches.

How did you pick your increaseFactor to divide the distance error by 200?
If the increaseFactor is too small your system will not respond quickly enough to compensate for deviations off your desired heading.

For reference, last year our drive straight code would compensate if we were +/- 1 degree off our heading. Our slow wheel’s commanded value would be increased by 10%. this yielded good results on our robot at speeds that were pretty close to our top travel speeds.

Have you calibrated your talons? We figured this out one meeting when we were having problems going straight.

2 problems our team found:
As mentioned before, calibrate your talons (if that’s what you’re using).
Second, make sure the bearings flanges inside of your gearbox is in the right orientation, so they’re not grinding together, slowing one side down.

[emphasis added by me] THIS. Our robot could hardy even move when we first drove it (a few days ago :stuck_out_tongue: ). We implemented a crude feedback loop to correct for it using the gyro (which worked fairly well), but we later disassembled the gearboxes to find out the problem. We checked our individual wheel speed and two of them were severely off. It turns out that two of our four gearboxes had the bearings improperly installed, causing them not to function properly. From what I heard, it was really difficult to spot, and was only found out when attempting to disassemble and reassemble the entire thing (meaning it wasn’t blatantly obvious, regardless of whether or not the gearbox was assembled).

How do you calibrate a Talon?

The Talon manual is located on the website, and it explains the calibration procedure in detail.

The calibration sounds easy. Thank you.