blob: ac77f052b4cbac07556a87ccbc8f5d449f0a9b49 [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
Jayanth Othayoth326cd762024-12-08 08:12:14 -060011typedef struct limits_t
Patrick Ventured8012182018-03-08 08:21:38 -080012{
Harvey Wu1b3b7302024-10-04 16:49:46 +080013 double min = 0.0;
14 double max = 0.0;
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 */
Jayanth Othayoth326cd762024-12-08 08:12:14 -060020typedef struct pid_info_t
Patrick Ventured8012182018-03-08 08:21:38 -080021{
Harvey Wu1b3b7302024-10-04 16:49:46 +080022 bool initialized = false; // has pid been initialized
23 bool checkHysterWithSetpt = false; // compare current input and setpoint to
24 // check hysteresis
Patrick Ventured8012182018-03-08 08:21:38 -080025
Harvey Wu1b3b7302024-10-04 16:49:46 +080026 double ts = 0.0; // sample time in seconds
27 double integral = 0.0; // integral of error
28 double lastOutput = 0.0; // value of last output
29 double lastError = 0.0; // value of last error
Patrick Ventured8012182018-03-08 08:21:38 -080030
Harvey Wu1b3b7302024-10-04 16:49:46 +080031 double proportionalCoeff = 0.0; // coeff for P
32 double integralCoeff = 0.0; // coeff for I
33 double derivativeCoeff = 0.0; // coeff for D
34 double feedFwdOffset = 0.0; // offset coeff for feed-forward term
35 double feedFwdGain = 0.0; // gain for feed-forward term
Patrick Ventured8012182018-03-08 08:21:38 -080036
Harvey Wu1b3b7302024-10-04 16:49:46 +080037 limits_t integralLimit; // clamp of integral
38 limits_t outLim; // clamp of output
39 double slewNeg = 0.0;
40 double slewPos = 0.0;
41 double positiveHysteresis = 0.0;
42 double negativeHysteresis = 0.0;
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{
Harvey Wu1b3b7302024-10-04 16:49:46 +080051 bool checkHysterWithSetpt = 0.0; // compare current input and setpoint to
52 // check hysteresis
Delphine CC Chiu97889632023-11-06 11:32:46 +080053
Harvey Wu1b3b7302024-10-04 16:49:46 +080054 double ts = 0.0; // sample time in seconds
55 double proportionalCoeff = 0.0; // coeff for P
56 double integralCoeff = 0.0; // coeff for I
57 double derivativeCoeff = 0.0; // coeff for D
58 double feedFwdOffset = 0.0; // offset coeff for feed-forward term
59 double feedFwdGain = 0.0; // gain for feed-forward term
60 ec::limits_t integralLimit; // clamp of integral
61 ec::limits_t outLim; // clamp of output
62 double slewNeg = 0.0;
63 double slewPos = 0.0;
64 double positiveHysteresis = 0.0;
65 double negativeHysteresis = 0.0;
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