Overview
A PID (Proportional-Integral-Derivative) controller maintains motor speed at a desired setpoint by calculating and applying corrective actions based on the error between setpoint and measured speed.Complete C++ Implementation (ESP32)
pid_controller.h
pid_controller.cpp
Usage Example
main.cpp
Python Implementation (ROS2)
pid_controller.py
Tuning Guidelines
Ziegler-Nichols Method
- Set Ki = 0, Kd = 0
- Increase Kp until system oscillates steadily
- Record Ku (ultimate gain) and Tu (oscillation period)
- Calculate PID gains:
Manual Tuning Steps
-
Start with Kp only:
- Increase until response is quick but slightly oscillatory
- Typical range: 1.0 - 5.0
-
Add Ki (if needed):
- Eliminates steady-state error
- Start small (0.1 - 0.5)
- Too high = overshoot and instability
-
Add Kd (if needed):
- Reduces overshoot
- Start small (0.01 - 0.1)
- Too high = amplifies noise
Recommended Starting Values (Mecanum Robot)
Common Issues
Integral windup (overshoot)
Integral windup (overshoot)
Solution: Set integral limits
Oscillation around setpoint
Oscillation around setpoint
Cause: Kp too high or Kd too lowSolution: Reduce Kp or increase Kd
Slow response
Slow response
Cause: Kp too lowSolution: Increase Kp
Steady-state error
Steady-state error
Cause: Ki = 0 or too smallSolution: Increase Ki
Noisy derivative term
Noisy derivative term
Solution: Low-pass filter or reduce Kd
Next Steps
Encoder Reading
Read motor encoder for velocity feedback
Motor Control
Complete motor control implementation
Serial Communication
Send PID parameters from ROS2
Troubleshooting
Debug PID tuning issues