-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPID_controller.cpp
157 lines (118 loc) · 3.04 KB
/
PID_controller.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "PID_controller.h"
PIDController::PIDController(){
_Kp = _Ki = _Kd= 0;
_integral_min = _output_min = -1000;
_integral_max = _output_max = 1000;
_acceptable_error = 0.1;
_frequency = 1;
reset();
}
PIDController::~PIDController(){
}
void PIDController::setKp(double Kp){
_Kp = Kp;
}
void PIDController::setKi(double Ki){
_Ki = Ki;
}
void PIDController::setKd(double Kd){
_Kd = Kd;
}
void PIDController::setFrequency(double frequency){
_frequency = frequency;
}
void PIDController::setAcceptableError(double acceptable_error){
_acceptable_error = acceptable_error;
}
double PIDController::getKp(){
return _Kp;
}
double PIDController::getKi(){
return _Ki;
}
double PIDController::getKd(){
return _Kd;
}
double PIDController::getAcceptableError(){
return _acceptable_error;
}
double PIDController::getFrequency(){
return _frequency;
}
void PIDController::setOutputMin(double output_min){
_output_min = output_min;
}
void PIDController::setOutputMax(double output_max){
_output_max = output_max;
}
void PIDController::setIntegralMin(double integral_min){
_integral_min = integral_min;
}
void PIDController::setIntegralMax(double integral_max){
_integral_max = integral_max;
}
double PIDController::getOutputMin(){
return _output_min;
}
double PIDController::getOutputMax(){
return _output_max;
}
double PIDController::getIntegralMin(){
return _integral_min;
}
double PIDController::getIntegralMax(){
return _integral_max;
}
void PIDController::setTargetValue(double target_value){
_target_value = target_value;
}
double PIDController::getTargetValue(){
return _target_value;
}
void PIDController::updateOutput(double current_value,double rate_of_change,double time_difference){
double error, p, i, d;
error = _target_value - current_value;
if ( ( error >= 0 ) && ( error <= _acceptable_error ) )
{
error = 0;
}
else if (( error < 0 ) && ( error >= _acceptable_error ))
{
error = 0;
}
p = _Kp * error;
i += _Ki * error* time_difference;
i = limitToRange(i,_integral_min,_integral_max);
_integrated_error += i;
d = _Kd * rate_of_change;
_output = p + i + d;
_output = limitToRange(_output,_output_min,_output_max);
}
void PIDController::updateOutput(double current_value,double rate_of_change,double time_difference,double target_value){
setTargetValue(target_value);
updateOutput(current_value,rate_of_change,time_difference);
}
void PIDController::updateOutput(double current_value,double rate_of_change){
updateOutput(current_value,rate_of_change,1/_frequency);
}
double PIDController::getOutput(){
return _output;
}
void PIDController::reset(){
_integrated_error = 0;
_output = 0;
}
double PIDController::limitToRange(double value, double minimum, double maximum){
if (value > maximum)
{
return maximum;
}
else if (value < minimum)
{
return minimum;
}
else
{
return value;
}
}