James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 1 | #include "pid/controller.hpp" |
| 2 | #include "pid/ec/stepwise.hpp" |
| 3 | #include "pid/stepwisecontroller.hpp" |
| 4 | #include "test/zone_mock.hpp" |
| 5 | |
| 6 | #include <string> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include <gmock/gmock.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 12 | namespace pid_control |
| 13 | { |
| 14 | namespace |
| 15 | { |
| 16 | |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 17 | using ::testing::Return; |
| 18 | using ::testing::StrEq; |
| 19 | |
| 20 | TEST(StepwiseControllerTest, HysteresisTestPositive) |
| 21 | { |
| 22 | // Verifies positive hysteresis works as expected |
| 23 | |
| 24 | ZoneMock z; |
| 25 | |
| 26 | std::vector<std::string> inputs = {"test"}; |
| 27 | ec::StepwiseInfo initial; |
| 28 | initial.negativeHysteresis = 3.0; |
| 29 | initial.positiveHysteresis = 2.0; |
| 30 | initial.reading[0] = 20.0; |
| 31 | initial.reading[1] = 30.0; |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 32 | initial.reading[2] = std::numeric_limits<double>::quiet_NaN(); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 33 | initial.output[0] = 40.0; |
| 34 | initial.output[1] = 60.0; |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 35 | initial.isCeiling = false; |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 36 | |
| 37 | std::unique_ptr<Controller> p = |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 38 | StepwiseController::createStepwiseController(&z, "foo", inputs, |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 39 | initial); |
| 40 | |
| 41 | EXPECT_CALL(z, getCachedValue(StrEq("test"))) |
| 42 | .Times(3) |
| 43 | .WillOnce(Return(29.0)) // return 40 |
| 44 | .WillOnce(Return(31.0)) // return 40 |
| 45 | .WillOnce(Return(32.0)); // return 60 |
| 46 | |
Patrick Venture | 9bbf333 | 2019-07-16 10:50:37 -0700 | [diff] [blame] | 47 | EXPECT_CALL(z, addSetPoint(40.0)).Times(2); |
| 48 | EXPECT_CALL(z, addSetPoint(60.0)).Times(1); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 49 | |
| 50 | for (int ii = 0; ii < 3; ii++) |
| 51 | { |
| 52 | p->process(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | TEST(StepwiseControllerTest, HysteresisTestNegative) |
| 57 | { |
| 58 | // Verifies negative hysteresis works as expected |
| 59 | |
| 60 | ZoneMock z; |
| 61 | |
| 62 | std::vector<std::string> inputs = {"test"}; |
| 63 | ec::StepwiseInfo initial; |
| 64 | initial.negativeHysteresis = 3.0; |
| 65 | initial.positiveHysteresis = 2.0; |
| 66 | initial.reading[0] = 20.0; |
| 67 | initial.reading[1] = 30.0; |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 68 | initial.reading[2] = std::numeric_limits<double>::quiet_NaN(); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 69 | initial.output[0] = 40.0; |
| 70 | initial.output[1] = 60.0; |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 71 | initial.isCeiling = false; |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 72 | |
| 73 | std::unique_ptr<Controller> p = |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 74 | StepwiseController::createStepwiseController(&z, "foo", inputs, |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 75 | initial); |
| 76 | |
| 77 | EXPECT_CALL(z, getCachedValue(StrEq("test"))) |
| 78 | .Times(3) |
| 79 | .WillOnce(Return(30.0)) // return 60 |
| 80 | .WillOnce(Return(27.0)) // return 60 |
| 81 | .WillOnce(Return(26.0)); // return 40 |
| 82 | |
Patrick Venture | 9bbf333 | 2019-07-16 10:50:37 -0700 | [diff] [blame] | 83 | EXPECT_CALL(z, addSetPoint(40.0)).Times(1); |
| 84 | EXPECT_CALL(z, addSetPoint(60.0)).Times(2); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 85 | |
| 86 | for (int ii = 0; ii < 3; ii++) |
| 87 | { |
| 88 | p->process(); |
| 89 | } |
| 90 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 91 | |
| 92 | } // namespace |
| 93 | } // namespace pid_control |