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