Using 2 tachometers and 2 motors, how do I get my robot to drive straight?

Hi there -

I found some useful info searching around the net, particularly the following, which after some trial and error gave me a motor speed offset for a pair of motors incorporated into a 2WD bot. The code is here in snippets, with value set to 0, as is drawn from a 4WD bot constructed of late, and seemingly, with four drive wheels, always traveled in a straight line. Lucky I suppose. I used the Adafruit motor shield library for a clone Adafruit V1 shield.

//...begin snippets
#include <AFMotor.h>
// ...

#define MAX_SPEED 200             // 200/255, about 85%
#define MAX_SPEED_OFFSET 0         // Set to compensate for uneven drive motors

// ...

AF_DCMotor motor1(1, MOTOR12_1KHZ); // LEFT FRONT-create motor #1 using M1 output on Motor Drive Shield, set to 1kHz PWM frequency
AF_DCMotor motor2(2, MOTOR12_1KHZ); // LEFT REAR-create motor #2, using M2 output, set to 1kHz PWM frequency
AF_DCMotor motor3(3, MOTOR34_1KHZ); // RIGHT REAR-create motor #3 using M3 output on Motor Drive Shield, set to 1kHz PWM frequency
AF_DCMotor motor4(4, MOTOR34_1KHZ); // RIGHT FRONT-create motor #4 using M4 output on Motor Drive Shield, set to 1kHz PWM frequency

//...

void setup () {
//...
}
void loop (){
//...
}

//    MOTOR CONTROL
void moveForward() {
  motorSet = "FORWARD";
  motor1.run(FORWARD);      // turn it on going forward
  motor2.run(FORWARD);      // turn it on going forward
  motor3.run(FORWARD);      // turn it on going forward
  motor4.run(FORWARD);      // turn it on going forward
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=1) // bring up speed, lowers draw on batts at startup
  {
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
    motor3.setSpeed(speedSet);
    motor4.setSpeed(speedSet+MAX_SPEED_OFFSET);
    delay(5);
  }
}
//...end snippets

I had wanted to incorporate the magnetometer from a 10DOF IMU breakout in concert with an analog feedback servo/ultrasound distance sensor to map obstructions and proceed accordingly, but was unable to shield the magnetometer sufficiently. Even with all the motors capped and wires twisted, it was a no go. Swinging the compass in the traditional manner seemed not to work either. The magnetometer would have also provided a new course heading for traveling in a straight line, and for governing amount of turn based on ping info. If by chance you endeavor for something like this, please share your successes.

Anyway, check around and see if you find something similar to above, and you should be on your way.

Mark