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, |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 58 | const std::vector<std::string>& 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 | ca79115 | 2020-09-20 23:41:39 -0700 | [diff] [blame] | 104 | bool acceptable = false; |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 105 | for (const auto& in : _inputs) |
| 106 | { |
Josh Lehan | ca79115 | 2020-09-20 23:41:39 -0700 | [diff] [blame] | 107 | double cachedValue = _owner->getCachedValue(in); |
| 108 | |
| 109 | // Less than 0 is perfectly OK for temperature, but must not be NAN |
| 110 | if (!(std::isfinite(cachedValue))) |
| 111 | { |
| 112 | continue; |
| 113 | } |
| 114 | |
Josh Lehan | 23e22b9 | 2022-11-12 22:37:58 -0800 | [diff] [blame] | 115 | if (doSummation) |
| 116 | { |
| 117 | value += cachedValue; |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | value = compare(value, cachedValue); |
| 122 | } |
| 123 | |
Josh Lehan | ca79115 | 2020-09-20 23:41:39 -0700 | [diff] [blame] | 124 | acceptable = true; |
| 125 | } |
| 126 | |
| 127 | if (!acceptable) |
| 128 | { |
| 129 | // While not optimal, zero is better than garbage |
| 130 | value = 0; |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 133 | if (debugEnabled) |
| 134 | { |
| 135 | std::cerr << getID() << " choose the temperature value: " << value |
| 136 | << "\n"; |
| 137 | } |
| 138 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 139 | return value; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // bmc_get_setpt |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 143 | double ThermalController::setptProc(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 144 | { |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 145 | double setpoint = getSetpoint(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 146 | |
| 147 | /* TODO(venture): Thermal setpoint invalid? */ |
| 148 | #if 0 |
| 149 | if (-1 == setpoint) |
| 150 | { |
| 151 | return 0.0f; |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | return setpoint; |
| 156 | } |
| 157 | #endif |
| 158 | return setpoint; |
| 159 | } |
| 160 | |
| 161 | // bmc_set_pid_output |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 162 | void ThermalController::outputProc(double value) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 163 | { |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 164 | _owner->addSetPoint(value, _id); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 165 | |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 166 | if (debugEnabled) |
| 167 | { |
| 168 | std::cerr << getID() << " pid output pwm: " << value << "\n"; |
| 169 | } |
| 170 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 171 | return; |
| 172 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 173 | |
| 174 | } // namespace pid_control |