ContentsPrepare the robot and feedback
- Prepare the robot and feedback
- Understand what PID commands
- Tune P, then I, then D if needed
- 1. Start with proportional gain
- 2. Add integral gain
- 3. Consider derivative only if needed
- Limit windup, slew and low-speed chatter
- Example: interpret a wheel-speed plot
- Frequently asked questions
- What should happen at a zero-speed command?
- Why does the wheel pulse at very low speed?
- Can I copy PID gains from another robot?
A PID wheel-speed controller adjusts motor drive to reduce the difference between requested and encoder-measured speed. This guide assumes a differential-drive robot with brushed DC gearmotors, encoders and a PWM driver. Tune one wheel at a time; there are no universal gains.
Prepare the robot and feedback
Secure the chassis with wheels clear of the surface. Keep hands, cables and clothing away; make the power switch reachable. Check motor, driver and supply compatibility, wiring and fuses. See matching a DC gearmotor to a driver and robot power distribution and fuses. Stop for driver faults, resets, overheating or unexpected motion.
Define positive wheel rotation as forward. With motor power disabled and encoder logic powered, turn each wheel by hand and check that its count increases in that direction. Briefly apply a low positive command and confirm the wheel turns forward. Correct encoder and driver polarity before closing the loop; otherwise feedback can worsen the error. Repeat for reverse.
Run the loop on a timer at fixed sample interval dt, in seconds. Choose it short enough to observe the response but long enough for useful encoder resolution and reliable execution. For each sample:
measured wheel rpm = (signed count change / counts per wheel revolution) × 60 / dt
Use decoded counts per wheel revolution, accounting for quadrature decoding and gearbox ratio if the encoder is on the motor shaft. The signed wheel-speed setpoint uses the same units: error = setpoint − measured speed. Positive error should request more positive drive. Read counts consistently and handle counter wrap.
At low speeds, few counts per interval make the estimate jump. A longer measurement window or light low-pass filtering can help, but adds delay. See SimpleFOC’s velocity-filter documentation for this trade-off.
Understand what PID commands
The output is a signed drive request: its sign selects direction and its magnitude sets PWM duty. Clamp it to hardware-safe forward and reverse limits. Behavior depends on driver mode, battery voltage, current limiting and load; PWM does not guarantee speed or safe current.
- P responds to present speed error. More P usually speeds response, but too much can cause overshoot or oscillation.
- I accumulates persistent error over time, helping correct a sustained speed offset. Too much can cause slow hunting and overshoot.
- D responds to the rate of change of the measured speed if implemented on measurement. It can add damping, but amplifies measurement noise.
The University of Michigan PID tutorial calls these effects tendencies, not guarantees. Encoder speed is already a rate of position; D in this loop is a rate of speed—approximately acceleration—not speed itself. An unfiltered D term is −Kd × Δ(measured speed)/dt; filter it. This avoids setpoint derivative kick, as Control Guru explains.
Tune P, then I, then D if needed
Log timestamp, setpoint, speed, requested and limited duty, and driver faults. Plot speed and setpoint against time, with duty separately. Use repeatable, modest speed steps within the achievable range. Let the wheel settle and stop between trials; change one setting at a time.
1. Start with proportional gain
Set I and D to zero. Increase P gradually from a small value, repeating the same step. The trace should rise toward the setpoint. If it overshoots or swings around the target, reduce P; do not sustain oscillation. P alone can settle below the setpoint because it needs error to produce drive.
2. Add integral gain
A common discrete update is I += Ki × error × dt. Increase I gradually to reduce that persistent gap. If the trace slowly cycles around the target or takes longer to settle, reduce I and, if needed, revisit P. Settling means entering and staying within your chosen tolerance, not merely crossing the target once.
3. Consider derivative only if needed
Many wheel loops work well as PI controllers. If repeatable overshoot remains, try a small filtered D on measured speed. If duty becomes noisy or the motor chatters, reduce D or return it to zero. Filtering adds lag, so reassess. SimpleFOC’s tuning guide illustrates this balance for a different motor platform.
Limit windup, slew and low-speed chatter
If duty reaches its limit while speed remains below target, I can keep accumulating even though more drive is unavailable. Later it must unwind, delaying recovery and causing overshoot. Use anti-windup: for example, pause integration when the total drive request is limited and the error would push it further into that limit; allow integration that unwinds it. Include downstream duty and output-rate limits in the anti-windup design. MathWorks explains clamping and back-calculation.
An output rate limit can soften normal duty changes, but it also restricts the actuator and can slow response. Do not let it delay the system’s intended emergency stop. Driver current limiting or battery sag may impose additional limits that firmware cannot see; a saturated or stalled motor will not be rescued by more PID gain. Stop and investigate instead of holding a powered motor stalled.
Static friction and driver deadband can prevent motion at small duty, then cause start-stop limit cycles. Inspect the trace and mechanism before raising gains. Optional feedforward can provide a baseline duty estimated from measured operation, with PID correcting the remaining error; apply the same output limits and account for feedforward in anti-windup.
Example: interpret a wheel-speed plot
Suppose a secured wheel receives a modest forward speed step. The P-only trace rises smoothly but levels off below the setpoint, and duty is not saturated. Add a little I and repeat: if the gap closes without repeated swings, keep that setting as a candidate. If duty instead stays at its limit, first lower the request or investigate available voltage and load; integral gain cannot create missing actuator authority. This is a hypothetical diagnostic example, not a measured result.
After bench tuning, test both wheels separately under realistic battery and floor loads in a clear, controlled area. Retune if necessary and record conditions. Two independent speed loops do not guarantee a straight path: wheel-size differences, traction and load can still cause drift.
Frequently asked questions
What should happen at a zero-speed command?
Define the stop behavior explicitly: command the driver’s documented brake or coast mode as appropriate, and reset or manage integral state to avoid a restart surge or zero-speed twitch. Brake and coast stop differently; neither automatically holds position. Verify stopping and command timeout safely.
Why does the wheel pulse at very low speed?
Encoder quantization, friction, deadband and excessive integral action can all contribute. Inspect speed and duty logs, adjust the measurement window or filtering, and reduce I if it hunts. Avoid assuming every pulse is a tuning problem.
Can I copy PID gains from another robot?
Not reliably. Motor, gearing, encoder resolution, sample time, output scaling and controller implementation all matter. Check whether firmware expects continuous-time gains or already includes dt in its discrete coefficients; changing the loop rate or units can change effective behavior.
