blob: b9ea1c26e407382f60f5adde34d1ea7c01c46c37 [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
Ed Tanousf8b6e552025-06-27 13:27:50 -070019#include "conf.hpp"
20#include "ec/pid.hpp"
James Feist734f9532018-11-15 12:13:18 -080021#include "errors/exception.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -070022#include "pidcontroller.hpp"
Bonnie Loc51ba912022-10-12 14:07:22 +080023#include "tuning.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080024#include "util.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -070025#include "zone_interface.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080026
Patrick Venturee94bdc42018-11-15 18:41:21 -080027#include <algorithm>
Josh Lehanca791152020-09-20 23:41:39 -070028#include <cmath>
29#include <iostream>
Ed Tanousf8b6e552025-06-27 13:27:50 -070030#include <limits>
31#include <memory>
32#include <string>
33#include <vector>
Patrick Venturee94bdc42018-11-15 18:41:21 -080034
Patrick Venturea0764872020-08-08 07:48:43 -070035namespace pid_control
36{
37
Patrick Venturee94bdc42018-11-15 18:41:21 -080038ThermalType getThermalType(const std::string& typeString)
39{
Josh Lehan23e22b92022-11-12 22:37:58 -080040 if (typeString == "margin")
41 {
42 return ThermalType::margin;
43 }
44 if ((typeString == "temp") || (typeString == "power"))
45 {
46 return ThermalType::absolute;
47 }
48 if (typeString == "powersum")
49 {
50 return ThermalType::summation;
51 }
52
53 throw ControllerBuildException("Unrecognized PID Type/Class string");
Patrick Venturee94bdc42018-11-15 18:41:21 -080054}
55
56bool isThermalType(const std::string& typeString)
57{
Patrick Williamsbd63bca2024-08-16 15:21:10 -040058 static const std::vector<std::string> thermalTypes = {
59 "temp", "margin", "power", "powersum"};
Patrick Venturee94bdc42018-11-15 18:41:21 -080060 return std::count(thermalTypes.begin(), thermalTypes.end(), typeString);
61}
62
Patrick Venture563a3562018-10-30 09:31:26 -070063std::unique_ptr<PIDController> ThermalController::createThermalPid(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070064 ZoneInterface* owner, const std::string& id,
Josh Lehan31058fd2023-01-13 11:06:16 -080065 const std::vector<pid_control::conf::SensorInput>& inputs, double setpoint,
James Feist734f9532018-11-15 12:13:18 -080066 const ec::pidinfo& initial, const ThermalType& type)
Patrick Ventured8012182018-03-08 08:21:38 -080067{
James Feist734f9532018-11-15 12:13:18 -080068 // ThermalController requires at least 1 input
69 if (inputs.empty())
Patrick Venture3349ef22018-06-12 14:09:29 -070070 {
James Feist734f9532018-11-15 12:13:18 -080071 throw ControllerBuildException("Thermal controller missing inputs");
Patrick Venture3349ef22018-06-12 14:09:29 -070072 }
73
James Feist734f9532018-11-15 12:13:18 -080074 auto thermal = std::make_unique<ThermalController>(id, inputs, type, owner);
Patrick Ventured8012182018-03-08 08:21:38 -080075
Patrick Venture563a3562018-10-30 09:31:26 -070076 ec::pid_info_t* info = thermal->getPIDInfo();
77 thermal->setSetpoint(setpoint);
Patrick Ventured8012182018-03-08 08:21:38 -080078
Patrick Venture7af157b2018-10-30 11:24:40 -070079 initializePIDStruct(info, initial);
Patrick Ventured8012182018-03-08 08:21:38 -080080
81 return thermal;
82}
83
Patrick Venture5f59c0f2018-11-11 12:55:14 -080084// bmc_host_sensor_value_double
85double ThermalController::inputProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080086{
James Feist734f9532018-11-15 12:13:18 -080087 double value;
88 const double& (*compare)(const double&, const double&);
Josh Lehan23e22b92022-11-12 22:37:58 -080089 bool doSummation = false;
90
James Feist734f9532018-11-15 12:13:18 -080091 if (type == ThermalType::margin)
92 {
93 value = std::numeric_limits<double>::max();
94 compare = std::min<double>;
95 }
Josh Lehan23e22b92022-11-12 22:37:58 -080096 else if (type == ThermalType::absolute)
James Feist734f9532018-11-15 12:13:18 -080097 {
98 value = std::numeric_limits<double>::lowest();
99 compare = std::max<double>;
100 }
Josh Lehan23e22b92022-11-12 22:37:58 -0800101 else if (type == ThermalType::summation)
102 {
103 doSummation = true;
104 value = 0.0;
105 }
106 else
107 {
108 throw ControllerBuildException("Unrecognized ThermalType");
109 }
James Feist734f9532018-11-15 12:13:18 -0800110
Josh Lehan31058fd2023-01-13 11:06:16 -0800111 std::string leaderName = _inputs.begin()->name;
Harvey Wu37180062023-10-02 09:42:50 +0800112
Josh Lehanca791152020-09-20 23:41:39 -0700113 bool acceptable = false;
James Feist734f9532018-11-15 12:13:18 -0800114 for (const auto& in : _inputs)
115 {
Josh Lehan31058fd2023-01-13 11:06:16 -0800116 double cachedValue = _owner->getCachedValue(in.name);
Josh Lehanca791152020-09-20 23:41:39 -0700117
118 // Less than 0 is perfectly OK for temperature, but must not be NAN
119 if (!(std::isfinite(cachedValue)))
120 {
121 continue;
122 }
123
Josh Lehan31058fd2023-01-13 11:06:16 -0800124 // Perform TempToMargin conversion before further processing
125 if (type == ThermalType::margin)
126 {
127 if (in.convertTempToMargin)
128 {
129 if (!(std::isfinite(in.convertMarginZero)))
130 {
131 throw ControllerBuildException("Unrecognized TempToMargin");
132 }
133
134 double marginValue = in.convertMarginZero - cachedValue;
135
136 if (debugEnabled)
137 {
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400138 std::cerr
139 << "Converting temp to margin: temp " << cachedValue
140 << ", Tjmax " << in.convertMarginZero << ", margin "
141 << marginValue << "\n";
Josh Lehan31058fd2023-01-13 11:06:16 -0800142 }
143
144 cachedValue = marginValue;
145 }
146 }
147
Harvey Wu37180062023-10-02 09:42:50 +0800148 double oldValue = value;
149
Josh Lehan23e22b92022-11-12 22:37:58 -0800150 if (doSummation)
151 {
152 value += cachedValue;
153 }
154 else
155 {
156 value = compare(value, cachedValue);
157 }
158
Harvey Wu37180062023-10-02 09:42:50 +0800159 if (oldValue != value)
160 {
Josh Lehan31058fd2023-01-13 11:06:16 -0800161 leaderName = in.name;
Harvey Wu37180062023-10-02 09:42:50 +0800162 _owner->updateThermalPowerDebugInterface(_id, leaderName, value, 0);
163 }
164
Josh Lehanca791152020-09-20 23:41:39 -0700165 acceptable = true;
166 }
167
168 if (!acceptable)
169 {
Josh Lehan31058fd2023-01-13 11:06:16 -0800170 // If none of the inputs were acceptable, use the setpoint as
171 // the input value. This will continue to run the PID loop, but
172 // make it a no-op, as the error will be zero. This provides safe
173 // behavior until the inputs become acceptable.
174 value = setptProc();
James Feist734f9532018-11-15 12:13:18 -0800175 }
176
Bonnie Loc51ba912022-10-12 14:07:22 +0800177 if (debugEnabled)
178 {
179 std::cerr << getID() << " choose the temperature value: " << value
Harvey Wu37180062023-10-02 09:42:50 +0800180 << " " << leaderName << "\n";
Bonnie Loc51ba912022-10-12 14:07:22 +0800181 }
182
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800183 return value;
Patrick Ventured8012182018-03-08 08:21:38 -0800184}
185
186// bmc_get_setpt
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800187double ThermalController::setptProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800188{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800189 double setpoint = getSetpoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800190
191 /* TODO(venture): Thermal setpoint invalid? */
192#if 0
193 if (-1 == setpoint)
194 {
195 return 0.0f;
196 }
197 else
198 {
199 return setpoint;
200 }
201#endif
202 return setpoint;
203}
204
205// bmc_set_pid_output
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800206void ThermalController::outputProc(double value)
Patrick Ventured8012182018-03-08 08:21:38 -0800207{
Nirav Shahccc8bb62022-02-17 21:06:51 -0800208 _owner->addSetPoint(value, _id);
Harvey Wu37180062023-10-02 09:42:50 +0800209 _owner->updateThermalPowerDebugInterface(_id, "", 0, value);
Patrick Ventured8012182018-03-08 08:21:38 -0800210
Bonnie Loc51ba912022-10-12 14:07:22 +0800211 if (debugEnabled)
212 {
213 std::cerr << getID() << " pid output pwm: " << value << "\n";
214 }
215
Patrick Ventured8012182018-03-08 08:21:38 -0800216 return;
217}
Patrick Venturea0764872020-08-08 07:48:43 -0700218
219} // namespace pid_control