# Tank robot drifting, quadrature feedback to keep straight

I've got a tank-style rover being driven by a Sabertooth 2X5 RC that accepts servo commands. The issue, as with almost any tank robot, is that it drifts from a straight line when going forward and backwards. My motors have quadrature encoders installed with a CHA and CHB.

Is there perhaps a tutorial, example code, something to help me get started in trying to adjust the speed of the motors using the quadrature feedback so that it goes in a straight line?

First, figure out how to read the encoders and calculate how far each wheel has traveled (in encoder ticks is fine for a start). There are plenty of tutorials on that topic.

For straight steering, one approach is that the difference in encoder ticks (summed for each wheel over a given time interval) should be zero. PID control can be used with the difference to make automatic adjustments to the speed of one or both motors.

A more general approach is to use an electronic compass to determine the direction of travel, and use PID to steer according to a given heading. No need for encoders with that approach.

I’m sure there are tutorials out there somewhere.

The simplest correction method is if the measured speed is faster than your desired speed subtract X from the servo command. If slower add X. Experiment to find a reasonable value for X. Within a few sample periods the speed reach what you want, then it will sit there running just a little over or one period, then just a little under for the next period.

A slightly more complex method is to calculate the difference between your desired speed and actual speed and adjust the servo command by exactly that amount. You need to know or measure the relationship between the servo command and the speed of the motor. Within a very few periods it will be changing the servo command by very small amounts.

A much more complex method is to use a PID control loop. There are tutorials and even a few libraries that will create a PID. I don’t recommend this unless it is really something that interests you.