-
Stability: The robot balances perfectly with tuned values (
$K_p=120, K_i=0.5, K_d=45$ ). - Disturbance Rejection: When pushed, it recovers quickly.
-
Low Damping (
$K_d=5$ ): Reducing$K_d$ triggers oscillations (instability). -
Wind Resistance (
$K_i$ ): With strong wind, the robot initially leans. Increasing$K_i$ removes this steady-state error, bringing it back to vertical.
An open-source project designed to explain and implement a PID (Proportional-Integral-Derivative) controller for a two-wheeled balancing robot. This repository bridges the gap between high-level Calculus theory and real-world embedded programming.
In robotics, Calculus is the language of movement. We use it to observe how "errors" change over time.
| Concept | Mathematical Meaning | Intuitive Action |
|---|---|---|
|
Derivative ( |
Rate of change at a specific moment. | Slicing: Measuring speed to predict the future. |
|
Integral ( |
Accumulation of values over time. | Gluing: Adding up past errors to ensure precision. |
To keep a robot upright, we calculate the Error (
- Logic: The more the robot tilts, the harder the motors push back.
-
Formula:
$K_p \cdot e(t)$
-
Logic: It remembers past errors. If the robot leans for a long time due to friction or weight imbalance, the Integral "adds up" these tiny errors until they generate enough force to push the robot back to exactly
$0^\circ$ . -
Formula:
$K_i \cdot \int_{0}^{t} e(\tau) , d\tau$
- Logic: It measures the velocity of the fall. If the robot is falling fast, the D-term applies a "brake" to dampen the movement and prevent overshooting.
-
Formula:
$K_d \cdot \frac{de}{dt}$
In computer science, we cannot calculate "infinitely small" changes. Instead, we use Discretization.
-
Derivatives become Subtraction:
$\frac{\Delta e}{\Delta t} = \frac{error_{now} - error_{last}}{time_{interval}}$ -
Integrals become Addition:
$\sum (error \times \Delta t)$
# Constants (Tuning knobs)
Kp = 15.0 # Aggressiveness
Ki = 0.8 # Precision
Kd = 1.2 # Stability
total_error = 0
last_error = 0
dt = 0.01 # 10ms sampling rate
while True:
current_angle = read_gyroscope()
error = current_angle - 0 # 0 is the target (upright)
# 1. Proportional
P = Kp * error
# 2. Integral (Summation)
total_error += error * dt
I = Ki * total_error
# 3. Derivative (Difference)
D = Kd * (error - last_error) / dt
# Output to Motors
motor_speed = P + I + D
apply_power(motor_speed)
last_error = error
wait(dt)- Set
$K_i$ and$K_d$ to zero. Increase$K_p$ until the robot starts oscillating (shaking back and forth). - Increase
$K_d$ to dampen the oscillations and make the robot move smoothly. - Increase
$K_i$ only if the robot settles at a slight angle instead of perfect vertical.
This project is licensed under the MIT License - feel free to use, modify, and share!
