Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 1 | /** |
| 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 Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 18 | |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 19 | #include "errors/exception.hpp" |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 20 | #include "tuning.hpp" |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 21 | #include "util.hpp" |
| 22 | #include "zone.hpp" |
| 23 | |
Patrick Venture | e94bdc4 | 2018-11-15 18:41:21 -0800 | [diff] [blame] | 24 | #include <algorithm> |
Josh Lehan | ca79115 | 2020-09-20 23:41:39 -0700 | [diff] [blame] | 25 | #include <cmath> |
| 26 | #include <iostream> |
Patrick Venture | e94bdc4 | 2018-11-15 18:41:21 -0800 | [diff] [blame] | 27 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 28 | namespace pid_control |
| 29 | { |
| 30 | |
Patrick Venture | e94bdc4 | 2018-11-15 18:41:21 -0800 | [diff] [blame] | 31 | ThermalType getThermalType(const std::string& typeString) |
| 32 | { |
Josh Lehan | 23e22b9 | 2022-11-12 22:37:58 -0800 | [diff] [blame] | 33 | 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 Venture | e94bdc4 | 2018-11-15 18:41:21 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | bool isThermalType(const std::string& typeString) |
| 50 | { |
Josh Lehan | 23e22b9 | 2022-11-12 22:37:58 -0800 | [diff] [blame] | 51 | static const std::vector<std::string> thermalTypes = {"temp", "margin", |
| 52 | "power", "powersum"}; |
Patrick Venture | e94bdc4 | 2018-11-15 18:41:21 -0800 | [diff] [blame] | 53 | return std::count(thermalTypes.begin(), thermalTypes.end(), typeString); |
| 54 | } |
| 55 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 56 | std::unique_ptr<PIDController> ThermalController::createThermalPid( |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 57 | ZoneInterface* owner, const std::string& id, |
Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 58 | const std::vector<pid_control::conf::SensorInput>& inputs, double setpoint, |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 59 | const ec::pidinfo& initial, const ThermalType& type) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 60 | { |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 61 | // ThermalController requires at least 1 input |
| 62 | if (inputs.empty()) |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 63 | { |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 64 | throw ControllerBuildException("Thermal controller missing inputs"); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 65 | } |
| 66 | |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 67 | auto thermal = std::make_unique<ThermalController>(id, inputs, type, owner); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 68 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 69 | ec::pid_info_t* info = thermal->getPIDInfo(); |
| 70 | thermal->setSetpoint(setpoint); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 71 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 72 | initializePIDStruct(info, initial); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 73 | |
| 74 | return thermal; |
| 75 | } |
| 76 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 77 | // bmc_host_sensor_value_double |
| 78 | double ThermalController::inputProc(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 79 | { |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 80 | double value; |
| 81 | const double& (*compare)(const double&, const double&); |
Josh Lehan | 23e22b9 | 2022-11-12 22:37:58 -0800 | [diff] [blame] | 82 | bool doSummation = false; |
| 83 | |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 84 | if (type == ThermalType::margin) |
| 85 | { |
| 86 | value = std::numeric_limits<double>::max(); |
| 87 | compare = std::min<double>; |
| 88 | } |
Josh Lehan | 23e22b9 | 2022-11-12 22:37:58 -0800 | [diff] [blame] | 89 | else if (type == ThermalType::absolute) |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 90 | { |
| 91 | value = std::numeric_limits<double>::lowest(); |
| 92 | compare = std::max<double>; |
| 93 | } |
Josh Lehan | 23e22b9 | 2022-11-12 22:37:58 -0800 | [diff] [blame] | 94 | else if (type == ThermalType::summation) |
| 95 | { |
| 96 | doSummation = true; |
| 97 | value = 0.0; |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | throw ControllerBuildException("Unrecognized ThermalType"); |
| 102 | } |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 103 | |
Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 104 | std::string leaderName = _inputs.begin()->name; |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 105 | |
Josh Lehan | ca79115 | 2020-09-20 23:41:39 -0700 | [diff] [blame] | 106 | bool acceptable = false; |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 107 | for (const auto& in : _inputs) |
| 108 | { |
Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 109 | double cachedValue = _owner->getCachedValue(in.name); |
Josh Lehan | ca79115 | 2020-09-20 23:41:39 -0700 | [diff] [blame] | 110 | |
| 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 Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 117 | // 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 Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 141 | double oldValue = value; |
| 142 | |
Josh Lehan | 23e22b9 | 2022-11-12 22:37:58 -0800 | [diff] [blame] | 143 | if (doSummation) |
| 144 | { |
| 145 | value += cachedValue; |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | value = compare(value, cachedValue); |
| 150 | } |
| 151 | |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 152 | if (oldValue != value) |
| 153 | { |
Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 154 | leaderName = in.name; |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 155 | _owner->updateThermalPowerDebugInterface(_id, leaderName, value, 0); |
| 156 | } |
| 157 | |
Josh Lehan | ca79115 | 2020-09-20 23:41:39 -0700 | [diff] [blame] | 158 | acceptable = true; |
| 159 | } |
| 160 | |
| 161 | if (!acceptable) |
| 162 | { |
Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 163 | // 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 Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 170 | if (debugEnabled) |
| 171 | { |
| 172 | std::cerr << getID() << " choose the temperature value: " << value |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 173 | << " " << leaderName << "\n"; |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 174 | } |
| 175 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 176 | return value; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // bmc_get_setpt |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 180 | double ThermalController::setptProc(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 181 | { |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 182 | double setpoint = getSetpoint(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 183 | |
| 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 Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 199 | void ThermalController::outputProc(double value) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 200 | { |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 201 | _owner->addSetPoint(value, _id); |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 202 | _owner->updateThermalPowerDebugInterface(_id, "", 0, value); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 203 | |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 204 | if (debugEnabled) |
| 205 | { |
| 206 | std::cerr << getID() << " pid output pwm: " << value << "\n"; |
| 207 | } |
| 208 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 209 | return; |
| 210 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 211 | |
| 212 | } // namespace pid_control |