-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPID.cpp
More file actions
72 lines (54 loc) · 1.57 KB
/
PID.cpp
File metadata and controls
72 lines (54 loc) · 1.57 KB
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
#include "PID.h"
PID::PID() {
this->I = 0;
this->D = 0;
this->prev_error = 0;
this->error_d1 = 0;
this->error_d2 = 0;
}
PID::~PID() = default;
void PID::set_gains(double kp, double ki, double kd) {
this->kp = kp;
this->ki = ki;
this->kd = kd;
}
void PID::set_input_range(double up_limit, double low_limit) {
this->up_limit = up_limit;
this->low_limit = low_limit;
}
double PID::saturation_check(double output) {
if (output > up_limit) return up_limit;
else if (output < low_limit) return low_limit;
else return output;
}
double PID::integrator_anti_windup(double error) {
// set max integral bounds
if (ki > 0.0) double I_max = 0.30 * up_limit; // 30% of max output
else double I_max = 0;
// use appropriate integration
// returns // appropriate error using anti-windup for integral
if ((I > I_max) && (error > 0)) return 0; // stop integration
else if ((I < -I_max) && (error < 0)) return 0; // stop
else return error;
}
double PID::calculate(double y_d, double y) {
double error = y_d - y;
this->I += integrator_anti_windup(error) * dt;
this->D = (error - prev_error) / dt;
this->prev_error = error;
// unsaturated output
output = kp * error + ki * I + kd * D;
return saturation_check(output);
}
double PID::calculate_digital(double y_d, double y) {
// current error
double error = y_d - y;
K1 = kp + ki + kd;
K2 = -kp - 2 * kd;
// unsaturated output
output = y_d1 + K1 * error + K2 * error_d1 + kd * error_d2;
this->error_d2 = error_d1;
this->error_d1 = error;
this->y_d1 = saturation_check(output);
return y_d1; // return saturated output
}