参考网址:

[P、I、D参数分析及倒立摆心得_一念之间、-CSDN博客_pid 倒立摆](https://blog.csdn.net/tqs_1220/article/details/75249145?ops_request_misc=&request_id=&biz_id=102&utm_term=p表示 i表示 d表示&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-8-75249145.pc_search_em_sort&spm=1018.2226.3001.4187)

PID控制算法的C语言实现_我的博客-CSDN博客_pid算法c程序

PID作用

1
2
3
4
5
P-------作用是提高系统响应速度,过大会出现震荡---------------------------快速性

I-------作用是消除静差----------------------------------------------准确性

D-------作用是抑制震荡----------------------------------------------稳定性/预测性

例程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// pid控制测试
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

// P:响应 I:稳差 D:预判
struct PID_CTRL
{
float kp;
float ki;
float kd;
};


float pos_pid_control(float target, float current, struct PID_CTRL pid)
{
static float err_last;
float out,err_sum=0,err_d,err;
err = target - current;
err_sum += err;
err_d = err - err_last;
out = pid.kp * err + pid.ki * err_sum + pid.kd * err_d;
err_last = err;
return out;
}

float gain_pid_control(float target, float current, struct PID_CTRL pid)
{
static float err_last;
float out=0;
float err = target - current;
out += pid.kp * (err-err_last) + pid.ki * err;
err_last = err;
return out;
}

void test_pos_pid_control()
{
struct PID_CTRL pid{0.5,0.1,0.1};
pid.kp = 0.5;
pid.ki = 0.2;
pid.kd = 0.1;

float target = 10.0;
float current = 0.0;
for(int i = 0; i < 100; i++)
{
current += 0.1;
printf("target: %f, current: %f, out: %f\n", target, current, pos_pid_control(target, current, pid));
}
}

void test_gain_pid_control()
{
struct PID_CTRL pid{0.5,0.1,0.1};
pid.kp = 0.5;
pid.ki = 0.2;
pid.kd = 0.1;

float target = 10.0;
float current = 0.0;
for(int i = 0; i < 100; i++)
{
current += 0.1;
printf("target: %f, current: %f, out: %f\n", target, current, gain_pid_control(target, current, pid));
}
}

int main()
{
test_pos_pid_control();
printf("********************************************\n");
test_gain_pid_control();
return 0;
}