blob: 0ab89cc15d8b14a45c01efd89851d288cd5e4d1f [file] [log] [blame]
Patrick Ventured8012182018-03-08 08:21:38 -08001#pragma once
2
Josh Lehande745422020-11-07 02:14:09 -08003#include <string>
Patrick Ventured8012182018-03-08 08:21:38 -08004
Patrick Venturea0764872020-08-08 07:48:43 -07005namespace pid_control
6{
Patrick Ventured8012182018-03-08 08:21:38 -08007namespace ec
8{
9
Ed Tanousd2768c52025-06-26 11:42:57 -070010struct limits_t
Patrick Ventured8012182018-03-08 08:21:38 -080011{
Harvey Wu1b3b7302024-10-04 16:49:46 +080012 double min = 0.0;
13 double max = 0.0;
Ed Tanousd2768c52025-06-26 11:42:57 -070014};
Patrick Ventured8012182018-03-08 08:21:38 -080015
16/* Note: If you update these structs you need to update the copy code in
Josh Lehan31058fd2023-01-13 11:06:16 -080017 * pid/util.cpp and the initialization code in pid/buildjson.hpp files.
Patrick Ventured8012182018-03-08 08:21:38 -080018 */
Ed Tanousd2768c52025-06-26 11:42:57 -070019struct pid_info_t
Patrick Ventured8012182018-03-08 08:21:38 -080020{
Harvey Wu1b3b7302024-10-04 16:49:46 +080021 bool initialized = false; // has pid been initialized
22 bool checkHysterWithSetpt = false; // compare current input and setpoint to
23 // check hysteresis
Patrick Ventured8012182018-03-08 08:21:38 -080024
Harvey Wu1b3b7302024-10-04 16:49:46 +080025 double ts = 0.0; // sample time in seconds
26 double integral = 0.0; // integral of error
27 double lastOutput = 0.0; // value of last output
28 double lastError = 0.0; // value of last error
Patrick Ventured8012182018-03-08 08:21:38 -080029
Harvey Wu1b3b7302024-10-04 16:49:46 +080030 double proportionalCoeff = 0.0; // coeff for P
31 double integralCoeff = 0.0; // coeff for I
32 double derivativeCoeff = 0.0; // coeff for D
33 double feedFwdOffset = 0.0; // offset coeff for feed-forward term
34 double feedFwdGain = 0.0; // gain for feed-forward term
Patrick Ventured8012182018-03-08 08:21:38 -080035
Harvey Wu1b3b7302024-10-04 16:49:46 +080036 limits_t integralLimit; // clamp of integral
37 limits_t outLim; // clamp of output
38 double slewNeg = 0.0;
39 double slewPos = 0.0;
40 double positiveHysteresis = 0.0;
41 double negativeHysteresis = 0.0;
Ed Tanousd2768c52025-06-26 11:42:57 -070042};
Patrick Ventured8012182018-03-08 08:21:38 -080043
Josh Lehande745422020-11-07 02:14:09 -080044double pid(pid_info_t* pidinfoptr, double input, double setpoint,
45 const std::string* nameptr = nullptr);
Patrick Ventured8012182018-03-08 08:21:38 -080046
47/* Condensed version for use by the configuration. */
48struct pidinfo
49{
Ed Tanous2c457bb2025-06-27 10:24:38 -070050 bool checkHysterWithSetpt = false; // compare current input and setpoint to
51 // check hysteresis
Delphine CC Chiu97889632023-11-06 11:32:46 +080052
Ed Tanous2c457bb2025-06-27 10:24:38 -070053 double ts = 0.0; // sample time in seconds
54 double proportionalCoeff = 0.0; // coeff for P
55 double integralCoeff = 0.0; // coeff for I
56 double derivativeCoeff = 0.0; // coeff for D
57 double feedFwdOffset = 0.0; // offset coeff for feed-forward term
58 double feedFwdGain = 0.0; // gain for feed-forward term
59 ec::limits_t integralLimit; // clamp of integral
60 ec::limits_t outLim; // clamp of output
Harvey Wu1b3b7302024-10-04 16:49:46 +080061 double slewNeg = 0.0;
62 double slewPos = 0.0;
63 double positiveHysteresis = 0.0;
64 double negativeHysteresis = 0.0;
Patrick Ventured8012182018-03-08 08:21:38 -080065};
66
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070067} // namespace ec
Patrick Venturea0764872020-08-08 07:48:43 -070068} // namespace pid_control