blob: 91147d9c3f0c352f38a81edf9c3943dc5600e067 [file] [log] [blame]
James Feist22c257a2018-08-31 14:07:12 -07001#pragma once
2
3#include "controller.hpp"
4#include "ec/pid.hpp"
5#include "fan.hpp"
6
Andrew Geisslere30916c2020-05-20 21:27:05 -05007#include <limits>
James Feist22c257a2018-08-31 14:07:12 -07008#include <memory>
9#include <vector>
10
Patrick Venturea0764872020-08-08 07:48:43 -070011namespace pid_control
12{
13
James Feist22c257a2018-08-31 14:07:12 -070014class ZoneInterface;
15
16/*
17 * Base class for PID controllers. Each PID that implements this needs to
Patrick Venture563a3562018-10-30 09:31:26 -070018 * provide an inputProc, setptProc, and outputProc.
James Feist22c257a2018-08-31 14:07:12 -070019 */
20class PIDController : public Controller
21{
22 public:
23 PIDController(const std::string& id, ZoneInterface* owner) :
Nirav Shahccc8bb62022-02-17 21:06:51 -080024 Controller(), _owner(owner), _id(id)
Harvey Wu22579ca2022-11-07 14:53:37 +080025 {
26 _pid_info.initialized = false;
Delphine CC Chiu97889632023-11-06 11:32:46 +080027 _pid_info.checkHysterWithSetpt = false;
Harvey Wu22579ca2022-11-07 14:53:37 +080028 _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);
Josh Lehan31058fd2023-01-13 11:06:16 -080033 _pid_info.derivativeCoeff = static_cast<double>(0.0);
Harvey Wu22579ca2022-11-07 14:53:37 +080034 _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 }
James Feist22c257a2018-08-31 14:07:12 -070045
Patrick Williams8c051122023-05-10 07:50:59 -050046 virtual ~PIDController() {}
James Feist22c257a2018-08-31 14:07:12 -070047
Patrick Venture5f59c0f2018-11-11 12:55:14 -080048 virtual double inputProc(void) override = 0;
49 virtual double setptProc(void) = 0;
50 virtual void outputProc(double value) override = 0;
James Feist22c257a2018-08-31 14:07:12 -070051
Patrick Ventureee306482018-10-30 13:35:37 -070052 void process(void) override;
James Feist22c257a2018-08-31 14:07:12 -070053
Patrick Ventureee306482018-10-30 13:35:37 -070054 std::string getID(void) override
James Feist22c257a2018-08-31 14:07:12 -070055 {
56 return _id;
57 }
Patrick Venture5f59c0f2018-11-11 12:55:14 -080058 double getSetpoint(void)
James Feist22c257a2018-08-31 14:07:12 -070059 {
60 return _setpoint;
61 }
Patrick Venture5f59c0f2018-11-11 12:55:14 -080062 void setSetpoint(double setpoint)
James Feist22c257a2018-08-31 14:07:12 -070063 {
64 _setpoint = setpoint;
65 }
66
Patrick Venture563a3562018-10-30 09:31:26 -070067 ec::pid_info_t* getPIDInfo(void)
James Feist22c257a2018-08-31 14:07:12 -070068 {
69 return &_pid_info;
70 }
71
James Feist572c43d2019-01-31 15:52:22 -080072 double getLastInput(void)
73 {
74 return lastInput;
75 }
76
Delphine CC Chiu97889632023-11-06 11:32:46 +080077 double calPIDOutput(double setpt, double input, ec::pid_info_t* info);
78
James Feist22c257a2018-08-31 14:07:12 -070079 protected:
80 ZoneInterface* _owner;
Nirav Shahccc8bb62022-02-17 21:06:51 -080081 std::string _id;
James Feist22c257a2018-08-31 14:07:12 -070082
83 private:
84 // parameters
85 ec::pid_info_t _pid_info;
Nirav Shahccc8bb62022-02-17 21:06:51 -080086 double _setpoint = 0;
James Feist572c43d2019-01-31 15:52:22 -080087 double lastInput = std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -070088};
Patrick Venturea0764872020-08-08 07:48:43 -070089
90} // namespace pid_control