A Proportional-Integral-Derivative (PID) controller is a control system mechanism that continuously calculates an error value as the difference between a desired setpoint and a measured process variable. It then applies corrections based on proportional, integral, and derivative terms, each multiplied by a tunable coefficient. PID controllers are crucial in systems where precise control is required, such as in robotics, industrial automation, and temperature regulation.
The key benefit of a PID controller is its ability to provide smooth, stable control by responding to the current error, the accumulation of past errors, and the rate of change of the error, ensuring that the system reaches and maintains the desired setpoint efficiently.
Continuous-Time PID Controller
The PID controller adjusts the output of a system to achieve a desired setpoint by minimizing the error between the desired and actual outputs based on three different types of corrections: proportional, integral, and derivative, each weighted by tunable parameters \( K_p \), \( K_i \), and \( K_d \), respectively. These weights allow the user to fine-tune the controller's response to the error.
\[ u(t) = K_p \cdot e(t) + K_i \cdot \int_{0}^{t} e(\tau) d\tau + K_d \cdot \frac{d e(t)}{dt} \]
- \( K_p \) (proportional gain) scales the control effort based on the current error \( e(t) \).
- \( K_i \) (integral gain) scales the accumulated error over time, addressing steady-state errors.
- \( K_d \) (derivative gain) scales the rate of change of the error, providing damping to prevent overshooting.
Discrete-Time PID Controller
In discrete-time systems, where control actions are computed at discrete and regular intervals (sampling period \( \Delta t \)), the PID formula is discretized as follows:
Proportional Term: \[ P[k] = K_p \cdot e[k] \] where \( e[k] \) is the error at the \( k \)th time step.
Integral Term: The integral is approximated by summing the errors over time: \[ I[k] = K_i \cdot\sum_{i=1}^{k} e[i] \cdot \Delta t \] where \( \Delta t \) is the time between steps.
Derivative Term: The derivative is the change in error by the change in time: \[ D[k] = K_d \cdot \frac{e[k] - e[k-1]}{\Delta t} \]
The discrete control signal at step \( k \) is thus:
\[ u[k] = K_p \cdot e[k] + K_i \sum_{i=1}^{k} e[i] \cdot \Delta t + K_d \cdot \frac{e[k] - e[k-1]}{\Delta t} \]
Since \( \Delta t \) is constant in most cases, it can be incorporated into the coefficients for simplification:
\[ u[k] = K_p \cdot e[k] + K_i' \cdot \sum_{i=1}^{k} e[i] + K_d' \cdot (e[k] - e[k-1]) \]
where \( K_i' = K_i \cdot \Delta t \) and \( K_d' = \frac{K_d}{\Delta t} \) are tuning parameters that include constant \(\Delta t\).
Tuning the PID Parameters
To effectively tune a PID controller:
- Start with \( K_p \): Set \( K_p = 1 \) with \( K_i = 0 \) and \( K_d = 0 \). Adjust \( K_p \) to achieve a basic level of control where the system responds to errors.
- Introduce \( K_d \): Increase \( K_d \) to reduce oscillations and stabilize the system.
- Adjust \( K_i \): Finally, increase \( K_i \) to eliminate any steady-state errors.
Example: Thermostat
Suppose a thermostat is set to maintain a room at \( r[k] = 22^\circ \text{C} \). At time step \( k \), the current temperature is \( y[k] = 18^\circ \text{C} \). The error at this time step is:
\[ e[k] = r[k] - y[k] = 22 - 18 = 4^\circ \text{C} \]
The control input for the heater, \( u[k] \), is calculated using:
\[ u[k] = K_p \cdot e[k] + K_i \cdot \sum_{i=1}^{k} e[i] \cdot \Delta t + K_d \cdot \frac{e[k] - e[k-1]}{\Delta t} \]
where:
- \( r[k] = 22 \) is the desired temperature.
- \( y[k] = 18 \) is the current temperature at time step \( k \).
- \( e[k] = 4 \) is the error.
- \( u[k] \) is the heater control signal.
Example: Car Steering
Consider a car that needs to follow a line set at \( r[k] = 5 \) units from a reference point. If the car’s current position is \( y[k] = 3 \) units at time step \( k \), the error is:
\[ e[k] = r[k] - y[k] = 5 - 3 = 2 \text{ units} \]
The steering angle adjustment, \( u[k] \), is calculated as:
\[ u[k] = K_p \cdot e[k] + K_i \cdot \sum_{i=1}^{k} e[i] \cdot \Delta t + K_d \cdot \frac{e[k] - e[k-1]}{\Delta t} \]
where:
- \( r[k] = 5 \) is the desired position relative to the reference point.
- \( y[k] = 3 \) is the current position of the car.
- \( e[k] = 2 \) is the error.
- \( u[k] \) is the steering angle adjustment.
JavaScript Implementation
// PID parameters
let Kp = 1.0;
let Ki = 0.1;
let Kd = 0.05;
let dt = 0.1; // Sampling period
// State variables
let prevError = 0;
let integral = 0;
while (true) {
// Setpoint and actual value
let setpoint = getGoal(); // Desired value
let actual = getActual(); // Measured value
// Calculate error
let error = setpoint - actual;
// Integral term
integral += error * dt;
// Derivative term
let derivative = (error - prevError) / dt;
// PID output
let output = Kp * error + Ki * integral + Kd * derivative;
console.log(output);
// Update for the next iteration
prevError = error;
}