blob: a832ed767b30dee31ec6dd2392c7f354e3feaac9 [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
Patrick Venturea0764872020-08-08 07:48:43 -070012namespace pid_control
13{
14namespace
15{
16
James Feist3dfaafd2018-09-20 15:46:58 -070017using ::testing::Return;
18using ::testing::StrEq;
19
20TEST(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;
Patrick Venture5f59c0f2018-11-11 12:55:14 -080032 initial.reading[2] = std::numeric_limits<double>::quiet_NaN();
James Feist3dfaafd2018-09-20 15:46:58 -070033 initial.output[0] = 40.0;
34 initial.output[1] = 60.0;
James Feist608304d2019-02-25 10:01:42 -080035 initial.isCeiling = false;
James Feist3dfaafd2018-09-20 15:46:58 -070036
37 std::unique_ptr<Controller> p =
Patrick Venture563a3562018-10-30 09:31:26 -070038 StepwiseController::createStepwiseController(&z, "foo", inputs,
James Feist3dfaafd2018-09-20 15:46:58 -070039 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
Nirav Shahccc8bb62022-02-17 21:06:51 -080047 EXPECT_CALL(z, addSetPoint(40.0, "foo")).Times(2);
48 EXPECT_CALL(z, addSetPoint(60.0, "foo")).Times(1);
James Feist3dfaafd2018-09-20 15:46:58 -070049
50 for (int ii = 0; ii < 3; ii++)
51 {
52 p->process();
53 }
54}
55
56TEST(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;
Patrick Venture5f59c0f2018-11-11 12:55:14 -080068 initial.reading[2] = std::numeric_limits<double>::quiet_NaN();
James Feist3dfaafd2018-09-20 15:46:58 -070069 initial.output[0] = 40.0;
70 initial.output[1] = 60.0;
James Feist608304d2019-02-25 10:01:42 -080071 initial.isCeiling = false;
James Feist3dfaafd2018-09-20 15:46:58 -070072
73 std::unique_ptr<Controller> p =
Patrick Venture563a3562018-10-30 09:31:26 -070074 StepwiseController::createStepwiseController(&z, "foo", inputs,
James Feist3dfaafd2018-09-20 15:46:58 -070075 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
Nirav Shahccc8bb62022-02-17 21:06:51 -080083 EXPECT_CALL(z, addSetPoint(40.0, "foo")).Times(1);
84 EXPECT_CALL(z, addSetPoint(60.0, "foo")).Times(2);
James Feist3dfaafd2018-09-20 15:46:58 -070085
86 for (int ii = 0; ii < 3; ii++)
87 {
88 p->process();
89 }
90}
Patrick Venturea0764872020-08-08 07:48:43 -070091
92} // namespace
93} // namespace pid_control