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 namespace pid_control
13 {
14 namespace
15 {
16 
17 using ::testing::Return;
18 using ::testing::StrEq;
19 
TEST(StepwiseControllerTest,HysteresisTestPositive)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;
32     initial.reading[2] = std::numeric_limits<double>::quiet_NaN();
33     initial.output[0] = 40.0;
34     initial.output[1] = 60.0;
35     initial.isCeiling = false;
36 
37     std::unique_ptr<Controller> p =
38         StepwiseController::createStepwiseController(&z, "foo", inputs,
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 
47     EXPECT_CALL(z, addSetPoint(40.0, "foo")).Times(2);
48     EXPECT_CALL(z, addSetPoint(60.0, "foo")).Times(1);
49 
50     for (int ii = 0; ii < 3; ii++)
51     {
52         p->process();
53     }
54 }
55 
TEST(StepwiseControllerTest,HysteresisTestNegative)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;
68     initial.reading[2] = std::numeric_limits<double>::quiet_NaN();
69     initial.output[0] = 40.0;
70     initial.output[1] = 60.0;
71     initial.isCeiling = false;
72 
73     std::unique_ptr<Controller> p =
74         StepwiseController::createStepwiseController(&z, "foo", inputs,
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 
83     EXPECT_CALL(z, addSetPoint(40.0, "foo")).Times(1);
84     EXPECT_CALL(z, addSetPoint(60.0, "foo")).Times(2);
85 
86     for (int ii = 0; ii < 3; ii++)
87     {
88         p->process();
89     }
90 }
91 
92 } // namespace
93 } // namespace pid_control
94