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