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 | |
| 12 | using ::testing::Return; |
| 13 | using ::testing::StrEq; |
| 14 | |
James Feist | dca2d48 | 2019-02-15 16:14:31 -0800 | [diff] [blame] | 15 | constexpr size_t scale = 100; // values are 10 for 10% |
| 16 | |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 17 | TEST(StepwiseControllerTest, HysteresisTestPositive) |
| 18 | { |
| 19 | // Verifies positive hysteresis works as expected |
| 20 | |
| 21 | ZoneMock z; |
| 22 | |
| 23 | std::vector<std::string> inputs = {"test"}; |
| 24 | ec::StepwiseInfo initial; |
| 25 | initial.negativeHysteresis = 3.0; |
| 26 | initial.positiveHysteresis = 2.0; |
| 27 | initial.reading[0] = 20.0; |
| 28 | initial.reading[1] = 30.0; |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 29 | initial.reading[2] = std::numeric_limits<double>::quiet_NaN(); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 30 | initial.output[0] = 40.0; |
| 31 | initial.output[1] = 60.0; |
| 32 | |
| 33 | std::unique_ptr<Controller> p = |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 34 | StepwiseController::createStepwiseController(&z, "foo", inputs, |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 35 | initial); |
| 36 | |
| 37 | EXPECT_CALL(z, getCachedValue(StrEq("test"))) |
| 38 | .Times(3) |
| 39 | .WillOnce(Return(29.0)) // return 40 |
| 40 | .WillOnce(Return(31.0)) // return 40 |
| 41 | .WillOnce(Return(32.0)); // return 60 |
| 42 | |
James Feist | dca2d48 | 2019-02-15 16:14:31 -0800 | [diff] [blame] | 43 | EXPECT_CALL(z, addRPMSetPoint(40.0 * scale)).Times(2); |
| 44 | EXPECT_CALL(z, addRPMSetPoint(60.0 * scale)).Times(1); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 45 | |
| 46 | for (int ii = 0; ii < 3; ii++) |
| 47 | { |
| 48 | p->process(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | TEST(StepwiseControllerTest, HysteresisTestNegative) |
| 53 | { |
| 54 | // Verifies negative hysteresis works as expected |
| 55 | |
| 56 | ZoneMock z; |
| 57 | |
| 58 | std::vector<std::string> inputs = {"test"}; |
| 59 | ec::StepwiseInfo initial; |
| 60 | initial.negativeHysteresis = 3.0; |
| 61 | initial.positiveHysteresis = 2.0; |
| 62 | initial.reading[0] = 20.0; |
| 63 | initial.reading[1] = 30.0; |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 64 | initial.reading[2] = std::numeric_limits<double>::quiet_NaN(); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 65 | initial.output[0] = 40.0; |
| 66 | initial.output[1] = 60.0; |
| 67 | |
| 68 | std::unique_ptr<Controller> p = |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 69 | StepwiseController::createStepwiseController(&z, "foo", inputs, |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 70 | initial); |
| 71 | |
| 72 | EXPECT_CALL(z, getCachedValue(StrEq("test"))) |
| 73 | .Times(3) |
| 74 | .WillOnce(Return(30.0)) // return 60 |
| 75 | .WillOnce(Return(27.0)) // return 60 |
| 76 | .WillOnce(Return(26.0)); // return 40 |
| 77 | |
James Feist | dca2d48 | 2019-02-15 16:14:31 -0800 | [diff] [blame] | 78 | EXPECT_CALL(z, addRPMSetPoint(40.0 * scale)).Times(1); |
| 79 | EXPECT_CALL(z, addRPMSetPoint(60.0 * scale)).Times(2); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 80 | |
| 81 | for (int ii = 0; ii < 3; ii++) |
| 82 | { |
| 83 | p->process(); |
| 84 | } |
| 85 | } |