xref: /openbmc/phosphor-pid-control/pid/pidcontroller.hpp (revision 8c051121ade815a46e39d5c669fee77302df2b6d)
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:
2322c257abSJames Feist     PIDController(const std::string& id, ZoneInterface* owner) :
24ccc8bb62SNirav Shah         Controller(), _owner(owner), _id(id)
2522579ca4SHarvey Wu     {
2622579ca4SHarvey Wu         _pid_info.initialized = false;
2722579ca4SHarvey Wu         _pid_info.ts = static_cast<double>(0.0);
2822579ca4SHarvey Wu         _pid_info.integral = static_cast<double>(0.0);
2922579ca4SHarvey Wu         _pid_info.lastOutput = static_cast<double>(0.0);
3022579ca4SHarvey Wu         _pid_info.proportionalCoeff = static_cast<double>(0.0);
3122579ca4SHarvey Wu         _pid_info.integralCoeff = static_cast<double>(0.0);
3222579ca4SHarvey Wu         _pid_info.feedFwdOffset = static_cast<double>(0.0);
3322579ca4SHarvey Wu         _pid_info.feedFwdGain = static_cast<double>(0.0);
3422579ca4SHarvey Wu         _pid_info.integralLimit.min = static_cast<double>(0.0);
3522579ca4SHarvey Wu         _pid_info.integralLimit.max = static_cast<double>(0.0);
3622579ca4SHarvey Wu         _pid_info.outLim.min = static_cast<double>(0.0);
3722579ca4SHarvey Wu         _pid_info.outLim.max = static_cast<double>(0.0);
3822579ca4SHarvey Wu         _pid_info.slewNeg = static_cast<double>(0.0);
3922579ca4SHarvey Wu         _pid_info.slewPos = static_cast<double>(0.0);
4022579ca4SHarvey Wu         _pid_info.negativeHysteresis = static_cast<double>(0.0);
4122579ca4SHarvey Wu         _pid_info.positiveHysteresis = static_cast<double>(0.0);
4222579ca4SHarvey Wu     }
4322c257abSJames Feist 
44*8c051121SPatrick Williams     virtual ~PIDController() {}
4522c257abSJames Feist 
465f59c0fdSPatrick Venture     virtual double inputProc(void) override = 0;
475f59c0fdSPatrick Venture     virtual double setptProc(void) = 0;
485f59c0fdSPatrick Venture     virtual void outputProc(double value) override = 0;
4922c257abSJames Feist 
50ee30648eSPatrick Venture     void process(void) override;
5122c257abSJames Feist 
52ee30648eSPatrick Venture     std::string getID(void) override
5322c257abSJames Feist     {
5422c257abSJames Feist         return _id;
5522c257abSJames Feist     }
565f59c0fdSPatrick Venture     double getSetpoint(void)
5722c257abSJames Feist     {
5822c257abSJames Feist         return _setpoint;
5922c257abSJames Feist     }
605f59c0fdSPatrick Venture     void setSetpoint(double setpoint)
6122c257abSJames Feist     {
6222c257abSJames Feist         _setpoint = setpoint;
6322c257abSJames Feist     }
6422c257abSJames Feist 
65563a356fSPatrick Venture     ec::pid_info_t* getPIDInfo(void)
6622c257abSJames Feist     {
6722c257abSJames Feist         return &_pid_info;
6822c257abSJames Feist     }
6922c257abSJames Feist 
70572c43daSJames Feist     double getLastInput(void)
71572c43daSJames Feist     {
72572c43daSJames Feist         return lastInput;
73572c43daSJames Feist     }
74572c43daSJames Feist 
7522c257abSJames Feist   protected:
7622c257abSJames Feist     ZoneInterface* _owner;
77ccc8bb62SNirav Shah     std::string _id;
7822c257abSJames Feist 
7922c257abSJames Feist   private:
8022c257abSJames Feist     // parameters
8122c257abSJames Feist     ec::pid_info_t _pid_info;
82ccc8bb62SNirav Shah     double _setpoint = 0;
83572c43daSJames Feist     double lastInput = std::numeric_limits<double>::quiet_NaN();
8422c257abSJames Feist };
85a076487aSPatrick Venture 
86a076487aSPatrick Venture } // namespace pid_control
87