blob: 866b74334de4458cb19f254dcb19b20153d8fead [file] [log] [blame]
Patrick Venture566a1512018-06-12 14:51:07 -07001#include "pid/ec/pid.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07002#include "pid/fancontroller.hpp"
Patrick Venture566a1512018-06-12 14:51:07 -07003#include "test/sensor_mock.hpp"
4#include "test/zone_mock.hpp"
5
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07006#include <string>
7#include <vector>
8
9#include <gmock/gmock.h>
10#include <gtest/gtest.h>
11
Patrick Venturea0764872020-08-08 07:48:43 -070012namespace pid_control
13{
14namespace
15{
16
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070017using ::testing::_;
Patrick Venture566a1512018-06-12 14:51:07 -070018using ::testing::DoubleEq;
19using ::testing::Invoke;
20using ::testing::Return;
21using ::testing::StrEq;
Patrick Venture566a1512018-06-12 14:51:07 -070022
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070023TEST(FanControllerTest, BoringFactoryTest)
24{
Patrick Venture566a1512018-06-12 14:51:07 -070025 // Verify the factory will properly build the FanPIDController in the
26 // boring (uninteresting) case.
27 ZoneMock z;
28
29 std::vector<std::string> inputs = {"fan0"};
30 ec::pidinfo initial;
31
32 std::unique_ptr<PIDController> p =
Patrick Venture563a3562018-10-30 09:31:26 -070033 FanController::createFanPid(&z, "fan1", inputs, initial);
Patrick Venture566a1512018-06-12 14:51:07 -070034 // Success
35 EXPECT_FALSE(p == nullptr);
36}
37
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070038TEST(FanControllerTest, VerifyFactoryFailsWithZeroInputs)
39{
Patrick Venture566a1512018-06-12 14:51:07 -070040 // A fan controller needs at least one input.
41
42 ZoneMock z;
43
44 std::vector<std::string> inputs = {};
45 ec::pidinfo initial;
46
47 std::unique_ptr<PIDController> p =
Patrick Venture563a3562018-10-30 09:31:26 -070048 FanController::createFanPid(&z, "fan1", inputs, initial);
Patrick Venture566a1512018-06-12 14:51:07 -070049 EXPECT_TRUE(p == nullptr);
50}
51
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070052TEST(FanControllerTest, InputProc_AllSensorsReturnZero)
53{
Patrick Venture566a1512018-06-12 14:51:07 -070054 // If all your inputs are 0, return 0.
55
56 ZoneMock z;
57
58 std::vector<std::string> inputs = {"fan0", "fan1"};
59 ec::pidinfo initial;
60
61 std::unique_ptr<PIDController> p =
Patrick Venture563a3562018-10-30 09:31:26 -070062 FanController::createFanPid(&z, "fan1", inputs, initial);
Patrick Venture566a1512018-06-12 14:51:07 -070063 EXPECT_FALSE(p == nullptr);
64
65 EXPECT_CALL(z, getCachedValue(StrEq("fan0"))).WillOnce(Return(0));
66 EXPECT_CALL(z, getCachedValue(StrEq("fan1"))).WillOnce(Return(0));
67
Patrick Venture563a3562018-10-30 09:31:26 -070068 EXPECT_EQ(0.0, p->inputProc());
Patrick Venture566a1512018-06-12 14:51:07 -070069}
70
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070071TEST(FanControllerTest, InputProc_IfSensorNegativeIsIgnored)
72{
Patrick Venture566a1512018-06-12 14:51:07 -070073 // A sensor value returning sub-zero is ignored as an error.
74 ZoneMock z;
75
76 std::vector<std::string> inputs = {"fan0", "fan1"};
77 ec::pidinfo initial;
78
79 std::unique_ptr<PIDController> p =
Patrick Venture563a3562018-10-30 09:31:26 -070080 FanController::createFanPid(&z, "fan1", inputs, initial);
Patrick Venture566a1512018-06-12 14:51:07 -070081 EXPECT_FALSE(p == nullptr);
82
83 EXPECT_CALL(z, getCachedValue(StrEq("fan0"))).WillOnce(Return(-1));
84 EXPECT_CALL(z, getCachedValue(StrEq("fan1"))).WillOnce(Return(-1));
85
Patrick Venture563a3562018-10-30 09:31:26 -070086 EXPECT_EQ(0.0, p->inputProc());
Patrick Venture566a1512018-06-12 14:51:07 -070087}
88
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070089TEST(FanControllerTest, InputProc_ChoosesMinimumValue)
90{
Patrick Venture566a1512018-06-12 14:51:07 -070091 // Verify it selects the minimum value from its inputs.
92
93 ZoneMock z;
94
95 std::vector<std::string> inputs = {"fan0", "fan1", "fan2"};
96 ec::pidinfo initial;
97
98 std::unique_ptr<PIDController> p =
Patrick Venture563a3562018-10-30 09:31:26 -070099 FanController::createFanPid(&z, "fan1", inputs, initial);
Patrick Venture566a1512018-06-12 14:51:07 -0700100 EXPECT_FALSE(p == nullptr);
101
102 EXPECT_CALL(z, getCachedValue(StrEq("fan0"))).WillOnce(Return(10.0));
103 EXPECT_CALL(z, getCachedValue(StrEq("fan1"))).WillOnce(Return(30.0));
104 EXPECT_CALL(z, getCachedValue(StrEq("fan2"))).WillOnce(Return(5.0));
105
Patrick Venture563a3562018-10-30 09:31:26 -0700106 EXPECT_EQ(5.0, p->inputProc());
Patrick Venture566a1512018-06-12 14:51:07 -0700107}
108
109// The direction is unused presently, but these tests validate the logic.
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700110TEST(FanControllerTest, SetPtProc_SpeedChanges_VerifyDirection)
111{
Patrick Venture566a1512018-06-12 14:51:07 -0700112 // The fan direction defaults to neutral, because we have no data. Verify
113 // that after this point it appropriately will indicate speeding up or
114 // slowing down based on the RPM values specified.
115
116 ZoneMock z;
117
118 std::vector<std::string> inputs = {"fan0", "fan1"};
119 ec::pidinfo initial;
120
121 std::unique_ptr<PIDController> p =
Patrick Venture563a3562018-10-30 09:31:26 -0700122 FanController::createFanPid(&z, "fan1", inputs, initial);
Patrick Venture566a1512018-06-12 14:51:07 -0700123 EXPECT_FALSE(p == nullptr);
124 // Grab pointer for mocking.
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700125 FanController* fp = reinterpret_cast<FanController*>(p.get());
Patrick Venture566a1512018-06-12 14:51:07 -0700126
127 // Fanspeed starts are Neutral.
128 EXPECT_EQ(FanSpeedDirection::NEUTRAL, fp->getFanDirection());
129
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700130 // getMaxSetPointRequest returns a higher value than 0, so the fans should
131 // be marked as speeding up.
132 EXPECT_CALL(z, getMaxSetPointRequest()).WillOnce(Return(10.0));
Patrick Venture563a3562018-10-30 09:31:26 -0700133 EXPECT_EQ(10.0, p->setptProc());
Patrick Venture566a1512018-06-12 14:51:07 -0700134 EXPECT_EQ(FanSpeedDirection::UP, fp->getFanDirection());
135
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700136 // getMaxSetPointRequest returns a lower value than 10, so the fans should
137 // be marked as slowing down.
138 EXPECT_CALL(z, getMaxSetPointRequest()).WillOnce(Return(5.0));
Patrick Venture563a3562018-10-30 09:31:26 -0700139 EXPECT_EQ(5.0, p->setptProc());
Patrick Venture566a1512018-06-12 14:51:07 -0700140 EXPECT_EQ(FanSpeedDirection::DOWN, fp->getFanDirection());
141
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700142 // getMaxSetPointRequest returns the same value, so the fans should be
143 // marked as neutral.
144 EXPECT_CALL(z, getMaxSetPointRequest()).WillOnce(Return(5.0));
Patrick Venture563a3562018-10-30 09:31:26 -0700145 EXPECT_EQ(5.0, p->setptProc());
Patrick Venture566a1512018-06-12 14:51:07 -0700146 EXPECT_EQ(FanSpeedDirection::NEUTRAL, fp->getFanDirection());
147}
148
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700149TEST(FanControllerTest, OutputProc_VerifiesIfFailsafeEnabledInputIsIgnored)
150{
Patrick Venture566a1512018-06-12 14:51:07 -0700151 // Verify that if failsafe mode is enabled and the input value for the fans
152 // is below the failsafe minimum value, the input is not used and the fans
153 // are driven at failsafe RPM.
154
155 ZoneMock z;
156
157 std::vector<std::string> inputs = {"fan0", "fan1"};
158 ec::pidinfo initial;
159
160 std::unique_ptr<PIDController> p =
Patrick Venture563a3562018-10-30 09:31:26 -0700161 FanController::createFanPid(&z, "fan1", inputs, initial);
Patrick Venture566a1512018-06-12 14:51:07 -0700162 EXPECT_FALSE(p == nullptr);
163
164 EXPECT_CALL(z, getFailSafeMode()).WillOnce(Return(true));
165 EXPECT_CALL(z, getFailSafePercent()).Times(2).WillRepeatedly(Return(75.0));
166
167 int64_t timeout = 0;
168 std::unique_ptr<Sensor> s1 = std::make_unique<SensorMock>("fan0", timeout);
169 std::unique_ptr<Sensor> s2 = std::make_unique<SensorMock>("fan1", timeout);
170 // Grab pointers for mocking.
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700171 SensorMock* sm1 = reinterpret_cast<SensorMock*>(s1.get());
172 SensorMock* sm2 = reinterpret_cast<SensorMock*>(s2.get());
Patrick Venture566a1512018-06-12 14:51:07 -0700173
174 EXPECT_CALL(z, getSensor(StrEq("fan0"))).WillOnce(Return(s1.get()));
175 EXPECT_CALL(*sm1, write(0.75));
176 EXPECT_CALL(z, getSensor(StrEq("fan1"))).WillOnce(Return(s2.get()));
177 EXPECT_CALL(*sm2, write(0.75));
178
Patrick Venture563a3562018-10-30 09:31:26 -0700179 // This is a fan PID, so calling outputProc will try to write this value
Patrick Venture566a1512018-06-12 14:51:07 -0700180 // to the sensors.
181
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700182 // Setting 50%, will end up being 75% because the sensors are in failsafe
183 // mode.
Patrick Venture563a3562018-10-30 09:31:26 -0700184 p->outputProc(50.0);
Patrick Venture566a1512018-06-12 14:51:07 -0700185}
186
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700187TEST(FanControllerTest, OutputProc_BehavesAsExpected)
188{
Patrick Venture566a1512018-06-12 14:51:07 -0700189 // Verifies that when the system is not in failsafe mode, the input value
Patrick Venture563a3562018-10-30 09:31:26 -0700190 // to outputProc is used to drive the sensors (fans).
Patrick Venture566a1512018-06-12 14:51:07 -0700191
192 ZoneMock z;
193
194 std::vector<std::string> inputs = {"fan0", "fan1"};
195 ec::pidinfo initial;
196
197 std::unique_ptr<PIDController> p =
Patrick Venture563a3562018-10-30 09:31:26 -0700198 FanController::createFanPid(&z, "fan1", inputs, initial);
Patrick Venture566a1512018-06-12 14:51:07 -0700199 EXPECT_FALSE(p == nullptr);
200
201 EXPECT_CALL(z, getFailSafeMode()).WillOnce(Return(false));
202
203 int64_t timeout = 0;
204 std::unique_ptr<Sensor> s1 = std::make_unique<SensorMock>("fan0", timeout);
205 std::unique_ptr<Sensor> s2 = std::make_unique<SensorMock>("fan1", timeout);
206 // Grab pointers for mocking.
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700207 SensorMock* sm1 = reinterpret_cast<SensorMock*>(s1.get());
208 SensorMock* sm2 = reinterpret_cast<SensorMock*>(s2.get());
Patrick Venture566a1512018-06-12 14:51:07 -0700209
210 EXPECT_CALL(z, getSensor(StrEq("fan0"))).WillOnce(Return(s1.get()));
211 EXPECT_CALL(*sm1, write(0.5));
212 EXPECT_CALL(z, getSensor(StrEq("fan1"))).WillOnce(Return(s2.get()));
213 EXPECT_CALL(*sm2, write(0.5));
214
Patrick Venture563a3562018-10-30 09:31:26 -0700215 // This is a fan PID, so calling outputProc will try to write this value
Patrick Venture566a1512018-06-12 14:51:07 -0700216 // to the sensors.
Patrick Venture563a3562018-10-30 09:31:26 -0700217 p->outputProc(50.0);
Patrick Venture566a1512018-06-12 14:51:07 -0700218}
219
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700220TEST(FanControllerTest, OutputProc_VerifyFailSafeIgnoredIfInputHigher)
221{
Patrick Venture566a1512018-06-12 14:51:07 -0700222 // If the requested output is higher than the failsafe value, then use the
Patrick Venture563a3562018-10-30 09:31:26 -0700223 // value provided to outputProc.
Patrick Venture566a1512018-06-12 14:51:07 -0700224
225 ZoneMock z;
226
227 std::vector<std::string> inputs = {"fan0"};
228 ec::pidinfo initial;
229
230 std::unique_ptr<PIDController> p =
Patrick Venture563a3562018-10-30 09:31:26 -0700231 FanController::createFanPid(&z, "fan1", inputs, initial);
Patrick Venture566a1512018-06-12 14:51:07 -0700232 EXPECT_FALSE(p == nullptr);
233
234 EXPECT_CALL(z, getFailSafeMode()).WillOnce(Return(true));
235 EXPECT_CALL(z, getFailSafePercent()).WillOnce(Return(75.0));
236
237 int64_t timeout = 0;
238 std::unique_ptr<Sensor> s1 = std::make_unique<SensorMock>("fan0", timeout);
239 // Grab pointer for mocking.
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700240 SensorMock* sm1 = reinterpret_cast<SensorMock*>(s1.get());
Patrick Venture566a1512018-06-12 14:51:07 -0700241
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800242 // Converting from double to double for expectation.
243 double percent = 80;
244 double value = percent / 100;
Patrick Venture566a1512018-06-12 14:51:07 -0700245
246 EXPECT_CALL(z, getSensor(StrEq("fan0"))).WillOnce(Return(s1.get()));
247 EXPECT_CALL(*sm1, write(value));
248
Patrick Venture563a3562018-10-30 09:31:26 -0700249 // This is a fan PID, so calling outputProc will try to write this value
Patrick Venture566a1512018-06-12 14:51:07 -0700250 // to the sensors.
Patrick Venture563a3562018-10-30 09:31:26 -0700251 p->outputProc(percent);
Patrick Venture566a1512018-06-12 14:51:07 -0700252}
Patrick Venturea0764872020-08-08 07:48:43 -0700253
254} // namespace
255} // namespace pid_control