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:
23     PIDController(const std::string& id, ZoneInterface* owner) :
24         Controller(), _owner(owner), _id(id)
25     {
26         _pid_info.initialized = false;
27         _pid_info.ts = static_cast<double>(0.0);
28         _pid_info.integral = static_cast<double>(0.0);
29         _pid_info.lastOutput = static_cast<double>(0.0);
30         _pid_info.proportionalCoeff = static_cast<double>(0.0);
31         _pid_info.integralCoeff = static_cast<double>(0.0);
32         _pid_info.feedFwdOffset = static_cast<double>(0.0);
33         _pid_info.feedFwdGain = static_cast<double>(0.0);
34         _pid_info.integralLimit.min = static_cast<double>(0.0);
35         _pid_info.integralLimit.max = static_cast<double>(0.0);
36         _pid_info.outLim.min = static_cast<double>(0.0);
37         _pid_info.outLim.max = static_cast<double>(0.0);
38         _pid_info.slewNeg = static_cast<double>(0.0);
39         _pid_info.slewPos = static_cast<double>(0.0);
40         _pid_info.negativeHysteresis = static_cast<double>(0.0);
41         _pid_info.positiveHysteresis = static_cast<double>(0.0);
42     }
43 
44     virtual ~PIDController() {}
45 
46     virtual double inputProc(void) override = 0;
47     virtual double setptProc(void) = 0;
48     virtual void outputProc(double value) override = 0;
49 
50     void process(void) override;
51 
52     std::string getID(void) override
53     {
54         return _id;
55     }
56     double getSetpoint(void)
57     {
58         return _setpoint;
59     }
60     void setSetpoint(double setpoint)
61     {
62         _setpoint = setpoint;
63     }
64 
65     ec::pid_info_t* getPIDInfo(void)
66     {
67         return &_pid_info;
68     }
69 
70     double getLastInput(void)
71     {
72         return lastInput;
73     }
74 
75   protected:
76     ZoneInterface* _owner;
77     std::string _id;
78 
79   private:
80     // parameters
81     ec::pid_info_t _pid_info;
82     double _setpoint = 0;
83     double lastInput = std::numeric_limits<double>::quiet_NaN();
84 };
85 
86 } // namespace pid_control
87