blob: 35dcd1532c1fa1eda3e3f0c78ac64b6b7193204c [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 Venturee94bdc42018-11-15 18:41:21 -080023#include <algorithm>
24
Patrick Venturea0764872020-08-08 07:48:43 -070025namespace pid_control
26{
27
Patrick Venturee94bdc42018-11-15 18:41:21 -080028ThermalType getThermalType(const std::string& typeString)
29{
30 /* Currently it only supports the two types. */
31 return (typeString == "temp") ? ThermalType::absolute : ThermalType::margin;
32}
33
34bool 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
Patrick Venture563a3562018-10-30 09:31:26 -070040std::unique_ptr<PIDController> ThermalController::createThermalPid(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070041 ZoneInterface* owner, const std::string& id,
Patrick Venture5f59c0f2018-11-11 12:55:14 -080042 const std::vector<std::string>& inputs, double setpoint,
James Feist734f9532018-11-15 12:13:18 -080043 const ec::pidinfo& initial, const ThermalType& type)
Patrick Ventured8012182018-03-08 08:21:38 -080044{
James Feist734f9532018-11-15 12:13:18 -080045 // ThermalController requires at least 1 input
46 if (inputs.empty())
Patrick Venture3349ef22018-06-12 14:09:29 -070047 {
James Feist734f9532018-11-15 12:13:18 -080048 throw ControllerBuildException("Thermal controller missing inputs");
Patrick Venture3349ef22018-06-12 14:09:29 -070049 return nullptr;
50 }
51
James Feist734f9532018-11-15 12:13:18 -080052 auto thermal = std::make_unique<ThermalController>(id, inputs, type, owner);
Patrick Ventured8012182018-03-08 08:21:38 -080053
Patrick Venture563a3562018-10-30 09:31:26 -070054 ec::pid_info_t* info = thermal->getPIDInfo();
55 thermal->setSetpoint(setpoint);
Patrick Ventured8012182018-03-08 08:21:38 -080056
Patrick Venture7af157b2018-10-30 11:24:40 -070057 initializePIDStruct(info, initial);
Patrick Ventured8012182018-03-08 08:21:38 -080058
59 return thermal;
60}
61
Patrick Venture5f59c0f2018-11-11 12:55:14 -080062// bmc_host_sensor_value_double
63double ThermalController::inputProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080064{
James Feist734f9532018-11-15 12:13:18 -080065 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
Patrick Venture5f59c0f2018-11-11 12:55:14 -080083 return value;
Patrick Ventured8012182018-03-08 08:21:38 -080084}
85
86// bmc_get_setpt
Patrick Venture5f59c0f2018-11-11 12:55:14 -080087double ThermalController::setptProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080088{
Patrick Venture5f59c0f2018-11-11 12:55:14 -080089 double setpoint = getSetpoint();
Patrick Ventured8012182018-03-08 08:21:38 -080090
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
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800106void ThermalController::outputProc(double value)
Patrick Ventured8012182018-03-08 08:21:38 -0800107{
Patrick Venture9bbf3332019-07-16 10:50:37 -0700108 _owner->addSetPoint(value);
Patrick Ventured8012182018-03-08 08:21:38 -0800109
110 return;
111}
Patrick Venturea0764872020-08-08 07:48:43 -0700112
113} // namespace pid_control