xref: /openbmc/phosphor-pid-control/pid/ec/pid.hpp (revision f8b6e55147148c3cfb42327ff267197a460b411c)
1 #pragma once
2 
3 #include <string>
4 
5 namespace pid_control
6 {
7 namespace ec
8 {
9 
10 struct limits_t
11 {
12     double min = 0.0;
13     double max = 0.0;
14 };
15 
16 /* Note: If you update these structs you need to update the copy code in
17  * pid/util.cpp and the initialization code in pid/buildjson.hpp files.
18  */
19 struct pid_info_t
20 {
21     bool initialized = false;          // has pid been initialized
22     bool checkHysterWithSetpt = false; // compare current input and setpoint to
23                                        // check hysteresis
24 
25     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
29 
30     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
35 
36     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;
42 };
43 
44 double pid(pid_info_t* pidinfoptr, double input, double setpoint,
45            const std::string* nameptr = nullptr);
46 
47 /* Condensed version for use by the configuration. */
48 struct pidinfo
49 {
50     bool checkHysterWithSetpt = false; // compare current input and setpoint to
51                                        // check hysteresis
52 
53     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
61     double slewNeg = 0.0;
62     double slewPos = 0.0;
63     double positiveHysteresis = 0.0;
64     double negativeHysteresis = 0.0;
65 };
66 
67 } // namespace ec
68 } // namespace pid_control
69