PID Control Explained for Robot Builders
PID control is how most robots hold a wheel at a set speed, an arm at a set angle, or a heading on a straight line. The algorithm fits in a dozen lines of…
Almost every wheeled robot depends on one small control loop: holding each wheel at the speed the navigation stack asks for, regardless of load, slope and battery voltage. This guide builds that loop for a DC gearmotor with an encoder: measuring speed, choosing the loop rate, adding feedforward, and tuning, with a complete Arduino sketch you can adapt.
The simplest estimate counts edges over a fixed interval:
Counts per revolution must be measured at the output shaft and must match how you decode. A typical small gearmotor's encoder gives 11 pulses per motor revolution per channel; through a 30:1 gearbox that is 330 pulses per wheel revolution. Counting both edges of channel A doubles it to 660; counting every edge of both channels (full quadrature) gives 1320.
The catch is quantisation. Each estimate is a whole number of counts, so at low speed and short intervals the reading is coarse:
| Encoder (counts per wheel revolution) | Loop rate | Counts per sample at 60 RPM | One count is |
|---|---|---|---|
| 660 | 100 Hz | 6.6 | 15 % of the reading |
| 1320 | 100 Hz | 13.2 | 7.6 % |
| 1320 | 1 kHz | 1.3 | 76 % |
So a faster loop is not automatically better: at 1 kHz this encoder can barely tell speeds apart. Use full quadrature decoding where you can, filter the estimate lightly, and at very low speeds measure the time between edges instead of counting them.
The loop must be several times faster than the motor's own response. A small gearmotor with a wheel typically reaches speed in a few hundred milliseconds, so 50 to 200 Hz is plenty. In our simulator's motor speed loop (a 0.15 s mechanical time constant), the same PI gains settle in 145 ms whether the loop runs at 1000 Hz or 100 Hz. Pick a rate your encoder can support and keep it exactly constant.
A DC motor's steady speed is roughly proportional to its voltage, plus a small offset to overcome friction. If you know that relationship, you can command roughly the right PWM directly and leave the PID to correct the remainder:
Measure KV and KS once: apply several fixed PWM values with the wheel on the ground, record the steady speeds, and fit a straight line. KS is the PWM where the wheel just starts moving; KV is the slope. With good feedforward, the robot responds to speed changes almost instantly, and the integral only has to handle load and battery variation.
// Speed control of one DC gearmotor with a quadrature encoder:
// feedforward + PI with clamping anti-windup, at a fixed 100 Hz.
const int ENC_A = 2, ENC_B = 3; // encoder channels (pin 2 can interrupt on an Uno)
const int PWM_PIN = 9, DIR_PIN = 8; // motor driver inputs
const float COUNTS_PER_REV = 660.0; // both edges of channel A, 11 PPR x 30:1 gearbox
const unsigned long PERIOD_US = 10000; // 100 Hz control loop
const float KV = 255.0 / 180.0; // feedforward: PWM per RPM (full PWM gave about 180 RPM)
const float KS = 20.0; // PWM needed just to overcome friction
const float KP = 1.2, KI = 8.0; // start here, then tune on your motor
volatile long encoderCount = 0;
float targetRpm = 0;
void onEncoderA() {
// count both edges of channel A; the level of B gives the direction
if (digitalRead(ENC_A) == digitalRead(ENC_B)) encoderCount--; else encoderCount++;
}
void setup() {
pinMode(ENC_A, INPUT_PULLUP);
pinMode(ENC_B, INPUT_PULLUP);
pinMode(DIR_PIN, OUTPUT);
attachInterrupt(digitalPinToInterrupt(ENC_A), onEncoderA, CHANGE);
Serial.begin(115200);
}
void loop() {
static unsigned long last = micros();
static long lastCount = 0;
static float integral = 0, rpmFiltered = 0;
if (Serial.available()) targetRpm = Serial.parseFloat(); // type a speed, e.g. 120
unsigned long now = micros();
if (now - last < PERIOD_US) return;
float dt = (now - last) * 1e-6;
last = now;
noInterrupts();
long count = encoderCount;
interrupts();
float rpm = (count - lastCount) / COUNTS_PER_REV / dt * 60.0;
lastCount = count;
rpmFiltered += 0.3 * (rpm - rpmFiltered); // light low-pass filter
float error = targetRpm - rpmFiltered;
float ff = 0;
if (targetRpm > 0) ff = KV * targetRpm + KS;
if (targetRpm < 0) ff = KV * targetRpm - KS;
float candidate = integral + KI * error * dt;
float unclamped = ff + KP * error + candidate;
bool pushingFurther = (unclamped > 255 && error > 0) || (unclamped < -255 && error < 0);
if (!pushingFurther) integral = candidate; // clamping anti-windup
if (targetRpm == 0) integral = 0; // stop cleanly
float out = constrain(ff + KP * error + integral, -255, 255);
digitalWrite(DIR_PIN, out >= 0 ? HIGH : LOW);
analogWrite(PWM_PIN, (int)fabs(out));
Serial.print("target:"); Serial.print(targetRpm);
Serial.print(",rpm:"); Serial.print(rpmFiltered);
Serial.print(",pwm:"); Serial.println(out);
}
The serial output uses name:value pairs, so the Web Serial Monitor plots target, speed and PWM live while you type new targets. If the wheel runs away when you first enable the loop, the encoder direction is reversed relative to the motor: swap A and B, or flip the sign of the count.
Try the same steps in the PID Tuning Simulator's motor speed loop first; the tuning guide explains the method in detail.
cmd_vel to wheel speeds is covered in from cmd_vel to wheel speeds.
PID control is how most robots hold a wheel at a set speed, an arm at a set angle, or a heading on a straight line. The algorithm fits in a dozen lines of…
Most PID tuning on robots is done by hand, and most hand tuning is done badly: three gains changed at once, by guesswork, until something looks roughly right.…
The Ziegler-Nichols rules, published in 1942, turn one simple experiment into a full set of PID gains. They are still the first tuning method most engineers…
The PID equation in a textbook assumes an actuator that can deliver any value, a measurement without noise, and a setpoint that never jumps. Real robots have…
If you have any query or problem
feel free to contact us
email: [email protected]
Comments 0