Last active
June 11, 2019 12:10
-
-
Save tanitanin/c31460baca320b46798dc0c5b80059ba to your computer and use it in GitHub Desktop.
Discrete-Time PID https://jp.mathworks.com/help/control/ug/discrete-time-proportional-integral-derivative-pid-controller.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <integrator.h> | |
template<typename T> | |
class PID2 { | |
T Kp; | |
T Ki; | |
T Kd; | |
T Tf; | |
T Ts; | |
T b; | |
T c; | |
DescreteTimeIntegrator<T> IF; | |
DescreteTimeIntegrator<T> DF; | |
public: | |
PID2(T kp, T ki, T kd, T tf, T b, T c, T ts, T reference=T(), T initial=T()) : Kp(kp), Ki(ki), Kd(kd), Tf(tf), b(b), c(c), IF(1, ts), DF(1, ts) { | |
IF.reset(reference - initial); | |
DF.reset(c * reference - initial); | |
} | |
T update(T reference, T input) { | |
auto u = Kp * (b * reference - input) + Ki * IF.update(reference - input) + Kd / (Tf + DF.update(c * reference - input)); | |
return u; | |
} | |
void reset(T reference=T(), T initial=T()) { | |
IF.reset(reference - initial); | |
DF.reset(c * reference - initial); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment