1 #pragma once
2 
3 #include "controller.hpp"
4 #include "ec/pid.hpp"
5 #include "fan.hpp"
6 
7 #include <limits>
8 #include <memory>
9 #include <vector>
10 
11 namespace pid_control
12 {
13 
14 class ZoneInterface;
15 
16 /*
17  * Base class for PID controllers.  Each PID that implements this needs to
18  * provide an inputProc, setptProc, and outputProc.
19  */
20 class PIDController : public Controller
21 {
22   public:
PIDController(const std::string & id,ZoneInterface * owner)23     PIDController(const std::string& id, ZoneInterface* owner) :
24         Controller(), _owner(owner), _id(id)
25     {
26         _pid_info.initialized = false;
27         _pid_info.checkHysterWithSetpt = false;
28         _pid_info.ts = static_cast<double>(0.0);
29         _pid_info.integral = static_cast<double>(0.0);
30         _pid_info.lastOutput = static_cast<double>(0.0);
31         _pid_info.proportionalCoeff = static_cast<double>(0.0);
32         _pid_info.integralCoeff = static_cast<double>(0.0);
33         _pid_info.derivativeCoeff = static_cast<double>(0.0);
34         _pid_info.feedFwdOffset = static_cast<double>(0.0);
35         _pid_info.feedFwdGain = static_cast<double>(0.0);
36         _pid_info.integralLimit.min = static_cast<double>(0.0);
37         _pid_info.integralLimit.max = static_cast<double>(0.0);
38         _pid_info.outLim.min = static_cast<double>(0.0);
39         _pid_info.outLim.max = static_cast<double>(0.0);
40         _pid_info.slewNeg = static_cast<double>(0.0);
41         _pid_info.slewPos = static_cast<double>(0.0);
42         _pid_info.negativeHysteresis = static_cast<double>(0.0);
43         _pid_info.positiveHysteresis = static_cast<double>(0.0);
44     }
45 
~PIDController()46     virtual ~PIDController() {}
47 
48     virtual double inputProc(void) override = 0;
49     virtual double setptProc(void) = 0;
50     virtual void outputProc(double value) override = 0;
51 
52     void process(void) override;
53 
getID(void)54     std::string getID(void) override
55     {
56         return _id;
57     }
getSetpoint(void)58     double getSetpoint(void)
59     {
60         return _setpoint;
61     }
setSetpoint(double setpoint)62     void setSetpoint(double setpoint)
63     {
64         _setpoint = setpoint;
65     }
66 
getPIDInfo(void)67     ec::pid_info_t* getPIDInfo(void)
68     {
69         return &_pid_info;
70     }
71 
getLastInput(void)72     double getLastInput(void)
73     {
74         return lastInput;
75     }
76 
77     double calPIDOutput(double setpt, double input, ec::pid_info_t* info);
78 
79   protected:
80     ZoneInterface* _owner;
81     std::string _id;
82 
83   private:
84     // parameters
85     ec::pid_info_t _pid_info;
86     double _setpoint = 0;
87     double lastInput = std::numeric_limits<double>::quiet_NaN();
88 };
89 
90 } // namespace pid_control
91