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