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 25 namespace pid_control 26 { 27 28 ThermalType getThermalType(const std::string& typeString) 29 { 30 /* Currently it only supports the two types. */ 31 return (typeString == "temp") ? ThermalType::absolute : ThermalType::margin; 32 } 33 34 bool isThermalType(const std::string& typeString) 35 { 36 static const std::vector<std::string> thermalTypes = {"temp", "margin"}; 37 return std::count(thermalTypes.begin(), thermalTypes.end(), typeString); 38 } 39 40 std::unique_ptr<PIDController> ThermalController::createThermalPid( 41 ZoneInterface* owner, const std::string& id, 42 const std::vector<std::string>& inputs, double setpoint, 43 const ec::pidinfo& initial, const ThermalType& type) 44 { 45 // ThermalController requires at least 1 input 46 if (inputs.empty()) 47 { 48 throw ControllerBuildException("Thermal controller missing inputs"); 49 return nullptr; 50 } 51 52 auto thermal = std::make_unique<ThermalController>(id, inputs, type, owner); 53 54 ec::pid_info_t* info = thermal->getPIDInfo(); 55 thermal->setSetpoint(setpoint); 56 57 initializePIDStruct(info, initial); 58 59 return thermal; 60 } 61 62 // bmc_host_sensor_value_double 63 double ThermalController::inputProc(void) 64 { 65 double value; 66 const double& (*compare)(const double&, const double&); 67 if (type == ThermalType::margin) 68 { 69 value = std::numeric_limits<double>::max(); 70 compare = std::min<double>; 71 } 72 else 73 { 74 value = std::numeric_limits<double>::lowest(); 75 compare = std::max<double>; 76 } 77 78 for (const auto& in : _inputs) 79 { 80 value = compare(value, _owner->getCachedValue(in)); 81 } 82 83 return value; 84 } 85 86 // bmc_get_setpt 87 double ThermalController::setptProc(void) 88 { 89 double setpoint = getSetpoint(); 90 91 /* TODO(venture): Thermal setpoint invalid? */ 92 #if 0 93 if (-1 == setpoint) 94 { 95 return 0.0f; 96 } 97 else 98 { 99 return setpoint; 100 } 101 #endif 102 return setpoint; 103 } 104 105 // bmc_set_pid_output 106 void ThermalController::outputProc(double value) 107 { 108 _owner->addSetPoint(value); 109 110 return; 111 } 112 113 } // namespace pid_control 114