-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPID.cpp
87 lines (67 loc) · 1.73 KB
/
PID.cpp
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
77
78
79
80
81
82
83
84
85
86
87
#include "PID.hpp"
#include <iostream>
#include "utils.h"
using namespace std;
#define DEBUG_PID_OUTPUT 0
void PID::clear(void)
{
set_point = 0.0;
p_term = 0.0;
i_term = 0.0;
d_term = 0.0;
last_error = 0.0;
last_feedback = 0.0;
windup_guard = 5.0;
output = 0.0;
}
PID::PID(float (*error_func)(float desired, float actual),
float kp_new, float ki_new, float kd_new)
{
current_time = elapsed_time_in_s();
last_time = current_time;
this->clear();
this->calculate_error = error_func;
this->setKp(kp_new);
this->setKi(ki_new);
this->setKd(kd_new);
}
void PID::update(float desired_value, float feedback_value)
{
current_time = elapsed_time_in_s();
delta_time = current_time - last_time;
set_point = desired_value;
error = (*calculate_error)(set_point, feedback_value);
delta_feedback = (*calculate_error)(last_feedback, feedback_value);
p_term = Kp * error;
i_term += Ki * error * delta_time;
if (i_term < -windup_guard)
i_term = -windup_guard;
else if (i_term > windup_guard)
i_term = windup_guard;
d_term = 0.0;
if (delta_time > 0)
d_term = -Kd * (delta_feedback / delta_time);
last_time = current_time;
last_error = error;
last_feedback = feedback_value;
output = p_term + i_term + d_term;
#if DEBUG_PID_OUTPUT
cout << p_term << " " << i_term << " " << d_term << " " << error;
#endif
}
void PID::setKp(float proportional_gain)
{
Kp = proportional_gain;
}
void PID::setKi(float integral_gain)
{
Ki = integral_gain;
}
void PID::setKd(float derivative_gain)
{
Kd = derivative_gain;
}
void PID::setWindup(float windup)
{
windup_guard = windup;
}