blob: 9b5471408ce9c7b8c791dc8153c2e3f59b03d1c4 [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
Patrick Ventured8012182018-03-08 08:21:38 -080017#include "fancontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070018
Ed Tanousf8b6e552025-06-27 13:27:50 -070019#include "ec/pid.hpp"
20#include "fan.hpp"
21#include "pidcontroller.hpp"
Patrick Venturec32e3fc2019-02-28 10:01:11 -080022#include "tuning.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080023#include "util.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -070024#include "zone_interface.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080025
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070026#include <algorithm>
Josh Lehanca791152020-09-20 23:41:39 -070027#include <cmath>
Ed Tanousf8b6e552025-06-27 13:27:50 -070028#include <cstdint>
29#include <exception>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070030#include <iostream>
Ed Tanousf8b6e552025-06-27 13:27:50 -070031#include <map>
32#include <memory>
33#include <string>
34#include <utility>
35#include <vector>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070036
Patrick Venturea0764872020-08-08 07:48:43 -070037namespace pid_control
38{
39
Patrick Williamsbd63bca2024-08-16 15:21:10 -040040std::unique_ptr<PIDController> FanController::createFanPid(
41 ZoneInterface* owner, const std::string& id,
42 const std::vector<std::string>& inputs, const ec::pidinfo& initial)
Patrick Ventured8012182018-03-08 08:21:38 -080043{
Patrick Venture566a1512018-06-12 14:51:07 -070044 if (inputs.size() == 0)
45 {
46 return nullptr;
47 }
Patrick Ventured8012182018-03-08 08:21:38 -080048 auto fan = std::make_unique<FanController>(id, inputs, owner);
Patrick Venture563a3562018-10-30 09:31:26 -070049 ec::pid_info_t* info = fan->getPIDInfo();
Patrick Ventured8012182018-03-08 08:21:38 -080050
Patrick Venture7af157b2018-10-30 11:24:40 -070051 initializePIDStruct(info, initial);
Patrick Ventured8012182018-03-08 08:21:38 -080052
53 return fan;
54}
55
Patrick Venture5f59c0f2018-11-11 12:55:14 -080056double FanController::inputProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080057{
Josh Lehand38ae272020-11-13 02:59:30 -080058 double value = 0.0;
59 std::vector<double> values;
60 std::vector<double>::iterator result;
Patrick Ventured8012182018-03-08 08:21:38 -080061
62 try
63 {
64 for (const auto& name : _inputs)
65 {
Josh Lehand38ae272020-11-13 02:59:30 -080066 // Read the unscaled value, to correctly recover the RPM
67 value = _owner->getCachedValues(name).unscaled;
68
Patrick Ventured8012182018-03-08 08:21:38 -080069 /* If we have a fan we can't read, its value will be 0 for at least
70 * some boards, while others... the fan will drop off dbus (if
71 * that's how it's being read and in that case its value will never
72 * be updated anymore, which is relatively harmless, except, when
73 * something tries to read its value through IPMI, and can't, they
74 * sort of have to guess -- all the other fans are reporting, why
75 * not this one? Maybe it's unable to be read, so it's "bad."
76 */
Josh Lehanca791152020-09-20 23:41:39 -070077 if (!(std::isfinite(value)))
Patrick Ventured8012182018-03-08 08:21:38 -080078 {
Josh Lehanca791152020-09-20 23:41:39 -070079 continue;
Patrick Ventured8012182018-03-08 08:21:38 -080080 }
Josh Lehand38ae272020-11-13 02:59:30 -080081 if (value <= 0.0)
Josh Lehanca791152020-09-20 23:41:39 -070082 {
83 continue;
84 }
85
86 values.push_back(value);
Patrick Ventured8012182018-03-08 08:21:38 -080087 }
88 }
89 catch (const std::exception& e)
90 {
Patrick Venture563a3562018-10-30 09:31:26 -070091 std::cerr << "exception on inputProc.\n";
Patrick Ventured8012182018-03-08 08:21:38 -080092 throw;
93 }
94
Patrick Venture566a1512018-06-12 14:51:07 -070095 /* Reset the value from the above loop. */
Josh Lehand38ae272020-11-13 02:59:30 -080096 value = 0.0;
Patrick Ventured8012182018-03-08 08:21:38 -080097 if (values.size() > 0)
98 {
Patrick Ventured8012182018-03-08 08:21:38 -080099 /* the fan PID algorithm was unstable with average, and seemed to work
100 * better with minimum. I had considered making this choice a variable
Patrick Venturedf766f22018-10-13 09:30:58 -0700101 * in the configuration, and it's a nice-to-have..
Patrick Ventured8012182018-03-08 08:21:38 -0800102 */
103 result = std::min_element(values.begin(), values.end());
104 value = *result;
105 }
106
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800107 return value;
Patrick Ventured8012182018-03-08 08:21:38 -0800108}
109
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800110double FanController::setptProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800111{
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700112 double maxRPM = _owner->getMaxSetPointRequest();
Patrick Ventured8012182018-03-08 08:21:38 -0800113
114 // store for reference, and check if more or less.
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800115 double prev = getSetpoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800116
117 if (maxRPM > prev)
118 {
119 setFanDirection(FanSpeedDirection::UP);
120 }
121 else if (prev > maxRPM)
122 {
123 setFanDirection(FanSpeedDirection::DOWN);
124 }
125 else
126 {
127 setFanDirection(FanSpeedDirection::NEUTRAL);
128 }
129
Patrick Venture563a3562018-10-30 09:31:26 -0700130 setSetpoint(maxRPM);
Patrick Ventured8012182018-03-08 08:21:38 -0800131
132 return (maxRPM);
133}
134
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800135void FanController::outputProc(double value)
Patrick Ventured8012182018-03-08 08:21:38 -0800136{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800137 double percent = value;
Patrick Ventured8012182018-03-08 08:21:38 -0800138
Patrick Venturede79ee02019-05-08 14:50:00 -0700139 /* If doing tuning, don't go into failsafe mode. */
140 if (!tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800141 {
Josh Lehandf597652023-12-19 15:05:53 -0800142 bool failsafeCurrState = _owner->getFailSafeMode();
143
144 // Note when failsafe state transitions happen
145 if (failsafePrevState != failsafeCurrState)
146 {
147 failsafePrevState = failsafeCurrState;
148 failsafeTransition = true;
149 }
150
151 if (failsafeCurrState)
Patrick Ventured8012182018-03-08 08:21:38 -0800152 {
Brandon Kimbcdeb832022-08-15 23:27:36 +0000153 double failsafePercent = _owner->getFailSafePercent();
154
155#ifdef STRICT_FAILSAFE_PWM
156 // Unconditionally replace the computed PWM with the
157 // failsafe PWM if STRICT_FAILSAFE_PWM is defined.
158 percent = failsafePercent;
159#else
160 // Ensure PWM is never lower than the failsafe PWM.
161 // The computed PWM is still allowed to rise higher than
162 // failsafe PWM if STRICT_FAILSAFE_PWM is NOT defined.
163 // This is the default behavior.
164 if (percent < failsafePercent)
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800165 {
Brandon Kimbcdeb832022-08-15 23:27:36 +0000166 percent = failsafePercent;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800167 }
Brandon Kimbcdeb832022-08-15 23:27:36 +0000168#endif
Patrick Ventured8012182018-03-08 08:21:38 -0800169 }
Josh Lehandf597652023-12-19 15:05:53 -0800170
171 // Always print if debug enabled
172 if (debugEnabled)
173 {
174 std::cerr << "Zone " << _owner->getZoneID() << " fans, "
175 << (failsafeCurrState ? "failsafe" : "normal")
176 << " mode, output pwm: " << percent << "\n";
177 }
Bonnie Loc51ba912022-10-12 14:07:22 +0800178 else
179 {
Josh Lehandf597652023-12-19 15:05:53 -0800180 // Only print once per transition when not debugging
181 if (failsafeTransition)
Bonnie Loc51ba912022-10-12 14:07:22 +0800182 {
Josh Lehandf597652023-12-19 15:05:53 -0800183 failsafeTransition = false;
184 std::cerr << "Zone " << _owner->getZoneID() << " fans, "
185 << (failsafeCurrState ? "entering failsafe"
186 : "returning to normal")
187 << " mode, output pwm: " << percent << "\n";
Harvey Wua4270072024-05-29 16:11:13 +0800188
189 std::map<std::string, std::pair<std::string, double>>
190 failSensorList = _owner->getFailSafeSensors();
191 for (const auto& it : failSensorList)
192 {
193 std::cerr << "Fail sensor: " << it.first
194 << ", reason: " << it.second.first << "\n";
195 }
Bonnie Loc51ba912022-10-12 14:07:22 +0800196 }
197 }
Patrick Ventured8012182018-03-08 08:21:38 -0800198 }
Josh Lehandf597652023-12-19 15:05:53 -0800199 else
200 {
201 if (debugEnabled)
202 {
203 std::cerr << "Zone " << _owner->getZoneID()
204 << " fans, tuning mode, bypassing failsafe, output pwm: "
205 << percent << "\n";
206 }
207 }
Patrick Ventured8012182018-03-08 08:21:38 -0800208
209 // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that.
Josh Lehand38ae272020-11-13 02:59:30 -0800210 percent /= 100.0;
Patrick Ventured8012182018-03-08 08:21:38 -0800211
212 // PidSensorMap for writing.
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700213 for (const auto& it : _inputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800214 {
Patrick Venturea58197c2018-06-11 15:29:45 -0700215 auto sensor = _owner->getSensor(it);
Josh Lehana4146eb2020-10-01 11:49:09 -0700216 auto redundantWrite = _owner->getRedundantWrite();
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800217 int64_t rawWritten = -1;
Josh Lehana4146eb2020-10-01 11:49:09 -0700218 sensor->write(percent, redundantWrite, &rawWritten);
Josh Lehanb3005752022-02-22 20:48:07 -0800219
220 // The outputCache will be used later,
221 // to store a record of the PWM commanded,
222 // so that this information can be included during logging.
223 auto unscaledWritten = static_cast<double>(rawWritten);
224 _owner->setOutputCache(sensor->getName(), {percent, unscaledWritten});
Patrick Ventured8012182018-03-08 08:21:38 -0800225 }
226
Patrick Ventured8012182018-03-08 08:21:38 -0800227 return;
228}
Patrick Venturea0764872020-08-08 07:48:43 -0700229
Patrick Rudolph7e635022023-10-13 12:40:14 +0200230FanController::~FanController()
231{
232#ifdef OFFLINE_FAILSAFE_PWM
233 double percent = _owner->getFailSafePercent();
234 if (debugEnabled)
235 {
236 std::cerr << "Zone " << _owner->getZoneID()
237 << " offline fans output pwm: " << percent << "\n";
238 }
239
240 // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that.
241 percent /= 100.0;
242
243 // PidSensorMap for writing.
244 for (const auto& it : _inputs)
245 {
246 auto sensor = _owner->getSensor(it);
247 auto redundantWrite = _owner->getRedundantWrite();
248 int64_t rawWritten;
249 sensor->write(percent, redundantWrite, &rawWritten);
250
251 // The outputCache will be used later,
252 // to store a record of the PWM commanded,
253 // so that this information can be included during logging.
254 auto unscaledWritten = static_cast<double>(rawWritten);
255 _owner->setOutputCache(sensor->getName(), {percent, unscaledWritten});
256 }
257#endif
258}
259
Patrick Venturea0764872020-08-08 07:48:43 -0700260} // namespace pid_control