blob: 05cbd7154fb715f46ea950cb54b9541a6cd6332a [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;
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 Lehan31058fd2023-01-13 11:06:16 -080032 _pid_info.derivativeCoeff = static_cast<double>(0.0);
Harvey Wu22579ca2022-11-07 14:53:37 +080033 _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 Feist22c257a2018-08-31 14:07:12 -070044
Patrick Williams8c051122023-05-10 07:50:59 -050045 virtual ~PIDController() {}
James Feist22c257a2018-08-31 14:07:12 -070046
Patrick Venture5f59c0f2018-11-11 12:55:14 -080047 virtual double inputProc(void) override = 0;
48 virtual double setptProc(void) = 0;
49 virtual void outputProc(double value) override = 0;
James Feist22c257a2018-08-31 14:07:12 -070050
Patrick Ventureee306482018-10-30 13:35:37 -070051 void process(void) override;
James Feist22c257a2018-08-31 14:07:12 -070052
Patrick Ventureee306482018-10-30 13:35:37 -070053 std::string getID(void) override
James Feist22c257a2018-08-31 14:07:12 -070054 {
55 return _id;
56 }
Patrick Venture5f59c0f2018-11-11 12:55:14 -080057 double getSetpoint(void)
James Feist22c257a2018-08-31 14:07:12 -070058 {
59 return _setpoint;
60 }
Patrick Venture5f59c0f2018-11-11 12:55:14 -080061 void setSetpoint(double setpoint)
James Feist22c257a2018-08-31 14:07:12 -070062 {
63 _setpoint = setpoint;
64 }
65
Patrick Venture563a3562018-10-30 09:31:26 -070066 ec::pid_info_t* getPIDInfo(void)
James Feist22c257a2018-08-31 14:07:12 -070067 {
68 return &_pid_info;
69 }
70
James Feist572c43d2019-01-31 15:52:22 -080071 double getLastInput(void)
72 {
73 return lastInput;
74 }
75
James Feist22c257a2018-08-31 14:07:12 -070076 protected:
77 ZoneInterface* _owner;
Nirav Shahccc8bb62022-02-17 21:06:51 -080078 std::string _id;
James Feist22c257a2018-08-31 14:07:12 -070079
80 private:
81 // parameters
82 ec::pid_info_t _pid_info;
Nirav Shahccc8bb62022-02-17 21:06:51 -080083 double _setpoint = 0;
James Feist572c43d2019-01-31 15:52:22 -080084 double lastInput = std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -070085};
Patrick Venturea0764872020-08-08 07:48:43 -070086
87} // namespace pid_control