blob: fc26243066834f7859878295d395b2048dd2b84a [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 */
Jonico Eustaquio9f1532d2023-12-21 09:32:14 -060016#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
Patrick Venturec32e3fc2019-02-28 10:01:11 -080020#include "tuning.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080021#include "util.hpp"
22#include "zone.hpp"
23
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070024#include <algorithm>
Josh Lehanca791152020-09-20 23:41:39 -070025#include <cmath>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070026#include <iostream>
27
Patrick Venturea0764872020-08-08 07:48:43 -070028namespace pid_control
29{
30
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070031std::unique_ptr<PIDController>
Patrick Venture563a3562018-10-30 09:31:26 -070032 FanController::createFanPid(ZoneInterface* owner, const std::string& id,
Patrick Venture4a2dc4d2018-10-23 09:02:55 -070033 const std::vector<std::string>& inputs,
Patrick Venturef77d5a52018-10-23 09:32:52 -070034 const ec::pidinfo& initial)
Patrick Ventured8012182018-03-08 08:21:38 -080035{
Patrick Venture566a1512018-06-12 14:51:07 -070036 if (inputs.size() == 0)
37 {
38 return nullptr;
39 }
Patrick Ventured8012182018-03-08 08:21:38 -080040 auto fan = std::make_unique<FanController>(id, inputs, owner);
Patrick Venture563a3562018-10-30 09:31:26 -070041 ec::pid_info_t* info = fan->getPIDInfo();
Patrick Ventured8012182018-03-08 08:21:38 -080042
Patrick Venture7af157b2018-10-30 11:24:40 -070043 initializePIDStruct(info, initial);
Patrick Ventured8012182018-03-08 08:21:38 -080044
45 return fan;
46}
47
Patrick Venture5f59c0f2018-11-11 12:55:14 -080048double FanController::inputProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080049{
Josh Lehand38ae272020-11-13 02:59:30 -080050 double value = 0.0;
51 std::vector<double> values;
52 std::vector<double>::iterator result;
Patrick Ventured8012182018-03-08 08:21:38 -080053
54 try
55 {
56 for (const auto& name : _inputs)
57 {
Josh Lehand38ae272020-11-13 02:59:30 -080058 // Read the unscaled value, to correctly recover the RPM
59 value = _owner->getCachedValues(name).unscaled;
60
Patrick Ventured8012182018-03-08 08:21:38 -080061 /* If we have a fan we can't read, its value will be 0 for at least
62 * some boards, while others... the fan will drop off dbus (if
63 * that's how it's being read and in that case its value will never
64 * be updated anymore, which is relatively harmless, except, when
65 * something tries to read its value through IPMI, and can't, they
66 * sort of have to guess -- all the other fans are reporting, why
67 * not this one? Maybe it's unable to be read, so it's "bad."
68 */
Josh Lehanca791152020-09-20 23:41:39 -070069 if (!(std::isfinite(value)))
Patrick Ventured8012182018-03-08 08:21:38 -080070 {
Josh Lehanca791152020-09-20 23:41:39 -070071 continue;
Patrick Ventured8012182018-03-08 08:21:38 -080072 }
Josh Lehand38ae272020-11-13 02:59:30 -080073 if (value <= 0.0)
Josh Lehanca791152020-09-20 23:41:39 -070074 {
75 continue;
76 }
77
78 values.push_back(value);
Patrick Ventured8012182018-03-08 08:21:38 -080079 }
80 }
81 catch (const std::exception& e)
82 {
Patrick Venture563a3562018-10-30 09:31:26 -070083 std::cerr << "exception on inputProc.\n";
Patrick Ventured8012182018-03-08 08:21:38 -080084 throw;
85 }
86
Patrick Venture566a1512018-06-12 14:51:07 -070087 /* Reset the value from the above loop. */
Josh Lehand38ae272020-11-13 02:59:30 -080088 value = 0.0;
Patrick Ventured8012182018-03-08 08:21:38 -080089 if (values.size() > 0)
90 {
Patrick Ventured8012182018-03-08 08:21:38 -080091 /* the fan PID algorithm was unstable with average, and seemed to work
92 * better with minimum. I had considered making this choice a variable
Patrick Venturedf766f22018-10-13 09:30:58 -070093 * in the configuration, and it's a nice-to-have..
Patrick Ventured8012182018-03-08 08:21:38 -080094 */
95 result = std::min_element(values.begin(), values.end());
96 value = *result;
97 }
98
Patrick Venture5f59c0f2018-11-11 12:55:14 -080099 return value;
Patrick Ventured8012182018-03-08 08:21:38 -0800100}
101
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800102double FanController::setptProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800103{
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700104 double maxRPM = _owner->getMaxSetPointRequest();
Patrick Ventured8012182018-03-08 08:21:38 -0800105
106 // store for reference, and check if more or less.
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800107 double prev = getSetpoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800108
109 if (maxRPM > prev)
110 {
111 setFanDirection(FanSpeedDirection::UP);
112 }
113 else if (prev > maxRPM)
114 {
115 setFanDirection(FanSpeedDirection::DOWN);
116 }
117 else
118 {
119 setFanDirection(FanSpeedDirection::NEUTRAL);
120 }
121
Patrick Venture563a3562018-10-30 09:31:26 -0700122 setSetpoint(maxRPM);
Patrick Ventured8012182018-03-08 08:21:38 -0800123
124 return (maxRPM);
125}
126
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800127void FanController::outputProc(double value)
Patrick Ventured8012182018-03-08 08:21:38 -0800128{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800129 double percent = value;
Patrick Ventured8012182018-03-08 08:21:38 -0800130
Patrick Venturede79ee02019-05-08 14:50:00 -0700131 /* If doing tuning, don't go into failsafe mode. */
132 if (!tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800133 {
Josh Lehandf597652023-12-19 15:05:53 -0800134 bool failsafeCurrState = _owner->getFailSafeMode();
135
136 // Note when failsafe state transitions happen
137 if (failsafePrevState != failsafeCurrState)
138 {
139 failsafePrevState = failsafeCurrState;
140 failsafeTransition = true;
141 }
142
143 if (failsafeCurrState)
Patrick Ventured8012182018-03-08 08:21:38 -0800144 {
Brandon Kimbcdeb832022-08-15 23:27:36 +0000145 double failsafePercent = _owner->getFailSafePercent();
146
147#ifdef STRICT_FAILSAFE_PWM
148 // Unconditionally replace the computed PWM with the
149 // failsafe PWM if STRICT_FAILSAFE_PWM is defined.
150 percent = failsafePercent;
151#else
152 // Ensure PWM is never lower than the failsafe PWM.
153 // The computed PWM is still allowed to rise higher than
154 // failsafe PWM if STRICT_FAILSAFE_PWM is NOT defined.
155 // This is the default behavior.
156 if (percent < failsafePercent)
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800157 {
Brandon Kimbcdeb832022-08-15 23:27:36 +0000158 percent = failsafePercent;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800159 }
Brandon Kimbcdeb832022-08-15 23:27:36 +0000160#endif
Patrick Ventured8012182018-03-08 08:21:38 -0800161 }
Josh Lehandf597652023-12-19 15:05:53 -0800162
163 // Always print if debug enabled
164 if (debugEnabled)
165 {
166 std::cerr << "Zone " << _owner->getZoneID() << " fans, "
167 << (failsafeCurrState ? "failsafe" : "normal")
168 << " mode, output pwm: " << percent << "\n";
169 }
Bonnie Loc51ba912022-10-12 14:07:22 +0800170 else
171 {
Josh Lehandf597652023-12-19 15:05:53 -0800172 // Only print once per transition when not debugging
173 if (failsafeTransition)
Bonnie Loc51ba912022-10-12 14:07:22 +0800174 {
Josh Lehandf597652023-12-19 15:05:53 -0800175 failsafeTransition = false;
176 std::cerr << "Zone " << _owner->getZoneID() << " fans, "
177 << (failsafeCurrState ? "entering failsafe"
178 : "returning to normal")
179 << " mode, output pwm: " << percent << "\n";
Bonnie Loc51ba912022-10-12 14:07:22 +0800180 }
181 }
Patrick Ventured8012182018-03-08 08:21:38 -0800182 }
Josh Lehandf597652023-12-19 15:05:53 -0800183 else
184 {
185 if (debugEnabled)
186 {
187 std::cerr << "Zone " << _owner->getZoneID()
188 << " fans, tuning mode, bypassing failsafe, output pwm: "
189 << percent << "\n";
190 }
191 }
Patrick Ventured8012182018-03-08 08:21:38 -0800192
193 // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that.
Josh Lehand38ae272020-11-13 02:59:30 -0800194 percent /= 100.0;
Patrick Ventured8012182018-03-08 08:21:38 -0800195
196 // PidSensorMap for writing.
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700197 for (const auto& it : _inputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800198 {
Patrick Venturea58197c2018-06-11 15:29:45 -0700199 auto sensor = _owner->getSensor(it);
Josh Lehana4146eb2020-10-01 11:49:09 -0700200 auto redundantWrite = _owner->getRedundantWrite();
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800201 int64_t rawWritten = -1;
Josh Lehana4146eb2020-10-01 11:49:09 -0700202 sensor->write(percent, redundantWrite, &rawWritten);
Josh Lehanb3005752022-02-22 20:48:07 -0800203
204 // The outputCache will be used later,
205 // to store a record of the PWM commanded,
206 // so that this information can be included during logging.
207 auto unscaledWritten = static_cast<double>(rawWritten);
208 _owner->setOutputCache(sensor->getName(), {percent, unscaledWritten});
Patrick Ventured8012182018-03-08 08:21:38 -0800209 }
210
Patrick Ventured8012182018-03-08 08:21:38 -0800211 return;
212}
Patrick Venturea0764872020-08-08 07:48:43 -0700213
Patrick Rudolph7e635022023-10-13 12:40:14 +0200214FanController::~FanController()
215{
216#ifdef OFFLINE_FAILSAFE_PWM
217 double percent = _owner->getFailSafePercent();
218 if (debugEnabled)
219 {
220 std::cerr << "Zone " << _owner->getZoneID()
221 << " offline fans output pwm: " << percent << "\n";
222 }
223
224 // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that.
225 percent /= 100.0;
226
227 // PidSensorMap for writing.
228 for (const auto& it : _inputs)
229 {
230 auto sensor = _owner->getSensor(it);
231 auto redundantWrite = _owner->getRedundantWrite();
232 int64_t rawWritten;
233 sensor->write(percent, redundantWrite, &rawWritten);
234
235 // The outputCache will be used later,
236 // to store a record of the PWM commanded,
237 // so that this information can be included during logging.
238 auto unscaledWritten = static_cast<double>(rawWritten);
239 _owner->setOutputCache(sensor->getName(), {percent, unscaledWritten});
240 }
241#endif
242}
243
Patrick Venturea0764872020-08-08 07:48:43 -0700244} // namespace pid_control