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