Skip to content

Instantly share code, notes, and snippets.

@tanitanin
Last active June 11, 2019 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanitanin/c31460baca320b46798dc0c5b80059ba to your computer and use it in GitHub Desktop.
Save tanitanin/c31460baca320b46798dc0c5b80059ba to your computer and use it in GitHub Desktop.
#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