blob: f5f6f9aa2c769e943ecf7f79d2507915d7fd40b7 [file] [log] [blame]
James Feist3dfaafd2018-09-20 15:46:58 -07001#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
12using ::testing::Return;
13using ::testing::StrEq;
14
15TEST(StepwiseControllerTest, HysteresisTestPositive)
16{
17 // Verifies positive hysteresis works as expected
18
19 ZoneMock z;
20
21 std::vector<std::string> inputs = {"test"};
22 ec::StepwiseInfo initial;
23 initial.negativeHysteresis = 3.0;
24 initial.positiveHysteresis = 2.0;
25 initial.reading[0] = 20.0;
26 initial.reading[1] = 30.0;
Patrick Venture5f59c0f2018-11-11 12:55:14 -080027 initial.reading[2] = std::numeric_limits<double>::quiet_NaN();
James Feist3dfaafd2018-09-20 15:46:58 -070028 initial.output[0] = 40.0;
29 initial.output[1] = 60.0;
James Feist608304d2019-02-25 10:01:42 -080030 initial.isCeiling = false;
James Feist3dfaafd2018-09-20 15:46:58 -070031
32 std::unique_ptr<Controller> p =
Patrick Venture563a3562018-10-30 09:31:26 -070033 StepwiseController::createStepwiseController(&z, "foo", inputs,
James Feist3dfaafd2018-09-20 15:46:58 -070034 initial);
35
36 EXPECT_CALL(z, getCachedValue(StrEq("test")))
37 .Times(3)
38 .WillOnce(Return(29.0)) // return 40
39 .WillOnce(Return(31.0)) // return 40
40 .WillOnce(Return(32.0)); // return 60
41
Patrick Venture9bbf3332019-07-16 10:50:37 -070042 EXPECT_CALL(z, addSetPoint(40.0)).Times(2);
43 EXPECT_CALL(z, addSetPoint(60.0)).Times(1);
James Feist3dfaafd2018-09-20 15:46:58 -070044
45 for (int ii = 0; ii < 3; ii++)
46 {
47 p->process();
48 }
49}
50
51TEST(StepwiseControllerTest, HysteresisTestNegative)
52{
53 // Verifies negative hysteresis works as expected
54
55 ZoneMock z;
56
57 std::vector<std::string> inputs = {"test"};
58 ec::StepwiseInfo initial;
59 initial.negativeHysteresis = 3.0;
60 initial.positiveHysteresis = 2.0;
61 initial.reading[0] = 20.0;
62 initial.reading[1] = 30.0;
Patrick Venture5f59c0f2018-11-11 12:55:14 -080063 initial.reading[2] = std::numeric_limits<double>::quiet_NaN();
James Feist3dfaafd2018-09-20 15:46:58 -070064 initial.output[0] = 40.0;
65 initial.output[1] = 60.0;
James Feist608304d2019-02-25 10:01:42 -080066 initial.isCeiling = false;
James Feist3dfaafd2018-09-20 15:46:58 -070067
68 std::unique_ptr<Controller> p =
Patrick Venture563a3562018-10-30 09:31:26 -070069 StepwiseController::createStepwiseController(&z, "foo", inputs,
James Feist3dfaafd2018-09-20 15:46:58 -070070 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
Patrick Venture9bbf3332019-07-16 10:50:37 -070078 EXPECT_CALL(z, addSetPoint(40.0)).Times(1);
79 EXPECT_CALL(z, addSetPoint(60.0)).Times(2);
James Feist3dfaafd2018-09-20 15:46:58 -070080
81 for (int ii = 0; ii < 3; ii++)
82 {
83 p->process();
84 }
85}