blob: 8e3d415c6cc759cc8296518913331221ab30b1ff [file] [log] [blame]
James Feist22c257a2018-08-31 14:07:12 -07001#pragma once
2
3#include "controller.hpp"
4#include "ec/pid.hpp"
James Feist22c257a2018-08-31 14:07:12 -07005
Andrew Geisslere30916c2020-05-20 21:27:05 -05006#include <limits>
Ed Tanousf8b6e552025-06-27 13:27:50 -07007#include <string>
James Feist22c257a2018-08-31 14:07:12 -07008
Patrick Venturea0764872020-08-08 07:48:43 -07009namespace pid_control
10{
11
James Feist22c257a2018-08-31 14:07:12 -070012class ZoneInterface;
13
14/*
15 * Base class for PID controllers. Each PID that implements this needs to
Patrick Venture563a3562018-10-30 09:31:26 -070016 * provide an inputProc, setptProc, and outputProc.
James Feist22c257a2018-08-31 14:07:12 -070017 */
18class PIDController : public Controller
19{
20 public:
21 PIDController(const std::string& id, ZoneInterface* owner) :
Nirav Shahccc8bb62022-02-17 21:06:51 -080022 Controller(), _owner(owner), _id(id)
Harvey Wu22579ca2022-11-07 14:53:37 +080023 {
24 _pid_info.initialized = false;
Delphine CC Chiu97889632023-11-06 11:32:46 +080025 _pid_info.checkHysterWithSetpt = false;
Harvey Wu22579ca2022-11-07 14:53:37 +080026 _pid_info.ts = static_cast<double>(0.0);
27 _pid_info.integral = static_cast<double>(0.0);
28 _pid_info.lastOutput = static_cast<double>(0.0);
29 _pid_info.proportionalCoeff = static_cast<double>(0.0);
30 _pid_info.integralCoeff = static_cast<double>(0.0);
Josh Lehan31058fd2023-01-13 11:06:16 -080031 _pid_info.derivativeCoeff = static_cast<double>(0.0);
Harvey Wu22579ca2022-11-07 14:53:37 +080032 _pid_info.feedFwdOffset = static_cast<double>(0.0);
33 _pid_info.feedFwdGain = static_cast<double>(0.0);
34 _pid_info.integralLimit.min = static_cast<double>(0.0);
35 _pid_info.integralLimit.max = static_cast<double>(0.0);
36 _pid_info.outLim.min = static_cast<double>(0.0);
37 _pid_info.outLim.max = static_cast<double>(0.0);
38 _pid_info.slewNeg = static_cast<double>(0.0);
39 _pid_info.slewPos = static_cast<double>(0.0);
40 _pid_info.negativeHysteresis = static_cast<double>(0.0);
41 _pid_info.positiveHysteresis = static_cast<double>(0.0);
42 }
James Feist22c257a2018-08-31 14:07:12 -070043
Ed Tanousd2768c52025-06-26 11:42:57 -070044 ~PIDController() override = default;
James Feist22c257a2018-08-31 14:07:12 -070045
Ed Tanousd2768c52025-06-26 11:42:57 -070046 double inputProc(void) override = 0;
Patrick Venture5f59c0f2018-11-11 12:55:14 -080047 virtual double setptProc(void) = 0;
Ed Tanousd2768c52025-06-26 11:42:57 -070048 void outputProc(double value) override = 0;
James Feist22c257a2018-08-31 14:07:12 -070049
Patrick Ventureee306482018-10-30 13:35:37 -070050 void process(void) override;
James Feist22c257a2018-08-31 14:07:12 -070051
Patrick Ventureee306482018-10-30 13:35:37 -070052 std::string getID(void) override
James Feist22c257a2018-08-31 14:07:12 -070053 {
54 return _id;
55 }
Patrick Venture5f59c0f2018-11-11 12:55:14 -080056 double getSetpoint(void)
James Feist22c257a2018-08-31 14:07:12 -070057 {
58 return _setpoint;
59 }
Patrick Venture5f59c0f2018-11-11 12:55:14 -080060 void setSetpoint(double setpoint)
James Feist22c257a2018-08-31 14:07:12 -070061 {
62 _setpoint = setpoint;
63 }
64
Patrick Venture563a3562018-10-30 09:31:26 -070065 ec::pid_info_t* getPIDInfo(void)
James Feist22c257a2018-08-31 14:07:12 -070066 {
67 return &_pid_info;
68 }
69
James Feist572c43d2019-01-31 15:52:22 -080070 double getLastInput(void)
71 {
72 return lastInput;
73 }
74
Delphine CC Chiu97889632023-11-06 11:32:46 +080075 double calPIDOutput(double setpt, double input, ec::pid_info_t* info);
76
James Feist22c257a2018-08-31 14:07:12 -070077 protected:
78 ZoneInterface* _owner;
Nirav Shahccc8bb62022-02-17 21:06:51 -080079 std::string _id;
James Feist22c257a2018-08-31 14:07:12 -070080
81 private:
82 // parameters
83 ec::pid_info_t _pid_info;
Nirav Shahccc8bb62022-02-17 21:06:51 -080084 double _setpoint = 0;
James Feist572c43d2019-01-31 15:52:22 -080085 double lastInput = std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -070086};
Patrick Venturea0764872020-08-08 07:48:43 -070087
88} // namespace pid_control