blob: ff2c7bc0fd2288ade7151c6f3fe3681a6e69892c [file] [log] [blame]
Patrick Ventured8012182018-03-08 08:21:38 -08001/**
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"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070018
James Feist734f9532018-11-15 12:13:18 -080019#include "errors/exception.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080020#include "util.hpp"
21#include "zone.hpp"
22
Patrick Venture563a3562018-10-30 09:31:26 -070023std::unique_ptr<PIDController> ThermalController::createThermalPid(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070024 ZoneInterface* owner, const std::string& id,
Patrick Venture5f59c0f2018-11-11 12:55:14 -080025 const std::vector<std::string>& inputs, double setpoint,
James Feist734f9532018-11-15 12:13:18 -080026 const ec::pidinfo& initial, const ThermalType& type)
Patrick Ventured8012182018-03-08 08:21:38 -080027{
James Feist734f9532018-11-15 12:13:18 -080028 // ThermalController requires at least 1 input
29 if (inputs.empty())
Patrick Venture3349ef22018-06-12 14:09:29 -070030 {
James Feist734f9532018-11-15 12:13:18 -080031 throw ControllerBuildException("Thermal controller missing inputs");
Patrick Venture3349ef22018-06-12 14:09:29 -070032 return nullptr;
33 }
34
James Feist734f9532018-11-15 12:13:18 -080035 auto thermal = std::make_unique<ThermalController>(id, inputs, type, owner);
Patrick Ventured8012182018-03-08 08:21:38 -080036
Patrick Venture563a3562018-10-30 09:31:26 -070037 ec::pid_info_t* info = thermal->getPIDInfo();
38 thermal->setSetpoint(setpoint);
Patrick Ventured8012182018-03-08 08:21:38 -080039
Patrick Venture7af157b2018-10-30 11:24:40 -070040 initializePIDStruct(info, initial);
Patrick Ventured8012182018-03-08 08:21:38 -080041
42 return thermal;
43}
44
Patrick Venture5f59c0f2018-11-11 12:55:14 -080045// bmc_host_sensor_value_double
46double ThermalController::inputProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080047{
James Feist734f9532018-11-15 12:13:18 -080048 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
Patrick Venture5f59c0f2018-11-11 12:55:14 -080066 return value;
Patrick Ventured8012182018-03-08 08:21:38 -080067}
68
69// bmc_get_setpt
Patrick Venture5f59c0f2018-11-11 12:55:14 -080070double ThermalController::setptProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080071{
Patrick Venture5f59c0f2018-11-11 12:55:14 -080072 double setpoint = getSetpoint();
Patrick Ventured8012182018-03-08 08:21:38 -080073
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
Patrick Venture5f59c0f2018-11-11 12:55:14 -080089void ThermalController::outputProc(double value)
Patrick Ventured8012182018-03-08 08:21:38 -080090{
91 _owner->addRPMSetPoint(value);
92
93 return;
94}