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 <limits>
7 #include <memory>
8 #include <string>
9 #include <vector>
10
11 #include <gmock/gmock.h>
12 #include <gtest/gtest.h>
13
14 namespace pid_control
15 {
16 namespace
17 {
18
19 using ::testing::Return;
20 using ::testing::StrEq;
21
TEST(StepwiseControllerTest,HysteresisTestPositive)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;
34 initial.reading[2] = std::numeric_limits<double>::quiet_NaN();
35 initial.output[0] = 40.0;
36 initial.output[1] = 60.0;
37 initial.isCeiling = false;
38
39 std::unique_ptr<Controller> p =
40 StepwiseController::createStepwiseController(&z, "foo", inputs,
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
49 EXPECT_CALL(z, addSetPoint(40.0, "foo")).Times(2);
50 EXPECT_CALL(z, addSetPoint(60.0, "foo")).Times(1);
51
52 for (int ii = 0; ii < 3; ii++)
53 {
54 p->process();
55 }
56 }
57
TEST(StepwiseControllerTest,HysteresisTestNegative)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;
70 initial.reading[2] = std::numeric_limits<double>::quiet_NaN();
71 initial.output[0] = 40.0;
72 initial.output[1] = 60.0;
73 initial.isCeiling = false;
74
75 std::unique_ptr<Controller> p =
76 StepwiseController::createStepwiseController(&z, "foo", inputs,
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
85 EXPECT_CALL(z, addSetPoint(40.0, "foo")).Times(1);
86 EXPECT_CALL(z, addSetPoint(60.0, "foo")).Times(2);
87
88 for (int ii = 0; ii < 3; ii++)
89 {
90 p->process();
91 }
92 }
93
94 } // namespace
95 } // namespace pid_control
96