3.5 KiB
Introduction
The PID algorithm, which stands for Proportional-Integral-Derivative algorithm, is a feedback control algorithm widely used in industrial control systems. It compares the error between the expected value and the actual value, and then adjusts the output of the controller based on the proportion (P), integral (I), and derivative (D) of the error, so as to make the output of the system closer to the expected value.
The basic form of the PID algorithm is as follows:
u(t) = K_p \times e(t) + K_i \times \int e(t)dt + K_d \times \frac{de(t)}{dt}
Here, u(t) is the output of the controller, e(t) is the error between the expected value and the actual value, and K_p, K_i, and K_d are the proportional, integral, and derivative coefficients respectively. These coefficients need to be adjusted according to the specific system to achieve the best control effect.
The advantages of the PID algorithm lie in its simplicity and ease of use, wide range of applicability, and the ability to adapt to different system characteristics by adjusting parameters. However, for some complex nonlinear systems, the PID algorithm may not be able to achieve the ideal control effect.
Interface
void pid_init(PIDC *pid);
Before performing the PID algorithm calculation, it is necessary to initialize the PID algorithm control structure.
double pid_compute(PIDC *pid);
This function is used to perform the actual PID algorithm calculation after the relevant structure has been initialized.
Test
static void test_sim(void)
{
PIDC pid;
pid_init(&pid);
pid.kp = 2;
pid.ki = 0.1;
pid.kd = 0.05;
pid.point = 100.0;
/* Simulate the process, assuming that the value of each cycle process increases by 5 */
for (int i = 0; i < 20; i++)
{
pid.process += 5;
pid_compute(&pid);
printf("Process Value: %.2f, PID Output: %.2f\n", pid.process, pid.output);
}
}
In this test function:
- First, a
PIDCstructure variablepidis defined. - Then, the
pidstructure is initialized using thepid_initfunction. - After that, specific values are assigned to the coefficients
kp,ki,kdand the targetpointof thePIDalgorithm within thepidstructure. - In the loop, it simulates a process where the
processvalue increases by 5 in each cycle. After each update of theprocessvalue, thepid_computefunction is called to calculate thePIDoutput based on the current state, and then the currentProcess ValueandPID Outputare printed out.
Results:
Process Value: 5.00, PID Output: 204.25
Process Value: 10.00, PID Output: 198.25
Process Value: 15.00, PID Output: 196.75
Process Value: 20.00, PID Output: 194.75
Process Value: 25.00, PID Output: 192.25
Process Value: 30.00, PID Output: 189.25
Process Value: 35.00, PID Output: 185.75
Process Value: 40.00, PID Output: 181.75
Process Value: 45.00, PID Output: 177.25
Process Value: 50.00, PID Output: 172.25
Process Value: 55.00, PID Output: 166.75
Process Value: 60.00, PID Output: 160.75
Process Value: 65.00, PID Output: 154.25
Process Value: 70.00, PID Output: 147.25
Process Value: 75.00, PID Output: 139.75
Process Value: 80.00, PID Output: 131.75
Process Value: 85.00, PID Output: 123.25
Process Value: 90.00, PID Output: 114.25
Process Value: 95.00, PID Output: 104.75
Process Value: 100.00, PID Output: 94.75
From the results, we can observe how the PID output changes as the Process Value gradually approaches the target point value of 100.0 under the influence of the set PID coefficients.