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