-
Notifications
You must be signed in to change notification settings - Fork 0
/
PIDF.cpp
36 lines (29 loc) · 909 Bytes
/
PIDF.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
#include <Arduino.h>
#include "PIDF.h"
PIDF::PIDF(float Kp, float Ki, float Kd, float Kb, float saturation, Filter& filt, float frequency) : Kp(Kp), Ki(Ki), Kd(Kd), Kb(Kb), saturation(saturation), frequency(frequency), filt(filt) {
//filt = f(Cf, frequency);
reset();
}
void PIDF::reset() {
//reset integral
yd = 0;
xd = 0;
xdf = 0;
integral = 0;
filt.frequency = frequency;
filt.reset();
}
float PIDF::process(float sample) {
float constrained = constrain(yd, -saturation, saturation);
float backcalc = constrained - yd;
//backcalc * Kb +
integral += (Kb * backcalc + Ki * sample) / frequency;
//integral = constrain(integral, -saturation, saturation);
float sample_filt = filt.process(sample) * frequency;
float derivative = (sample_filt - xdf) * Kd;
float out = sample * Kp + integral + derivative;
xd = sample;
xdf = sample_filt;
yd = out;
return out;
}