Introduction
PID (Proportional-Integral-Derivative) control is the foundation of movement algorithms in Mars-RS. The PID controller continuously calculates an error value and applies corrections to minimize that error.PID Structure
PidConstants
ThePidConstants struct defines the tuning parameters for a PID controller:
All fields are public (
pub) to allow easy configuration and experimentation with different tuning parameters.Pid Controller
ThePid struct maintains the controller state:
PID Algorithm
Initialization
Create a new PID controller with specific constants:Output Calculation
Theout method computes the control signal:
PID Terms Explained
Proportional (P)
The proportional term provides an output proportional to the current error:- Immediate response to current error
- Cannot eliminate steady-state error alone
- Dominant term in most Mars-RS controllers
Integral (I)
The integral term accounts for accumulated past errors:-
Integral Threshold: Only accumulates when error is below threshold
-
Tolerance Reset: Clears integral when error is very small
-
Anti-Windup: Caps integral to prevent excessive accumulation
In many Mars-RS movement algorithms, the integral term is set to 0.0, relying primarily on proportional and derivative control.
Derivative (D)
The derivative term predicts future error based on the rate of change:Tuning Guidelines
Manual Tuning Process
Start with P only
Set I and D to zero. Increase P until the system responds quickly but oscillates slightly.
Example Configurations
FrommoveToPurePursuit implementation:
Notice the angular (rotation) controller has a lower P value than the linear controller. This is because rotational control is typically more sensitive.
Common Tuning Issues
Oscillation around target
Oscillation around target
Problem: P gain too highSolution: Reduce P or increase D to dampen oscillations
Slow response
Slow response
Problem: P gain too lowSolution: Gradually increase P until response improves
Steady-state error
Steady-state error
Problem: P and D cannot eliminate final errorSolution: Add small I term with appropriate threshold and max values
Overshoot and instability
Overshoot and instability
Problem: I term too large or no anti-windupSolution: Reduce I, set appropriate
maxIntegral, or increase integralThresholdDual PID System
Mars-RS uses separate PID controllers for different aspects of motion:- Linear control: How aggressively the robot moves toward targets
- Angular control: How quickly the robot corrects heading errors
Mathematical Formula
The complete PID formula implemented in Mars-RS: Where:- = control output
- = current error
- = proportional gain
- = integral gain
- = derivative gain
Related Topics
Movement Algorithms
See PID in action with movement functions
Robot Physics
Understand how PID output controls the robot