# PID help

Can someone help me with making my robot drive forward without curving (one side is always moving slightly slower) this would be with PID not fixing the drive base because I have replaced all of the motor with new ones and the drive base is squared up.

It’s a tricky problem. The solution will depend on what sensors you’re using. You’ll need to establish an error term, either via encoder ticks or a compass/gyro.

Then a P or PD loop will likely be the answer. Writing the code for this is one challenge. The next challenge will be tuning it.

I don’t have a turn key solution for you, but you can find my PROS PID code here.

Is the weight distribution unbalanced? Friction reduced? Seems like it’s not a PID problem to me.

The friction could be off on one side vs the other.

PID overall averaging the two sides does not always lead to left/right compensation.

PID on each wheel independently should get you to the same end point, but left/right drift can occur.

Controlling left/right drift via a gyro works well as long as the gyro does not drift.

Ensuring the rate of change on left and right sides being similar has produced the best results but it is harder to implement. (PD on the speed and PID on the position)

I traditionally do drive straight PID with 2 control loops. PID for distance and PID (really just P but ID are coded) for correcting the drive. Code below

``````
straight.error=straight.target -((SensorValue[leftDrive]-initialLeft) + (SensorValue[rightDrive]-initialRight));
angle.error=((SensorValue[leftDrive]-initialLeft)-(SensorValue[rightDrive]-initialRight));

int StraightOut = straight.kP*straight.error + straight.kI*straight.integral + straight.kD*straight.derivative;
int AngleOut = usedUltra?0:angle.kP*angle.error + angle.kI*angle.integral + angle.kD*angle.derivative;
StraightOut = limit(minSpeed,maxSpeed,StraightOut);

setLeftWheelSpeed(StraightOut-AngleOut);
setRightWheelSpeed(StraightOut+AngleOut);

``````

The essence is that they both drive at some agreed upon speed and as the robot drifts the fast side slows down and the slow side speeds up. Note the limit is fairly important because if the agreed upon speed is already full speed you will only change the speed of 1 side(127-20,127+20 = 107,147) thus correcting slower and drifting farther away.

If your system has frictional differences, or just motor power differences, the integrator will be the term that winds up the difference to allow them to move at the same speed.

If you are using ROBOTC, just add an IME to left and right side motors (one each side) then enable PID in the motors&sensors setup dialog. You may also need to slave additional motors on left and right, it all depends on the drive configuration. See if that helps.