blob: 1f23e0b0a4a83dd1107a9a68e9fb0e80df05a3b9 [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
26std::unique_ptr<PIDController>
Patrick Venture563a3562018-10-30 09:31:26 -070027 FanController::createFanPid(ZoneInterface* owner, const std::string& id,
Patrick Venture4a2dc4d2018-10-23 09:02:55 -070028 const std::vector<std::string>& inputs,
Patrick Venturef77d5a52018-10-23 09:32:52 -070029 const ec::pidinfo& initial)
Patrick Ventured8012182018-03-08 08:21:38 -080030{
Patrick Venture566a1512018-06-12 14:51:07 -070031 if (inputs.size() == 0)
32 {
33 return nullptr;
34 }
Patrick Ventured8012182018-03-08 08:21:38 -080035 auto fan = std::make_unique<FanController>(id, inputs, owner);
Patrick Venture563a3562018-10-30 09:31:26 -070036 ec::pid_info_t* info = fan->getPIDInfo();
Patrick Ventured8012182018-03-08 08:21:38 -080037
Patrick Venture7af157b2018-10-30 11:24:40 -070038 initializePIDStruct(info, initial);
Patrick Ventured8012182018-03-08 08:21:38 -080039
40 return fan;
41}
42
Patrick Venture5f59c0f2018-11-11 12:55:14 -080043double FanController::inputProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080044{
Patrick Ventured8012182018-03-08 08:21:38 -080045 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 Ventured8012182018-03-08 08:21:38 -080065 }
66 }
67 }
68 catch (const std::exception& e)
69 {
Patrick Venture563a3562018-10-30 09:31:26 -070070 std::cerr << "exception on inputProc.\n";
Patrick Ventured8012182018-03-08 08:21:38 -080071 throw;
72 }
73
Patrick Venture566a1512018-06-12 14:51:07 -070074 /* Reset the value from the above loop. */
75 value = 0;
Patrick Ventured8012182018-03-08 08:21:38 -080076 if (values.size() > 0)
77 {
Patrick Ventured8012182018-03-08 08:21:38 -080078 /* 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 Venturedf766f22018-10-13 09:30:58 -070080 * in the configuration, and it's a nice-to-have..
Patrick Ventured8012182018-03-08 08:21:38 -080081 */
82 result = std::min_element(values.begin(), values.end());
83 value = *result;
84 }
85
Patrick Venture5f59c0f2018-11-11 12:55:14 -080086 return value;
Patrick Ventured8012182018-03-08 08:21:38 -080087}
88
Patrick Venture5f59c0f2018-11-11 12:55:14 -080089double FanController::setptProc(void)
Patrick Ventured8012182018-03-08 08:21:38 -080090{
Patrick Venture5f59c0f2018-11-11 12:55:14 -080091 double maxRPM = _owner->getMaxRPMRequest();
Patrick Ventured8012182018-03-08 08:21:38 -080092
93 // store for reference, and check if more or less.
Patrick Venture5f59c0f2018-11-11 12:55:14 -080094 double prev = getSetpoint();
Patrick Ventured8012182018-03-08 08:21:38 -080095
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 Venture563a3562018-10-30 09:31:26 -0700109 setSetpoint(maxRPM);
Patrick Ventured8012182018-03-08 08:21:38 -0800110
111 return (maxRPM);
112}
113
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800114void FanController::outputProc(double value)
Patrick Ventured8012182018-03-08 08:21:38 -0800115{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800116 double percent = value;
Patrick Ventured8012182018-03-08 08:21:38 -0800117
Patrick Venturede79ee02019-05-08 14:50:00 -0700118 /* If doing tuning, don't go into failsafe mode. */
119 if (!tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800120 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800121 if (_owner->getFailSafeMode())
Patrick Ventured8012182018-03-08 08:21:38 -0800122 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800123 /* In case it's being set to 100% */
124 if (percent < _owner->getFailSafePercent())
125 {
126 percent = _owner->getFailSafePercent();
127 }
Patrick Ventured8012182018-03-08 08:21:38 -0800128 }
129 }
Patrick Ventured8012182018-03-08 08:21:38 -0800130
131 // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that.
132 percent /= 100;
133
134 // PidSensorMap for writing.
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700135 for (const auto& it : _inputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800136 {
Patrick Venturea58197c2018-06-11 15:29:45 -0700137 auto sensor = _owner->getSensor(it);
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800138 sensor->write(percent);
Patrick Ventured8012182018-03-08 08:21:38 -0800139 }
140
Patrick Ventured8012182018-03-08 08:21:38 -0800141 return;
142}