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 | */ |
| 16 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 17 | #include "fancontroller.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 18 | |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 19 | #include "tuning.hpp" |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 20 | #include "util.hpp" |
| 21 | #include "zone.hpp" |
| 22 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | #include <iostream> |
| 25 | |
| 26 | std::unique_ptr<PIDController> |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 27 | FanController::createFanPid(ZoneInterface* owner, const std::string& id, |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 28 | const std::vector<std::string>& inputs, |
Patrick Venture | f77d5a5 | 2018-10-23 09:32:52 -0700 | [diff] [blame] | 29 | const ec::pidinfo& initial) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 30 | { |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 31 | if (inputs.size() == 0) |
| 32 | { |
| 33 | return nullptr; |
| 34 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 35 | auto fan = std::make_unique<FanController>(id, inputs, owner); |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 36 | ec::pid_info_t* info = fan->getPIDInfo(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 37 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 38 | initializePIDStruct(info, initial); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 39 | |
| 40 | return fan; |
| 41 | } |
| 42 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 43 | double FanController::inputProc(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 44 | { |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 45 | double value = 0; |
| 46 | std::vector<int64_t> values; |
| 47 | std::vector<int64_t>::iterator result; |
| 48 | |
| 49 | try |
| 50 | { |
| 51 | for (const auto& name : _inputs) |
| 52 | { |
| 53 | value = _owner->getCachedValue(name); |
| 54 | /* If we have a fan we can't read, its value will be 0 for at least |
| 55 | * some boards, while others... the fan will drop off dbus (if |
| 56 | * that's how it's being read and in that case its value will never |
| 57 | * be updated anymore, which is relatively harmless, except, when |
| 58 | * something tries to read its value through IPMI, and can't, they |
| 59 | * sort of have to guess -- all the other fans are reporting, why |
| 60 | * not this one? Maybe it's unable to be read, so it's "bad." |
| 61 | */ |
| 62 | if (value > 0) |
| 63 | { |
| 64 | values.push_back(value); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | catch (const std::exception& e) |
| 69 | { |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 70 | std::cerr << "exception on inputProc.\n"; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 71 | throw; |
| 72 | } |
| 73 | |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 74 | /* Reset the value from the above loop. */ |
| 75 | value = 0; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 76 | if (values.size() > 0) |
| 77 | { |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 78 | /* the fan PID algorithm was unstable with average, and seemed to work |
| 79 | * better with minimum. I had considered making this choice a variable |
Patrick Venture | df766f2 | 2018-10-13 09:30:58 -0700 | [diff] [blame] | 80 | * in the configuration, and it's a nice-to-have.. |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 81 | */ |
| 82 | result = std::min_element(values.begin(), values.end()); |
| 83 | value = *result; |
| 84 | } |
| 85 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 86 | return value; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 89 | double FanController::setptProc(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 90 | { |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 91 | double maxRPM = _owner->getMaxSetPointRequest(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 92 | |
| 93 | // store for reference, and check if more or less. |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 94 | double prev = getSetpoint(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 95 | |
| 96 | if (maxRPM > prev) |
| 97 | { |
| 98 | setFanDirection(FanSpeedDirection::UP); |
| 99 | } |
| 100 | else if (prev > maxRPM) |
| 101 | { |
| 102 | setFanDirection(FanSpeedDirection::DOWN); |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | setFanDirection(FanSpeedDirection::NEUTRAL); |
| 107 | } |
| 108 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 109 | setSetpoint(maxRPM); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 110 | |
| 111 | return (maxRPM); |
| 112 | } |
| 113 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 114 | void FanController::outputProc(double value) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 115 | { |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 116 | double percent = value; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 117 | |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 118 | /* If doing tuning, don't go into failsafe mode. */ |
| 119 | if (!tuningEnabled) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 120 | { |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 121 | if (_owner->getFailSafeMode()) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 122 | { |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 123 | /* In case it's being set to 100% */ |
| 124 | if (percent < _owner->getFailSafePercent()) |
| 125 | { |
| 126 | percent = _owner->getFailSafePercent(); |
| 127 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 130 | |
| 131 | // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that. |
| 132 | percent /= 100; |
| 133 | |
| 134 | // PidSensorMap for writing. |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 135 | for (const auto& it : _inputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 136 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 137 | auto sensor = _owner->getSensor(it); |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 138 | sensor->write(percent); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 141 | return; |
| 142 | } |