raw control theory

Introduction to PID Controllers

Robert Eisele

This article introduces Proportional-Integral-Derivative (PID) controllers, likening them to a driver adjusting a car's steering to stay on course. By balancing proportional, integral, and derivative actions, PID controllers help systems correct errors and maintain desired performance. This guide simplifies PID concepts and applications, making it easier to understand how these controllers keep systems and stable.

Read the story

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} \]

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:

  1. Proportional Term: \[ P[k] = K_p \cdot e[k] \] where \( e[k] \) is the error at the \( k \)th time step.

  2. 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.

  3. 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:

  1. 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.
  2. Introduce \( K_d \): Increase \( K_d \) to reduce oscillations and stabilize the system.
  3. 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:

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:

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;
}