Motor control for AMRs: a closed PID speed loop for each wheel
Atlasbot · 22/8/2026 · 11 min read
The previous lessons learned each piece individually: motor, Motor Driver, Encoder, and PID principle in the embedded programming section. This article assembles them into a complete control loop — specifically, the speed control loop for an AMR wheel, the link that runs dozens of times per second on the MCU of any real AMR.
Key concepts
The speed control loop of a wheel consists of 4 components connected in series into a closed loop:
- Setpoint — desired speed, calculated from the mixing step (learned in Differential Drive/Mecanum Drive lesson)
- PID controller — compare setpoint with measured speed, calculate adjustment signal
- Driver + Motor — receives PWM signal, actually turns wheel
- Encoder — measures actual rotation speed, sends feedback back to PID
This is the classic "closed-loop control" model — the core difference is just outputting PWM proportional to the desired speed (open-loop) without feedback.
How it works
Each control cycle (usually 50-100Hz), the firmware performs exactly 4 steps:
1. Read the encoder → calculate the actual measured speed (learned in the Encoder lesson)
2. Calculate error: error = setpoint − measured_speed
3. PID calculates the adjustment signal: output = Kp×error + Ki×∫error + Kd×(d error/dt)
4. Convert output to PWM value, output to motor driver
Why do we need all three components P, I, D instead of just simple P: component P reacts immediately to the current error but leaves a steady-state error when there is a continuous resisting load/friction; component I accumulates error over time to eliminate that residual error; Component D reacts to the rate of change of the error, helping to reduce overshoot when the setpoint changes suddenly. The full principles of these three components are presented in the PID article in the embedded programming section — this article applies directly to the wheel speed problem.
Technical details
Formulas
Discrete PID controller, executed on MCU every cycle Δt:
error(t) = setpoint − measured(t)
integral += error(t) × Δt
derivative = (error(t) − error(t−1)) / Δt
output(t) = Kp×error(t) + Ki×integral + Kd×derivative
PWM = limit(output(t), PWM_min, PWM_max)
Step clamp (clamp/saturation) is required — the driver only receives PWM within a valid range (eg: 0-255 or -100% to 100%), and missing this step causes integral to accumulate uncontrollably (integral windup) when the motor is blocked from rotating despite the calculated output being very large.
Parameters
| Parameter | Effect when increased | Risk if too great |
|---|---|---|
| Kp | React faster to errors | Oscillation, overshoot |
| Ki | Eliminate steady-state error faster | Integral windup, prolonged slow oscillation |
| Kd | Reduced overshoot, smoother | Amplify measurement noise from encoder |
Practical applications
In a real AMR architecture, each wheel runs an independent PID instance with its own set of gain (Kp, Ki, Kd) — although in theory the left/right wheels of a Differential Drive can share the same gain, the reality of assembly (uneven bearing friction, different lengths of wires causing different voltage drops) makes fine-tuning each wheel separately gives significantly more stable results. Adjusting the gain is usually done manually by trial and error: increase Kp until it begins to fluctuate slightly, add Kd to reduce that fluctuation, then add Ki if there is still residual error under heavy load.
Practical example
def pid_step(setpoint, measured, state, Kp=2.0, Ki=0.8, Kd=0.05, dt=0.02, pwm_max=255):
error = setpoint - measured
state["integral"] += error * dt
derivative = (error - state["prev_error"]) / dt
output = Kp*error + Ki*state["integral"] + Kd*derivative
pwm = max(-pwm_max, min(pwm_max, output)) # Anti-integral windup
state["prev_error"] = error
return pwm, state
state = {"integral": 0, "prev_error": 0}
setpoint = 0.5 # m/s
# Simulate 5 cycles; motor starts from 0 and gradually speeds up
measured_values = [0, 0.15, 0.32, 0.44, 0.49]
for v in measured_values:
pwm, state = pid_step(setpoint, v, state)
print(f"do_duoc={v:.2f} m/s -> PWM={pwm:.1f}")
Output:
do_duoc=0.00 m/s -> PWM=25.0
do_duoc=0.15 m/s -> PWM=22.6
do_duoc=0.32 m/s -> PWM=17.5
do_duoc=0.44 m/s -> PWM=10.8
do_duoc=0.49 m/s -> PWM=4.1
The PWM gradually decreases as the measured speed approaches the setpoint — the expected behavior of a closed control loop, not a fixed PWM value like open-loop calculations.
Common mistakes
- Lack of PWM limit step (clamp), causing integral windup — as stated in the Formula section, the motor is blocked hard causing
integralto accumulate indefinitely, when the obstacle is removed the motor jerks abnormally strongly due to too large accumulated error. - Increasing Kp too high to react quickly, causing fluctuations — need to increase Kd to reduce overshoot, not just increase Kp alone.
- Running the PID loop at the wrong fixed cycle — calculation
Δtis inconsistent (due to other code running in between on the same MCU) causing components I and D to calculate incorrectly, the control loop is unstable. - Using the same gain set for all wheels without actual testing — as stated in the Application section, small mechanical deviations between wheels cause the optimal gain to not be completely the same, especially important with Mecanum Drive (4 wheels) learned in the previous lesson.
Conclusion
The closed PID loop is the link that turns the "mixing formula that calculates the desired speed" into "the wheel actually turns at that speed" — without it, the robot only controls the open-loop, drifting in speed according to actual load and friction. The next article in the category goes into Odometry — closing a larger loop: using the same encoder data just used for the PID to calculate the robot position over time.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.