Humidistat
Arduino firmware for a humidistat (humidity controller)
Loading...
Searching...
No Matches
PID.cpp
Go to the documentation of this file.
1#include "PID.h"
2
3PID::PID(const double *pv, double *cv, const double *sp, double Kp, double Ki, double Kd, double Kf, uint16_t dt,
4 double cvMin, double cvMax, double a)
5 : pv(*pv), cv(*cv), sp(*sp), a(a), cvMin(cvMin), cvMax(cvMax) {
6 setGains(Kp, Ki, Kd, Kf, dt);
7}
8
9void PID::init() {
10 if(Ki != 0) {
11 integral = (cv - Kf * sp) / Ki;
12 }
13 lastPv = pv;
14 lastE = sp - pv;
15}
16
18 // Terminate if not in auto
19 if (!inAuto)
20 return false;
21
22 // Proportional error
23 double e = sp - pv;
24 // Derivative (Derivative on Measurement)
25 double dPv = (1-a) * lastDPV + a * (pv - lastPv); // Backwards difference, EWA smoothed
26 // Integral error
27 double delta = (lastE + e) / 2; // Trapezoidal integration
28 if ((cv < cvMax || delta < 0) && (cv > cvMin || delta > 0)) // Anti-windup through conditional integration
29 integral += delta;
30
31 pTerm = Kp * e;
32 iTerm = Ki * integral;
33 dTerm = -Kd * dPv;
34 fTerm = Kf * sp;
35
36 cv = clip(pTerm + iTerm + dTerm + fTerm);
37
38 lastPv = pv;
39 lastE = e;
40 lastDPV = dPv;
41 return true;
42}
43
44double PID::clip(double value) const {
45 if (value > cvMax)
46 return cvMax;
47 if (value < cvMin)
48 return cvMin;
49 return value;
50}
51
52void PID::setAuto(bool inAuto) {
53 // When going from manual to auto, run init() for bumpless transfer
54 if (inAuto && !this->inAuto)
55 init();
56 this->inAuto = inAuto;
57}
58
59void PID::setGains(double Kp, double Ki, double Kd, double Kf, uint16_t dt) {
60 this->dt = dt;
61 this->Kp = Kp;
62
63 // The timestep is constant, so we include it in Ki and Kd for convenience
64 this->Ki = Ki * dt / 1000;
65 this->Kd = Kd / (static_cast<double>(dt) / 1000);
66
67 this->Kf = Kf;
68
69 init();
70}
double & cv
Control variable.
Definition PID.h:11
void setAuto(bool inAuto)
Set the mode of the controller.
Definition PID.cpp:52
void init()
Method to be called when the controller goes from manual to auto mode for proper bumpless transfer.
Definition PID.cpp:9
double clip(double value) const
Clip value to [cvMin, cvMax].
Definition PID.cpp:44
double Kf
Gains.
Definition PID.h:14
const double & sp
Setpoint.
Definition PID.h:12
double a
Smoothing factor for EWA filter for derivative.
Definition PID.h:15
double Kp
Definition PID.h:14
double lastE
Last value of error.
Definition PID.h:20
PID(const double *pv, double *cv, const double *sp, double Kp, double Ki, double Kd, double Kf, uint16_t dt, double cvMin, double cvMax, double a)
Constructor.
Definition PID.cpp:3
double iTerm
Definition PID.h:33
bool compute()
Run a cycle of the PID loop.
Definition PID.cpp:17
double integral
Integral of pv.
Definition PID.h:22
double Ki
Definition PID.h:14
double dTerm
Definition PID.h:33
double lastPv
Last value of pv.
Definition PID.h:19
double lastDPV
Last value of derivative term.
Definition PID.h:21
double cvMax
Lower/upper limits for cv.
Definition PID.h:34
uint16_t dt
Timestep.
Definition PID.h:16
bool inAuto
Mode.
Definition PID.h:18
double cvMin
Definition PID.h:34
const double & pv
Process variable.
Definition PID.h:10
void setGains(double Kp, double Ki, double Kd, double Kf, uint16_t dt)
Set the gains and timestep.
Definition PID.cpp:59
double Kd
Definition PID.h:14
double fTerm
PID terms.
Definition PID.h:33
double pTerm
Definition PID.h:33