James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "controller.hpp" |
| 4 | #include "ec/pid.hpp" |
| 5 | #include "fan.hpp" |
| 6 | |
Andrew Geissler | e30916c | 2020-05-20 21:27:05 -0500 | [diff] [blame] | 7 | #include <limits> |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 8 | #include <memory> |
| 9 | #include <vector> |
| 10 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 11 | namespace pid_control |
| 12 | { |
| 13 | |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 14 | class ZoneInterface; |
| 15 | |
| 16 | /* |
| 17 | * Base class for PID controllers. Each PID that implements this needs to |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 18 | * provide an inputProc, setptProc, and outputProc. |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 19 | */ |
| 20 | class PIDController : public Controller |
| 21 | { |
| 22 | public: |
| 23 | PIDController(const std::string& id, ZoneInterface* owner) : |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 24 | Controller(), _owner(owner), _id(id) |
Harvey Wu | 22579ca | 2022-11-07 14:53:37 +0800 | [diff] [blame] | 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); |
Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 32 | _pid_info.derivativeCoeff = static_cast<double>(0.0); |
Harvey Wu | 22579ca | 2022-11-07 14:53:37 +0800 | [diff] [blame] | 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 | } |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 44 | |
Patrick Williams | 8c05112 | 2023-05-10 07:50:59 -0500 | [diff] [blame] | 45 | virtual ~PIDController() {} |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 46 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 47 | virtual double inputProc(void) override = 0; |
| 48 | virtual double setptProc(void) = 0; |
| 49 | virtual void outputProc(double value) override = 0; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 50 | |
Patrick Venture | ee30648 | 2018-10-30 13:35:37 -0700 | [diff] [blame] | 51 | void process(void) override; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 52 | |
Patrick Venture | ee30648 | 2018-10-30 13:35:37 -0700 | [diff] [blame] | 53 | std::string getID(void) override |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 54 | { |
| 55 | return _id; |
| 56 | } |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 57 | double getSetpoint(void) |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 58 | { |
| 59 | return _setpoint; |
| 60 | } |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 61 | void setSetpoint(double setpoint) |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 62 | { |
| 63 | _setpoint = setpoint; |
| 64 | } |
| 65 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 66 | ec::pid_info_t* getPIDInfo(void) |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 67 | { |
| 68 | return &_pid_info; |
| 69 | } |
| 70 | |
James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 71 | double getLastInput(void) |
| 72 | { |
| 73 | return lastInput; |
| 74 | } |
| 75 | |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 76 | protected: |
| 77 | ZoneInterface* _owner; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 78 | std::string _id; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 79 | |
| 80 | private: |
| 81 | // parameters |
| 82 | ec::pid_info_t _pid_info; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 83 | double _setpoint = 0; |
James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 84 | double lastInput = std::numeric_limits<double>::quiet_NaN(); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 85 | }; |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 86 | |
| 87 | } // namespace pid_control |