Pascal Keller
/
GRT_VC_PIDT1
Regler von Kellep15
PID_Cntrl.cpp@2:394782101261, 2019-05-16 (annotated)
- Committer:
- kellep15
- Date:
- Thu May 16 15:34:29 2019 +0000
- Revision:
- 2:394782101261
- Parent:
- 1:92f175969d90
rEGLER TUT NICHT
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pmic | 1:92f175969d90 | 1 | /* |
pmic | 1:92f175969d90 | 2 | PID-T1 Controller class |
pmic | 1:92f175969d90 | 3 | |
pmic | 1:92f175969d90 | 4 | 1 s |
pmic | 1:92f175969d90 | 5 | G(s) = Kp + Ki --- + Kd --------- |
pmic | 1:92f175969d90 | 6 | s T_f*s + p |
pmic | 1:92f175969d90 | 7 | |
pmic | 1:92f175969d90 | 8 | Eigther reseting the Nucleo via the black button or save a new software on |
pmic | 1:92f175969d90 | 9 | the Nucleo sets the analog output to zero. Zero is equal to -4 Ampere!!! |
pmic | 1:92f175969d90 | 10 | Therefor: NEVER !!! reset or save a new software while the VC is powered on |
pmic | 1:92f175969d90 | 11 | (the green button on the VC is glowing green) |
pmic | 1:92f175969d90 | 12 | |
altb2 | 0:05dd1de8cc3f | 13 | */ |
altb2 | 0:05dd1de8cc3f | 14 | |
altb2 | 0:05dd1de8cc3f | 15 | #include "PID_Cntrl.h" |
altb2 | 0:05dd1de8cc3f | 16 | using namespace std; |
altb2 | 0:05dd1de8cc3f | 17 | |
kellep15 | 2:394782101261 | 18 | PID_Cntrl::PID_Cntrl(float Kp, float Ki, float Kd, float Tf, float Ts, float yMin, float yMax) |
altb2 | 0:05dd1de8cc3f | 19 | { |
pmic | 1:92f175969d90 | 20 | // link member variables |
kellep15 | 2:394782101261 | 21 | this->Kp = Kp; |
kellep15 | 2:394782101261 | 22 | this->Ki = Ki; |
kellep15 | 2:394782101261 | 23 | this->Tf = Tf; |
kellep15 | 2:394782101261 | 24 | this->Ts = Ts; |
kellep15 | 2:394782101261 | 25 | this->yMin = yMin; |
kellep15 | 2:394782101261 | 26 | this->yMax = yMax; |
pmic | 1:92f175969d90 | 27 | |
altb2 | 0:05dd1de8cc3f | 28 | reset(0.0f); |
altb2 | 0:05dd1de8cc3f | 29 | } |
altb2 | 0:05dd1de8cc3f | 30 | |
altb2 | 0:05dd1de8cc3f | 31 | PID_Cntrl::~PID_Cntrl() {} |
altb2 | 0:05dd1de8cc3f | 32 | |
altb2 | 0:05dd1de8cc3f | 33 | void PID_Cntrl::reset(float initValue) |
altb2 | 0:05dd1de8cc3f | 34 | { |
pmic | 1:92f175969d90 | 35 | |
pmic | 1:92f175969d90 | 36 | // implement controller reset |
kellep15 | 2:394782101261 | 37 | e_old = 0.0; |
kellep15 | 2:394782101261 | 38 | y_Dold = 0.0; |
kellep15 | 2:394782101261 | 39 | y_I = initValue; |
altb2 | 0:05dd1de8cc3f | 40 | } |
altb2 | 0:05dd1de8cc3f | 41 | |
kellep15 | 2:394782101261 | 42 | float PID_Cntrl::update(float e) |
altb2 | 0:05dd1de8cc3f | 43 | { |
pmic | 1:92f175969d90 | 44 | |
pmic | 1:92f175969d90 | 45 | // controller update function |
pmic | 1:92f175969d90 | 46 | |
pmic | 1:92f175969d90 | 47 | // calculate uI |
kellep15 | 2:394782101261 | 48 | y_I = y_I + Ki*Ts*e; |
altb2 | 0:05dd1de8cc3f | 49 | |
pmic | 1:92f175969d90 | 50 | // saturate uI, uMin <= uI <= uMax (anti-windup for the integrator part) |
kellep15 | 2:394782101261 | 51 | if(y_I > yMax) |
kellep15 | 2:394782101261 | 52 | y_I = yMax; |
kellep15 | 2:394782101261 | 53 | else if(y_I < yMin) |
kellep15 | 2:394782101261 | 54 | y_I = yMin; |
pmic | 1:92f175969d90 | 55 | |
pmic | 1:92f175969d90 | 56 | // calculate uD |
kellep15 | 2:394782101261 | 57 | y_Dold = (1.0f - Ts/Tf)*y_Dold + Kd / Tf * (e-e_old); |
kellep15 | 2:394782101261 | 58 | |
kellep15 | 2:394782101261 | 59 | float y = Kp * e + y_I + y_Dold; |
kellep15 | 2:394782101261 | 60 | |
kellep15 | 2:394782101261 | 61 | // update signal storage |
kellep15 | 2:394782101261 | 62 | e_old = e; |
kellep15 | 2:394782101261 | 63 | |
pmic | 1:92f175969d90 | 64 | |
pmic | 1:92f175969d90 | 65 | // calculate u |
pmic | 1:92f175969d90 | 66 | // ??? |
pmic | 1:92f175969d90 | 67 | |
pmic | 1:92f175969d90 | 68 | // saturate u, uMin <= u <= uMax |
kellep15 | 2:394782101261 | 69 | if(y > yMax) |
kellep15 | 2:394782101261 | 70 | y = yMax; |
kellep15 | 2:394782101261 | 71 | else if(y < yMin) |
kellep15 | 2:394782101261 | 72 | y = yMin; |
pmic | 1:92f175969d90 | 73 | |
pmic | 1:92f175969d90 | 74 | // update signal storage |
pmic | 1:92f175969d90 | 75 | // ??? |
pmic | 1:92f175969d90 | 76 | |
kellep15 | 2:394782101261 | 77 | return y; |
altb2 | 0:05dd1de8cc3f | 78 | } |
altb2 | 0:05dd1de8cc3f | 79 |