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