| Alexander Hansen | 46a755f | 2025-10-27 16:31:08 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright 2017 Google Inc |
| Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 3 | |
| 4 | #include "thermalcontroller.hpp" |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 5 | |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 6 | #include "conf.hpp" |
| 7 | #include "ec/pid.hpp" |
| James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 8 | #include "errors/exception.hpp" |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 9 | #include "pidcontroller.hpp" |
| Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 10 | #include "tuning.hpp" |
| Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 11 | #include "util.hpp" |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 12 | #include "zone_interface.hpp" |
| Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 13 | |
| Patrick Venture | e94bdc4 | 2018-11-15 18:41:21 -0800 | [diff] [blame] | 14 | #include <algorithm> |
| Josh Lehan | ca79115 | 2020-09-20 23:41:39 -0700 | [diff] [blame] | 15 | #include <cmath> |
| 16 | #include <iostream> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 17 | #include <limits> |
| 18 | #include <memory> |
| 19 | #include <string> |
| 20 | #include <vector> |
| Patrick Venture | e94bdc4 | 2018-11-15 18:41:21 -0800 | [diff] [blame] | 21 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 22 | namespace pid_control |
| 23 | { |
| 24 | |
| Patrick Venture | e94bdc4 | 2018-11-15 18:41:21 -0800 | [diff] [blame] | 25 | ThermalType getThermalType(const std::string& typeString) |
| 26 | { |
| Josh Lehan | 23e22b9 | 2022-11-12 22:37:58 -0800 | [diff] [blame] | 27 | 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 Venture | e94bdc4 | 2018-11-15 18:41:21 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | bool isThermalType(const std::string& typeString) |
| 44 | { |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 45 | static const std::vector<std::string> thermalTypes = { |
| 46 | "temp", "margin", "power", "powersum"}; |
| Patrick Venture | e94bdc4 | 2018-11-15 18:41:21 -0800 | [diff] [blame] | 47 | return std::count(thermalTypes.begin(), thermalTypes.end(), typeString); |
| 48 | } |
| 49 | |
| Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 50 | std::unique_ptr<PIDController> ThermalController::createThermalPid( |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 51 | ZoneInterface* owner, const std::string& id, |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 52 | const std::vector<pid_control::conf::SensorInput>& inputs, double setpoint, |
| James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 53 | const ec::pidinfo& initial, const ThermalType& type) |
| Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 54 | { |
| James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 55 | // ThermalController requires at least 1 input |
| 56 | if (inputs.empty()) |
| Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 57 | { |
| James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 58 | throw ControllerBuildException("Thermal controller missing inputs"); |
| Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 61 | auto thermal = std::make_unique<ThermalController>(id, inputs, type, owner); |
| Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 62 | |
| Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 63 | ec::pid_info_t* info = thermal->getPIDInfo(); |
| 64 | thermal->setSetpoint(setpoint); |
| Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 65 | |
| Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 66 | initializePIDStruct(info, initial); |
| Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 67 | |
| 68 | return thermal; |
| 69 | } |
| 70 | |
| Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 71 | // bmc_host_sensor_value_double |
| 72 | double ThermalController::inputProc(void) |
| Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 73 | { |
| James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 74 | double value; |
| 75 | const double& (*compare)(const double&, const double&); |
| Josh Lehan | 23e22b9 | 2022-11-12 22:37:58 -0800 | [diff] [blame] | 76 | bool doSummation = false; |
| 77 | |
| James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 78 | if (type == ThermalType::margin) |
| 79 | { |
| 80 | value = std::numeric_limits<double>::max(); |
| 81 | compare = std::min<double>; |
| 82 | } |
| Josh Lehan | 23e22b9 | 2022-11-12 22:37:58 -0800 | [diff] [blame] | 83 | else if (type == ThermalType::absolute) |
| James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 84 | { |
| 85 | value = std::numeric_limits<double>::lowest(); |
| 86 | compare = std::max<double>; |
| 87 | } |
| Josh Lehan | 23e22b9 | 2022-11-12 22:37:58 -0800 | [diff] [blame] | 88 | else if (type == ThermalType::summation) |
| 89 | { |
| 90 | doSummation = true; |
| 91 | value = 0.0; |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | throw ControllerBuildException("Unrecognized ThermalType"); |
| 96 | } |
| James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 97 | |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 98 | std::string leaderName = _inputs.begin()->name; |
| Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 99 | |
| Josh Lehan | ca79115 | 2020-09-20 23:41:39 -0700 | [diff] [blame] | 100 | bool acceptable = false; |
| James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 101 | for (const auto& in : _inputs) |
| 102 | { |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 103 | double cachedValue = _owner->getCachedValue(in.name); |
| Josh Lehan | ca79115 | 2020-09-20 23:41:39 -0700 | [diff] [blame] | 104 | |
| 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 Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 111 | // 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 Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 125 | std::cerr |
| 126 | << "Converting temp to margin: temp " << cachedValue |
| 127 | << ", Tjmax " << in.convertMarginZero << ", margin " |
| 128 | << marginValue << "\n"; |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | cachedValue = marginValue; |
| 132 | } |
| 133 | } |
| 134 | |
| Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 135 | double oldValue = value; |
| 136 | |
| Josh Lehan | 23e22b9 | 2022-11-12 22:37:58 -0800 | [diff] [blame] | 137 | if (doSummation) |
| 138 | { |
| 139 | value += cachedValue; |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | value = compare(value, cachedValue); |
| 144 | } |
| 145 | |
| Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 146 | if (oldValue != value) |
| 147 | { |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 148 | leaderName = in.name; |
| Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 149 | _owner->updateThermalPowerDebugInterface(_id, leaderName, value, 0); |
| 150 | } |
| 151 | |
| Josh Lehan | ca79115 | 2020-09-20 23:41:39 -0700 | [diff] [blame] | 152 | acceptable = true; |
| 153 | } |
| 154 | |
| 155 | if (!acceptable) |
| 156 | { |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 157 | // 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 Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 164 | if (debugEnabled) |
| 165 | { |
| 166 | std::cerr << getID() << " choose the temperature value: " << value |
| Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 167 | << " " << leaderName << "\n"; |
| Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 168 | } |
| 169 | |
| Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 170 | return value; |
| Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // bmc_get_setpt |
| Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 174 | double ThermalController::setptProc(void) |
| Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 175 | { |
| Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 176 | double setpoint = getSetpoint(); |
| Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 177 | |
| 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 Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 193 | void ThermalController::outputProc(double value) |
| Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 194 | { |
| Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 195 | _owner->addSetPoint(value, _id); |
| Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 196 | _owner->updateThermalPowerDebugInterface(_id, "", 0, value); |
| Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 197 | |
| Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 198 | if (debugEnabled) |
| 199 | { |
| 200 | std::cerr << getID() << " pid output pwm: " << value << "\n"; |
| 201 | } |
| 202 | |
| Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 203 | return; |
| 204 | } |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 205 | |
| 206 | } // namespace pid_control |