Library containing essential automation elements with parameters in continuous Laplace domain, and implemented with fixed sample time. This means that "out" member functions should be called using Ticker object.
Dependents: CurrentMeasurement Zavrsni_rad_NXP_cup HC-SR04 Nucleo_SSD1306_DS1302_ESP8266_AM2320_BME280 ... more
Diff: PT2.cpp
- Revision:
- 0:3dd7aeceee65
diff -r 000000000000 -r 3dd7aeceee65 PT2.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PT2.cpp Thu Jan 22 11:41:02 2015 +0000 @@ -0,0 +1,42 @@ +#include "PT2.h" + +PT2::PT2() { + u = y = u_p1 = y_p1 = u_p2 = y_p2 = 0; + setParameters(1, 1, 0.5, 0.1); +} + +PT2::PT2(double K_, double T_1_, double T_2_, double T_d_) { + u = y = u_p1 = y_p1 = u_p2 = y_p2 = 0; + setParameters(K_, T_1_, T_2_, T_d_); +} + +void PT2::setParameters(double K_, double T_1_, double T_2_, double T_d_) { + if (T_d_ > 0) + T_d = T_d_; + else + T_d = 0.1; + K = K_; + T_1 = T_1_; + T_2 = T_2_; + a = 1 / T_1; + b = 1 / T_2; + c = exp(-a * T_d); + d = exp(-b * T_d); + b_1 = -K * ( (a - b)*(c + d) + b*(1 + d) - a*(1 + c) ) / (a - b); + b_2 = K * ( (a - b)*c*d + b*d - a*c ) / (a - b); + a_1 = -(c + d); + a_2 = c*d; +} + +double PT2::out() { + y = -a_1 * y_p1 - a_2 * y_p2 + b_1 * u_p1 + b_2 * u_p2; + y_p2 = y_p1; + y_p1 = y; + u_p2 = u_p1; + u_p1 = u; + return y; +} + +void PT2::in(double u_) { + u = u_; +} \ No newline at end of file