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 "conf.hpp"
20 #include "ec/pid.hpp"
21 #include "errors/exception.hpp"
22 #include "pidcontroller.hpp"
23 #include "tuning.hpp"
24 #include "util.hpp"
25 #include "zone_interface.hpp"
26
27 #include <algorithm>
28 #include <cmath>
29 #include <iostream>
30 #include <limits>
31 #include <memory>
32 #include <string>
33 #include <vector>
34
35 namespace pid_control
36 {
37
getThermalType(const std::string & typeString)38 ThermalType getThermalType(const std::string& typeString)
39 {
40 if (typeString == "margin")
41 {
42 return ThermalType::margin;
43 }
44 if ((typeString == "temp") || (typeString == "power"))
45 {
46 return ThermalType::absolute;
47 }
48 if (typeString == "powersum")
49 {
50 return ThermalType::summation;
51 }
52
53 throw ControllerBuildException("Unrecognized PID Type/Class string");
54 }
55
isThermalType(const std::string & typeString)56 bool isThermalType(const std::string& typeString)
57 {
58 static const std::vector<std::string> thermalTypes = {
59 "temp", "margin", "power", "powersum"};
60 return std::count(thermalTypes.begin(), thermalTypes.end(), typeString);
61 }
62
createThermalPid(ZoneInterface * owner,const std::string & id,const std::vector<pid_control::conf::SensorInput> & inputs,double setpoint,const ec::pidinfo & initial,const ThermalType & type)63 std::unique_ptr<PIDController> ThermalController::createThermalPid(
64 ZoneInterface* owner, const std::string& id,
65 const std::vector<pid_control::conf::SensorInput>& inputs, double setpoint,
66 const ec::pidinfo& initial, const ThermalType& type)
67 {
68 // ThermalController requires at least 1 input
69 if (inputs.empty())
70 {
71 throw ControllerBuildException("Thermal controller missing inputs");
72 }
73
74 auto thermal = std::make_unique<ThermalController>(id, inputs, type, owner);
75
76 ec::pid_info_t* info = thermal->getPIDInfo();
77 thermal->setSetpoint(setpoint);
78
79 initializePIDStruct(info, initial);
80
81 return thermal;
82 }
83
84 // bmc_host_sensor_value_double
inputProc(void)85 double ThermalController::inputProc(void)
86 {
87 double value;
88 const double& (*compare)(const double&, const double&);
89 bool doSummation = false;
90
91 if (type == ThermalType::margin)
92 {
93 value = std::numeric_limits<double>::max();
94 compare = std::min<double>;
95 }
96 else if (type == ThermalType::absolute)
97 {
98 value = std::numeric_limits<double>::lowest();
99 compare = std::max<double>;
100 }
101 else if (type == ThermalType::summation)
102 {
103 doSummation = true;
104 value = 0.0;
105 }
106 else
107 {
108 throw ControllerBuildException("Unrecognized ThermalType");
109 }
110
111 std::string leaderName = _inputs.begin()->name;
112
113 bool acceptable = false;
114 for (const auto& in : _inputs)
115 {
116 double cachedValue = _owner->getCachedValue(in.name);
117
118 // Less than 0 is perfectly OK for temperature, but must not be NAN
119 if (!(std::isfinite(cachedValue)))
120 {
121 continue;
122 }
123
124 // Perform TempToMargin conversion before further processing
125 if (type == ThermalType::margin)
126 {
127 if (in.convertTempToMargin)
128 {
129 if (!(std::isfinite(in.convertMarginZero)))
130 {
131 throw ControllerBuildException("Unrecognized TempToMargin");
132 }
133
134 double marginValue = in.convertMarginZero - cachedValue;
135
136 if (debugEnabled)
137 {
138 std::cerr
139 << "Converting temp to margin: temp " << cachedValue
140 << ", Tjmax " << in.convertMarginZero << ", margin "
141 << marginValue << "\n";
142 }
143
144 cachedValue = marginValue;
145 }
146 }
147
148 double oldValue = value;
149
150 if (doSummation)
151 {
152 value += cachedValue;
153 }
154 else
155 {
156 value = compare(value, cachedValue);
157 }
158
159 if (oldValue != value)
160 {
161 leaderName = in.name;
162 _owner->updateThermalPowerDebugInterface(_id, leaderName, value, 0);
163 }
164
165 acceptable = true;
166 }
167
168 if (!acceptable)
169 {
170 // If none of the inputs were acceptable, use the setpoint as
171 // the input value. This will continue to run the PID loop, but
172 // make it a no-op, as the error will be zero. This provides safe
173 // behavior until the inputs become acceptable.
174 value = setptProc();
175 }
176
177 if (debugEnabled)
178 {
179 std::cerr << getID() << " choose the temperature value: " << value
180 << " " << leaderName << "\n";
181 }
182
183 return value;
184 }
185
186 // bmc_get_setpt
setptProc(void)187 double ThermalController::setptProc(void)
188 {
189 double setpoint = getSetpoint();
190
191 /* TODO(venture): Thermal setpoint invalid? */
192 #if 0
193 if (-1 == setpoint)
194 {
195 return 0.0f;
196 }
197 else
198 {
199 return setpoint;
200 }
201 #endif
202 return setpoint;
203 }
204
205 // bmc_set_pid_output
outputProc(double value)206 void ThermalController::outputProc(double value)
207 {
208 _owner->addSetPoint(value, _id);
209 _owner->updateThermalPowerDebugInterface(_id, "", 0, value);
210
211 if (debugEnabled)
212 {
213 std::cerr << getID() << " pid output pwm: " << value << "\n";
214 }
215
216 return;
217 }
218
219 } // namespace pid_control
220