Template for group 4
Dependencies: mbed
Fork of RT2_P3_students_G4 by
PI_Cntrl.cpp@6:8ed679044a72, 2018-04-17 (annotated)
- Committer:
- altb
- Date:
- Tue Apr 17 11:47:35 2018 +0000
- Revision:
- 6:8ed679044a72
- Parent:
- 2:769ce5f06d3e
- Child:
- 11:67af6d24c588
RT2_P3_W2, 2nd session
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
altb | 6:8ed679044a72 | 1 | /* |
altb | 6:8ed679044a72 | 2 | PI Controller class with anti windup reset in biquad transposed direct form 2 |
altb | 6:8ed679044a72 | 3 | see e.g.: https://www.dsprelated.com/freebooks/filters/Four_Direct_Forms.html |
altb | 6:8ed679044a72 | 4 | everything is calculated in double |
altb | 6:8ed679044a72 | 5 | |
altb | 6:8ed679044a72 | 6 | Tn*s + 1 |
altb | 6:8ed679044a72 | 7 | G(s) = Kp ------------- with s ~ (1 - z^-1)/Ts |
altb | 6:8ed679044a72 | 8 | Ts*s |
altb | 6:8ed679044a72 | 9 | */ |
altb | 6:8ed679044a72 | 10 | |
altb | 6:8ed679044a72 | 11 | #include "PI_Cntrl.h" |
altb | 0:78ca29b4c49e | 12 | |
altb | 0:78ca29b4c49e | 13 | using namespace std; |
altb | 0:78ca29b4c49e | 14 | |
altb | 6:8ed679044a72 | 15 | PI_Cntrl::PI_Cntrl(float Kp, float Tn, float Ts) |
altb | 6:8ed679044a72 | 16 | { |
altb | 6:8ed679044a72 | 17 | setCoefficients(Kp, Tn, Ts); |
altb | 6:8ed679044a72 | 18 | uMax = 10000000000.0; |
altb | 6:8ed679044a72 | 19 | uMin = -uMax; |
altb | 6:8ed679044a72 | 20 | reset(0.0f); |
altb | 6:8ed679044a72 | 21 | } |
altb | 0:78ca29b4c49e | 22 | |
altb | 6:8ed679044a72 | 23 | PI_Cntrl::PI_Cntrl(float Kp, float Tn, float Ts, float uMax) |
altb | 6:8ed679044a72 | 24 | { |
altb | 6:8ed679044a72 | 25 | setCoefficients(Kp, Tn, Ts); |
altb | 6:8ed679044a72 | 26 | this->uMax = (double)uMax; |
altb | 6:8ed679044a72 | 27 | uMin = -(double)uMax; |
altb | 6:8ed679044a72 | 28 | reset(0.0f); |
altb | 6:8ed679044a72 | 29 | } |
altb | 0:78ca29b4c49e | 30 | |
altb | 6:8ed679044a72 | 31 | PI_Cntrl::PI_Cntrl(float Kp, float Tn, float Ts, float uMax, float uMin) |
altb | 6:8ed679044a72 | 32 | { |
altb | 6:8ed679044a72 | 33 | setCoefficients(Kp, Tn, Ts); |
altb | 6:8ed679044a72 | 34 | this->uMax = (double)uMax; |
altb | 6:8ed679044a72 | 35 | this->uMin = (double)uMin; |
altb | 6:8ed679044a72 | 36 | reset(0.0f); |
altb | 0:78ca29b4c49e | 37 | } |
altb | 0:78ca29b4c49e | 38 | |
altb | 6:8ed679044a72 | 39 | PI_Cntrl::~PI_Cntrl() {} |
altb | 6:8ed679044a72 | 40 | |
altb | 6:8ed679044a72 | 41 | void PI_Cntrl::reset(float initValue) |
altb | 6:8ed679044a72 | 42 | { |
altb | 6:8ed679044a72 | 43 | s = (double)initValue; |
altb | 6:8ed679044a72 | 44 | } |
altb | 6:8ed679044a72 | 45 | |
altb | 6:8ed679044a72 | 46 | void PI_Cntrl::setCoefficients(float Kp, float Tn, float Ts) |
altb | 6:8ed679044a72 | 47 | { |
altb | 6:8ed679044a72 | 48 | b0 = (double)Kp*(1.0 + (double)Ts/(double)Tn); |
altb | 6:8ed679044a72 | 49 | b1 = -(double)Kp; |
altb | 6:8ed679044a72 | 50 | b2 = (double)Ts/(double)Tn; |
altb | 6:8ed679044a72 | 51 | } |
altb | 6:8ed679044a72 | 52 | |
altb | 6:8ed679044a72 | 53 | float PI_Cntrl::doStep(double e) |
altb | 6:8ed679044a72 | 54 | { |
altb | 6:8ed679044a72 | 55 | double u = b0*e + s; // unconstrained output |
altb | 6:8ed679044a72 | 56 | double uc = u; // constrained output |
altb | 6:8ed679044a72 | 57 | if(u > uMax) uc = uMax; |
altb | 6:8ed679044a72 | 58 | else if(u < uMin) uc = uMin; |
altb | 6:8ed679044a72 | 59 | s = b1*e + u - b2*(u - uc); |
altb | 6:8ed679044a72 | 60 | return (float)uc; |
altb | 6:8ed679044a72 | 61 | } |