blob: ecf405e83aaa538853232a6d97a73a3209a322d8 [file] [log] [blame]
Alexander Hansen46a755f2025-10-27 16:31:08 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2017 Google Inc
3
Pete O_o765a6d82025-07-23 21:44:14 -07004#include "config.h"
Patrick Ventured8012182018-03-08 08:21:38 -08005
Patrick Ventured8012182018-03-08 08:21:38 -08006#include "fancontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07007
Ed Tanousf8b6e552025-06-27 13:27:50 -07008#include "ec/pid.hpp"
9#include "fan.hpp"
10#include "pidcontroller.hpp"
Patrick Venturec32e3fc2019-02-28 10:01:11 -080011#include "tuning.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080012#include "util.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -070013#include "zone_interface.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080014
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070015#include <algorithm>
Josh Lehanca791152020-09-20 23:41:39 -070016#include <cmath>
Ed Tanousf8b6e552025-06-27 13:27:50 -070017#include <cstdint>
18#include <exception>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070019#include <iostream>
Ed Tanousf8b6e552025-06-27 13:27:50 -070020#include <map>
21#include <memory>
22#include <string>
23#include <utility>
24#include <vector>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070025
Patrick Venturea0764872020-08-08 07:48:43 -070026namespace pid_control
27{
28
Patrick Williamsbd63bca2024-08-16 15:21:10 -040029std::unique_ptr<PIDController> FanController::createFanPid(
30 ZoneInterface* owner, const std::string& id,
31 const std::vector<std::string>& inputs, const ec::pidinfo& initial)
Patrick Ventured8012182018-03-08 08:21:38 -080032{
Patrick Venture566a1512018-06-12 14:51:07 -070033 if (inputs.size() == 0)
34 {
35 return nullptr;
36 }
Patrick Ventured8012182018-03-08 08:21:38 -080037 auto fan = std::make_unique<FanController>(id, inputs, owner);
Patrick Venture563a3562018-10-30 09:31:26 -070038 ec::pid_info_t* info = fan->getPIDInfo();
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 fan;
43}
44
Patrick Venture5f59c0f2018-11-11 12:55:14 -080045double FanController::inputProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080046{
Josh Lehand38ae272020-11-13 02:59:30 -080047 double value = 0.0;
48 std::vector<double> values;
49 std::vector<double>::iterator result;
Patrick Ventured8012182018-03-08 08:21:38 -080050
51 try
52 {
53 for (const auto& name : _inputs)
54 {
Josh Lehand38ae272020-11-13 02:59:30 -080055 // Read the unscaled value, to correctly recover the RPM
56 value = _owner->getCachedValues(name).unscaled;
57
Patrick Ventured8012182018-03-08 08:21:38 -080058 /* If we have a fan we can't read, its value will be 0 for at least
59 * some boards, while others... the fan will drop off dbus (if
60 * that's how it's being read and in that case its value will never
61 * be updated anymore, which is relatively harmless, except, when
62 * something tries to read its value through IPMI, and can't, they
63 * sort of have to guess -- all the other fans are reporting, why
64 * not this one? Maybe it's unable to be read, so it's "bad."
65 */
Josh Lehanca791152020-09-20 23:41:39 -070066 if (!(std::isfinite(value)))
Patrick Ventured8012182018-03-08 08:21:38 -080067 {
Josh Lehanca791152020-09-20 23:41:39 -070068 continue;
Patrick Ventured8012182018-03-08 08:21:38 -080069 }
Josh Lehand38ae272020-11-13 02:59:30 -080070 if (value <= 0.0)
Josh Lehanca791152020-09-20 23:41:39 -070071 {
72 continue;
73 }
74
75 values.push_back(value);
Patrick Ventured8012182018-03-08 08:21:38 -080076 }
77 }
78 catch (const std::exception& e)
79 {
Patrick Venture563a3562018-10-30 09:31:26 -070080 std::cerr << "exception on inputProc.\n";
Patrick Ventured8012182018-03-08 08:21:38 -080081 throw;
82 }
83
Patrick Venture566a1512018-06-12 14:51:07 -070084 /* Reset the value from the above loop. */
Josh Lehand38ae272020-11-13 02:59:30 -080085 value = 0.0;
Patrick Ventured8012182018-03-08 08:21:38 -080086 if (values.size() > 0)
87 {
Patrick Ventured8012182018-03-08 08:21:38 -080088 /* the fan PID algorithm was unstable with average, and seemed to work
89 * better with minimum. I had considered making this choice a variable
Patrick Venturedf766f22018-10-13 09:30:58 -070090 * in the configuration, and it's a nice-to-have..
Patrick Ventured8012182018-03-08 08:21:38 -080091 */
92 result = std::min_element(values.begin(), values.end());
93 value = *result;
94 }
95
Patrick Venture5f59c0f2018-11-11 12:55:14 -080096 return value;
Patrick Ventured8012182018-03-08 08:21:38 -080097}
98
Patrick Venture5f59c0f2018-11-11 12:55:14 -080099double FanController::setptProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800100{
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700101 double maxRPM = _owner->getMaxSetPointRequest();
Patrick Ventured8012182018-03-08 08:21:38 -0800102
103 // store for reference, and check if more or less.
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800104 double prev = getSetpoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800105
106 if (maxRPM > prev)
107 {
108 setFanDirection(FanSpeedDirection::UP);
109 }
110 else if (prev > maxRPM)
111 {
112 setFanDirection(FanSpeedDirection::DOWN);
113 }
114 else
115 {
116 setFanDirection(FanSpeedDirection::NEUTRAL);
117 }
118
Patrick Venture563a3562018-10-30 09:31:26 -0700119 setSetpoint(maxRPM);
Patrick Ventured8012182018-03-08 08:21:38 -0800120
121 return (maxRPM);
122}
123
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800124void FanController::outputProc(double value)
Patrick Ventured8012182018-03-08 08:21:38 -0800125{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800126 double percent = value;
Patrick Ventured8012182018-03-08 08:21:38 -0800127
Patrick Venturede79ee02019-05-08 14:50:00 -0700128 /* If doing tuning, don't go into failsafe mode. */
129 if (!tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800130 {
Josh Lehandf597652023-12-19 15:05:53 -0800131 bool failsafeCurrState = _owner->getFailSafeMode();
132
133 // Note when failsafe state transitions happen
134 if (failsafePrevState != failsafeCurrState)
135 {
136 failsafePrevState = failsafeCurrState;
137 failsafeTransition = true;
138 }
139
140 if (failsafeCurrState)
Patrick Ventured8012182018-03-08 08:21:38 -0800141 {
Brandon Kimbcdeb832022-08-15 23:27:36 +0000142 double failsafePercent = _owner->getFailSafePercent();
143
144#ifdef STRICT_FAILSAFE_PWM
145 // Unconditionally replace the computed PWM with the
146 // failsafe PWM if STRICT_FAILSAFE_PWM is defined.
147 percent = failsafePercent;
148#else
149 // Ensure PWM is never lower than the failsafe PWM.
150 // The computed PWM is still allowed to rise higher than
151 // failsafe PWM if STRICT_FAILSAFE_PWM is NOT defined.
152 // This is the default behavior.
153 if (percent < failsafePercent)
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800154 {
Brandon Kimbcdeb832022-08-15 23:27:36 +0000155 percent = failsafePercent;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800156 }
Brandon Kimbcdeb832022-08-15 23:27:36 +0000157#endif
Patrick Ventured8012182018-03-08 08:21:38 -0800158 }
Josh Lehandf597652023-12-19 15:05:53 -0800159
160 // Always print if debug enabled
161 if (debugEnabled)
162 {
163 std::cerr << "Zone " << _owner->getZoneID() << " fans, "
164 << (failsafeCurrState ? "failsafe" : "normal")
165 << " mode, output pwm: " << percent << "\n";
166 }
Bonnie Loc51ba912022-10-12 14:07:22 +0800167 else
168 {
Josh Lehandf597652023-12-19 15:05:53 -0800169 // Only print once per transition when not debugging
170 if (failsafeTransition)
Bonnie Loc51ba912022-10-12 14:07:22 +0800171 {
Josh Lehandf597652023-12-19 15:05:53 -0800172 failsafeTransition = false;
173 std::cerr << "Zone " << _owner->getZoneID() << " fans, "
174 << (failsafeCurrState ? "entering failsafe"
175 : "returning to normal")
176 << " mode, output pwm: " << percent << "\n";
Harvey Wua4270072024-05-29 16:11:13 +0800177
178 std::map<std::string, std::pair<std::string, double>>
179 failSensorList = _owner->getFailSafeSensors();
180 for (const auto& it : failSensorList)
181 {
182 std::cerr << "Fail sensor: " << it.first
183 << ", reason: " << it.second.first << "\n";
184 }
Bonnie Loc51ba912022-10-12 14:07:22 +0800185 }
186 }
Patrick Ventured8012182018-03-08 08:21:38 -0800187 }
Josh Lehandf597652023-12-19 15:05:53 -0800188 else
189 {
190 if (debugEnabled)
191 {
192 std::cerr << "Zone " << _owner->getZoneID()
193 << " fans, tuning mode, bypassing failsafe, output pwm: "
194 << percent << "\n";
195 }
196 }
Patrick Ventured8012182018-03-08 08:21:38 -0800197
198 // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that.
Josh Lehand38ae272020-11-13 02:59:30 -0800199 percent /= 100.0;
Patrick Ventured8012182018-03-08 08:21:38 -0800200
201 // PidSensorMap for writing.
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700202 for (const auto& it : _inputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800203 {
Patrick Venturea58197c2018-06-11 15:29:45 -0700204 auto sensor = _owner->getSensor(it);
Josh Lehana4146eb2020-10-01 11:49:09 -0700205 auto redundantWrite = _owner->getRedundantWrite();
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800206 int64_t rawWritten = -1;
Josh Lehana4146eb2020-10-01 11:49:09 -0700207 sensor->write(percent, redundantWrite, &rawWritten);
Josh Lehanb3005752022-02-22 20:48:07 -0800208
209 // The outputCache will be used later,
210 // to store a record of the PWM commanded,
211 // so that this information can be included during logging.
212 auto unscaledWritten = static_cast<double>(rawWritten);
213 _owner->setOutputCache(sensor->getName(), {percent, unscaledWritten});
Patrick Ventured8012182018-03-08 08:21:38 -0800214 }
215
Patrick Ventured8012182018-03-08 08:21:38 -0800216 return;
217}
Patrick Venturea0764872020-08-08 07:48:43 -0700218
Patrick Rudolph7e635022023-10-13 12:40:14 +0200219FanController::~FanController()
220{
221#ifdef OFFLINE_FAILSAFE_PWM
222 double percent = _owner->getFailSafePercent();
223 if (debugEnabled)
224 {
225 std::cerr << "Zone " << _owner->getZoneID()
226 << " offline fans output pwm: " << percent << "\n";
227 }
228
229 // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that.
230 percent /= 100.0;
231
232 // PidSensorMap for writing.
233 for (const auto& it : _inputs)
234 {
235 auto sensor = _owner->getSensor(it);
236 auto redundantWrite = _owner->getRedundantWrite();
237 int64_t rawWritten;
238 sensor->write(percent, redundantWrite, &rawWritten);
239
240 // The outputCache will be used later,
241 // to store a record of the PWM commanded,
242 // so that this information can be included during logging.
243 auto unscaledWritten = static_cast<double>(rawWritten);
244 _owner->setOutputCache(sensor->getName(), {percent, unscaledWritten});
245 }
246#endif
247}
248
Patrick Venturea0764872020-08-08 07:48:43 -0700249} // namespace pid_control