blob: 9dac6a4690ac6ccc0d9a35e2a25781264355c137 [file] [log] [blame]
Patrick Ventured8012182018-03-08 08:21:38 -08001#pragma once
2
3#include <cstdint>
Josh Lehande745422020-11-07 02:14:09 -08004#include <string>
Patrick Ventured8012182018-03-08 08:21:38 -08005
Patrick Venturea0764872020-08-08 07:48:43 -07006namespace pid_control
7{
Patrick Ventured8012182018-03-08 08:21:38 -08008namespace ec
9{
10
11typedef struct
12{
Patrick Venture5f59c0f2018-11-11 12:55:14 -080013 double min;
14 double max;
Patrick Ventured8012182018-03-08 08:21:38 -080015} limits_t;
16
17/* Note: If you update these structs you need to update the copy code in
Josh Lehan31058fd2023-01-13 11:06:16 -080018 * pid/util.cpp and the initialization code in pid/buildjson.hpp files.
Patrick Ventured8012182018-03-08 08:21:38 -080019 */
20typedef struct
21{
Patrick Williams8c051122023-05-10 07:50:59 -050022 bool initialized; // has pid been initialized
Patrick Ventured8012182018-03-08 08:21:38 -080023
Patrick Williams8c051122023-05-10 07:50:59 -050024 double ts; // sample time in seconds
Josh Lehan31058fd2023-01-13 11:06:16 -080025 double integral; // integral of error
Patrick Williams8c051122023-05-10 07:50:59 -050026 double lastOutput; // value of last output
27 double lastError; // value of last error
Patrick Ventured8012182018-03-08 08:21:38 -080028
Patrick Venture4b0df322019-02-11 09:04:57 -080029 double proportionalCoeff; // coeff for P
30 double integralCoeff; // coeff for I
Bonnie Lo0e8fc392022-10-05 10:20:55 +080031 double derivativeCoeff; // coeff for D
Patrick Venture4b0df322019-02-11 09:04:57 -080032 double feedFwdOffset; // offset coeff for feed-forward term
33 double feedFwdGain; // gain for feed-forward term
Patrick Ventured8012182018-03-08 08:21:38 -080034
Patrick Williams8c051122023-05-10 07:50:59 -050035 limits_t integralLimit; // clamp of integral
36 limits_t outLim; // clamp of output
Patrick Venture4b0df322019-02-11 09:04:57 -080037 double slewNeg;
38 double slewPos;
James Feist572c43d2019-01-31 15:52:22 -080039 double positiveHysteresis;
40 double negativeHysteresis;
Patrick Ventured8012182018-03-08 08:21:38 -080041} pid_info_t;
42
Josh Lehande745422020-11-07 02:14:09 -080043double pid(pid_info_t* pidinfoptr, double input, double setpoint,
44 const std::string* nameptr = nullptr);
Patrick Ventured8012182018-03-08 08:21:38 -080045
46/* Condensed version for use by the configuration. */
47struct pidinfo
48{
Patrick Venture7442c372019-02-11 10:21:05 -080049 double ts; // sample time in seconds
50 double proportionalCoeff; // coeff for P
51 double integralCoeff; // coeff for I
Bonnie Lo0e8fc392022-10-05 10:20:55 +080052 double derivativeCoeff; // coeff for D
Patrick Venture7442c372019-02-11 10:21:05 -080053 double feedFwdOffset; // offset coeff for feed-forward term
54 double feedFwdGain; // gain for feed-forward term
55 ec::limits_t integralLimit; // clamp of integral
56 ec::limits_t outLim; // clamp of output
57 double slewNeg;
58 double slewPos;
James Feist572c43d2019-01-31 15:52:22 -080059 double positiveHysteresis;
60 double negativeHysteresis;
Patrick Ventured8012182018-03-08 08:21:38 -080061};
62
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070063} // namespace ec
Patrick Venturea0764872020-08-08 07:48:43 -070064} // namespace pid_control