-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPID.ino
62 lines (55 loc) · 1.42 KB
/
PID.ino
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
/***************角度环**************/
void angle_PID_compute(void)
{
double angle_error = angle - Setpoint;
angle_output = kp * angle_error + kd * angle_dot;
}
/***************速度环**************/
void speed_PID_compute(void)
{
double speed_error, speed_last;
int L = count_L; count_L = 0;
int R = count_R; count_R = 0;
if (Setpoints == 0 && e == 0)
{
if (speed_integral > 40) speed_integral -= 4;
if (speed_integral < -40) speed_integral += 4;
}
if (Setpoints > 0 && speed_n <= Setpoints)
{
speed_n += 5;
}
if (Setpoints < 0 && speed_n >= Setpoints)
{
speed_n -= 5;
}
if (Setpoints == 0)
{
if (speed_n > 0) speed_n -= 15;
if (speed_n < 0) speed_n += 15;
if (abs(speed_n) < 15) speed_n = 0;
}
speed_last = L + R;
speed_error *= 0.7;
speed_error += speed_last * 0.3; //一阶低通滤波
speed_integral += speed_error;
speed_integral -= speed_n; //控制前进后退
speed_output = sp * speed_error + si * speed_integral;
if (e == 0)
{
if (speed_integral > 100) speed_integral = 100;
if (speed_integral < -100) speed_integral = -100;
}
}
/***************转向环**************/
void turn_PID_compute(void)
{
if (turn != 0.035 && Setpoints != 0 && flag == 0)
{
if (turn < 0) turn = -4;
else turn = 4;
}
turn_integral += gyroZ;
turn_i += turn; //控制左右转向
turn_output = P_turn * gyroZ + I_turn * turn_integral - turn_i ;
}