Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 1 | /** |
| 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 Eustaquio | 9f1532d | 2023-12-21 09:32:14 -0600 | [diff] [blame] | 16 | #include "config.h" |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 17 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 18 | #include "fancontroller.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 19 | |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 20 | #include "tuning.hpp" |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 21 | #include "util.hpp" |
| 22 | #include "zone.hpp" |
| 23 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 24 | #include <algorithm> |
Josh Lehan | ca79115 | 2020-09-20 23:41:39 -0700 | [diff] [blame] | 25 | #include <cmath> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 26 | #include <iostream> |
| 27 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 28 | namespace pid_control |
| 29 | { |
| 30 | |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 31 | std::unique_ptr<PIDController> FanController::createFanPid( |
| 32 | ZoneInterface* owner, const std::string& id, |
| 33 | const std::vector<std::string>& inputs, const ec::pidinfo& initial) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 34 | { |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 35 | if (inputs.size() == 0) |
| 36 | { |
| 37 | return nullptr; |
| 38 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 39 | auto fan = std::make_unique<FanController>(id, inputs, owner); |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 40 | ec::pid_info_t* info = fan->getPIDInfo(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 41 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 42 | initializePIDStruct(info, initial); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 43 | |
| 44 | return fan; |
| 45 | } |
| 46 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 47 | double FanController::inputProc(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 48 | { |
Josh Lehan | d38ae27 | 2020-11-13 02:59:30 -0800 | [diff] [blame] | 49 | double value = 0.0; |
| 50 | std::vector<double> values; |
| 51 | std::vector<double>::iterator result; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 52 | |
| 53 | try |
| 54 | { |
| 55 | for (const auto& name : _inputs) |
| 56 | { |
Josh Lehan | d38ae27 | 2020-11-13 02:59:30 -0800 | [diff] [blame] | 57 | // Read the unscaled value, to correctly recover the RPM |
| 58 | value = _owner->getCachedValues(name).unscaled; |
| 59 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 60 | /* If we have a fan we can't read, its value will be 0 for at least |
| 61 | * some boards, while others... the fan will drop off dbus (if |
| 62 | * that's how it's being read and in that case its value will never |
| 63 | * be updated anymore, which is relatively harmless, except, when |
| 64 | * something tries to read its value through IPMI, and can't, they |
| 65 | * sort of have to guess -- all the other fans are reporting, why |
| 66 | * not this one? Maybe it's unable to be read, so it's "bad." |
| 67 | */ |
Josh Lehan | ca79115 | 2020-09-20 23:41:39 -0700 | [diff] [blame] | 68 | if (!(std::isfinite(value))) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 69 | { |
Josh Lehan | ca79115 | 2020-09-20 23:41:39 -0700 | [diff] [blame] | 70 | continue; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 71 | } |
Josh Lehan | d38ae27 | 2020-11-13 02:59:30 -0800 | [diff] [blame] | 72 | if (value <= 0.0) |
Josh Lehan | ca79115 | 2020-09-20 23:41:39 -0700 | [diff] [blame] | 73 | { |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | values.push_back(value); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | catch (const std::exception& e) |
| 81 | { |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 82 | std::cerr << "exception on inputProc.\n"; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 83 | throw; |
| 84 | } |
| 85 | |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 86 | /* Reset the value from the above loop. */ |
Josh Lehan | d38ae27 | 2020-11-13 02:59:30 -0800 | [diff] [blame] | 87 | value = 0.0; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 88 | if (values.size() > 0) |
| 89 | { |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 90 | /* the fan PID algorithm was unstable with average, and seemed to work |
| 91 | * better with minimum. I had considered making this choice a variable |
Patrick Venture | df766f2 | 2018-10-13 09:30:58 -0700 | [diff] [blame] | 92 | * in the configuration, and it's a nice-to-have.. |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 93 | */ |
| 94 | result = std::min_element(values.begin(), values.end()); |
| 95 | value = *result; |
| 96 | } |
| 97 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 98 | return value; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 101 | double FanController::setptProc(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 102 | { |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 103 | double maxRPM = _owner->getMaxSetPointRequest(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 104 | |
| 105 | // store for reference, and check if more or less. |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 106 | double prev = getSetpoint(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 107 | |
| 108 | if (maxRPM > prev) |
| 109 | { |
| 110 | setFanDirection(FanSpeedDirection::UP); |
| 111 | } |
| 112 | else if (prev > maxRPM) |
| 113 | { |
| 114 | setFanDirection(FanSpeedDirection::DOWN); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | setFanDirection(FanSpeedDirection::NEUTRAL); |
| 119 | } |
| 120 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 121 | setSetpoint(maxRPM); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 122 | |
| 123 | return (maxRPM); |
| 124 | } |
| 125 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 126 | void FanController::outputProc(double value) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 127 | { |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 128 | double percent = value; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 129 | |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 130 | /* If doing tuning, don't go into failsafe mode. */ |
| 131 | if (!tuningEnabled) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 132 | { |
Josh Lehan | df59765 | 2023-12-19 15:05:53 -0800 | [diff] [blame] | 133 | bool failsafeCurrState = _owner->getFailSafeMode(); |
| 134 | |
| 135 | // Note when failsafe state transitions happen |
| 136 | if (failsafePrevState != failsafeCurrState) |
| 137 | { |
| 138 | failsafePrevState = failsafeCurrState; |
| 139 | failsafeTransition = true; |
| 140 | } |
| 141 | |
| 142 | if (failsafeCurrState) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 143 | { |
Brandon Kim | bcdeb83 | 2022-08-15 23:27:36 +0000 | [diff] [blame] | 144 | double failsafePercent = _owner->getFailSafePercent(); |
| 145 | |
| 146 | #ifdef STRICT_FAILSAFE_PWM |
| 147 | // Unconditionally replace the computed PWM with the |
| 148 | // failsafe PWM if STRICT_FAILSAFE_PWM is defined. |
| 149 | percent = failsafePercent; |
| 150 | #else |
| 151 | // Ensure PWM is never lower than the failsafe PWM. |
| 152 | // The computed PWM is still allowed to rise higher than |
| 153 | // failsafe PWM if STRICT_FAILSAFE_PWM is NOT defined. |
| 154 | // This is the default behavior. |
| 155 | if (percent < failsafePercent) |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 156 | { |
Brandon Kim | bcdeb83 | 2022-08-15 23:27:36 +0000 | [diff] [blame] | 157 | percent = failsafePercent; |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 158 | } |
Brandon Kim | bcdeb83 | 2022-08-15 23:27:36 +0000 | [diff] [blame] | 159 | #endif |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 160 | } |
Josh Lehan | df59765 | 2023-12-19 15:05:53 -0800 | [diff] [blame] | 161 | |
| 162 | // Always print if debug enabled |
| 163 | if (debugEnabled) |
| 164 | { |
| 165 | std::cerr << "Zone " << _owner->getZoneID() << " fans, " |
| 166 | << (failsafeCurrState ? "failsafe" : "normal") |
| 167 | << " mode, output pwm: " << percent << "\n"; |
| 168 | } |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 169 | else |
| 170 | { |
Josh Lehan | df59765 | 2023-12-19 15:05:53 -0800 | [diff] [blame] | 171 | // Only print once per transition when not debugging |
| 172 | if (failsafeTransition) |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 173 | { |
Josh Lehan | df59765 | 2023-12-19 15:05:53 -0800 | [diff] [blame] | 174 | failsafeTransition = false; |
| 175 | std::cerr << "Zone " << _owner->getZoneID() << " fans, " |
| 176 | << (failsafeCurrState ? "entering failsafe" |
| 177 | : "returning to normal") |
| 178 | << " mode, output pwm: " << percent << "\n"; |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 179 | } |
| 180 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 181 | } |
Josh Lehan | df59765 | 2023-12-19 15:05:53 -0800 | [diff] [blame] | 182 | else |
| 183 | { |
| 184 | if (debugEnabled) |
| 185 | { |
| 186 | std::cerr << "Zone " << _owner->getZoneID() |
| 187 | << " fans, tuning mode, bypassing failsafe, output pwm: " |
| 188 | << percent << "\n"; |
| 189 | } |
| 190 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 191 | |
| 192 | // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that. |
Josh Lehan | d38ae27 | 2020-11-13 02:59:30 -0800 | [diff] [blame] | 193 | percent /= 100.0; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 194 | |
| 195 | // PidSensorMap for writing. |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 196 | for (const auto& it : _inputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 197 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 198 | auto sensor = _owner->getSensor(it); |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 199 | auto redundantWrite = _owner->getRedundantWrite(); |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 200 | int64_t rawWritten = -1; |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 201 | sensor->write(percent, redundantWrite, &rawWritten); |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 202 | |
| 203 | // The outputCache will be used later, |
| 204 | // to store a record of the PWM commanded, |
| 205 | // so that this information can be included during logging. |
| 206 | auto unscaledWritten = static_cast<double>(rawWritten); |
| 207 | _owner->setOutputCache(sensor->getName(), {percent, unscaledWritten}); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 210 | return; |
| 211 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 212 | |
Patrick Rudolph | 7e63502 | 2023-10-13 12:40:14 +0200 | [diff] [blame] | 213 | FanController::~FanController() |
| 214 | { |
| 215 | #ifdef OFFLINE_FAILSAFE_PWM |
| 216 | double percent = _owner->getFailSafePercent(); |
| 217 | if (debugEnabled) |
| 218 | { |
| 219 | std::cerr << "Zone " << _owner->getZoneID() |
| 220 | << " offline fans output pwm: " << percent << "\n"; |
| 221 | } |
| 222 | |
| 223 | // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that. |
| 224 | percent /= 100.0; |
| 225 | |
| 226 | // PidSensorMap for writing. |
| 227 | for (const auto& it : _inputs) |
| 228 | { |
| 229 | auto sensor = _owner->getSensor(it); |
| 230 | auto redundantWrite = _owner->getRedundantWrite(); |
| 231 | int64_t rawWritten; |
| 232 | sensor->write(percent, redundantWrite, &rawWritten); |
| 233 | |
| 234 | // The outputCache will be used later, |
| 235 | // to store a record of the PWM commanded, |
| 236 | // so that this information can be included during logging. |
| 237 | auto unscaledWritten = static_cast<double>(rawWritten); |
| 238 | _owner->setOutputCache(sensor->getName(), {percent, unscaledWritten}); |
| 239 | } |
| 240 | #endif |
| 241 | } |
| 242 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 243 | } // namespace pid_control |