blob: d7ac0e6355591001248b553131acff06e4d378da [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 Ventured8012182018-03-08 08:21:38 -080017#include "fancontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070018
Patrick Venturec32e3fc2019-02-28 10:01:11 -080019#include "tuning.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080020#include "util.hpp"
21#include "zone.hpp"
22
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070023#include <algorithm>
Josh Lehanca791152020-09-20 23:41:39 -070024#include <cmath>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070025#include <iostream>
26
Patrick Venturea0764872020-08-08 07:48:43 -070027namespace pid_control
28{
29
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070030std::unique_ptr<PIDController>
Patrick Venture563a3562018-10-30 09:31:26 -070031 FanController::createFanPid(ZoneInterface* owner, const std::string& id,
Patrick Venture4a2dc4d2018-10-23 09:02:55 -070032 const std::vector<std::string>& inputs,
Patrick Venturef77d5a52018-10-23 09:32:52 -070033 const ec::pidinfo& initial)
Patrick Ventured8012182018-03-08 08:21:38 -080034{
Patrick Venture566a1512018-06-12 14:51:07 -070035 if (inputs.size() == 0)
36 {
37 return nullptr;
38 }
Patrick Ventured8012182018-03-08 08:21:38 -080039 auto fan = std::make_unique<FanController>(id, inputs, owner);
Patrick Venture563a3562018-10-30 09:31:26 -070040 ec::pid_info_t* info = fan->getPIDInfo();
Patrick Ventured8012182018-03-08 08:21:38 -080041
Patrick Venture7af157b2018-10-30 11:24:40 -070042 initializePIDStruct(info, initial);
Patrick Ventured8012182018-03-08 08:21:38 -080043
44 return fan;
45}
46
Patrick Venture5f59c0f2018-11-11 12:55:14 -080047double FanController::inputProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080048{
Patrick Ventured8012182018-03-08 08:21:38 -080049 double value = 0;
50 std::vector<int64_t> values;
51 std::vector<int64_t>::iterator result;
52
53 try
54 {
55 for (const auto& name : _inputs)
56 {
57 value = _owner->getCachedValue(name);
58 /* If we have a fan we can't read, its value will be 0 for at least
59 * some boards, while others... the fan will drop off dbus (if
60 * that's how it's being read and in that case its value will never
61 * be updated anymore, which is relatively harmless, except, when
62 * something tries to read its value through IPMI, and can't, they
63 * sort of have to guess -- all the other fans are reporting, why
64 * not this one? Maybe it's unable to be read, so it's "bad."
65 */
Josh Lehanca791152020-09-20 23:41:39 -070066 if (!(std::isfinite(value)))
Patrick Ventured8012182018-03-08 08:21:38 -080067 {
Josh Lehanca791152020-09-20 23:41:39 -070068 continue;
Patrick Ventured8012182018-03-08 08:21:38 -080069 }
Josh Lehanca791152020-09-20 23:41:39 -070070 if (value <= 0)
71 {
72 continue;
73 }
74
75 values.push_back(value);
Patrick Ventured8012182018-03-08 08:21:38 -080076 }
77 }
78 catch (const std::exception& e)
79 {
Patrick Venture563a3562018-10-30 09:31:26 -070080 std::cerr << "exception on inputProc.\n";
Patrick Ventured8012182018-03-08 08:21:38 -080081 throw;
82 }
83
Patrick Venture566a1512018-06-12 14:51:07 -070084 /* Reset the value from the above loop. */
85 value = 0;
Patrick Ventured8012182018-03-08 08:21:38 -080086 if (values.size() > 0)
87 {
Patrick Ventured8012182018-03-08 08:21:38 -080088 /* the fan PID algorithm was unstable with average, and seemed to work
89 * better with minimum. I had considered making this choice a variable
Patrick Venturedf766f22018-10-13 09:30:58 -070090 * in the configuration, and it's a nice-to-have..
Patrick Ventured8012182018-03-08 08:21:38 -080091 */
92 result = std::min_element(values.begin(), values.end());
93 value = *result;
94 }
95
Patrick Venture5f59c0f2018-11-11 12:55:14 -080096 return value;
Patrick Ventured8012182018-03-08 08:21:38 -080097}
98
Patrick Venture5f59c0f2018-11-11 12:55:14 -080099double FanController::setptProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800100{
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700101 double maxRPM = _owner->getMaxSetPointRequest();
Patrick Ventured8012182018-03-08 08:21:38 -0800102
103 // store for reference, and check if more or less.
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800104 double prev = getSetpoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800105
106 if (maxRPM > prev)
107 {
108 setFanDirection(FanSpeedDirection::UP);
109 }
110 else if (prev > maxRPM)
111 {
112 setFanDirection(FanSpeedDirection::DOWN);
113 }
114 else
115 {
116 setFanDirection(FanSpeedDirection::NEUTRAL);
117 }
118
Patrick Venture563a3562018-10-30 09:31:26 -0700119 setSetpoint(maxRPM);
Patrick Ventured8012182018-03-08 08:21:38 -0800120
121 return (maxRPM);
122}
123
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800124void FanController::outputProc(double value)
Patrick Ventured8012182018-03-08 08:21:38 -0800125{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800126 double percent = value;
Patrick Ventured8012182018-03-08 08:21:38 -0800127
Patrick Venturede79ee02019-05-08 14:50:00 -0700128 /* If doing tuning, don't go into failsafe mode. */
129 if (!tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800130 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800131 if (_owner->getFailSafeMode())
Patrick Ventured8012182018-03-08 08:21:38 -0800132 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800133 /* In case it's being set to 100% */
134 if (percent < _owner->getFailSafePercent())
135 {
136 percent = _owner->getFailSafePercent();
137 }
Patrick Ventured8012182018-03-08 08:21:38 -0800138 }
139 }
Patrick Ventured8012182018-03-08 08:21:38 -0800140
141 // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that.
142 percent /= 100;
143
144 // PidSensorMap for writing.
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700145 for (const auto& it : _inputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800146 {
Patrick Venturea58197c2018-06-11 15:29:45 -0700147 auto sensor = _owner->getSensor(it);
Josh Lehana4146eb2020-10-01 11:49:09 -0700148 auto redundantWrite = _owner->getRedundantWrite();
149 int64_t rawWritten;
150 sensor->write(percent, redundantWrite, &rawWritten);
Patrick Ventured8012182018-03-08 08:21:38 -0800151 }
152
Patrick Ventured8012182018-03-08 08:21:38 -0800153 return;
154}
Patrick Venturea0764872020-08-08 07:48:43 -0700155
156} // namespace pid_control