blob: ddb5893942faf4f44d7ece6784a9e37fda112410 [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
17#include "thermalcontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070018
James Feist734f9532018-11-15 12:13:18 -080019#include "errors/exception.hpp"
Bonnie Loc51ba912022-10-12 14:07:22 +080020#include "tuning.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080021#include "util.hpp"
22#include "zone.hpp"
23
Patrick Venturee94bdc42018-11-15 18:41:21 -080024#include <algorithm>
Josh Lehanca791152020-09-20 23:41:39 -070025#include <cmath>
26#include <iostream>
Patrick Venturee94bdc42018-11-15 18:41:21 -080027
Patrick Venturea0764872020-08-08 07:48:43 -070028namespace pid_control
29{
30
Patrick Venturee94bdc42018-11-15 18:41:21 -080031ThermalType getThermalType(const std::string& typeString)
32{
Josh Lehan23e22b92022-11-12 22:37:58 -080033 if (typeString == "margin")
34 {
35 return ThermalType::margin;
36 }
37 if ((typeString == "temp") || (typeString == "power"))
38 {
39 return ThermalType::absolute;
40 }
41 if (typeString == "powersum")
42 {
43 return ThermalType::summation;
44 }
45
46 throw ControllerBuildException("Unrecognized PID Type/Class string");
Patrick Venturee94bdc42018-11-15 18:41:21 -080047}
48
49bool isThermalType(const std::string& typeString)
50{
Josh Lehan23e22b92022-11-12 22:37:58 -080051 static const std::vector<std::string> thermalTypes = {"temp", "margin",
52 "power", "powersum"};
Patrick Venturee94bdc42018-11-15 18:41:21 -080053 return std::count(thermalTypes.begin(), thermalTypes.end(), typeString);
54}
55
Patrick Venture563a3562018-10-30 09:31:26 -070056std::unique_ptr<PIDController> ThermalController::createThermalPid(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070057 ZoneInterface* owner, const std::string& id,
Josh Lehan31058fd2023-01-13 11:06:16 -080058 const std::vector<pid_control::conf::SensorInput>& inputs, double setpoint,
James Feist734f9532018-11-15 12:13:18 -080059 const ec::pidinfo& initial, const ThermalType& type)
Patrick Ventured8012182018-03-08 08:21:38 -080060{
James Feist734f9532018-11-15 12:13:18 -080061 // ThermalController requires at least 1 input
62 if (inputs.empty())
Patrick Venture3349ef22018-06-12 14:09:29 -070063 {
James Feist734f9532018-11-15 12:13:18 -080064 throw ControllerBuildException("Thermal controller missing inputs");
Patrick Venture3349ef22018-06-12 14:09:29 -070065 }
66
James Feist734f9532018-11-15 12:13:18 -080067 auto thermal = std::make_unique<ThermalController>(id, inputs, type, owner);
Patrick Ventured8012182018-03-08 08:21:38 -080068
Patrick Venture563a3562018-10-30 09:31:26 -070069 ec::pid_info_t* info = thermal->getPIDInfo();
70 thermal->setSetpoint(setpoint);
Patrick Ventured8012182018-03-08 08:21:38 -080071
Patrick Venture7af157b2018-10-30 11:24:40 -070072 initializePIDStruct(info, initial);
Patrick Ventured8012182018-03-08 08:21:38 -080073
74 return thermal;
75}
76
Patrick Venture5f59c0f2018-11-11 12:55:14 -080077// bmc_host_sensor_value_double
78double ThermalController::inputProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080079{
James Feist734f9532018-11-15 12:13:18 -080080 double value;
81 const double& (*compare)(const double&, const double&);
Josh Lehan23e22b92022-11-12 22:37:58 -080082 bool doSummation = false;
83
James Feist734f9532018-11-15 12:13:18 -080084 if (type == ThermalType::margin)
85 {
86 value = std::numeric_limits<double>::max();
87 compare = std::min<double>;
88 }
Josh Lehan23e22b92022-11-12 22:37:58 -080089 else if (type == ThermalType::absolute)
James Feist734f9532018-11-15 12:13:18 -080090 {
91 value = std::numeric_limits<double>::lowest();
92 compare = std::max<double>;
93 }
Josh Lehan23e22b92022-11-12 22:37:58 -080094 else if (type == ThermalType::summation)
95 {
96 doSummation = true;
97 value = 0.0;
98 }
99 else
100 {
101 throw ControllerBuildException("Unrecognized ThermalType");
102 }
James Feist734f9532018-11-15 12:13:18 -0800103
Josh Lehan31058fd2023-01-13 11:06:16 -0800104 std::string leaderName = _inputs.begin()->name;
Harvey Wu37180062023-10-02 09:42:50 +0800105
Josh Lehanca791152020-09-20 23:41:39 -0700106 bool acceptable = false;
James Feist734f9532018-11-15 12:13:18 -0800107 for (const auto& in : _inputs)
108 {
Josh Lehan31058fd2023-01-13 11:06:16 -0800109 double cachedValue = _owner->getCachedValue(in.name);
Josh Lehanca791152020-09-20 23:41:39 -0700110
111 // Less than 0 is perfectly OK for temperature, but must not be NAN
112 if (!(std::isfinite(cachedValue)))
113 {
114 continue;
115 }
116
Josh Lehan31058fd2023-01-13 11:06:16 -0800117 // Perform TempToMargin conversion before further processing
118 if (type == ThermalType::margin)
119 {
120 if (in.convertTempToMargin)
121 {
122 if (!(std::isfinite(in.convertMarginZero)))
123 {
124 throw ControllerBuildException("Unrecognized TempToMargin");
125 }
126
127 double marginValue = in.convertMarginZero - cachedValue;
128
129 if (debugEnabled)
130 {
131 std::cerr << "Converting temp to margin: temp "
132 << cachedValue << ", Tjmax "
133 << in.convertMarginZero << ", margin "
134 << marginValue << "\n";
135 }
136
137 cachedValue = marginValue;
138 }
139 }
140
Harvey Wu37180062023-10-02 09:42:50 +0800141 double oldValue = value;
142
Josh Lehan23e22b92022-11-12 22:37:58 -0800143 if (doSummation)
144 {
145 value += cachedValue;
146 }
147 else
148 {
149 value = compare(value, cachedValue);
150 }
151
Harvey Wu37180062023-10-02 09:42:50 +0800152 if (oldValue != value)
153 {
Josh Lehan31058fd2023-01-13 11:06:16 -0800154 leaderName = in.name;
Harvey Wu37180062023-10-02 09:42:50 +0800155 _owner->updateThermalPowerDebugInterface(_id, leaderName, value, 0);
156 }
157
Josh Lehanca791152020-09-20 23:41:39 -0700158 acceptable = true;
159 }
160
161 if (!acceptable)
162 {
Josh Lehan31058fd2023-01-13 11:06:16 -0800163 // If none of the inputs were acceptable, use the setpoint as
164 // the input value. This will continue to run the PID loop, but
165 // make it a no-op, as the error will be zero. This provides safe
166 // behavior until the inputs become acceptable.
167 value = setptProc();
James Feist734f9532018-11-15 12:13:18 -0800168 }
169
Bonnie Loc51ba912022-10-12 14:07:22 +0800170 if (debugEnabled)
171 {
172 std::cerr << getID() << " choose the temperature value: " << value
Harvey Wu37180062023-10-02 09:42:50 +0800173 << " " << leaderName << "\n";
Bonnie Loc51ba912022-10-12 14:07:22 +0800174 }
175
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800176 return value;
Patrick Ventured8012182018-03-08 08:21:38 -0800177}
178
179// bmc_get_setpt
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800180double ThermalController::setptProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800181{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800182 double setpoint = getSetpoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800183
184 /* TODO(venture): Thermal setpoint invalid? */
185#if 0
186 if (-1 == setpoint)
187 {
188 return 0.0f;
189 }
190 else
191 {
192 return setpoint;
193 }
194#endif
195 return setpoint;
196}
197
198// bmc_set_pid_output
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800199void ThermalController::outputProc(double value)
Patrick Ventured8012182018-03-08 08:21:38 -0800200{
Nirav Shahccc8bb62022-02-17 21:06:51 -0800201 _owner->addSetPoint(value, _id);
Harvey Wu37180062023-10-02 09:42:50 +0800202 _owner->updateThermalPowerDebugInterface(_id, "", 0, value);
Patrick Ventured8012182018-03-08 08:21:38 -0800203
Bonnie Loc51ba912022-10-12 14:07:22 +0800204 if (debugEnabled)
205 {
206 std::cerr << getID() << " pid output pwm: " << value << "\n";
207 }
208
Patrick Ventured8012182018-03-08 08:21:38 -0800209 return;
210}
Patrick Venturea0764872020-08-08 07:48:43 -0700211
212} // namespace pid_control