blob: 255c91106a31dff4c997ee002ea38e6b2f2d9a38 [file] [log] [blame]
Patrick Ventured8012182018-03-08 08:21:38 -08001#pragma once
2
3#include <cstdint>
4
Patrick Venturea0764872020-08-08 07:48:43 -07005namespace pid_control
6{
Patrick Ventured8012182018-03-08 08:21:38 -08007namespace ec
8{
9
10typedef struct
11{
Patrick Venture5f59c0f2018-11-11 12:55:14 -080012 double min;
13 double max;
Patrick Ventured8012182018-03-08 08:21:38 -080014} limits_t;
15
16/* Note: If you update these structs you need to update the copy code in
17 * pid/util.cpp.
18 */
19typedef struct
20{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070021 bool initialized; // has pid been initialized
Patrick Ventured8012182018-03-08 08:21:38 -080022
Patrick Venture4b0df322019-02-11 09:04:57 -080023 double ts; // sample time in seconds
24 double integral; // intergal of error
25 double lastOutput; // value of last output
Bonnie Lo0e8fc392022-10-05 10:20:55 +080026 double lastError; // value of last error
Patrick Ventured8012182018-03-08 08:21:38 -080027
Patrick Venture4b0df322019-02-11 09:04:57 -080028 double proportionalCoeff; // coeff for P
29 double integralCoeff; // coeff for I
Bonnie Lo0e8fc392022-10-05 10:20:55 +080030 double derivativeCoeff; // coeff for D
Patrick Venture4b0df322019-02-11 09:04:57 -080031 double feedFwdOffset; // offset coeff for feed-forward term
32 double feedFwdGain; // gain for feed-forward term
Patrick Ventured8012182018-03-08 08:21:38 -080033
Patrick Venture4b0df322019-02-11 09:04:57 -080034 limits_t integralLimit; // clamp of integral
35 limits_t outLim; // clamp of output
36 double slewNeg;
37 double slewPos;
James Feist572c43d2019-01-31 15:52:22 -080038 double positiveHysteresis;
39 double negativeHysteresis;
Patrick Ventured8012182018-03-08 08:21:38 -080040} pid_info_t;
41
Patrick Venture5f59c0f2018-11-11 12:55:14 -080042double pid(pid_info_t* pidinfoptr, double input, double setpoint);
Patrick Ventured8012182018-03-08 08:21:38 -080043
44/* Condensed version for use by the configuration. */
45struct pidinfo
46{
Patrick Venture7442c372019-02-11 10:21:05 -080047 double ts; // sample time in seconds
48 double proportionalCoeff; // coeff for P
49 double integralCoeff; // coeff for I
Bonnie Lo0e8fc392022-10-05 10:20:55 +080050 double derivativeCoeff; // coeff for D
Patrick Venture7442c372019-02-11 10:21:05 -080051 double feedFwdOffset; // offset coeff for feed-forward term
52 double feedFwdGain; // gain for feed-forward term
53 ec::limits_t integralLimit; // clamp of integral
54 ec::limits_t outLim; // clamp of output
55 double slewNeg;
56 double slewPos;
James Feist572c43d2019-01-31 15:52:22 -080057 double positiveHysteresis;
58 double negativeHysteresis;
Patrick Ventured8012182018-03-08 08:21:38 -080059};
60
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070061} // namespace ec
Patrick Venturea0764872020-08-08 07:48:43 -070062} // namespace pid_control