blob: e3c4f09ec4683beed9368c78b3fc2d9dc80578e5 [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;
30
31 std::unique_ptr<Controller> p =
Patrick Venture563a3562018-10-30 09:31:26 -070032 StepwiseController::createStepwiseController(&z, "foo", inputs,
James Feist3dfaafd2018-09-20 15:46:58 -070033 initial);
34
35 EXPECT_CALL(z, getCachedValue(StrEq("test")))
36 .Times(3)
37 .WillOnce(Return(29.0)) // return 40
38 .WillOnce(Return(31.0)) // return 40
39 .WillOnce(Return(32.0)); // return 60
40
41 EXPECT_CALL(z, addRPMSetPoint(40.0)).Times(2);
42 EXPECT_CALL(z, addRPMSetPoint(60.0)).Times(1);
43
44 for (int ii = 0; ii < 3; ii++)
45 {
46 p->process();
47 }
48}
49
50TEST(StepwiseControllerTest, HysteresisTestNegative)
51{
52 // Verifies negative hysteresis works as expected
53
54 ZoneMock z;
55
56 std::vector<std::string> inputs = {"test"};
57 ec::StepwiseInfo initial;
58 initial.negativeHysteresis = 3.0;
59 initial.positiveHysteresis = 2.0;
60 initial.reading[0] = 20.0;
61 initial.reading[1] = 30.0;
Patrick Venture5f59c0f2018-11-11 12:55:14 -080062 initial.reading[2] = std::numeric_limits<double>::quiet_NaN();
James Feist3dfaafd2018-09-20 15:46:58 -070063 initial.output[0] = 40.0;
64 initial.output[1] = 60.0;
65
66 std::unique_ptr<Controller> p =
Patrick Venture563a3562018-10-30 09:31:26 -070067 StepwiseController::createStepwiseController(&z, "foo", inputs,
James Feist3dfaafd2018-09-20 15:46:58 -070068 initial);
69
70 EXPECT_CALL(z, getCachedValue(StrEq("test")))
71 .Times(3)
72 .WillOnce(Return(30.0)) // return 60
73 .WillOnce(Return(27.0)) // return 60
74 .WillOnce(Return(26.0)); // return 40
75
76 EXPECT_CALL(z, addRPMSetPoint(40.0)).Times(1);
77 EXPECT_CALL(z, addRPMSetPoint(60.0)).Times(2);
78
79 for (int ii = 0; ii < 3; ii++)
80 {
81 p->process();
82 }
83}