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