blob: 160928d3e64747e9e6f27d4dce9cbcea41ff9241 [file] [log] [blame]
Alexander Hansen46a755f2025-10-27 16:31:08 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2017 Google Inc
Patrick Ventured8012182018-03-08 08:21:38 -08003
James Feist22c257a2018-08-31 14:07:12 -07004#include "pidcontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07005
6#include "ec/pid.hpp"
7
James Feist572c43d2019-01-31 15:52:22 -08008#include <cmath>
Patrick Ventured8012182018-03-08 08:21:38 -08009
Patrick Venturea0764872020-08-08 07:48:43 -070010namespace pid_control
11{
12
Delphine CC Chiu97889632023-11-06 11:32:46 +080013double PIDController::calPIDOutput(double setpt, double input,
14 ec::pid_info_t* info)
15{
16 double output;
17 auto name = getID();
18
19 if (info->checkHysterWithSetpt)
20 {
21 // Over the hysteresis bounds, keep counting pid
22 if (input > (setpt + info->positiveHysteresis))
23 {
24 // Calculate new output
25 output = ec::pid(info, input, setpt, &name);
26
27 // this variable isn't actually used in this context, but we're
Manojkiran Eda7ca88872024-06-17 11:55:48 +053028 // setting it here in case somebody uses it later it's the correct
Delphine CC Chiu97889632023-11-06 11:32:46 +080029 // value
30 lastInput = input;
31 }
32 // Under the hysteresis bounds, initialize pid
33 else if (input < (setpt - info->negativeHysteresis))
34 {
35 lastInput = setpt;
36 info->integral = 0;
37 output = 0;
38 }
39 // inside the hysteresis bounds, keep last output
40 else
41 {
42 lastInput = input;
43 output = info->lastOutput;
44 }
45
46 info->lastOutput = output;
47 }
48 else
49 {
50 // if no hysteresis, maintain previous behavior
51 if (info->positiveHysteresis == 0 && info->negativeHysteresis == 0)
52 {
53 // Calculate new output
54 output = ec::pid(info, input, setpt, &name);
55
56 // this variable isn't actually used in this context, but we're
Manojkiran Eda7ca88872024-06-17 11:55:48 +053057 // setting it here in case somebody uses it later it's the correct
Delphine CC Chiu97889632023-11-06 11:32:46 +080058 // value
59 lastInput = input;
60 }
61 else
62 {
63 // initialize if the value is not set (NAN) or abnormal (+INF or
64 // -INF)
65 if (!(std::isfinite(lastInput)))
66 {
67 lastInput = input;
68 }
69
70 // if reading is outside of hysteresis bounds, use it for reading,
71 // otherwise use last reading without updating it first
72 else if ((input - lastInput) > info->positiveHysteresis)
73 {
74 lastInput = input;
75 }
76 else if ((lastInput - input) > info->negativeHysteresis)
77 {
78 lastInput = input;
79 }
80
81 output = ec::pid(info, lastInput, setpt, &name);
82 }
83 }
84
85 return output;
86}
87
James Feist22c257a2018-08-31 14:07:12 -070088void PIDController::process(void)
Patrick Ventured8012182018-03-08 08:21:38 -080089{
Patrick Venture5f59c0f2018-11-11 12:55:14 -080090 double input;
91 double setpt;
92 double output;
Patrick Ventured8012182018-03-08 08:21:38 -080093
94 // Get setpt value
Patrick Venture563a3562018-10-30 09:31:26 -070095 setpt = setptProc();
Patrick Ventured8012182018-03-08 08:21:38 -080096
97 // Get input value
Patrick Venture563a3562018-10-30 09:31:26 -070098 input = inputProc();
Patrick Ventured8012182018-03-08 08:21:38 -080099
James Feist572c43d2019-01-31 15:52:22 -0800100 auto info = getPIDInfo();
101
Delphine CC Chiu97889632023-11-06 11:32:46 +0800102 // Calculate output value
103 output = calPIDOutput(setpt, input, info);
James Feist572c43d2019-01-31 15:52:22 -0800104
Delphine CC Chiu97889632023-11-06 11:32:46 +0800105 info->lastOutput = output;
Patrick Ventured8012182018-03-08 08:21:38 -0800106
107 // Output new value
Patrick Venture563a3562018-10-30 09:31:26 -0700108 outputProc(output);
Patrick Ventured8012182018-03-08 08:21:38 -0800109
110 return;
111}
Patrick Venturea0764872020-08-08 07:48:43 -0700112
113} // namespace pid_control