blob: 9db08fd0305056158f4517e276b8810bc53468b9 [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{
Delphine CC Chiu97889632023-11-06 11:32:46 +080022 bool initialized; // has pid been initialized
23 bool checkHysterWithSetpt; // compare current input and setpoint to check
24 // hysteresis
Patrick Ventured8012182018-03-08 08:21:38 -080025
Delphine CC Chiu97889632023-11-06 11:32:46 +080026 double ts; // sample time in seconds
27 double integral; // integral of error
28 double lastOutput; // value of last output
29 double lastError; // value of last error
Patrick Ventured8012182018-03-08 08:21:38 -080030
Delphine CC Chiu97889632023-11-06 11:32:46 +080031 double proportionalCoeff; // coeff for P
32 double integralCoeff; // coeff for I
33 double derivativeCoeff; // coeff for D
34 double feedFwdOffset; // offset coeff for feed-forward term
35 double feedFwdGain; // gain for feed-forward term
Patrick Ventured8012182018-03-08 08:21:38 -080036
Delphine CC Chiu97889632023-11-06 11:32:46 +080037 limits_t integralLimit; // clamp of integral
38 limits_t outLim; // clamp of output
Patrick Venture4b0df322019-02-11 09:04:57 -080039 double slewNeg;
40 double slewPos;
James Feist572c43d2019-01-31 15:52:22 -080041 double positiveHysteresis;
42 double negativeHysteresis;
Patrick Ventured8012182018-03-08 08:21:38 -080043} pid_info_t;
44
Josh Lehande745422020-11-07 02:14:09 -080045double pid(pid_info_t* pidinfoptr, double input, double setpoint,
46 const std::string* nameptr = nullptr);
Patrick Ventured8012182018-03-08 08:21:38 -080047
48/* Condensed version for use by the configuration. */
49struct pidinfo
50{
Delphine CC Chiu97889632023-11-06 11:32:46 +080051 bool checkHysterWithSetpt; // compare current input and setpoint to check
52 // hysteresis
53
Patrick Venture7442c372019-02-11 10:21:05 -080054 double ts; // sample time in seconds
55 double proportionalCoeff; // coeff for P
56 double integralCoeff; // coeff for I
Bonnie Lo0e8fc392022-10-05 10:20:55 +080057 double derivativeCoeff; // coeff for D
Patrick Venture7442c372019-02-11 10:21:05 -080058 double feedFwdOffset; // offset coeff for feed-forward term
59 double feedFwdGain; // gain for feed-forward term
60 ec::limits_t integralLimit; // clamp of integral
61 ec::limits_t outLim; // clamp of output
62 double slewNeg;
63 double slewPos;
James Feist572c43d2019-01-31 15:52:22 -080064 double positiveHysteresis;
65 double negativeHysteresis;
Patrick Ventured8012182018-03-08 08:21:38 -080066};
67
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070068} // namespace ec
Patrick Venturea0764872020-08-08 07:48:43 -070069} // namespace pid_control