forked from chriskonstad/OpenPID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qpid.cpp
271 lines (220 loc) · 4.9 KB
/
qpid.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "qpid.h"
QPID::QPID(QObject *parent) :
QObject(parent)
{
timeChecker = new QTime; //create QTime to find deltaTime
//Set to default values
pAlgorithm = false;
piAlgorithm = false;
pidAlgorithm = false;
pdAlgorithm = false;
kP = 0;
kI = 0;
kD = 0;
outputMin = 0;
outputMax = 100;
integral = 0;
derivative = 0;
deltaTime = 0;
direction = true; //true = direct; false = indirect
previousError = 0; //set previous error to a stable value
error = 0;
setSetPoint(0); //set to default (zero) value
timeChecker->start(); //start the timer
}
void QPID::configP(double kp, double outMin, double outMax)
{
pAlgorithm = true;
piAlgorithm = false;
pidAlgorithm = false;
pdAlgorithm = false;
kP = kp;
//Invert?
if(!direction) //if reverse, then invert
{
kP = -kP;
}
outputMin = outMin;
outputMax = outMax;
integral = 0;
}
void QPID::configPI(double kp, double ki, double outMin, double outMax)
{
pAlgorithm = false;
piAlgorithm = true;
pidAlgorithm = false;
pdAlgorithm = false;
kP = kp;
kI = ki;
//Invert?
if(!direction) //if reverse, then invert
{
kP = -kP;
kI = -kI;
}
outputMin = outMin;
outputMax = outMax;
integral = 0;
}
void QPID::configPID(double kp, double ki, double kd, double outMin, double outMax)
{
pAlgorithm = false;
piAlgorithm = false;
pidAlgorithm = true;
pdAlgorithm = false;
kP = kp;
kI = ki;
kD = kd;
//Invert?
if(!direction) //if reverse, then invert
{
kP = -kP;
kI = -kI;
kD = -kD;
}
outputMin = outMin;
outputMax = outMax;
integral = 0;
}
void QPID::configPD(double kp, double kd, double outMin, double outMax)
{
pAlgorithm = false;
piAlgorithm = false;
pidAlgorithm = false;
pdAlgorithm = true;
kP = kp;
kD = kd;
//Invert?
if(!direction) //if reverse, then swap
{
kP = -kP;
kD = -kD;
}
outputMin = outMin;
outputMax = outMax;
integral = 0;
}
void QPID::setDirection(bool direct)
{
direction = direct;
}
double QPID::calculate(double in)
{
double _input = in;
double result;
deltaTime = timeChecker->restart(); //restart and get change of time
//deltaTime = deltaTime / 1000; //convert to seconds
error = setPoint - _input; //find error
//emit signals based on error value
if(error > 0)
{
emit errorPositive();
}
else if(error < 0)
{
emit errorNegative();
}
integral = integral + (error*deltaTime); //find integral
if(error == 0) //reset integral value so that it doesn't overshoot too much
{
integral = 0;
emit errorZero(); //emit signal if there is no error
}
derivative = (error - previousError)/deltaTime; //find derivative
if(error != previousError) //if the error has changed
{
emit errorChanged();
}
previousError = error; //set the previous error to current error
if(pAlgorithm) //if only Proportional
{
result = (kP*error);
//Make output value within output range
if(result > outputMax)
{
result = outputMax;
}
if(result < outputMin)
{
result = outputMin;
}
return result;
}
if(piAlgorithm) //if Proportional-Integral
{
result = (kP*error) + (kI*integral);
//Make output value within output range
if(result > outputMax)
{
result = outputMax;
}
if(result < outputMin)
{
result = outputMin;
}
return result;
}
if(pidAlgorithm) //if Proportional-Integral-Derivative
{
result = (kP*error) + (kI*integral) + (kD*derivative);
//Make output values within output range
if(result > outputMax)
{
result = outputMax;
}
if(result < outputMin)
{
result = outputMin;
}
return result;
}
if(pdAlgorithm) //if Proportional-Derivative
{
result = (kP*error) + (kD*derivative);
//Make output values within output range
if(result > outputMax)
{
result = outputMax;
}
if(result < outputMin)
{
result = outputMin;
}
return result;
}
}
void QPID::setSetPoint(double set)
{
setPoint = set;
}
bool QPID::getDirection()
{
return direction;
}
double QPID::getSetPoint()
{
return setPoint;
}
int QPID::getType()
{
if(pAlgorithm)
{
return 0;
}
else if(piAlgorithm)
{
return 1;
}
else if(pidAlgorithm)
{
return 2;
}
else if(pdAlgorithm)
{
return 3;
}
else
{
return 4; //if none are set (return error code)
}
}