blob: 41f24daca2e60eb8a8cf26c04648a2a5a55cd13f [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>
24#include <iostream>
25
Patrick Venturea0764872020-08-08 07:48:43 -070026namespace pid_control
27{
28
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070029std::unique_ptr<PIDController>
Patrick Venture563a3562018-10-30 09:31:26 -070030 FanController::createFanPid(ZoneInterface* owner, const std::string& id,
Patrick Venture4a2dc4d2018-10-23 09:02:55 -070031 const std::vector<std::string>& inputs,
Patrick Venturef77d5a52018-10-23 09:32:52 -070032 const ec::pidinfo& initial)
Patrick Ventured8012182018-03-08 08:21:38 -080033{
Patrick Venture566a1512018-06-12 14:51:07 -070034 if (inputs.size() == 0)
35 {
36 return nullptr;
37 }
Patrick Ventured8012182018-03-08 08:21:38 -080038 auto fan = std::make_unique<FanController>(id, inputs, owner);
Patrick Venture563a3562018-10-30 09:31:26 -070039 ec::pid_info_t* info = fan->getPIDInfo();
Patrick Ventured8012182018-03-08 08:21:38 -080040
Patrick Venture7af157b2018-10-30 11:24:40 -070041 initializePIDStruct(info, initial);
Patrick Ventured8012182018-03-08 08:21:38 -080042
43 return fan;
44}
45
Patrick Venture5f59c0f2018-11-11 12:55:14 -080046double FanController::inputProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080047{
Patrick Ventured8012182018-03-08 08:21:38 -080048 double value = 0;
49 std::vector<int64_t> values;
50 std::vector<int64_t>::iterator result;
51
52 try
53 {
54 for (const auto& name : _inputs)
55 {
56 value = _owner->getCachedValue(name);
57 /* If we have a fan we can't read, its value will be 0 for at least
58 * some boards, while others... the fan will drop off dbus (if
59 * that's how it's being read and in that case its value will never
60 * be updated anymore, which is relatively harmless, except, when
61 * something tries to read its value through IPMI, and can't, they
62 * sort of have to guess -- all the other fans are reporting, why
63 * not this one? Maybe it's unable to be read, so it's "bad."
64 */
65 if (value > 0)
66 {
67 values.push_back(value);
Patrick Ventured8012182018-03-08 08:21:38 -080068 }
69 }
70 }
71 catch (const std::exception& e)
72 {
Patrick Venture563a3562018-10-30 09:31:26 -070073 std::cerr << "exception on inputProc.\n";
Patrick Ventured8012182018-03-08 08:21:38 -080074 throw;
75 }
76
Patrick Venture566a1512018-06-12 14:51:07 -070077 /* Reset the value from the above loop. */
78 value = 0;
Patrick Ventured8012182018-03-08 08:21:38 -080079 if (values.size() > 0)
80 {
Patrick Ventured8012182018-03-08 08:21:38 -080081 /* the fan PID algorithm was unstable with average, and seemed to work
82 * better with minimum. I had considered making this choice a variable
Patrick Venturedf766f22018-10-13 09:30:58 -070083 * in the configuration, and it's a nice-to-have..
Patrick Ventured8012182018-03-08 08:21:38 -080084 */
85 result = std::min_element(values.begin(), values.end());
86 value = *result;
87 }
88
Patrick Venture5f59c0f2018-11-11 12:55:14 -080089 return value;
Patrick Ventured8012182018-03-08 08:21:38 -080090}
91
Patrick Venture5f59c0f2018-11-11 12:55:14 -080092double FanController::setptProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080093{
Patrick Venturef7a2dd52019-07-16 14:31:13 -070094 double maxRPM = _owner->getMaxSetPointRequest();
Patrick Ventured8012182018-03-08 08:21:38 -080095
96 // store for reference, and check if more or less.
Patrick Venture5f59c0f2018-11-11 12:55:14 -080097 double prev = getSetpoint();
Patrick Ventured8012182018-03-08 08:21:38 -080098
99 if (maxRPM > prev)
100 {
101 setFanDirection(FanSpeedDirection::UP);
102 }
103 else if (prev > maxRPM)
104 {
105 setFanDirection(FanSpeedDirection::DOWN);
106 }
107 else
108 {
109 setFanDirection(FanSpeedDirection::NEUTRAL);
110 }
111
Patrick Venture563a3562018-10-30 09:31:26 -0700112 setSetpoint(maxRPM);
Patrick Ventured8012182018-03-08 08:21:38 -0800113
114 return (maxRPM);
115}
116
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800117void FanController::outputProc(double value)
Patrick Ventured8012182018-03-08 08:21:38 -0800118{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800119 double percent = value;
Patrick Ventured8012182018-03-08 08:21:38 -0800120
Patrick Venturede79ee02019-05-08 14:50:00 -0700121 /* If doing tuning, don't go into failsafe mode. */
122 if (!tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800123 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800124 if (_owner->getFailSafeMode())
Patrick Ventured8012182018-03-08 08:21:38 -0800125 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800126 /* In case it's being set to 100% */
127 if (percent < _owner->getFailSafePercent())
128 {
129 percent = _owner->getFailSafePercent();
130 }
Patrick Ventured8012182018-03-08 08:21:38 -0800131 }
132 }
Patrick Ventured8012182018-03-08 08:21:38 -0800133
134 // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that.
135 percent /= 100;
136
137 // PidSensorMap for writing.
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700138 for (const auto& it : _inputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800139 {
Patrick Venturea58197c2018-06-11 15:29:45 -0700140 auto sensor = _owner->getSensor(it);
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800141 sensor->write(percent);
Patrick Ventured8012182018-03-08 08:21:38 -0800142 }
143
Patrick Ventured8012182018-03-08 08:21:38 -0800144 return;
145}
Patrick Venturea0764872020-08-08 07:48:43 -0700146
147} // namespace pid_control