blob: adccfcee4fa6a09010e5511dffdcd0cbdb41c1d3 [file] [log] [blame]
Patrick Ventured8012182018-03-08 08:21:38 -08001/**
2 * Copyright 2017 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
James Feist22c257a2018-08-31 14:07:12 -070017#include "pidcontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070018
19#include "ec/pid.hpp"
20
James Feist572c43d2019-01-31 15:52:22 -080021#include <cmath>
Patrick Ventured8012182018-03-08 08:21:38 -080022
Patrick Venturea0764872020-08-08 07:48:43 -070023namespace pid_control
24{
25
Delphine CC Chiu97889632023-11-06 11:32:46 +080026double PIDController::calPIDOutput(double setpt, double input,
27 ec::pid_info_t* info)
28{
29 double output;
30 auto name = getID();
31
32 if (info->checkHysterWithSetpt)
33 {
34 // Over the hysteresis bounds, keep counting pid
35 if (input > (setpt + info->positiveHysteresis))
36 {
37 // Calculate new output
38 output = ec::pid(info, input, setpt, &name);
39
40 // this variable isn't actually used in this context, but we're
Manojkiran Eda7ca88872024-06-17 11:55:48 +053041 // setting it here in case somebody uses it later it's the correct
Delphine CC Chiu97889632023-11-06 11:32:46 +080042 // value
43 lastInput = input;
44 }
45 // Under the hysteresis bounds, initialize pid
46 else if (input < (setpt - info->negativeHysteresis))
47 {
48 lastInput = setpt;
49 info->integral = 0;
50 output = 0;
51 }
52 // inside the hysteresis bounds, keep last output
53 else
54 {
55 lastInput = input;
56 output = info->lastOutput;
57 }
58
59 info->lastOutput = output;
60 }
61 else
62 {
63 // if no hysteresis, maintain previous behavior
64 if (info->positiveHysteresis == 0 && info->negativeHysteresis == 0)
65 {
66 // Calculate new output
67 output = ec::pid(info, input, setpt, &name);
68
69 // this variable isn't actually used in this context, but we're
Manojkiran Eda7ca88872024-06-17 11:55:48 +053070 // setting it here in case somebody uses it later it's the correct
Delphine CC Chiu97889632023-11-06 11:32:46 +080071 // value
72 lastInput = input;
73 }
74 else
75 {
76 // initialize if the value is not set (NAN) or abnormal (+INF or
77 // -INF)
78 if (!(std::isfinite(lastInput)))
79 {
80 lastInput = input;
81 }
82
83 // if reading is outside of hysteresis bounds, use it for reading,
84 // otherwise use last reading without updating it first
85 else if ((input - lastInput) > info->positiveHysteresis)
86 {
87 lastInput = input;
88 }
89 else if ((lastInput - input) > info->negativeHysteresis)
90 {
91 lastInput = input;
92 }
93
94 output = ec::pid(info, lastInput, setpt, &name);
95 }
96 }
97
98 return output;
99}
100
James Feist22c257a2018-08-31 14:07:12 -0700101void PIDController::process(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800102{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800103 double input;
104 double setpt;
105 double output;
Patrick Ventured8012182018-03-08 08:21:38 -0800106
107 // Get setpt value
Patrick Venture563a3562018-10-30 09:31:26 -0700108 setpt = setptProc();
Patrick Ventured8012182018-03-08 08:21:38 -0800109
110 // Get input value
Patrick Venture563a3562018-10-30 09:31:26 -0700111 input = inputProc();
Patrick Ventured8012182018-03-08 08:21:38 -0800112
James Feist572c43d2019-01-31 15:52:22 -0800113 auto info = getPIDInfo();
114
Delphine CC Chiu97889632023-11-06 11:32:46 +0800115 // Calculate output value
116 output = calPIDOutput(setpt, input, info);
James Feist572c43d2019-01-31 15:52:22 -0800117
Delphine CC Chiu97889632023-11-06 11:32:46 +0800118 info->lastOutput = output;
Patrick Ventured8012182018-03-08 08:21:38 -0800119
120 // Output new value
Patrick Venture563a3562018-10-30 09:31:26 -0700121 outputProc(output);
Patrick Ventured8012182018-03-08 08:21:38 -0800122
123 return;
124}
Patrick Venturea0764872020-08-08 07:48:43 -0700125
126} // namespace pid_control