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 | { |
| 33 | /* Currently it only supports the two types. */ |
| 34 | return (typeString == "temp") ? ThermalType::absolute : ThermalType::margin; |
| 35 | } |
| 36 | |
| 37 | bool isThermalType(const std::string& typeString) |
| 38 | { |
| 39 | static const std::vector<std::string> thermalTypes = {"temp", "margin"}; |
| 40 | return std::count(thermalTypes.begin(), thermalTypes.end(), typeString); |
| 41 | } |
| 42 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 43 | std::unique_ptr<PIDController> ThermalController::createThermalPid( |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 44 | ZoneInterface* owner, const std::string& id, |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 45 | const std::vector<std::string>& inputs, double setpoint, |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 46 | const ec::pidinfo& initial, const ThermalType& type) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 47 | { |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 48 | // ThermalController requires at least 1 input |
| 49 | if (inputs.empty()) |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 50 | { |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 51 | throw ControllerBuildException("Thermal controller missing inputs"); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 52 | return nullptr; |
| 53 | } |
| 54 | |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 55 | auto thermal = std::make_unique<ThermalController>(id, inputs, type, owner); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 56 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 57 | ec::pid_info_t* info = thermal->getPIDInfo(); |
| 58 | thermal->setSetpoint(setpoint); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 59 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 60 | initializePIDStruct(info, initial); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 61 | |
| 62 | return thermal; |
| 63 | } |
| 64 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 65 | // bmc_host_sensor_value_double |
| 66 | double ThermalController::inputProc(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 67 | { |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 68 | double value; |
| 69 | const double& (*compare)(const double&, const double&); |
| 70 | if (type == ThermalType::margin) |
| 71 | { |
| 72 | value = std::numeric_limits<double>::max(); |
| 73 | compare = std::min<double>; |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | value = std::numeric_limits<double>::lowest(); |
| 78 | compare = std::max<double>; |
| 79 | } |
| 80 | |
Josh Lehan | ca79115 | 2020-09-20 23:41:39 -0700 | [diff] [blame] | 81 | bool acceptable = false; |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 82 | for (const auto& in : _inputs) |
| 83 | { |
Josh Lehan | ca79115 | 2020-09-20 23:41:39 -0700 | [diff] [blame] | 84 | double cachedValue = _owner->getCachedValue(in); |
| 85 | |
| 86 | // Less than 0 is perfectly OK for temperature, but must not be NAN |
| 87 | if (!(std::isfinite(cachedValue))) |
| 88 | { |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | value = compare(value, cachedValue); |
| 93 | acceptable = true; |
| 94 | } |
| 95 | |
| 96 | if (!acceptable) |
| 97 | { |
| 98 | // While not optimal, zero is better than garbage |
| 99 | value = 0; |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame^] | 102 | if (debugEnabled) |
| 103 | { |
| 104 | std::cerr << getID() << " choose the temperature value: " << value |
| 105 | << "\n"; |
| 106 | } |
| 107 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 108 | return value; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // bmc_get_setpt |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 112 | double ThermalController::setptProc(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 113 | { |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 114 | double setpoint = getSetpoint(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 115 | |
| 116 | /* TODO(venture): Thermal setpoint invalid? */ |
| 117 | #if 0 |
| 118 | if (-1 == setpoint) |
| 119 | { |
| 120 | return 0.0f; |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | return setpoint; |
| 125 | } |
| 126 | #endif |
| 127 | return setpoint; |
| 128 | } |
| 129 | |
| 130 | // bmc_set_pid_output |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 131 | void ThermalController::outputProc(double value) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 132 | { |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 133 | _owner->addSetPoint(value, _id); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 134 | |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame^] | 135 | if (debugEnabled) |
| 136 | { |
| 137 | std::cerr << getID() << " pid output pwm: " << value << "\n"; |
| 138 | } |
| 139 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 140 | return; |
| 141 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 142 | |
| 143 | } // namespace pid_control |