blob: 0b588555e6fb01636d7efc63e22b1ee5624cb221 [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
4#include "thermalcontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07005
Ed Tanousf8b6e552025-06-27 13:27:50 -07006#include "conf.hpp"
7#include "ec/pid.hpp"
James Feist734f9532018-11-15 12:13:18 -08008#include "errors/exception.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -07009#include "pidcontroller.hpp"
Bonnie Loc51ba912022-10-12 14:07:22 +080010#include "tuning.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080011#include "util.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -070012#include "zone_interface.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080013
Patrick Venturee94bdc42018-11-15 18:41:21 -080014#include <algorithm>
Josh Lehanca791152020-09-20 23:41:39 -070015#include <cmath>
16#include <iostream>
Ed Tanousf8b6e552025-06-27 13:27:50 -070017#include <limits>
18#include <memory>
19#include <string>
20#include <vector>
Patrick Venturee94bdc42018-11-15 18:41:21 -080021
Patrick Venturea0764872020-08-08 07:48:43 -070022namespace pid_control
23{
24
Patrick Venturee94bdc42018-11-15 18:41:21 -080025ThermalType getThermalType(const std::string& typeString)
26{
Josh Lehan23e22b92022-11-12 22:37:58 -080027 if (typeString == "margin")
28 {
29 return ThermalType::margin;
30 }
31 if ((typeString == "temp") || (typeString == "power"))
32 {
33 return ThermalType::absolute;
34 }
35 if (typeString == "powersum")
36 {
37 return ThermalType::summation;
38 }
39
40 throw ControllerBuildException("Unrecognized PID Type/Class string");
Patrick Venturee94bdc42018-11-15 18:41:21 -080041}
42
43bool isThermalType(const std::string& typeString)
44{
Patrick Williamsbd63bca2024-08-16 15:21:10 -040045 static const std::vector<std::string> thermalTypes = {
46 "temp", "margin", "power", "powersum"};
Patrick Venturee94bdc42018-11-15 18:41:21 -080047 return std::count(thermalTypes.begin(), thermalTypes.end(), typeString);
48}
49
Patrick Venture563a3562018-10-30 09:31:26 -070050std::unique_ptr<PIDController> ThermalController::createThermalPid(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070051 ZoneInterface* owner, const std::string& id,
Josh Lehan31058fd2023-01-13 11:06:16 -080052 const std::vector<pid_control::conf::SensorInput>& inputs, double setpoint,
James Feist734f9532018-11-15 12:13:18 -080053 const ec::pidinfo& initial, const ThermalType& type)
Patrick Ventured8012182018-03-08 08:21:38 -080054{
James Feist734f9532018-11-15 12:13:18 -080055 // ThermalController requires at least 1 input
56 if (inputs.empty())
Patrick Venture3349ef22018-06-12 14:09:29 -070057 {
James Feist734f9532018-11-15 12:13:18 -080058 throw ControllerBuildException("Thermal controller missing inputs");
Patrick Venture3349ef22018-06-12 14:09:29 -070059 }
60
James Feist734f9532018-11-15 12:13:18 -080061 auto thermal = std::make_unique<ThermalController>(id, inputs, type, owner);
Patrick Ventured8012182018-03-08 08:21:38 -080062
Patrick Venture563a3562018-10-30 09:31:26 -070063 ec::pid_info_t* info = thermal->getPIDInfo();
64 thermal->setSetpoint(setpoint);
Patrick Ventured8012182018-03-08 08:21:38 -080065
Patrick Venture7af157b2018-10-30 11:24:40 -070066 initializePIDStruct(info, initial);
Patrick Ventured8012182018-03-08 08:21:38 -080067
68 return thermal;
69}
70
Patrick Venture5f59c0f2018-11-11 12:55:14 -080071// bmc_host_sensor_value_double
72double ThermalController::inputProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080073{
James Feist734f9532018-11-15 12:13:18 -080074 double value;
75 const double& (*compare)(const double&, const double&);
Josh Lehan23e22b92022-11-12 22:37:58 -080076 bool doSummation = false;
77
James Feist734f9532018-11-15 12:13:18 -080078 if (type == ThermalType::margin)
79 {
80 value = std::numeric_limits<double>::max();
81 compare = std::min<double>;
82 }
Josh Lehan23e22b92022-11-12 22:37:58 -080083 else if (type == ThermalType::absolute)
James Feist734f9532018-11-15 12:13:18 -080084 {
85 value = std::numeric_limits<double>::lowest();
86 compare = std::max<double>;
87 }
Josh Lehan23e22b92022-11-12 22:37:58 -080088 else if (type == ThermalType::summation)
89 {
90 doSummation = true;
91 value = 0.0;
92 }
93 else
94 {
95 throw ControllerBuildException("Unrecognized ThermalType");
96 }
James Feist734f9532018-11-15 12:13:18 -080097
Josh Lehan31058fd2023-01-13 11:06:16 -080098 std::string leaderName = _inputs.begin()->name;
Harvey Wu37180062023-10-02 09:42:50 +080099
Josh Lehanca791152020-09-20 23:41:39 -0700100 bool acceptable = false;
James Feist734f9532018-11-15 12:13:18 -0800101 for (const auto& in : _inputs)
102 {
Josh Lehan31058fd2023-01-13 11:06:16 -0800103 double cachedValue = _owner->getCachedValue(in.name);
Josh Lehanca791152020-09-20 23:41:39 -0700104
105 // Less than 0 is perfectly OK for temperature, but must not be NAN
106 if (!(std::isfinite(cachedValue)))
107 {
108 continue;
109 }
110
Josh Lehan31058fd2023-01-13 11:06:16 -0800111 // Perform TempToMargin conversion before further processing
112 if (type == ThermalType::margin)
113 {
114 if (in.convertTempToMargin)
115 {
116 if (!(std::isfinite(in.convertMarginZero)))
117 {
118 throw ControllerBuildException("Unrecognized TempToMargin");
119 }
120
121 double marginValue = in.convertMarginZero - cachedValue;
122
123 if (debugEnabled)
124 {
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400125 std::cerr
126 << "Converting temp to margin: temp " << cachedValue
127 << ", Tjmax " << in.convertMarginZero << ", margin "
128 << marginValue << "\n";
Josh Lehan31058fd2023-01-13 11:06:16 -0800129 }
130
131 cachedValue = marginValue;
132 }
133 }
134
Harvey Wu37180062023-10-02 09:42:50 +0800135 double oldValue = value;
136
Josh Lehan23e22b92022-11-12 22:37:58 -0800137 if (doSummation)
138 {
139 value += cachedValue;
140 }
141 else
142 {
143 value = compare(value, cachedValue);
144 }
145
Harvey Wu37180062023-10-02 09:42:50 +0800146 if (oldValue != value)
147 {
Josh Lehan31058fd2023-01-13 11:06:16 -0800148 leaderName = in.name;
Harvey Wu37180062023-10-02 09:42:50 +0800149 _owner->updateThermalPowerDebugInterface(_id, leaderName, value, 0);
150 }
151
Josh Lehanca791152020-09-20 23:41:39 -0700152 acceptable = true;
153 }
154
155 if (!acceptable)
156 {
Josh Lehan31058fd2023-01-13 11:06:16 -0800157 // If none of the inputs were acceptable, use the setpoint as
158 // the input value. This will continue to run the PID loop, but
159 // make it a no-op, as the error will be zero. This provides safe
160 // behavior until the inputs become acceptable.
161 value = setptProc();
James Feist734f9532018-11-15 12:13:18 -0800162 }
163
Bonnie Loc51ba912022-10-12 14:07:22 +0800164 if (debugEnabled)
165 {
166 std::cerr << getID() << " choose the temperature value: " << value
Harvey Wu37180062023-10-02 09:42:50 +0800167 << " " << leaderName << "\n";
Bonnie Loc51ba912022-10-12 14:07:22 +0800168 }
169
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800170 return value;
Patrick Ventured8012182018-03-08 08:21:38 -0800171}
172
173// bmc_get_setpt
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800174double ThermalController::setptProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800175{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800176 double setpoint = getSetpoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800177
178 /* TODO(venture): Thermal setpoint invalid? */
179#if 0
180 if (-1 == setpoint)
181 {
182 return 0.0f;
183 }
184 else
185 {
186 return setpoint;
187 }
188#endif
189 return setpoint;
190}
191
192// bmc_set_pid_output
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800193void ThermalController::outputProc(double value)
Patrick Ventured8012182018-03-08 08:21:38 -0800194{
Nirav Shahccc8bb62022-02-17 21:06:51 -0800195 _owner->addSetPoint(value, _id);
Harvey Wu37180062023-10-02 09:42:50 +0800196 _owner->updateThermalPowerDebugInterface(_id, "", 0, value);
Patrick Ventured8012182018-03-08 08:21:38 -0800197
Bonnie Loc51ba912022-10-12 14:07:22 +0800198 if (debugEnabled)
199 {
200 std::cerr << getID() << " pid output pwm: " << value << "\n";
201 }
202
Patrick Ventured8012182018-03-08 08:21:38 -0800203 return;
204}
Patrick Venturea0764872020-08-08 07:48:43 -0700205
206} // namespace pid_control