Teaching notes: robot geometry, the meaning of v and ω, the wheel equations, and the PID law that produces ω.
The robot’s pose is three numbers:
q = (x, y, θ)
A differential-drive robot has only two independent actuators, the left and right wheels. It cannot slide sideways, which is the non-holonomic constraint.
Instead of commanding wheels directly, the controller commands two body-level quantities:
The robot’s motion in the world follows from these:
In code, with timestep Δt:
A good place to stress: v moves the robot along its heading and ω only rotates that heading. Steering is indirect, which is why a controller is needed.
Treat the robot as a rigid body rotating about its centre with angular velocity ω while the centre translates at v. Any point on a rigid body has velocity
v_point = v_centre + ω × r
where r is the point’s offset from the centre. Use body axes: x_b forward, y_b to the left. Then:
The cross product ω × r for r = (0, r_y) gives (−ω r_y, 0) in body coordinates. So the forward speed of each wheel is:
v_L = v − ω L / 2
v_R = v + ω L / 2
These are the equations in the diagram. A sanity check for students: for a left turn, ω > 0, so v_R > v_L. The outer wheel (right) spins faster, which is exactly what happens when you turn left.
Inverse relations (useful for odometry, which goes from wheels back to the body):
So v is the average of the two wheel speeds and ω is their difference divided by L.
Special cases:
| Command | Result |
|---|---|
| ω = 0 | v_L = v_R = v: straight line |
| v = 0, ω ≠ 0 | v_L = −v_R: spin on the spot |
| v_L = 0 | Pivot about the left wheel |
Turning radius: the robot follows a circle of radius R = v / ω (infinite when ω = 0).
To motor commands: wheel linear speeds convert to wheel angular speeds with φ̇ = v_wheel / r_w, where r_w is the wheel radius.
The path planner gives waypoints (x₁, y₁), (x₂, y₂), …, (xₙ, yₙ). The controller must turn the current target waypoint (x_g, y_g) into v and ω. A common and teachable design:
Heading error. First compute the desired heading toward the waypoint:
θ_d = atan2(y_g − y, x_g − x)
Then the error:
e = θ_d − θ
Angle wrapping matters. Angles live on a circle, so e must be wrapped into (−π, π]:
e ← atan2(sin e, cos e)
Without this, a robot at θ = 179° with target −179° sees an error of −358° and spins nearly all the way round instead of turning 2°. This is the most common student bug.
ω(t) = K_p · e(t) + K_i · ∫ e(τ) dτ + K_d · de(t)/dt
Proportional term (K_p e). Turn harder the further off-heading you are. Too small is sluggish. Too large overshoots and oscillates (the robot weaves).
Integral term (K_i ∫ e). It accumulates past error, so it removes persistent bias, such as one motor being slightly weaker, which would otherwise leave a steady offset. Too large causes slow oscillation and windup.
Derivative term (K_d de/dt). It reacts to how fast the error is changing, acting as damping. It anticipates overshoot: if the error is shrinking quickly, it backs off the turn.
Units (a nice check for students). The error is in rad and ω in rad/s, so K_p has units of 1/s, K_i of 1/s², and K_d is dimensionless.
Discrete version (what you actually code, with Δt the control period):
The heading obeys dθ/dt = ω. With P-only control and a fixed target, de/dt = −K_p e, so
e(t) = e(0) · exp(−K_p t)
The error decays exponentially with time constant 1/K_p. In an ideal world there is no overshoot. Real overshoot comes from motor lag, delays and the moving target, which is exactly why K_d helps.
| Slow down when misaligned. For example v = v_max · cos(e) for | e | < π/2, otherwise 0. The robot then turns before driving. |
Take L = 0.5 m, v = 0.4 m/s, K_p = 1.5 s⁻¹, K_i = 0, K_d = 0.2. Suppose the heading error is e = 0.3 rad and it is shrinking at de/dt = −0.1 rad/s.
Check with the inverse: ω = (0.5075 − 0.2925) / 0.5 = 0.43 ✓, and v = (0.5075 + 0.2925) / 2 = 0.4 ✓. The right wheel is faster, so the robot turns left, which is correct since e > 0.
Instead of heading error, you can run PID on cross-track error d (the perpendicular distance to the path segment). This suits line following better, and with the same structure, ω = K_p d + K_i ∫ d + K_d dd/dt. Heading-to-waypoint is simpler to teach first.