blob: c730606e456b2119888f0345953dc466a0ebfd72 [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
Ed Tanousf8b6e552025-06-27 13:27:50 -07006#include <limits>
7#include <memory>
James Feist3dfaafd2018-09-20 15:46:58 -07008#include <string>
9#include <vector>
10
11#include <gmock/gmock.h>
12#include <gtest/gtest.h>
13
Patrick Venturea0764872020-08-08 07:48:43 -070014namespace pid_control
15{
16namespace
17{
18
James Feist3dfaafd2018-09-20 15:46:58 -070019using ::testing::Return;
20using ::testing::StrEq;
21
22TEST(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;
Patrick Venture5f59c0f2018-11-11 12:55:14 -080034 initial.reading[2] = std::numeric_limits<double>::quiet_NaN();
James Feist3dfaafd2018-09-20 15:46:58 -070035 initial.output[0] = 40.0;
36 initial.output[1] = 60.0;
James Feist608304d2019-02-25 10:01:42 -080037 initial.isCeiling = false;
James Feist3dfaafd2018-09-20 15:46:58 -070038
39 std::unique_ptr<Controller> p =
Patrick Venture563a3562018-10-30 09:31:26 -070040 StepwiseController::createStepwiseController(&z, "foo", inputs,
James Feist3dfaafd2018-09-20 15:46:58 -070041 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
Nirav Shahccc8bb62022-02-17 21:06:51 -080049 EXPECT_CALL(z, addSetPoint(40.0, "foo")).Times(2);
50 EXPECT_CALL(z, addSetPoint(60.0, "foo")).Times(1);
James Feist3dfaafd2018-09-20 15:46:58 -070051
52 for (int ii = 0; ii < 3; ii++)
53 {
54 p->process();
55 }
56}
57
58TEST(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;
Patrick Venture5f59c0f2018-11-11 12:55:14 -080070 initial.reading[2] = std::numeric_limits<double>::quiet_NaN();
James Feist3dfaafd2018-09-20 15:46:58 -070071 initial.output[0] = 40.0;
72 initial.output[1] = 60.0;
James Feist608304d2019-02-25 10:01:42 -080073 initial.isCeiling = false;
James Feist3dfaafd2018-09-20 15:46:58 -070074
75 std::unique_ptr<Controller> p =
Patrick Venture563a3562018-10-30 09:31:26 -070076 StepwiseController::createStepwiseController(&z, "foo", inputs,
James Feist3dfaafd2018-09-20 15:46:58 -070077 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
Nirav Shahccc8bb62022-02-17 21:06:51 -080085 EXPECT_CALL(z, addSetPoint(40.0, "foo")).Times(1);
86 EXPECT_CALL(z, addSetPoint(60.0, "foo")).Times(2);
James Feist3dfaafd2018-09-20 15:46:58 -070087
88 for (int ii = 0; ii < 3; ii++)
89 {
90 p->process();
91 }
92}
Patrick Venturea0764872020-08-08 07:48:43 -070093
94} // namespace
95} // namespace pid_control