blob: 71374a10ca08f813c479b00a313d6c54153388dd [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>
Josh Lehanca791152020-09-20 23:41:39 -070024#include <cmath>
25#include <iostream>
Patrick Venturee94bdc42018-11-15 18:41:21 -080026
Patrick Venturea0764872020-08-08 07:48:43 -070027namespace pid_control
28{
29
Patrick Venturee94bdc42018-11-15 18:41:21 -080030ThermalType getThermalType(const std::string& typeString)
31{
32 /* Currently it only supports the two types. */
33 return (typeString == "temp") ? ThermalType::absolute : ThermalType::margin;
34}
35
36bool isThermalType(const std::string& typeString)
37{
38 static const std::vector<std::string> thermalTypes = {"temp", "margin"};
39 return std::count(thermalTypes.begin(), thermalTypes.end(), typeString);
40}
41
Patrick Venture563a3562018-10-30 09:31:26 -070042std::unique_ptr<PIDController> ThermalController::createThermalPid(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070043 ZoneInterface* owner, const std::string& id,
Patrick Venture5f59c0f2018-11-11 12:55:14 -080044 const std::vector<std::string>& inputs, double setpoint,
James Feist734f9532018-11-15 12:13:18 -080045 const ec::pidinfo& initial, const ThermalType& type)
Patrick Ventured8012182018-03-08 08:21:38 -080046{
James Feist734f9532018-11-15 12:13:18 -080047 // ThermalController requires at least 1 input
48 if (inputs.empty())
Patrick Venture3349ef22018-06-12 14:09:29 -070049 {
James Feist734f9532018-11-15 12:13:18 -080050 throw ControllerBuildException("Thermal controller missing inputs");
Patrick Venture3349ef22018-06-12 14:09:29 -070051 return nullptr;
52 }
53
James Feist734f9532018-11-15 12:13:18 -080054 auto thermal = std::make_unique<ThermalController>(id, inputs, type, owner);
Patrick Ventured8012182018-03-08 08:21:38 -080055
Patrick Venture563a3562018-10-30 09:31:26 -070056 ec::pid_info_t* info = thermal->getPIDInfo();
57 thermal->setSetpoint(setpoint);
Patrick Ventured8012182018-03-08 08:21:38 -080058
Patrick Venture7af157b2018-10-30 11:24:40 -070059 initializePIDStruct(info, initial);
Patrick Ventured8012182018-03-08 08:21:38 -080060
61 return thermal;
62}
63
Patrick Venture5f59c0f2018-11-11 12:55:14 -080064// bmc_host_sensor_value_double
65double ThermalController::inputProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080066{
James Feist734f9532018-11-15 12:13:18 -080067 double value;
68 const double& (*compare)(const double&, const double&);
69 if (type == ThermalType::margin)
70 {
71 value = std::numeric_limits<double>::max();
72 compare = std::min<double>;
73 }
74 else
75 {
76 value = std::numeric_limits<double>::lowest();
77 compare = std::max<double>;
78 }
79
Josh Lehanca791152020-09-20 23:41:39 -070080 bool acceptable = false;
James Feist734f9532018-11-15 12:13:18 -080081 for (const auto& in : _inputs)
82 {
Josh Lehanca791152020-09-20 23:41:39 -070083 double cachedValue = _owner->getCachedValue(in);
84
85 // Less than 0 is perfectly OK for temperature, but must not be NAN
86 if (!(std::isfinite(cachedValue)))
87 {
88 continue;
89 }
90
91 value = compare(value, cachedValue);
92 acceptable = true;
93 }
94
95 if (!acceptable)
96 {
97 // While not optimal, zero is better than garbage
98 value = 0;
James Feist734f9532018-11-15 12:13:18 -080099 }
100
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800101 return value;
Patrick Ventured8012182018-03-08 08:21:38 -0800102}
103
104// bmc_get_setpt
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800105double ThermalController::setptProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800106{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800107 double setpoint = getSetpoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800108
109 /* TODO(venture): Thermal setpoint invalid? */
110#if 0
111 if (-1 == setpoint)
112 {
113 return 0.0f;
114 }
115 else
116 {
117 return setpoint;
118 }
119#endif
120 return setpoint;
121}
122
123// bmc_set_pid_output
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800124void ThermalController::outputProc(double value)
Patrick Ventured8012182018-03-08 08:21:38 -0800125{
Nirav Shahccc8bb62022-02-17 21:06:51 -0800126 _owner->addSetPoint(value, _id);
Patrick Ventured8012182018-03-08 08:21:38 -0800127
128 return;
129}
Patrick Venturea0764872020-08-08 07:48:43 -0700130
131} // namespace pid_control