blob: 68c4f68210b305757a9c4a3617a04ba936064660 [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 Rudolph93660892023-10-13 14:18:46 +020018#include "config.h"
19
Patrick Ventured8012182018-03-08 08:21:38 -080020#include "fancontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070021
Patrick Venturec32e3fc2019-02-28 10:01:11 -080022#include "tuning.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080023#include "util.hpp"
24#include "zone.hpp"
25
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070026#include <algorithm>
Josh Lehanca791152020-09-20 23:41:39 -070027#include <cmath>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070028#include <iostream>
29
Patrick Venturea0764872020-08-08 07:48:43 -070030namespace pid_control
31{
32
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070033std::unique_ptr<PIDController>
Patrick Venture563a3562018-10-30 09:31:26 -070034 FanController::createFanPid(ZoneInterface* owner, const std::string& id,
Patrick Venture4a2dc4d2018-10-23 09:02:55 -070035 const std::vector<std::string>& inputs,
Patrick Venturef77d5a52018-10-23 09:32:52 -070036 const ec::pidinfo& initial)
Patrick Ventured8012182018-03-08 08:21:38 -080037{
Patrick Venture566a1512018-06-12 14:51:07 -070038 if (inputs.size() == 0)
39 {
40 return nullptr;
41 }
Patrick Ventured8012182018-03-08 08:21:38 -080042 auto fan = std::make_unique<FanController>(id, inputs, owner);
Patrick Venture563a3562018-10-30 09:31:26 -070043 ec::pid_info_t* info = fan->getPIDInfo();
Patrick Ventured8012182018-03-08 08:21:38 -080044
Patrick Venture7af157b2018-10-30 11:24:40 -070045 initializePIDStruct(info, initial);
Patrick Ventured8012182018-03-08 08:21:38 -080046
47 return fan;
48}
49
Patrick Venture5f59c0f2018-11-11 12:55:14 -080050double FanController::inputProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080051{
Josh Lehand38ae272020-11-13 02:59:30 -080052 double value = 0.0;
53 std::vector<double> values;
54 std::vector<double>::iterator result;
Patrick Ventured8012182018-03-08 08:21:38 -080055
56 try
57 {
58 for (const auto& name : _inputs)
59 {
Josh Lehand38ae272020-11-13 02:59:30 -080060 // Read the unscaled value, to correctly recover the RPM
61 value = _owner->getCachedValues(name).unscaled;
62
Patrick Ventured8012182018-03-08 08:21:38 -080063 /* If we have a fan we can't read, its value will be 0 for at least
64 * some boards, while others... the fan will drop off dbus (if
65 * that's how it's being read and in that case its value will never
66 * be updated anymore, which is relatively harmless, except, when
67 * something tries to read its value through IPMI, and can't, they
68 * sort of have to guess -- all the other fans are reporting, why
69 * not this one? Maybe it's unable to be read, so it's "bad."
70 */
Josh Lehanca791152020-09-20 23:41:39 -070071 if (!(std::isfinite(value)))
Patrick Ventured8012182018-03-08 08:21:38 -080072 {
Josh Lehanca791152020-09-20 23:41:39 -070073 continue;
Patrick Ventured8012182018-03-08 08:21:38 -080074 }
Josh Lehand38ae272020-11-13 02:59:30 -080075 if (value <= 0.0)
Josh Lehanca791152020-09-20 23:41:39 -070076 {
77 continue;
78 }
79
80 values.push_back(value);
Patrick Ventured8012182018-03-08 08:21:38 -080081 }
82 }
83 catch (const std::exception& e)
84 {
Patrick Venture563a3562018-10-30 09:31:26 -070085 std::cerr << "exception on inputProc.\n";
Patrick Ventured8012182018-03-08 08:21:38 -080086 throw;
87 }
88
Patrick Venture566a1512018-06-12 14:51:07 -070089 /* Reset the value from the above loop. */
Josh Lehand38ae272020-11-13 02:59:30 -080090 value = 0.0;
Patrick Ventured8012182018-03-08 08:21:38 -080091 if (values.size() > 0)
92 {
Patrick Ventured8012182018-03-08 08:21:38 -080093 /* the fan PID algorithm was unstable with average, and seemed to work
94 * better with minimum. I had considered making this choice a variable
Patrick Venturedf766f22018-10-13 09:30:58 -070095 * in the configuration, and it's a nice-to-have..
Patrick Ventured8012182018-03-08 08:21:38 -080096 */
97 result = std::min_element(values.begin(), values.end());
98 value = *result;
99 }
100
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800101 return value;
Patrick Ventured8012182018-03-08 08:21:38 -0800102}
103
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800104double FanController::setptProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800105{
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700106 double maxRPM = _owner->getMaxSetPointRequest();
Patrick Ventured8012182018-03-08 08:21:38 -0800107
108 // store for reference, and check if more or less.
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800109 double prev = getSetpoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800110
111 if (maxRPM > prev)
112 {
113 setFanDirection(FanSpeedDirection::UP);
114 }
115 else if (prev > maxRPM)
116 {
117 setFanDirection(FanSpeedDirection::DOWN);
118 }
119 else
120 {
121 setFanDirection(FanSpeedDirection::NEUTRAL);
122 }
123
Patrick Venture563a3562018-10-30 09:31:26 -0700124 setSetpoint(maxRPM);
Patrick Ventured8012182018-03-08 08:21:38 -0800125
126 return (maxRPM);
127}
128
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800129void FanController::outputProc(double value)
Patrick Ventured8012182018-03-08 08:21:38 -0800130{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800131 double percent = value;
Patrick Ventured8012182018-03-08 08:21:38 -0800132
Patrick Venturede79ee02019-05-08 14:50:00 -0700133 /* If doing tuning, don't go into failsafe mode. */
134 if (!tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800135 {
Josh Lehandf597652023-12-19 15:05:53 -0800136 bool failsafeCurrState = _owner->getFailSafeMode();
137
138 // Note when failsafe state transitions happen
139 if (failsafePrevState != failsafeCurrState)
140 {
141 failsafePrevState = failsafeCurrState;
142 failsafeTransition = true;
143 }
144
145 if (failsafeCurrState)
Patrick Ventured8012182018-03-08 08:21:38 -0800146 {
Brandon Kimbcdeb832022-08-15 23:27:36 +0000147 double failsafePercent = _owner->getFailSafePercent();
148
149#ifdef STRICT_FAILSAFE_PWM
150 // Unconditionally replace the computed PWM with the
151 // failsafe PWM if STRICT_FAILSAFE_PWM is defined.
152 percent = failsafePercent;
153#else
154 // Ensure PWM is never lower than the failsafe PWM.
155 // The computed PWM is still allowed to rise higher than
156 // failsafe PWM if STRICT_FAILSAFE_PWM is NOT defined.
157 // This is the default behavior.
158 if (percent < failsafePercent)
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800159 {
Brandon Kimbcdeb832022-08-15 23:27:36 +0000160 percent = failsafePercent;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800161 }
Brandon Kimbcdeb832022-08-15 23:27:36 +0000162#endif
Patrick Ventured8012182018-03-08 08:21:38 -0800163 }
Josh Lehandf597652023-12-19 15:05:53 -0800164
165 // Always print if debug enabled
166 if (debugEnabled)
167 {
168 std::cerr << "Zone " << _owner->getZoneID() << " fans, "
169 << (failsafeCurrState ? "failsafe" : "normal")
170 << " mode, output pwm: " << percent << "\n";
171 }
Bonnie Loc51ba912022-10-12 14:07:22 +0800172 else
173 {
Josh Lehandf597652023-12-19 15:05:53 -0800174 // Only print once per transition when not debugging
175 if (failsafeTransition)
Bonnie Loc51ba912022-10-12 14:07:22 +0800176 {
Josh Lehandf597652023-12-19 15:05:53 -0800177 failsafeTransition = false;
178 std::cerr << "Zone " << _owner->getZoneID() << " fans, "
179 << (failsafeCurrState ? "entering failsafe"
180 : "returning to normal")
181 << " mode, output pwm: " << percent << "\n";
Bonnie Loc51ba912022-10-12 14:07:22 +0800182 }
183 }
Patrick Ventured8012182018-03-08 08:21:38 -0800184 }
Josh Lehandf597652023-12-19 15:05:53 -0800185 else
186 {
187 if (debugEnabled)
188 {
189 std::cerr << "Zone " << _owner->getZoneID()
190 << " fans, tuning mode, bypassing failsafe, output pwm: "
191 << percent << "\n";
192 }
193 }
Patrick Ventured8012182018-03-08 08:21:38 -0800194
195 // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that.
Josh Lehand38ae272020-11-13 02:59:30 -0800196 percent /= 100.0;
Patrick Ventured8012182018-03-08 08:21:38 -0800197
198 // PidSensorMap for writing.
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700199 for (const auto& it : _inputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800200 {
Patrick Venturea58197c2018-06-11 15:29:45 -0700201 auto sensor = _owner->getSensor(it);
Josh Lehana4146eb2020-10-01 11:49:09 -0700202 auto redundantWrite = _owner->getRedundantWrite();
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800203 int64_t rawWritten = -1;
Josh Lehana4146eb2020-10-01 11:49:09 -0700204 sensor->write(percent, redundantWrite, &rawWritten);
Josh Lehanb3005752022-02-22 20:48:07 -0800205
206 // The outputCache will be used later,
207 // to store a record of the PWM commanded,
208 // so that this information can be included during logging.
209 auto unscaledWritten = static_cast<double>(rawWritten);
210 _owner->setOutputCache(sensor->getName(), {percent, unscaledWritten});
Patrick Ventured8012182018-03-08 08:21:38 -0800211 }
212
Patrick Ventured8012182018-03-08 08:21:38 -0800213 return;
214}
Patrick Venturea0764872020-08-08 07:48:43 -0700215
Patrick Rudolph7e635022023-10-13 12:40:14 +0200216FanController::~FanController()
217{
218#ifdef OFFLINE_FAILSAFE_PWM
219 double percent = _owner->getFailSafePercent();
220 if (debugEnabled)
221 {
222 std::cerr << "Zone " << _owner->getZoneID()
223 << " offline fans output pwm: " << percent << "\n";
224 }
225
226 // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that.
227 percent /= 100.0;
228
229 // PidSensorMap for writing.
230 for (const auto& it : _inputs)
231 {
232 auto sensor = _owner->getSensor(it);
233 auto redundantWrite = _owner->getRedundantWrite();
234 int64_t rawWritten;
235 sensor->write(percent, redundantWrite, &rawWritten);
236
237 // The outputCache will be used later,
238 // to store a record of the PWM commanded,
239 // so that this information can be included during logging.
240 auto unscaledWritten = static_cast<double>(rawWritten);
241 _owner->setOutputCache(sensor->getName(), {percent, unscaledWritten});
242 }
243#endif
244}
245
Patrick Venturea0764872020-08-08 07:48:43 -0700246} // namespace pid_control