Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 1 | #include "pid/ec/logging.hpp" |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 2 | #include "pid/ec/pid.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 3 | #include "pid/fancontroller.hpp" |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 4 | #include "test/sensor_mock.hpp" |
| 5 | #include "test/zone_mock.hpp" |
| 6 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include <gmock/gmock.h> |
| 11 | #include <gtest/gtest.h> |
| 12 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 13 | namespace pid_control |
| 14 | { |
| 15 | namespace |
| 16 | { |
| 17 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 18 | using ::testing::_; |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 19 | using ::testing::DoubleEq; |
| 20 | using ::testing::Invoke; |
| 21 | using ::testing::Return; |
| 22 | using ::testing::StrEq; |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 23 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 24 | TEST(FanControllerTest, BoringFactoryTest) |
| 25 | { |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 26 | // Verify the factory will properly build the FanPIDController in the |
| 27 | // boring (uninteresting) case. |
| 28 | ZoneMock z; |
| 29 | |
| 30 | std::vector<std::string> inputs = {"fan0"}; |
| 31 | ec::pidinfo initial; |
| 32 | |
| 33 | std::unique_ptr<PIDController> p = |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 34 | FanController::createFanPid(&z, "fan1", inputs, initial); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 35 | // Success |
| 36 | EXPECT_FALSE(p == nullptr); |
| 37 | } |
| 38 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 39 | TEST(FanControllerTest, VerifyFactoryFailsWithZeroInputs) |
| 40 | { |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 41 | // A fan controller needs at least one input. |
| 42 | |
| 43 | ZoneMock z; |
| 44 | |
| 45 | std::vector<std::string> inputs = {}; |
| 46 | ec::pidinfo initial; |
| 47 | |
| 48 | std::unique_ptr<PIDController> p = |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 49 | FanController::createFanPid(&z, "fan1", inputs, initial); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 50 | EXPECT_TRUE(p == nullptr); |
| 51 | } |
| 52 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 53 | TEST(FanControllerTest, InputProc_AllSensorsReturnZero) |
| 54 | { |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 55 | // If all your inputs are 0, return 0. |
| 56 | |
| 57 | ZoneMock z; |
| 58 | |
| 59 | std::vector<std::string> inputs = {"fan0", "fan1"}; |
| 60 | ec::pidinfo initial; |
| 61 | |
| 62 | std::unique_ptr<PIDController> p = |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 63 | FanController::createFanPid(&z, "fan1", inputs, initial); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 64 | EXPECT_FALSE(p == nullptr); |
| 65 | |
| 66 | EXPECT_CALL(z, getCachedValue(StrEq("fan0"))).WillOnce(Return(0)); |
| 67 | EXPECT_CALL(z, getCachedValue(StrEq("fan1"))).WillOnce(Return(0)); |
| 68 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 69 | EXPECT_EQ(0.0, p->inputProc()); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 72 | TEST(FanControllerTest, InputProc_IfSensorNegativeIsIgnored) |
| 73 | { |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 74 | // A sensor value returning sub-zero is ignored as an error. |
| 75 | ZoneMock z; |
| 76 | |
| 77 | std::vector<std::string> inputs = {"fan0", "fan1"}; |
| 78 | ec::pidinfo initial; |
| 79 | |
| 80 | std::unique_ptr<PIDController> p = |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 81 | FanController::createFanPid(&z, "fan1", inputs, initial); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 82 | EXPECT_FALSE(p == nullptr); |
| 83 | |
| 84 | EXPECT_CALL(z, getCachedValue(StrEq("fan0"))).WillOnce(Return(-1)); |
| 85 | EXPECT_CALL(z, getCachedValue(StrEq("fan1"))).WillOnce(Return(-1)); |
| 86 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 87 | EXPECT_EQ(0.0, p->inputProc()); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 90 | TEST(FanControllerTest, InputProc_ChoosesMinimumValue) |
| 91 | { |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 92 | // Verify it selects the minimum value from its inputs. |
| 93 | |
| 94 | ZoneMock z; |
| 95 | |
| 96 | std::vector<std::string> inputs = {"fan0", "fan1", "fan2"}; |
| 97 | ec::pidinfo initial; |
| 98 | |
| 99 | std::unique_ptr<PIDController> p = |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 100 | FanController::createFanPid(&z, "fan1", inputs, initial); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 101 | EXPECT_FALSE(p == nullptr); |
| 102 | |
| 103 | EXPECT_CALL(z, getCachedValue(StrEq("fan0"))).WillOnce(Return(10.0)); |
| 104 | EXPECT_CALL(z, getCachedValue(StrEq("fan1"))).WillOnce(Return(30.0)); |
| 105 | EXPECT_CALL(z, getCachedValue(StrEq("fan2"))).WillOnce(Return(5.0)); |
| 106 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 107 | EXPECT_EQ(5.0, p->inputProc()); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // The direction is unused presently, but these tests validate the logic. |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 111 | TEST(FanControllerTest, SetPtProc_SpeedChanges_VerifyDirection) |
| 112 | { |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 113 | // The fan direction defaults to neutral, because we have no data. Verify |
| 114 | // that after this point it appropriately will indicate speeding up or |
| 115 | // slowing down based on the RPM values specified. |
| 116 | |
| 117 | ZoneMock z; |
| 118 | |
| 119 | std::vector<std::string> inputs = {"fan0", "fan1"}; |
| 120 | ec::pidinfo initial; |
| 121 | |
| 122 | std::unique_ptr<PIDController> p = |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 123 | FanController::createFanPid(&z, "fan1", inputs, initial); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 124 | EXPECT_FALSE(p == nullptr); |
| 125 | // Grab pointer for mocking. |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 126 | FanController* fp = reinterpret_cast<FanController*>(p.get()); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 127 | |
| 128 | // Fanspeed starts are Neutral. |
| 129 | EXPECT_EQ(FanSpeedDirection::NEUTRAL, fp->getFanDirection()); |
| 130 | |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 131 | // getMaxSetPointRequest returns a higher value than 0, so the fans should |
| 132 | // be marked as speeding up. |
| 133 | EXPECT_CALL(z, getMaxSetPointRequest()).WillOnce(Return(10.0)); |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 134 | EXPECT_EQ(10.0, p->setptProc()); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 135 | EXPECT_EQ(FanSpeedDirection::UP, fp->getFanDirection()); |
| 136 | |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 137 | // getMaxSetPointRequest returns a lower value than 10, so the fans should |
| 138 | // be marked as slowing down. |
| 139 | EXPECT_CALL(z, getMaxSetPointRequest()).WillOnce(Return(5.0)); |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 140 | EXPECT_EQ(5.0, p->setptProc()); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 141 | EXPECT_EQ(FanSpeedDirection::DOWN, fp->getFanDirection()); |
| 142 | |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 143 | // getMaxSetPointRequest returns the same value, so the fans should be |
| 144 | // marked as neutral. |
| 145 | EXPECT_CALL(z, getMaxSetPointRequest()).WillOnce(Return(5.0)); |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 146 | EXPECT_EQ(5.0, p->setptProc()); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 147 | EXPECT_EQ(FanSpeedDirection::NEUTRAL, fp->getFanDirection()); |
| 148 | } |
| 149 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 150 | TEST(FanControllerTest, OutputProc_VerifiesIfFailsafeEnabledInputIsIgnored) |
| 151 | { |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 152 | // Verify that if failsafe mode is enabled and the input value for the fans |
| 153 | // is below the failsafe minimum value, the input is not used and the fans |
Brandon Kim | bcdeb83 | 2022-08-15 23:27:36 +0000 | [diff] [blame] | 154 | // are driven at failsafe RPM (this assumes STRICT_FAILSAFE_PWM is not set) |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 155 | |
| 156 | ZoneMock z; |
| 157 | |
| 158 | std::vector<std::string> inputs = {"fan0", "fan1"}; |
| 159 | ec::pidinfo initial; |
| 160 | |
| 161 | std::unique_ptr<PIDController> p = |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 162 | FanController::createFanPid(&z, "fan1", inputs, initial); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 163 | EXPECT_FALSE(p == nullptr); |
| 164 | |
| 165 | EXPECT_CALL(z, getFailSafeMode()).WillOnce(Return(true)); |
Brandon Kim | bcdeb83 | 2022-08-15 23:27:36 +0000 | [diff] [blame] | 166 | EXPECT_CALL(z, getFailSafePercent()).WillOnce(Return(75.0)); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 167 | |
| 168 | int64_t timeout = 0; |
| 169 | std::unique_ptr<Sensor> s1 = std::make_unique<SensorMock>("fan0", timeout); |
| 170 | std::unique_ptr<Sensor> s2 = std::make_unique<SensorMock>("fan1", timeout); |
| 171 | // Grab pointers for mocking. |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 172 | SensorMock* sm1 = reinterpret_cast<SensorMock*>(s1.get()); |
| 173 | SensorMock* sm2 = reinterpret_cast<SensorMock*>(s2.get()); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 174 | |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 175 | EXPECT_CALL(z, getRedundantWrite()) |
| 176 | .WillOnce(Return(false)) |
| 177 | .WillOnce(Return(false)); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 178 | EXPECT_CALL(z, getSensor(StrEq("fan0"))).WillOnce(Return(s1.get())); |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 179 | EXPECT_CALL(*sm1, write(0.75, false, _)); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 180 | EXPECT_CALL(z, getSensor(StrEq("fan1"))).WillOnce(Return(s2.get())); |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 181 | EXPECT_CALL(*sm2, write(0.75, false, _)); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 182 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 183 | // This is a fan PID, so calling outputProc will try to write this value |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 184 | // to the sensors. |
| 185 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 186 | // Setting 50%, will end up being 75% because the sensors are in failsafe |
| 187 | // mode. |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 188 | p->outputProc(50.0); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 191 | TEST(FanControllerTest, OutputProc_BehavesAsExpected) |
| 192 | { |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 193 | // Verifies that when the system is not in failsafe mode, the input value |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 194 | // to outputProc is used to drive the sensors (fans). |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 195 | |
| 196 | ZoneMock z; |
| 197 | |
| 198 | std::vector<std::string> inputs = {"fan0", "fan1"}; |
| 199 | ec::pidinfo initial; |
| 200 | |
| 201 | std::unique_ptr<PIDController> p = |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 202 | FanController::createFanPid(&z, "fan1", inputs, initial); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 203 | EXPECT_FALSE(p == nullptr); |
| 204 | |
| 205 | EXPECT_CALL(z, getFailSafeMode()).WillOnce(Return(false)); |
| 206 | |
| 207 | int64_t timeout = 0; |
| 208 | std::unique_ptr<Sensor> s1 = std::make_unique<SensorMock>("fan0", timeout); |
| 209 | std::unique_ptr<Sensor> s2 = std::make_unique<SensorMock>("fan1", timeout); |
| 210 | // Grab pointers for mocking. |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 211 | SensorMock* sm1 = reinterpret_cast<SensorMock*>(s1.get()); |
| 212 | SensorMock* sm2 = reinterpret_cast<SensorMock*>(s2.get()); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 213 | |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 214 | EXPECT_CALL(z, getRedundantWrite()) |
| 215 | .WillOnce(Return(false)) |
| 216 | .WillOnce(Return(false)); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 217 | EXPECT_CALL(z, getSensor(StrEq("fan0"))).WillOnce(Return(s1.get())); |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 218 | EXPECT_CALL(*sm1, write(0.5, false, _)); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 219 | EXPECT_CALL(z, getSensor(StrEq("fan1"))).WillOnce(Return(s2.get())); |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 220 | EXPECT_CALL(*sm2, write(0.5, false, _)); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 221 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 222 | // This is a fan PID, so calling outputProc will try to write this value |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 223 | // to the sensors. |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 224 | p->outputProc(50.0); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Brandon Kim | bcdeb83 | 2022-08-15 23:27:36 +0000 | [diff] [blame] | 227 | TEST(FanControllerTest, OutputProc_VerifyFailSafeWhenInputHigher) |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 228 | { |
Brandon Kim | bcdeb83 | 2022-08-15 23:27:36 +0000 | [diff] [blame] | 229 | // If STRICT_FAILSAFE_PWM flag is NOT defined and the requested output is |
| 230 | // higher than the failsafe value, then use the value provided to outputProc |
| 231 | // |
| 232 | // If STRICT_FAILSAFE_PWM is defined, we expect the FailSafe PWM to be |
| 233 | // capped to the failsafe PWM, and not go higher than that. |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 234 | |
| 235 | ZoneMock z; |
| 236 | |
| 237 | std::vector<std::string> inputs = {"fan0"}; |
| 238 | ec::pidinfo initial; |
Brandon Kim | bcdeb83 | 2022-08-15 23:27:36 +0000 | [diff] [blame] | 239 | const double failsafePWM = 75.0; |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 240 | |
| 241 | std::unique_ptr<PIDController> p = |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 242 | FanController::createFanPid(&z, "fan1", inputs, initial); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 243 | EXPECT_FALSE(p == nullptr); |
| 244 | |
| 245 | EXPECT_CALL(z, getFailSafeMode()).WillOnce(Return(true)); |
Brandon Kim | bcdeb83 | 2022-08-15 23:27:36 +0000 | [diff] [blame] | 246 | EXPECT_CALL(z, getFailSafePercent()).WillOnce(Return(failsafePWM)); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 247 | |
| 248 | int64_t timeout = 0; |
| 249 | std::unique_ptr<Sensor> s1 = std::make_unique<SensorMock>("fan0", timeout); |
| 250 | // Grab pointer for mocking. |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 251 | SensorMock* sm1 = reinterpret_cast<SensorMock*>(s1.get()); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 252 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 253 | double percent = 80; |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 254 | |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 255 | EXPECT_CALL(z, getRedundantWrite()).WillOnce(Return(false)); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 256 | EXPECT_CALL(z, getSensor(StrEq("fan0"))).WillOnce(Return(s1.get())); |
Brandon Kim | bcdeb83 | 2022-08-15 23:27:36 +0000 | [diff] [blame] | 257 | #ifdef STRICT_FAILSAFE_PWM |
| 258 | double failsafeValue = failsafePWM / 100; |
| 259 | EXPECT_CALL(*sm1, write(failsafeValue, false, _)); |
| 260 | #else |
| 261 | // Converting from double to double for expectation. |
| 262 | double value = percent / 100; |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 263 | EXPECT_CALL(*sm1, write(value, false, _)); |
Brandon Kim | bcdeb83 | 2022-08-15 23:27:36 +0000 | [diff] [blame] | 264 | #endif |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 265 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 266 | // This is a fan PID, so calling outputProc will try to write this value |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 267 | // to the sensors. |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 268 | p->outputProc(percent); |
Patrick Venture | 566a151 | 2018-06-12 14:51:07 -0700 | [diff] [blame] | 269 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 270 | |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 271 | TEST(FanControllerTest, OutputProc_VerifyRedundantWrites) |
| 272 | { |
| 273 | /* when a zone indicates that redundant writes are enabled |
| 274 | * make sure the fan controller honors this by forcing a sensor write |
| 275 | */ |
| 276 | ZoneMock z; |
| 277 | |
| 278 | std::vector<std::string> inputs = {"fan0", "fan1"}; |
| 279 | ec::pidinfo initial; |
| 280 | |
| 281 | std::unique_ptr<PIDController> p = |
| 282 | FanController::createFanPid(&z, "fan1", inputs, initial); |
| 283 | EXPECT_FALSE(p == nullptr); |
| 284 | |
| 285 | EXPECT_CALL(z, getFailSafeMode()).WillOnce(Return(false)); |
| 286 | |
| 287 | int64_t timeout = 0; |
| 288 | std::unique_ptr<Sensor> s1 = std::make_unique<SensorMock>("fan0", timeout); |
| 289 | std::unique_ptr<Sensor> s2 = std::make_unique<SensorMock>("fan1", timeout); |
| 290 | // Grab pointers for mocking. |
| 291 | SensorMock* sm1 = reinterpret_cast<SensorMock*>(s1.get()); |
| 292 | SensorMock* sm2 = reinterpret_cast<SensorMock*>(s2.get()); |
| 293 | |
| 294 | EXPECT_CALL(z, getRedundantWrite()) |
| 295 | .WillOnce(Return(true)) |
| 296 | .WillOnce(Return(true)); |
| 297 | EXPECT_CALL(z, getSensor(StrEq("fan0"))).WillOnce(Return(s1.get())); |
| 298 | EXPECT_CALL(*sm1, write(0.5, true, _)); |
| 299 | EXPECT_CALL(z, getSensor(StrEq("fan1"))).WillOnce(Return(s2.get())); |
| 300 | EXPECT_CALL(*sm2, write(0.5, true, _)); |
| 301 | |
| 302 | // This is a fan PID, so calling outputProc will try to write this value |
| 303 | // to the sensors. |
| 304 | p->outputProc(50.0); |
| 305 | } |
| 306 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 307 | } // namespace |
| 308 | } // namespace pid_control |