Using Encoders to Correct Drive

I need help with using encoders to keep a robot going straight, basically adjusting the speeds of Victors based on encoder rates to make sure one motor doesn’t slack behind another.

I know that this is possible with PID loops but I have not found a single Java implementation of PID for this task (and I really don’t understand conceptually how PID works either)

We’re using the RobotDrive class with tankDrive for driving with 4 motors and 2 encoders (one per 2 motors on each side).

There is a PIDController class/object.

import edu.wpi.first.wpilibj.PIDController;

We know, but how would we implement it with tank drive?

Have you considered using a gyro instead?

The general method that I’m thinking of would be to use the difference in the encoder distances as the error for a PID loop. I would use the arcade drive function, since the PID would “output” how fast you should turn.

Also, when you say “tank drive” it sounds a little like you’re saying you want to use it during teleop. Is that correct? I think it may get annoying for drivers if they want to turn just a little while going forward, and you’ll have other logistical issues to think about.

We would have been fine with doing this for only autonomous but the robot really doesn’t go straight for more than a few feet, which would annoy the driver. We tried adjusting the ratios of motor speeds and went pretty straight for 10 feet, but slower.

In any case, how would we achieve this in autonomous? Give examples in Java preferably.

As stated earlier, you’d be better off using a gyro to accomplish the task of straight driving. When the robot is initialized, the gyro will zero itself. If there is any change in heading, you can use that to create a loop that will steer the robot to correct itself. You may encounter a slight drifting effect, but it shouldn’t be noticeable in a single match.