1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright 2017 Google Inc
3
4 #include "config.h"
5
6 #include "fancontroller.hpp"
7
8 #include "ec/pid.hpp"
9 #include "fan.hpp"
10 #include "pidcontroller.hpp"
11 #include "tuning.hpp"
12 #include "util.hpp"
13 #include "zone_interface.hpp"
14
15 #include <algorithm>
16 #include <cmath>
17 #include <cstdint>
18 #include <exception>
19 #include <iostream>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25
26 namespace pid_control
27 {
28
createFanPid(ZoneInterface * owner,const std::string & id,const std::vector<std::string> & inputs,const ec::pidinfo & initial)29 std::unique_ptr<PIDController> FanController::createFanPid(
30 ZoneInterface* owner, const std::string& id,
31 const std::vector<std::string>& inputs, const ec::pidinfo& initial)
32 {
33 if (inputs.size() == 0)
34 {
35 return nullptr;
36 }
37 auto fan = std::make_unique<FanController>(id, inputs, owner);
38 ec::pid_info_t* info = fan->getPIDInfo();
39
40 initializePIDStruct(info, initial);
41
42 return fan;
43 }
44
inputProc(void)45 double FanController::inputProc(void)
46 {
47 double value = 0.0;
48 std::vector<double> values;
49 std::vector<double>::iterator result;
50
51 try
52 {
53 for (const auto& name : _inputs)
54 {
55 // Read the unscaled value, to correctly recover the RPM
56 value = _owner->getCachedValues(name).unscaled;
57
58 /* 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 */
66 if (!(std::isfinite(value)))
67 {
68 continue;
69 }
70 if (value <= 0.0)
71 {
72 continue;
73 }
74
75 values.push_back(value);
76 }
77 }
78 catch (const std::exception& e)
79 {
80 std::cerr << "exception on inputProc.\n";
81 throw;
82 }
83
84 /* Reset the value from the above loop. */
85 value = 0.0;
86 if (values.size() > 0)
87 {
88 /* the fan PID algorithm was unstable with average, and seemed to work
89 * better with minimum. I had considered making this choice a variable
90 * in the configuration, and it's a nice-to-have..
91 */
92 result = std::min_element(values.begin(), values.end());
93 value = *result;
94 }
95
96 return value;
97 }
98
setptProc(void)99 double FanController::setptProc(void)
100 {
101 double maxRPM = _owner->getMaxSetPointRequest();
102
103 // store for reference, and check if more or less.
104 double prev = getSetpoint();
105
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
119 setSetpoint(maxRPM);
120
121 return (maxRPM);
122 }
123
outputProc(double value)124 void FanController::outputProc(double value)
125 {
126 double percent = value;
127
128 /* If doing tuning, don't go into failsafe mode. */
129 if (!tuningEnabled)
130 {
131 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)
141 {
142 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)
154 {
155 percent = failsafePercent;
156 }
157 #endif
158 }
159
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 }
167 else
168 {
169 // Only print once per transition when not debugging
170 if (failsafeTransition)
171 {
172 failsafeTransition = false;
173 std::cerr << "Zone " << _owner->getZoneID() << " fans, "
174 << (failsafeCurrState ? "entering failsafe"
175 : "returning to normal")
176 << " mode, output pwm: " << percent << "\n";
177
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 }
185 }
186 }
187 }
188 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 }
197
198 // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that.
199 percent /= 100.0;
200
201 // PidSensorMap for writing.
202 for (const auto& it : _inputs)
203 {
204 auto sensor = _owner->getSensor(it);
205 auto redundantWrite = _owner->getRedundantWrite();
206 int64_t rawWritten = -1;
207 sensor->write(percent, redundantWrite, &rawWritten);
208
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});
214 }
215
216 return;
217 }
218
~FanController()219 FanController::~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
249 } // namespace pid_control
250