Chapter 9: PID Motor Speed Closed-Loop Control
9.1 Key Points
- Closed-loop control structure: target, feedback, error, and output
- Encoder speed as feedback
- PID parameter meaning and tuning direction
- Mapping controller output to ESC PWM pulse width
9.2 Course Content
Open-loop throttle control cannot compensate for battery voltage, load, or ground friction. A closed-loop speed controller compares target speed with encoder-measured speed and adjusts the ESC command using PID.
9.3 Basic Learning
PID Formula
text
error = target_speed - measured_speed
integral += error × dt
derivative = (error - last_error) / dt
output = kp × error + ki × integral + kd × derivativeParameter Meaning
| Parameter | Effect |
|---|---|
kp | Main response strength. Too high causes oscillation. |
ki | Eliminates steady-state error. Too high causes windup. |
kd | Damps fast changes. Too high amplifies noise. |
9.4 Program Study
A simple PID step function:
c
static float pid_update(pid_t *pid, float target, float measured, float dt)
{
float error = target - measured;
pid->integral += error * dt;
float derivative = (error - pid->last_error) / dt;
pid->last_error = error;
return pid->kp * error + pid->ki * pid->integral + pid->kd * derivative;
}Map PID output to a safe ESC command:
c
float out = pid_update(&speed_pid, target_speed, measured_speed, dt);
uint32_t pulse = 1500 + (int32_t)out;
pulse = CLAMP(pulse, 1000, 2000);
set_pulse(CH_THROTTLE, pulse);Safety handling should set the throttle to neutral when failsafe is active or when the measured data is invalid.
9.5 Summary
The PID loop connects encoder feedback to ESC output. It is the first step from peripheral examples toward a controllable robot motion system.