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" 18 19 #include "errors/exception.hpp" 20 #include "util.hpp" 21 #include "zone.hpp" 22 23 #include <algorithm> 24 #include <cmath> 25 #include <iostream> 26 27 namespace pid_control 28 { 29 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 42 std::unique_ptr<PIDController> ThermalController::createThermalPid( 43 ZoneInterface* owner, const std::string& id, 44 const std::vector<std::string>& inputs, double setpoint, 45 const ec::pidinfo& initial, const ThermalType& type) 46 { 47 // ThermalController requires at least 1 input 48 if (inputs.empty()) 49 { 50 throw ControllerBuildException("Thermal controller missing inputs"); 51 return nullptr; 52 } 53 54 auto thermal = std::make_unique<ThermalController>(id, inputs, type, owner); 55 56 ec::pid_info_t* info = thermal->getPIDInfo(); 57 thermal->setSetpoint(setpoint); 58 59 initializePIDStruct(info, initial); 60 61 return thermal; 62 } 63 64 // bmc_host_sensor_value_double 65 double ThermalController::inputProc(void) 66 { 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 80 bool acceptable = false; 81 for (const auto& in : _inputs) 82 { 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; 99 } 100 101 return value; 102 } 103 104 // bmc_get_setpt 105 double ThermalController::setptProc(void) 106 { 107 double setpoint = getSetpoint(); 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 124 void ThermalController::outputProc(double value) 125 { 126 _owner->addSetPoint(value); 127 128 return; 129 } 130 131 } // namespace pid_control 132