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