Skip to content

Instantly share code, notes, and snippets.

@tanitanin
Last active June 10, 2019 16:28
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/4328e07e5fa982ccb51d631e62dcde47 to your computer and use it in GitHub Desktop.
Save tanitanin/4328e07e5fa982ccb51d631e62dcde47 to your computer and use it in GitHub Desktop.
#pragma once
#include <integrator.h>
template<typename T>
class PID {
T Kp;
T Ki;
T Kd;
T Tf;
T Ts;
T x;
T y;
DescreteTimeIntegrator<T> IF;
DescreteTimeIntegrator<T> DF;
public:
PID(T kp, T ki, T kd, T tf, T ts, T initial=T()) : Kp(kp), Ki(ki), Kd(kd), Tf(tf), IF(1, ts), DF(1, ts) {
y = initial;
IF.reset(y);
DF.reset(0);
}
T update(T input) {
x = y;
y = Kp * x + Ki * IF.update(x) + Kd / (Tf + DF.update(x));
return y;
}
void reset(T initial=T()) {
y = initial;
IF.reset(initial);
DF.reset(0);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment