blob: 8bbe3f29d58414c072dcf45387bd2877c7188224 [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 */
16
Patrick Rudolph93660892023-10-13 14:18:46 +020017#include "config.h"
18
Patrick Ventured8012182018-03-08 08:21:38 -080019#include "fancontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070020
Patrick Venturec32e3fc2019-02-28 10:01:11 -080021#include "tuning.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080022#include "util.hpp"
23#include "zone.hpp"
24
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070025#include <algorithm>
Josh Lehanca791152020-09-20 23:41:39 -070026#include <cmath>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070027#include <iostream>
28
Patrick Venturea0764872020-08-08 07:48:43 -070029namespace pid_control
30{
31
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070032std::unique_ptr<PIDController>
Patrick Venture563a3562018-10-30 09:31:26 -070033 FanController::createFanPid(ZoneInterface* owner, const std::string& id,
Patrick Venture4a2dc4d2018-10-23 09:02:55 -070034 const std::vector<std::string>& inputs,
Patrick Venturef77d5a52018-10-23 09:32:52 -070035 const ec::pidinfo& initial)
Patrick Ventured8012182018-03-08 08:21:38 -080036{
Patrick Venture566a1512018-06-12 14:51:07 -070037 if (inputs.size() == 0)
38 {
39 return nullptr;
40 }
Patrick Ventured8012182018-03-08 08:21:38 -080041 auto fan = std::make_unique<FanController>(id, inputs, owner);
Patrick Venture563a3562018-10-30 09:31:26 -070042 ec::pid_info_t* info = fan->getPIDInfo();
Patrick Ventured8012182018-03-08 08:21:38 -080043
Patrick Venture7af157b2018-10-30 11:24:40 -070044 initializePIDStruct(info, initial);
Patrick Ventured8012182018-03-08 08:21:38 -080045
46 return fan;
47}
48
Patrick Venture5f59c0f2018-11-11 12:55:14 -080049double FanController::inputProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080050{
Josh Lehand38ae272020-11-13 02:59:30 -080051 double value = 0.0;
52 std::vector<double> values;
53 std::vector<double>::iterator result;
Patrick Ventured8012182018-03-08 08:21:38 -080054
55 try
56 {
57 for (const auto& name : _inputs)
58 {
Josh Lehand38ae272020-11-13 02:59:30 -080059 // Read the unscaled value, to correctly recover the RPM
60 value = _owner->getCachedValues(name).unscaled;
61
Patrick Ventured8012182018-03-08 08:21:38 -080062 /* If we have a fan we can't read, its value will be 0 for at least
63 * some boards, while others... the fan will drop off dbus (if
64 * that's how it's being read and in that case its value will never
65 * be updated anymore, which is relatively harmless, except, when
66 * something tries to read its value through IPMI, and can't, they
67 * sort of have to guess -- all the other fans are reporting, why
68 * not this one? Maybe it's unable to be read, so it's "bad."
69 */
Josh Lehanca791152020-09-20 23:41:39 -070070 if (!(std::isfinite(value)))
Patrick Ventured8012182018-03-08 08:21:38 -080071 {
Josh Lehanca791152020-09-20 23:41:39 -070072 continue;
Patrick Ventured8012182018-03-08 08:21:38 -080073 }
Josh Lehand38ae272020-11-13 02:59:30 -080074 if (value <= 0.0)
Josh Lehanca791152020-09-20 23:41:39 -070075 {
76 continue;
77 }
78
79 values.push_back(value);
Patrick Ventured8012182018-03-08 08:21:38 -080080 }
81 }
82 catch (const std::exception& e)
83 {
Patrick Venture563a3562018-10-30 09:31:26 -070084 std::cerr << "exception on inputProc.\n";
Patrick Ventured8012182018-03-08 08:21:38 -080085 throw;
86 }
87
Patrick Venture566a1512018-06-12 14:51:07 -070088 /* Reset the value from the above loop. */
Josh Lehand38ae272020-11-13 02:59:30 -080089 value = 0.0;
Patrick Ventured8012182018-03-08 08:21:38 -080090 if (values.size() > 0)
91 {
Patrick Ventured8012182018-03-08 08:21:38 -080092 /* the fan PID algorithm was unstable with average, and seemed to work
93 * better with minimum. I had considered making this choice a variable
Patrick Venturedf766f22018-10-13 09:30:58 -070094 * in the configuration, and it's a nice-to-have..
Patrick Ventured8012182018-03-08 08:21:38 -080095 */
96 result = std::min_element(values.begin(), values.end());
97 value = *result;
98 }
99
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800100 return value;
Patrick Ventured8012182018-03-08 08:21:38 -0800101}
102
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800103double FanController::setptProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800104{
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700105 double maxRPM = _owner->getMaxSetPointRequest();
Patrick Ventured8012182018-03-08 08:21:38 -0800106
107 // store for reference, and check if more or less.
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800108 double prev = getSetpoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800109
110 if (maxRPM > prev)
111 {
112 setFanDirection(FanSpeedDirection::UP);
113 }
114 else if (prev > maxRPM)
115 {
116 setFanDirection(FanSpeedDirection::DOWN);
117 }
118 else
119 {
120 setFanDirection(FanSpeedDirection::NEUTRAL);
121 }
122
Patrick Venture563a3562018-10-30 09:31:26 -0700123 setSetpoint(maxRPM);
Patrick Ventured8012182018-03-08 08:21:38 -0800124
125 return (maxRPM);
126}
127
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800128void FanController::outputProc(double value)
Patrick Ventured8012182018-03-08 08:21:38 -0800129{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800130 double percent = value;
Patrick Ventured8012182018-03-08 08:21:38 -0800131
Patrick Venturede79ee02019-05-08 14:50:00 -0700132 /* If doing tuning, don't go into failsafe mode. */
133 if (!tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800134 {
Josh Lehandf597652023-12-19 15:05:53 -0800135 bool failsafeCurrState = _owner->getFailSafeMode();
136
137 // Note when failsafe state transitions happen
138 if (failsafePrevState != failsafeCurrState)
139 {
140 failsafePrevState = failsafeCurrState;
141 failsafeTransition = true;
142 }
143
144 if (failsafeCurrState)
Patrick Ventured8012182018-03-08 08:21:38 -0800145 {
Brandon Kimbcdeb832022-08-15 23:27:36 +0000146 double failsafePercent = _owner->getFailSafePercent();
147
148#ifdef STRICT_FAILSAFE_PWM
149 // Unconditionally replace the computed PWM with the
150 // failsafe PWM if STRICT_FAILSAFE_PWM is defined.
151 percent = failsafePercent;
152#else
153 // Ensure PWM is never lower than the failsafe PWM.
154 // The computed PWM is still allowed to rise higher than
155 // failsafe PWM if STRICT_FAILSAFE_PWM is NOT defined.
156 // This is the default behavior.
157 if (percent < failsafePercent)
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800158 {
Brandon Kimbcdeb832022-08-15 23:27:36 +0000159 percent = failsafePercent;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800160 }
Brandon Kimbcdeb832022-08-15 23:27:36 +0000161#endif
Patrick Ventured8012182018-03-08 08:21:38 -0800162 }
Josh Lehandf597652023-12-19 15:05:53 -0800163
164 // Always print if debug enabled
165 if (debugEnabled)
166 {
167 std::cerr << "Zone " << _owner->getZoneID() << " fans, "
168 << (failsafeCurrState ? "failsafe" : "normal")
169 << " mode, output pwm: " << percent << "\n";
170 }
Bonnie Loc51ba912022-10-12 14:07:22 +0800171 else
172 {
Josh Lehandf597652023-12-19 15:05:53 -0800173 // Only print once per transition when not debugging
174 if (failsafeTransition)
Bonnie Loc51ba912022-10-12 14:07:22 +0800175 {
Josh Lehandf597652023-12-19 15:05:53 -0800176 failsafeTransition = false;
177 std::cerr << "Zone " << _owner->getZoneID() << " fans, "
178 << (failsafeCurrState ? "entering failsafe"
179 : "returning to normal")
180 << " mode, output pwm: " << percent << "\n";
Bonnie Loc51ba912022-10-12 14:07:22 +0800181 }
182 }
Patrick Ventured8012182018-03-08 08:21:38 -0800183 }
Josh Lehandf597652023-12-19 15:05:53 -0800184 else
185 {
186 if (debugEnabled)
187 {
188 std::cerr << "Zone " << _owner->getZoneID()
189 << " fans, tuning mode, bypassing failsafe, output pwm: "
190 << percent << "\n";
191 }
192 }
Patrick Ventured8012182018-03-08 08:21:38 -0800193
194 // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that.
Josh Lehand38ae272020-11-13 02:59:30 -0800195 percent /= 100.0;
Patrick Ventured8012182018-03-08 08:21:38 -0800196
197 // PidSensorMap for writing.
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700198 for (const auto& it : _inputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800199 {
Patrick Venturea58197c2018-06-11 15:29:45 -0700200 auto sensor = _owner->getSensor(it);
Josh Lehana4146eb2020-10-01 11:49:09 -0700201 auto redundantWrite = _owner->getRedundantWrite();
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800202 int64_t rawWritten = -1;
Josh Lehana4146eb2020-10-01 11:49:09 -0700203 sensor->write(percent, redundantWrite, &rawWritten);
Josh Lehanb3005752022-02-22 20:48:07 -0800204
205 // The outputCache will be used later,
206 // to store a record of the PWM commanded,
207 // so that this information can be included during logging.
208 auto unscaledWritten = static_cast<double>(rawWritten);
209 _owner->setOutputCache(sensor->getName(), {percent, unscaledWritten});
Patrick Ventured8012182018-03-08 08:21:38 -0800210 }
211
Patrick Ventured8012182018-03-08 08:21:38 -0800212 return;
213}
Patrick Venturea0764872020-08-08 07:48:43 -0700214
215} // namespace pid_control