PID in a robot system: multiple nested loops rather than one loop
Atlasbot · 17/8/2026 · 11 min read
What is PID card? (embedded programming section) has explained in detail the three P/I/D components, the C code, and the manual tuning process for a specific PID loop — keeping the motor speed. This article does not repeat that content, but puts PID in its rightful place in the bigger picture: a complete robot rarely has just one PID rings — but many nested rings, each responsible for a different layer.
Key concepts
The article Sense-Think-Act describes the general loop Sensor→Processing→Control; The Closed-Loop Control article describes the general closed-loop principle. PID is the most specific, most popular algorithm to realize that "compare → correct" step — not a separate concept, but an algorithmic choice within a closed-loop framework.
How it works
Real-life example in an AMR robot: the Nav2 controller (high layer) does not directly set PWM — it calculates the desired speed (v, ω), sending it to the lower layer via /cmd_vel. That lower level runs a separate PID loop to maintain the correct wheel speed according to the command just received. This is the cascade control model — the output of the high-level loop is the input setpoint for the lower-level loop:
Nav2 controller_server (10-50Hz)
│ Calculate the desired v, ω to follow the path and avoid obstacles
▼
/cmd_vel
│
▼
PID velocity control on MCU (100Hz-1kHz)
│ Keep the speed of each wheel following the correct v and ω just received
▼
PWM → Motor
In short: The outer loop (Nav2) does not need to know anything about PWM or motor characteristics. The inner loop (PID on MCU) does not need to know anything about path planning or costmap. Separating stages makes each PID loop simple and easy to tune independently, even though the whole system is very complex.
In theory, it is possible to imagine a "giant" controller that directly receives LiDAR data, directly calculating the PWM for each wheel — but this method is almost impossible for anyone to do in practice, because the frequencies vary too much between floors, and debugging is much easier when separated.
Technical details
Formulas
Common rule of thumb for stable cascade control: the inner loop must run significantly faster than the outer loop, usually at least 5-10 times faster:
Inner loop frequency ≥ 5 to 10 × Outer loop frequency
For example: the outer loop (Nav2) runs at 20Hz
The inner loop (speed PID) should run at least 100-200Hz to sufficiently "keep up"
If the inner ring is too slow compared to the outer ring, set a new point from the outer ring
constantly changing before the inner circle can stabilize according to the old setpoint
→ oscillation/instability of the entire system, even though each individual loop is tuned correctly
Parameters
Extending the 2-stage cascade example (Nav2 → Speed PID) to a 3-stage model common in industrial servo drives:
| Floor | Control variable | Typical frequency |
|---|---|---|
| Position ring (outermost) | Shaft position | 10 – 100Hz |
| Velocity ring (middle) | Rotation speed | 100Hz – 1kHz |
| Current loop (innermost) | Current through the motor coil | 1 – 20kHz |
Practical applications
The 3-stage cascade model in the table above is the standard architecture of most industrial servo drives that control manipulators: the current loop (innermost, fastest) ensures the motor generates the correct force/torque required instantly; The velocity ring (middle) uses that output to maintain the desired rotation speed; The position ring (outermost, slowest) uses the velocity ring to bring the shaft to the correct target position. These three stages are tuned independently in order from inside to outside — the current ring must be stable first, then tune the velocity ring based on the stabilized current ring, and finally the position ring is tuned — tuning the wrong order (from outside to inside) will almost certainly cause instability because the outer ring "relies" on an unreliable inner ring.
Practical example
# Illustration of a simple 2-level cascade: the outer ring outputs a setpoint for the inner ring
target_position = 10.0
current_position = 0.0
current_velocity = 0.0
Kp_outer = 0.5 # position ring (outside) - runs slower
Kp_inner = 2.0 # speed ring (in) - run much faster
for step in range(5):
# Outer ring: position error -> desired velocity setpoint
pos_error = target_position - current_position
velocity_setpoint = Kp_outer * pos_error
# Inner circle: velocity error -> actual control force
vel_error = velocity_setpoint - current_velocity
control_output = Kp_inner * vel_error
current_velocity += control_output * 0.1
current_position += current_velocity * 0.1
print(f"Step {step}: pos={current_position:.2f}, vel={current_velocity:.2f}")
Output:
Step 0: pos=0.10, vel=1.00
Step 1: pos=0.42, vel=3.15
Step 2: pos=0.99, vel=5.68
Step 3: pos=1.86, vel=8.72
Step 4: pos=3.06, vel=12.14
The outer loop (position) continuously updates the velocity setpoint based on the remaining error; The inner loop (velocity) runs according to that setpoint — correctly illustrates the cascade structure even though this is just a very simplified simulation (in reality, both the I/D and frequency differences between the two loops are much larger).
Common mistakes
- Tune the outer ring before the inner ring is stable — as mentioned in the Practical Application section, always tune from the inside out — tuning the position ring when the velocity ring below is still fluctuating makes tuning meaningless because the foundation it is based on is not stable.
- The inner ring is not fast enough compared to the outer ring — violates the 5-10 times rule stated in the Formula section, causing oscillation/instability even if each individual ring is tuned correctly.
- Integral windup propagates through multiple layers — if the inner loop is saturated for a long time, the cumulative portion (I) of the outer loop continues to accumulate based on the irreducible error, causing severe overshoot when the inner loop finally escapes saturation — requiring anti-windup mechanisms at both layers, not just the inner layer.
- The outer loop's setpoint changes suddenly, faster than the inner loop's ability to respond — causing a jerky response even though each individual PID loop is tuned smoothly — it is necessary to limit the rate of setpoint change (rate limiting) in the outer loop, the topic of the next Acceleration Limiting article in the section.
Conclusion
PID is not a single block but a building block that can be nested in multiple layers - each layer is separated by frequency and responsibility, making it easy for complex systems to tune and debug each part independently. Always tune from the inner ring to the outer ring, and ensure sufficient frequency separation between stages to avoid instability. The next article in this section is about Velocity Control — diving into the very same speed PID loop mentioned in the cascade example above.
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.