Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 1 | #include "pid/ec/logging.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 2 | #include "pid/ec/pid.hpp" |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 3 | #include "pid/thermalcontroller.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 4 | #include "test/zone_mock.hpp" |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 5 | |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 6 | #include <string> |
| 7 | #include <vector> |
| 8 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 9 | #include <gmock/gmock.h> |
| 10 | #include <gtest/gtest.h> |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 11 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 12 | namespace pid_control |
| 13 | { |
| 14 | namespace |
| 15 | { |
| 16 | |
James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 17 | using ::testing::_; |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 18 | using ::testing::Return; |
| 19 | using ::testing::StrEq; |
| 20 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 21 | TEST(ThermalControllerTest, BoringFactoryTest) |
| 22 | { |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 23 | // Verifies building a ThermalPIDController with the factory works as |
| 24 | // expected in the boring (uninteresting) case. |
| 25 | |
| 26 | ZoneMock z; |
| 27 | |
| 28 | std::vector<std::string> inputs = {"fleeting0"}; |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 29 | double setpoint = 10.0; |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 30 | ec::pidinfo initial; |
| 31 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 32 | std::unique_ptr<PIDController> p = ThermalController::createThermalPid( |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 33 | &z, "therm1", inputs, setpoint, initial, ThermalType::margin); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 34 | // Success |
| 35 | EXPECT_FALSE(p == nullptr); |
| 36 | } |
| 37 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 38 | TEST(ThermalControllerTest, VerifyFactoryFailsWithZeroInputs) |
| 39 | { |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 40 | // A thermal controller needs at least one input. |
| 41 | |
| 42 | ZoneMock z; |
| 43 | |
| 44 | std::vector<std::string> inputs = {}; |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 45 | double setpoint = 10.0; |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 46 | ec::pidinfo initial; |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 47 | std::unique_ptr<PIDController> p; |
| 48 | EXPECT_THROW( |
| 49 | { |
| 50 | p = ThermalController::createThermalPid( |
| 51 | &z, "therm1", inputs, setpoint, initial, ThermalType::margin); |
| 52 | }, |
| 53 | std::exception); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 54 | EXPECT_TRUE(p == nullptr); |
| 55 | } |
| 56 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 57 | TEST(ThermalControllerTest, InputProc_BehavesAsExpected) |
| 58 | { |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 59 | // This test just verifies inputProc behaves as expected. |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 60 | |
| 61 | ZoneMock z; |
| 62 | |
| 63 | std::vector<std::string> inputs = {"fleeting0"}; |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 64 | double setpoint = 10.0; |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 65 | ec::pidinfo initial; |
| 66 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 67 | std::unique_ptr<PIDController> p = ThermalController::createThermalPid( |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 68 | &z, "therm1", inputs, setpoint, initial, ThermalType::margin); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 69 | EXPECT_FALSE(p == nullptr); |
| 70 | |
| 71 | EXPECT_CALL(z, getCachedValue(StrEq("fleeting0"))).WillOnce(Return(5.0)); |
| 72 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 73 | EXPECT_EQ(5.0, p->inputProc()); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 76 | TEST(ThermalControllerTest, SetPtProc_BehavesAsExpected) |
| 77 | { |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 78 | // This test just verifies inputProc behaves as expected. |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 79 | |
| 80 | ZoneMock z; |
| 81 | |
| 82 | std::vector<std::string> inputs = {"fleeting0"}; |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 83 | double setpoint = 10.0; |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 84 | ec::pidinfo initial; |
| 85 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 86 | std::unique_ptr<PIDController> p = ThermalController::createThermalPid( |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 87 | &z, "therm1", inputs, setpoint, initial, ThermalType::margin); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 88 | EXPECT_FALSE(p == nullptr); |
| 89 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 90 | EXPECT_EQ(setpoint, p->setptProc()); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 93 | TEST(ThermalControllerTest, OutputProc_BehavesAsExpected) |
| 94 | { |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 95 | // This test just verifies outputProc behaves as expected. |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 96 | |
| 97 | ZoneMock z; |
| 98 | |
| 99 | std::vector<std::string> inputs = {"fleeting0"}; |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 100 | double setpoint = 10.0; |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 101 | ec::pidinfo initial; |
| 102 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 103 | std::unique_ptr<PIDController> p = ThermalController::createThermalPid( |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 104 | &z, "therm1", inputs, setpoint, initial, ThermalType::margin); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 105 | EXPECT_FALSE(p == nullptr); |
| 106 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 107 | double value = 90.0; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 108 | EXPECT_CALL(z, addSetPoint(value, "therm1")); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 109 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 110 | p->outputProc(value); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 111 | } |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 112 | |
| 113 | TEST(ThermalControllerTest, InputProc_MultipleInputsAbsolute) |
| 114 | { |
| 115 | // This test verifies inputProc behaves as expected with multiple absolute |
| 116 | // inputs. |
| 117 | |
| 118 | ZoneMock z; |
| 119 | |
| 120 | std::vector<std::string> inputs = {"fleeting0", "fleeting1"}; |
| 121 | double setpoint = 10.0; |
| 122 | ec::pidinfo initial; |
| 123 | |
| 124 | std::unique_ptr<PIDController> p = ThermalController::createThermalPid( |
| 125 | &z, "therm1", inputs, setpoint, initial, ThermalType::absolute); |
| 126 | EXPECT_FALSE(p == nullptr); |
| 127 | |
| 128 | EXPECT_CALL(z, getCachedValue(StrEq("fleeting0"))).WillOnce(Return(5.0)); |
| 129 | EXPECT_CALL(z, getCachedValue(StrEq("fleeting1"))).WillOnce(Return(10.0)); |
| 130 | |
| 131 | EXPECT_EQ(10.0, p->inputProc()); |
| 132 | } |
| 133 | |
| 134 | TEST(ThermalControllerTest, InputProc_MultipleInputsMargin) |
| 135 | { |
| 136 | // This test verifies inputProc behaves as expected with multiple margin |
| 137 | // inputs. |
| 138 | |
| 139 | ZoneMock z; |
| 140 | |
| 141 | std::vector<std::string> inputs = {"fleeting0", "fleeting1"}; |
| 142 | double setpoint = 10.0; |
| 143 | ec::pidinfo initial; |
| 144 | |
| 145 | std::unique_ptr<PIDController> p = ThermalController::createThermalPid( |
| 146 | &z, "therm1", inputs, setpoint, initial, ThermalType::margin); |
| 147 | EXPECT_FALSE(p == nullptr); |
| 148 | |
| 149 | EXPECT_CALL(z, getCachedValue(StrEq("fleeting0"))).WillOnce(Return(5.0)); |
| 150 | EXPECT_CALL(z, getCachedValue(StrEq("fleeting1"))).WillOnce(Return(10.0)); |
| 151 | |
| 152 | EXPECT_EQ(5.0, p->inputProc()); |
James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Josh Lehan | 23e22b9 | 2022-11-12 22:37:58 -0800 | [diff] [blame] | 155 | TEST(ThermalControllerTest, InputProc_MultipleInputsSummation) |
| 156 | { |
| 157 | // This test verifies inputProc behaves as expected with multiple summation |
| 158 | // inputs. |
| 159 | |
| 160 | ZoneMock z; |
| 161 | |
| 162 | std::vector<std::string> inputs = {"fleeting0", "fleeting1"}; |
| 163 | double setpoint = 10.0; |
| 164 | ec::pidinfo initial; |
| 165 | |
| 166 | std::unique_ptr<PIDController> p = ThermalController::createThermalPid( |
| 167 | &z, "therm1", inputs, setpoint, initial, ThermalType::summation); |
| 168 | EXPECT_FALSE(p == nullptr); |
| 169 | |
| 170 | EXPECT_CALL(z, getCachedValue(StrEq("fleeting0"))).WillOnce(Return(5.0)); |
| 171 | EXPECT_CALL(z, getCachedValue(StrEq("fleeting1"))).WillOnce(Return(10.0)); |
| 172 | |
| 173 | EXPECT_EQ(15.0, p->inputProc()); |
| 174 | } |
| 175 | |
James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 176 | TEST(ThermalControllerTest, NegHysteresis_BehavesAsExpected) |
| 177 | { |
James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 178 | // This test verifies Negative hysteresis behaves as expected by |
| 179 | // crossing the setpoint and noticing readings don't change until past the |
| 180 | // hysteresis value |
| 181 | |
| 182 | ZoneMock z; |
| 183 | |
| 184 | std::vector<std::string> inputs = {"fleeting0"}; |
| 185 | double setpoint = 10.0; |
| 186 | ec::pidinfo initial; |
| 187 | initial.negativeHysteresis = 4.0; |
| 188 | |
| 189 | std::unique_ptr<PIDController> p = ThermalController::createThermalPid( |
| 190 | &z, "therm1", inputs, setpoint, initial, ThermalType::margin); |
| 191 | EXPECT_FALSE(p == nullptr); |
| 192 | |
| 193 | EXPECT_CALL(z, getCachedValue(StrEq("fleeting0"))) |
| 194 | .Times(3) |
| 195 | .WillOnce(Return(12.0)) |
| 196 | .WillOnce(Return(9.0)) |
| 197 | .WillOnce(Return(7.0)); |
| 198 | |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 199 | EXPECT_CALL(z, addSetPoint(_, "therm1")).Times(3); |
James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 200 | |
| 201 | std::vector<double> lastReadings = {12.0, 12.0, 7.0}; |
| 202 | for (auto& reading : lastReadings) |
| 203 | { |
| 204 | p->process(); |
| 205 | EXPECT_EQ(p->getLastInput(), reading); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | TEST(ThermalControllerTest, PosHysteresis_BehavesAsExpected) |
| 210 | { |
| 211 | // This test verifies Positive hysteresis behaves as expected by |
| 212 | // crossing the setpoint and noticing readings don't change until past the |
| 213 | // hysteresis value |
| 214 | |
| 215 | ZoneMock z; |
| 216 | |
| 217 | std::vector<std::string> inputs = {"fleeting0"}; |
| 218 | double setpoint = 10.0; |
| 219 | ec::pidinfo initial; |
| 220 | initial.positiveHysteresis = 5.0; |
| 221 | |
| 222 | std::unique_ptr<PIDController> p = ThermalController::createThermalPid( |
| 223 | &z, "therm1", inputs, setpoint, initial, ThermalType::margin); |
| 224 | EXPECT_FALSE(p == nullptr); |
| 225 | |
| 226 | EXPECT_CALL(z, getCachedValue(StrEq("fleeting0"))) |
| 227 | .Times(3) |
| 228 | .WillOnce(Return(8.0)) |
| 229 | .WillOnce(Return(13.0)) |
| 230 | .WillOnce(Return(14.0)); |
| 231 | |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 232 | EXPECT_CALL(z, addSetPoint(_, "therm1")).Times(3); |
James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 233 | |
| 234 | std::vector<double> lastReadings = {8.0, 8.0, 14.0}; |
| 235 | for (auto& reading : lastReadings) |
| 236 | { |
| 237 | p->process(); |
| 238 | EXPECT_EQ(p->getLastInput(), reading); |
| 239 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | } // namespace |
| 243 | } // namespace pid_control |