blob: 29c7bb3a9493c80af08c435f724930196f7b4064 [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
Patrick Ventured8012182018-03-08 08:21:38 -080026
Patrick Venture4b0df322019-02-11 09:04:57 -080027 double proportionalCoeff; // coeff for P
28 double integralCoeff; // coeff for I
29 double feedFwdOffset; // offset coeff for feed-forward term
30 double feedFwdGain; // gain for feed-forward term
Patrick Ventured8012182018-03-08 08:21:38 -080031
Patrick Venture4b0df322019-02-11 09:04:57 -080032 limits_t integralLimit; // clamp of integral
33 limits_t outLim; // clamp of output
34 double slewNeg;
35 double slewPos;
James Feist572c43d2019-01-31 15:52:22 -080036 double positiveHysteresis;
37 double negativeHysteresis;
Patrick Ventured8012182018-03-08 08:21:38 -080038} pid_info_t;
39
Patrick Venture5f59c0f2018-11-11 12:55:14 -080040double pid(pid_info_t* pidinfoptr, double input, double setpoint);
Patrick Ventured8012182018-03-08 08:21:38 -080041
42/* Condensed version for use by the configuration. */
43struct pidinfo
44{
Patrick Venture7442c372019-02-11 10:21:05 -080045 double ts; // sample time in seconds
46 double proportionalCoeff; // coeff for P
47 double integralCoeff; // coeff for I
48 double feedFwdOffset; // offset coeff for feed-forward term
49 double feedFwdGain; // gain for feed-forward term
50 ec::limits_t integralLimit; // clamp of integral
51 ec::limits_t outLim; // clamp of output
52 double slewNeg;
53 double slewPos;
James Feist572c43d2019-01-31 15:52:22 -080054 double positiveHysteresis;
55 double negativeHysteresis;
Patrick Ventured8012182018-03-08 08:21:38 -080056};
57
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070058} // namespace ec
Patrick Venturea0764872020-08-08 07:48:43 -070059} // namespace pid_control