[Internet of Things] Implementing PID Algorithm in C Language: Principles, Examples and Detailed Code Explanation

PID (Proportional-Integral-Derivative) is a common control algorithm widely used in industrial control systems. This article will introduce the principle of the PID algorithm in detail, and give a specific example and the corresponding C language code implementation.
insert image description here

1. Principle of PID algorithm

The PID algorithm makes the actual value of the system gradually approach the expected value by continuously adjusting the output value. It consists of three parts: Proportional (P), Integral (I) and Derivative (D) .

insert image description here

  1. Proportional control (P) : According to the size of the error, the output value is adjusted according to a certain proportional relationship. Proportional control can respond quickly to changes in the system, but may cause overshoot and oscillation.

  2. Integral control (I) : According to the cumulative value of the error, the output value is adjusted according to a certain proportional relationship. Integral control can eliminate steady-state errors, but may cause the system to respond too slowly and overshoot.

  3. Derivative control (D) : According to the rate of error change, the output value is adjusted according to a certain proportional relationship. Differential control can improve the stability and response speed of the system, but it is sensitive to noise.

The calculation formula of the output value of the PID algorithm is:
output = K p ∗ error + K i ∗ integral + K d ∗ derivative output = Kp * error + Ki * integral + Kd * derivativeoutput=Kperror+I _integral+Kdd er i v a t i v e
Among them, Kp, Ki and Kd are proportional, integral and differential coefficients respectively, error is the current error, integral is the integral value of the error, and derivative is the rate of change of the error.

Two, PID algorithm implementation example

In order to better understand the implementation of the PID algorithm, we take a simple temperature control system as an example.

Suppose we need to control the temperature of a room at 25 degrees Celsius, and the current temperature is 20 degrees Celsius. We can use the PID algorithm to achieve temperature control.

2.1 Initialize PID parameters and variables:

float Kp = 0.5;    // 比例系数
float Ki = 0.2;    // 积分系数
float Kd = 0.1;    // 微分系数

float setpoint = 25.0;    // 期望温度
float temperature = 20.0; // 当前温度

float error = 0.0;        // 误差
float integral = 0.0;     // 误差累积值
float derivative = 0.0;   // 误差变化率

float output = 0.0;       // 输出值

2.2 Calculate PID output:

while (1) {
    
    
    error = setpoint - temperature;                  // 计算误差
    integral += error;                               // 更新误差累积值
    derivative = error - previous_error;             // 计算误差变化率

    output = Kp * error + Ki * integral + Kd * derivative;    // 计算输出值

    previous_error = error;                          // 保存当前误差作为上一次误差

    // 根据输出值进行相应的控制操作,例如调整加热器功率或风扇转速

    // 更新当前温度
    // temperature = ...;
}

2.3 Complete code

#include <stdio.h>

float Kp = 0.5;    // 比例系数
float Ki = 0.2;    // 积分系数
float Kd = 0.1;    // 微分系数

float setpoint = 25.0;    // 期望温度
float temperature = 20.0; // 当前温度

float error = 0.0;        // 误差
float integral = 0.0;     // 误差累积值
float derivative = 0.0;   // 误差变化率
float previous_error = 0.0; // 上一次误差

float output = 0.0;       // 输出值

int main() {
    
    
    while (1) {
    
    
        error = setpoint - temperature;                  // 计算误差
        integral += error;                               // 更新误差累积值
        derivative = error - previous_error;             // 计算误差变化率

        output = Kp * error + Ki * integral + Kd * derivative;    // 计算输出值

        previous_error = error;                          // 保存当前误差作为上一次误差

        // 根据输出值进行相应的控制操作,例如调整加热器功率或风扇转速

        // 更新当前温度
        // temperature = ...;

        printf("Output: %.2f\n", output);    // 打印输出值
    }

    return 0;
}

The main steps in the code are as follows:

  1. Initialize PID parameters and variables.
  2. Into the cycle, constant temperature control.
  3. In the loop, first calculate the current error (desired temperature minus current temperature), then update the error accumulation value and the error rate of change.
  4. The output value is calculated according to the formula of the PID algorithm.
  5. According to the output value, the corresponding control operation is carried out, such as adjusting the heater power or the fan speed.
  6. Update the current temperature (here is just an example, the actual application needs to update the temperature according to the specific situation).
  7. Print out the value.

3. Summary

This paper introduces the principle of PID algorithm in detail, and gives an example of a specific temperature control system and the corresponding C language code implementation. By understanding the working principle and practical application of the PID algorithm, we can better apply the PID algorithm for system control and improve the stability and response speed of the system.

Guess you like

Origin blog.csdn.net/Goforyouqp/article/details/132155511