Differential drive in AMRs: from /cmd_vel to PWM for each wheel
Atlasbot · 22/8/2026 · 11 min read
Lesson kinematics Differential Drive in the Robotics Fundamentals section has fully solved the math formula v, ω. This article doesn't repeat that math — it focuses on the questions of the actual AMR builder: what hardware the recipe runs on, what code it turns out, and what mechanical decisions go into it.
Key concepts
A complete AMR differential drive requires 3 coordinated hardware blocks, as stated in article Hardware Architecture:
- 2 sets of motor + driver (left/right) — learned in lessons motor and Motor Driver
- 2 encoders (one per wheel) — actual speed feedback for control loop
- 1 caster wheel — not active, only balances the 3-fulcrum robot frame
How it works
The firmware receives the command /cmd_vel (containing the desired v, ω) from the ROS2 layer above, performing 2 consecutive steps:
Step 1 — Mixing: convert v, ω into the desired speed for each wheel
v_L = v − ω×L/2
v_R = v + ω×L/2
Step 2 — separate PID loop control for each wheel:
encoder measures actual v_L, actual v_R → compares with desired v_L, v_R
→ PID calculates the PWM needed to output for each motor driver
An important point that is easy to miss: the Mixing step only calculates the desired speed, not directly to PWM — if the code directly outputs PWM proportional to v_L/v_R but ignores the PID feedback loop from the encoder, the robot will run at the wrong actual speed whenever there is uneven friction, the battery is weakening, or the load is skewed between the two sides — right problem. closed-loop vs open-loop has been learned in the PID lesson (embedded programming section), specifically applied here.
Technical details
Formulas
The control loop frequency directly affects the smoothness and response latency — a loop that is too slow causes the robot to react jerkily to changing velocity commands:
Loop_cycle = 1 / Loop_frequency
Example: loop runs at 50Hz → period = 1/50 = 20ms between each time
Read encoder → calculate PID → output new PWM
A frequency that is too low (eg: 5Hz, cycle 200ms) causes the robot to "jerk" clearly when
sudden change in velocity command — the loop is not fast enough to follow the setpoint
Parameters
| Component | Role in the loop | Typical frequency |
|---|---|---|
| Read encoder | Measure the actual speed of each wheel | 50-100Hz |
| Calculate PID + output PWM | Bring the actual speed to the correct setpoint | 50-100Hz (same encoder read cycle) |
| Publish odometry to ROS2 | Update /odom for Navigation layer | 20-50Hz (can be lower than PID loop) |
Practical applications
In the actual architecture (stated in the AMR Architecture article), the entire mixing + PID + read encoder loop runs on the MCU (ESP32/STM32) of the Driver Layer — not on the MPU via ROS2, because the network latency/schedule of ROS2 does not ensure fast and stable enough for the high-speed control loop at 50-100Hz. The MPU only sends down a new /cmd_vel each time the Nav2 recalculates the path (much lower frequency), while the high-speed PID loop runs completely independently on the MCU between those updates.
Practical example
L = 0.35 # wheel separation (m)
def mixing(v, omega):
v_L = v - omega * L / 2
v_R = v + omega * L / 2
return v_L, v_R
# Command: move forward at 0.5 m/s, turning gently at 0.3 rad/s
v_L, v_R = mixing(v=0.5, omega=0.3)
print(f"v_L={v_L:.3f} m/s, v_R={v_R:.3f} m/s")
# PID loop (separate gains for each wheel, runs every 20ms)
def pid_step(setpoint, measured, integral, prev_error, Kp=2.0, Ki=0.5, Kd=0.1, dt=0.02):
error = setpoint - measured
integral += error * dt
derivative = (error - prev_error) / dt
output = Kp*error + Ki*integral + Kd*derivative
return output, integral, error
Output:
v_L=0.448 m/s, v_R=0.553 m/s
The left wheel is slower than the right wheel as expected with positive rotation (by convention positive ω is left rotation) — each wheel then has a separate PID that adjusts the PWM to keep track of this setpoint.
Common mistakes
- Skip the PID loop, the PWM output is directly proportional to
v_L/v_R— as stated in the Principle section, this is open-loop control, the actual speed will drift according to load/battery/friction without a self-adjusting mechanism. - Running the PID loop at too low a frequency — as calculated in the Formula section, the robot reacts jerkily, especially clearly when Nav2 sends continuously changing speed commands.
- Use the same gain PID for both wheels even though the mechanics are not completely symmetrical — actual friction/load on both sides is rarely identical, gain PID should be adjusted separately or at least tested separately for each wheel.
- Not checking the sign (direction) when mixing — mistake the
+/−sign betweenv_Landv_Rcausing the robot to rotate in the opposite direction of the command command even though the calculated magnitude is correct, the same error as warned in the article kinematics Differential Drive.
Conclusion
Formulas v_L = v − ωL/2, v_R = v + ωL/2 are just the beginning — turning it into reliable real motion requires adding a feedback PID loop from an encoder running at the right frequency on the right hardware layer (MCU, not via ROS2). The next article in the category looks at Mecanum Drive — expanding this mixing problem to 4 wheels to achieve the ability to move sideways.
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.