blob: 5b1e65538c04fb7058c697f0e6b55de64b0f7c83 [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
James Feistdca2d482019-02-15 16:14:31 -080015constexpr size_t scale = 100; // values are 10 for 10%
16
James Feist3dfaafd2018-09-20 15:46:58 -070017TEST(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 Venture5f59c0f2018-11-11 12:55:14 -080029 initial.reading[2] = std::numeric_limits<double>::quiet_NaN();
James Feist3dfaafd2018-09-20 15:46:58 -070030 initial.output[0] = 40.0;
31 initial.output[1] = 60.0;
32
33 std::unique_ptr<Controller> p =
Patrick Venture563a3562018-10-30 09:31:26 -070034 StepwiseController::createStepwiseController(&z, "foo", inputs,
James Feist3dfaafd2018-09-20 15:46:58 -070035 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 Feistdca2d482019-02-15 16:14:31 -080043 EXPECT_CALL(z, addRPMSetPoint(40.0 * scale)).Times(2);
44 EXPECT_CALL(z, addRPMSetPoint(60.0 * scale)).Times(1);
James Feist3dfaafd2018-09-20 15:46:58 -070045
46 for (int ii = 0; ii < 3; ii++)
47 {
48 p->process();
49 }
50}
51
52TEST(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 Venture5f59c0f2018-11-11 12:55:14 -080064 initial.reading[2] = std::numeric_limits<double>::quiet_NaN();
James Feist3dfaafd2018-09-20 15:46:58 -070065 initial.output[0] = 40.0;
66 initial.output[1] = 60.0;
67
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
James Feistdca2d482019-02-15 16:14:31 -080078 EXPECT_CALL(z, addRPMSetPoint(40.0 * scale)).Times(1);
79 EXPECT_CALL(z, addRPMSetPoint(60.0 * scale)).Times(2);
James Feist3dfaafd2018-09-20 15:46:58 -070080
81 for (int ii = 0; ii < 3; ii++)
82 {
83 p->process();
84 }
85}