blob: 5598c4f42d7b3c39714e34ea4210efbd79800e9c [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 */
Pete O_o765a6d82025-07-23 21:44:14 -070016#include "config.h"
Patrick Ventured8012182018-03-08 08:21:38 -080017
Patrick Ventured8012182018-03-08 08:21:38 -080018#include "fancontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070019
Ed Tanousf8b6e552025-06-27 13:27:50 -070020#include "ec/pid.hpp"
21#include "fan.hpp"
22#include "pidcontroller.hpp"
Patrick Venturec32e3fc2019-02-28 10:01:11 -080023#include "tuning.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080024#include "util.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -070025#include "zone_interface.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080026
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070027#include <algorithm>
Josh Lehanca791152020-09-20 23:41:39 -070028#include <cmath>
Ed Tanousf8b6e552025-06-27 13:27:50 -070029#include <cstdint>
30#include <exception>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070031#include <iostream>
Ed Tanousf8b6e552025-06-27 13:27:50 -070032#include <map>
33#include <memory>
34#include <string>
35#include <utility>
36#include <vector>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070037
Patrick Venturea0764872020-08-08 07:48:43 -070038namespace pid_control
39{
40
Patrick Williamsbd63bca2024-08-16 15:21:10 -040041std::unique_ptr<PIDController> FanController::createFanPid(
42 ZoneInterface* owner, const std::string& id,
43 const std::vector<std::string>& inputs, const ec::pidinfo& initial)
Patrick Ventured8012182018-03-08 08:21:38 -080044{
Patrick Venture566a1512018-06-12 14:51:07 -070045 if (inputs.size() == 0)
46 {
47 return nullptr;
48 }
Patrick Ventured8012182018-03-08 08:21:38 -080049 auto fan = std::make_unique<FanController>(id, inputs, owner);
Patrick Venture563a3562018-10-30 09:31:26 -070050 ec::pid_info_t* info = fan->getPIDInfo();
Patrick Ventured8012182018-03-08 08:21:38 -080051
Patrick Venture7af157b2018-10-30 11:24:40 -070052 initializePIDStruct(info, initial);
Patrick Ventured8012182018-03-08 08:21:38 -080053
54 return fan;
55}
56
Patrick Venture5f59c0f2018-11-11 12:55:14 -080057double FanController::inputProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080058{
Josh Lehand38ae272020-11-13 02:59:30 -080059 double value = 0.0;
60 std::vector<double> values;
61 std::vector<double>::iterator result;
Patrick Ventured8012182018-03-08 08:21:38 -080062
63 try
64 {
65 for (const auto& name : _inputs)
66 {
Josh Lehand38ae272020-11-13 02:59:30 -080067 // Read the unscaled value, to correctly recover the RPM
68 value = _owner->getCachedValues(name).unscaled;
69
Patrick Ventured8012182018-03-08 08:21:38 -080070 /* If we have a fan we can't read, its value will be 0 for at least
71 * some boards, while others... the fan will drop off dbus (if
72 * that's how it's being read and in that case its value will never
73 * be updated anymore, which is relatively harmless, except, when
74 * something tries to read its value through IPMI, and can't, they
75 * sort of have to guess -- all the other fans are reporting, why
76 * not this one? Maybe it's unable to be read, so it's "bad."
77 */
Josh Lehanca791152020-09-20 23:41:39 -070078 if (!(std::isfinite(value)))
Patrick Ventured8012182018-03-08 08:21:38 -080079 {
Josh Lehanca791152020-09-20 23:41:39 -070080 continue;
Patrick Ventured8012182018-03-08 08:21:38 -080081 }
Josh Lehand38ae272020-11-13 02:59:30 -080082 if (value <= 0.0)
Josh Lehanca791152020-09-20 23:41:39 -070083 {
84 continue;
85 }
86
87 values.push_back(value);
Patrick Ventured8012182018-03-08 08:21:38 -080088 }
89 }
90 catch (const std::exception& e)
91 {
Patrick Venture563a3562018-10-30 09:31:26 -070092 std::cerr << "exception on inputProc.\n";
Patrick Ventured8012182018-03-08 08:21:38 -080093 throw;
94 }
95
Patrick Venture566a1512018-06-12 14:51:07 -070096 /* Reset the value from the above loop. */
Josh Lehand38ae272020-11-13 02:59:30 -080097 value = 0.0;
Patrick Ventured8012182018-03-08 08:21:38 -080098 if (values.size() > 0)
99 {
Patrick Ventured8012182018-03-08 08:21:38 -0800100 /* the fan PID algorithm was unstable with average, and seemed to work
101 * better with minimum. I had considered making this choice a variable
Patrick Venturedf766f22018-10-13 09:30:58 -0700102 * in the configuration, and it's a nice-to-have..
Patrick Ventured8012182018-03-08 08:21:38 -0800103 */
104 result = std::min_element(values.begin(), values.end());
105 value = *result;
106 }
107
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800108 return value;
Patrick Ventured8012182018-03-08 08:21:38 -0800109}
110
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800111double FanController::setptProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800112{
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700113 double maxRPM = _owner->getMaxSetPointRequest();
Patrick Ventured8012182018-03-08 08:21:38 -0800114
115 // store for reference, and check if more or less.
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800116 double prev = getSetpoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800117
118 if (maxRPM > prev)
119 {
120 setFanDirection(FanSpeedDirection::UP);
121 }
122 else if (prev > maxRPM)
123 {
124 setFanDirection(FanSpeedDirection::DOWN);
125 }
126 else
127 {
128 setFanDirection(FanSpeedDirection::NEUTRAL);
129 }
130
Patrick Venture563a3562018-10-30 09:31:26 -0700131 setSetpoint(maxRPM);
Patrick Ventured8012182018-03-08 08:21:38 -0800132
133 return (maxRPM);
134}
135
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800136void FanController::outputProc(double value)
Patrick Ventured8012182018-03-08 08:21:38 -0800137{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800138 double percent = value;
Patrick Ventured8012182018-03-08 08:21:38 -0800139
Patrick Venturede79ee02019-05-08 14:50:00 -0700140 /* If doing tuning, don't go into failsafe mode. */
141 if (!tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800142 {
Josh Lehandf597652023-12-19 15:05:53 -0800143 bool failsafeCurrState = _owner->getFailSafeMode();
144
145 // Note when failsafe state transitions happen
146 if (failsafePrevState != failsafeCurrState)
147 {
148 failsafePrevState = failsafeCurrState;
149 failsafeTransition = true;
150 }
151
152 if (failsafeCurrState)
Patrick Ventured8012182018-03-08 08:21:38 -0800153 {
Brandon Kimbcdeb832022-08-15 23:27:36 +0000154 double failsafePercent = _owner->getFailSafePercent();
155
156#ifdef STRICT_FAILSAFE_PWM
157 // Unconditionally replace the computed PWM with the
158 // failsafe PWM if STRICT_FAILSAFE_PWM is defined.
159 percent = failsafePercent;
160#else
161 // Ensure PWM is never lower than the failsafe PWM.
162 // The computed PWM is still allowed to rise higher than
163 // failsafe PWM if STRICT_FAILSAFE_PWM is NOT defined.
164 // This is the default behavior.
165 if (percent < failsafePercent)
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800166 {
Brandon Kimbcdeb832022-08-15 23:27:36 +0000167 percent = failsafePercent;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800168 }
Brandon Kimbcdeb832022-08-15 23:27:36 +0000169#endif
Patrick Ventured8012182018-03-08 08:21:38 -0800170 }
Josh Lehandf597652023-12-19 15:05:53 -0800171
172 // Always print if debug enabled
173 if (debugEnabled)
174 {
175 std::cerr << "Zone " << _owner->getZoneID() << " fans, "
176 << (failsafeCurrState ? "failsafe" : "normal")
177 << " mode, output pwm: " << percent << "\n";
178 }
Bonnie Loc51ba912022-10-12 14:07:22 +0800179 else
180 {
Josh Lehandf597652023-12-19 15:05:53 -0800181 // Only print once per transition when not debugging
182 if (failsafeTransition)
Bonnie Loc51ba912022-10-12 14:07:22 +0800183 {
Josh Lehandf597652023-12-19 15:05:53 -0800184 failsafeTransition = false;
185 std::cerr << "Zone " << _owner->getZoneID() << " fans, "
186 << (failsafeCurrState ? "entering failsafe"
187 : "returning to normal")
188 << " mode, output pwm: " << percent << "\n";
Harvey Wua4270072024-05-29 16:11:13 +0800189
190 std::map<std::string, std::pair<std::string, double>>
191 failSensorList = _owner->getFailSafeSensors();
192 for (const auto& it : failSensorList)
193 {
194 std::cerr << "Fail sensor: " << it.first
195 << ", reason: " << it.second.first << "\n";
196 }
Bonnie Loc51ba912022-10-12 14:07:22 +0800197 }
198 }
Patrick Ventured8012182018-03-08 08:21:38 -0800199 }
Josh Lehandf597652023-12-19 15:05:53 -0800200 else
201 {
202 if (debugEnabled)
203 {
204 std::cerr << "Zone " << _owner->getZoneID()
205 << " fans, tuning mode, bypassing failsafe, output pwm: "
206 << percent << "\n";
207 }
208 }
Patrick Ventured8012182018-03-08 08:21:38 -0800209
210 // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that.
Josh Lehand38ae272020-11-13 02:59:30 -0800211 percent /= 100.0;
Patrick Ventured8012182018-03-08 08:21:38 -0800212
213 // PidSensorMap for writing.
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700214 for (const auto& it : _inputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800215 {
Patrick Venturea58197c2018-06-11 15:29:45 -0700216 auto sensor = _owner->getSensor(it);
Josh Lehana4146eb2020-10-01 11:49:09 -0700217 auto redundantWrite = _owner->getRedundantWrite();
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800218 int64_t rawWritten = -1;
Josh Lehana4146eb2020-10-01 11:49:09 -0700219 sensor->write(percent, redundantWrite, &rawWritten);
Josh Lehanb3005752022-02-22 20:48:07 -0800220
221 // The outputCache will be used later,
222 // to store a record of the PWM commanded,
223 // so that this information can be included during logging.
224 auto unscaledWritten = static_cast<double>(rawWritten);
225 _owner->setOutputCache(sensor->getName(), {percent, unscaledWritten});
Patrick Ventured8012182018-03-08 08:21:38 -0800226 }
227
Patrick Ventured8012182018-03-08 08:21:38 -0800228 return;
229}
Patrick Venturea0764872020-08-08 07:48:43 -0700230
Patrick Rudolph7e635022023-10-13 12:40:14 +0200231FanController::~FanController()
232{
233#ifdef OFFLINE_FAILSAFE_PWM
234 double percent = _owner->getFailSafePercent();
235 if (debugEnabled)
236 {
237 std::cerr << "Zone " << _owner->getZoneID()
238 << " offline fans output pwm: " << percent << "\n";
239 }
240
241 // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that.
242 percent /= 100.0;
243
244 // PidSensorMap for writing.
245 for (const auto& it : _inputs)
246 {
247 auto sensor = _owner->getSensor(it);
248 auto redundantWrite = _owner->getRedundantWrite();
249 int64_t rawWritten;
250 sensor->write(percent, redundantWrite, &rawWritten);
251
252 // The outputCache will be used later,
253 // to store a record of the PWM commanded,
254 // so that this information can be included during logging.
255 auto unscaledWritten = static_cast<double>(rawWritten);
256 _owner->setOutputCache(sensor->getName(), {percent, unscaledWritten});
257 }
258#endif
259}
260
Patrick Venturea0764872020-08-08 07:48:43 -0700261} // namespace pid_control