122c257abSJames Feist #pragma once
222c257abSJames Feist 
322c257abSJames Feist #include "controller.hpp"
422c257abSJames Feist #include "ec/pid.hpp"
522c257abSJames Feist #include "fan.hpp"
622c257abSJames Feist 
7e30916c9SAndrew Geissler #include <limits>
822c257abSJames Feist #include <memory>
922c257abSJames Feist #include <vector>
1022c257abSJames Feist 
11a076487aSPatrick Venture namespace pid_control
12a076487aSPatrick Venture {
13a076487aSPatrick Venture 
1422c257abSJames Feist class ZoneInterface;
1522c257abSJames Feist 
1622c257abSJames Feist /*
1722c257abSJames Feist  * Base class for PID controllers.  Each PID that implements this needs to
18563a356fSPatrick Venture  * provide an inputProc, setptProc, and outputProc.
1922c257abSJames Feist  */
2022c257abSJames Feist class PIDController : public Controller
2122c257abSJames Feist {
2222c257abSJames Feist   public:
PIDController(const std::string & id,ZoneInterface * owner)2322c257abSJames Feist     PIDController(const std::string& id, ZoneInterface* owner) :
24ccc8bb62SNirav Shah         Controller(), _owner(owner), _id(id)
2522579ca4SHarvey Wu     {
2622579ca4SHarvey Wu         _pid_info.initialized = false;
27*9788963cSDelphine CC Chiu         _pid_info.checkHysterWithSetpt = false;
2822579ca4SHarvey Wu         _pid_info.ts = static_cast<double>(0.0);
2922579ca4SHarvey Wu         _pid_info.integral = static_cast<double>(0.0);
3022579ca4SHarvey Wu         _pid_info.lastOutput = static_cast<double>(0.0);
3122579ca4SHarvey Wu         _pid_info.proportionalCoeff = static_cast<double>(0.0);
3222579ca4SHarvey Wu         _pid_info.integralCoeff = static_cast<double>(0.0);
3331058fd3SJosh Lehan         _pid_info.derivativeCoeff = static_cast<double>(0.0);
3422579ca4SHarvey Wu         _pid_info.feedFwdOffset = static_cast<double>(0.0);
3522579ca4SHarvey Wu         _pid_info.feedFwdGain = static_cast<double>(0.0);
3622579ca4SHarvey Wu         _pid_info.integralLimit.min = static_cast<double>(0.0);
3722579ca4SHarvey Wu         _pid_info.integralLimit.max = static_cast<double>(0.0);
3822579ca4SHarvey Wu         _pid_info.outLim.min = static_cast<double>(0.0);
3922579ca4SHarvey Wu         _pid_info.outLim.max = static_cast<double>(0.0);
4022579ca4SHarvey Wu         _pid_info.slewNeg = static_cast<double>(0.0);
4122579ca4SHarvey Wu         _pid_info.slewPos = static_cast<double>(0.0);
4222579ca4SHarvey Wu         _pid_info.negativeHysteresis = static_cast<double>(0.0);
4322579ca4SHarvey Wu         _pid_info.positiveHysteresis = static_cast<double>(0.0);
4422579ca4SHarvey Wu     }
4522c257abSJames Feist 
~PIDController()468c051121SPatrick Williams     virtual ~PIDController() {}
4722c257abSJames Feist 
485f59c0fdSPatrick Venture     virtual double inputProc(void) override = 0;
495f59c0fdSPatrick Venture     virtual double setptProc(void) = 0;
505f59c0fdSPatrick Venture     virtual void outputProc(double value) override = 0;
5122c257abSJames Feist 
52ee30648eSPatrick Venture     void process(void) override;
5322c257abSJames Feist 
getID(void)54ee30648eSPatrick Venture     std::string getID(void) override
5522c257abSJames Feist     {
5622c257abSJames Feist         return _id;
5722c257abSJames Feist     }
getSetpoint(void)585f59c0fdSPatrick Venture     double getSetpoint(void)
5922c257abSJames Feist     {
6022c257abSJames Feist         return _setpoint;
6122c257abSJames Feist     }
setSetpoint(double setpoint)625f59c0fdSPatrick Venture     void setSetpoint(double setpoint)
6322c257abSJames Feist     {
6422c257abSJames Feist         _setpoint = setpoint;
6522c257abSJames Feist     }
6622c257abSJames Feist 
getPIDInfo(void)67563a356fSPatrick Venture     ec::pid_info_t* getPIDInfo(void)
6822c257abSJames Feist     {
6922c257abSJames Feist         return &_pid_info;
7022c257abSJames Feist     }
7122c257abSJames Feist 
getLastInput(void)72572c43daSJames Feist     double getLastInput(void)
73572c43daSJames Feist     {
74572c43daSJames Feist         return lastInput;
75572c43daSJames Feist     }
76572c43daSJames Feist 
77*9788963cSDelphine CC Chiu     double calPIDOutput(double setpt, double input, ec::pid_info_t* info);
78*9788963cSDelphine CC Chiu 
7922c257abSJames Feist   protected:
8022c257abSJames Feist     ZoneInterface* _owner;
81ccc8bb62SNirav Shah     std::string _id;
8222c257abSJames Feist 
8322c257abSJames Feist   private:
8422c257abSJames Feist     // parameters
8522c257abSJames Feist     ec::pid_info_t _pid_info;
86ccc8bb62SNirav Shah     double _setpoint = 0;
87572c43daSJames Feist     double lastInput = std::numeric_limits<double>::quiet_NaN();
8822c257abSJames Feist };
89a076487aSPatrick Venture 
90a076487aSPatrick Venture } // namespace pid_control
91