blob: d95f16ec4701f0bc7e2e7a6a02733f397eb9be65 [file] [log] [blame]
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07001#include "pid/ec/pid.hpp"
Patrick Venture3349ef22018-06-12 14:09:29 -07002#include "pid/thermalcontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07003#include "test/zone_mock.hpp"
Patrick Venture3349ef22018-06-12 14:09:29 -07004
Patrick Venture3349ef22018-06-12 14:09:29 -07005#include <string>
6#include <vector>
7
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07008#include <gmock/gmock.h>
9#include <gtest/gtest.h>
Patrick Venture3349ef22018-06-12 14:09:29 -070010
James Feist572c43d2019-01-31 15:52:22 -080011using ::testing::_;
Patrick Venture3349ef22018-06-12 14:09:29 -070012using ::testing::Return;
13using ::testing::StrEq;
14
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070015TEST(ThermalControllerTest, BoringFactoryTest)
16{
Patrick Venture3349ef22018-06-12 14:09:29 -070017 // Verifies building a ThermalPIDController with the factory works as
18 // expected in the boring (uninteresting) case.
19
20 ZoneMock z;
21
22 std::vector<std::string> inputs = {"fleeting0"};
Patrick Venture5f59c0f2018-11-11 12:55:14 -080023 double setpoint = 10.0;
Patrick Venture3349ef22018-06-12 14:09:29 -070024 ec::pidinfo initial;
25
Patrick Venture563a3562018-10-30 09:31:26 -070026 std::unique_ptr<PIDController> p = ThermalController::createThermalPid(
James Feist734f9532018-11-15 12:13:18 -080027 &z, "therm1", inputs, setpoint, initial, ThermalType::margin);
Patrick Venture3349ef22018-06-12 14:09:29 -070028 // Success
29 EXPECT_FALSE(p == nullptr);
30}
31
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070032TEST(ThermalControllerTest, VerifyFactoryFailsWithZeroInputs)
33{
Patrick Venture3349ef22018-06-12 14:09:29 -070034 // A thermal controller needs at least one input.
35
36 ZoneMock z;
37
38 std::vector<std::string> inputs = {};
Patrick Venture5f59c0f2018-11-11 12:55:14 -080039 double setpoint = 10.0;
Patrick Venture3349ef22018-06-12 14:09:29 -070040 ec::pidinfo initial;
James Feist734f9532018-11-15 12:13:18 -080041 std::unique_ptr<PIDController> p;
42 EXPECT_THROW(
43 {
44 p = ThermalController::createThermalPid(
45 &z, "therm1", inputs, setpoint, initial, ThermalType::margin);
46 },
47 std::exception);
Patrick Venture3349ef22018-06-12 14:09:29 -070048 EXPECT_TRUE(p == nullptr);
49}
50
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070051TEST(ThermalControllerTest, InputProc_BehavesAsExpected)
52{
Patrick Venture563a3562018-10-30 09:31:26 -070053 // This test just verifies inputProc behaves as expected.
Patrick Venture3349ef22018-06-12 14:09:29 -070054
55 ZoneMock z;
56
57 std::vector<std::string> inputs = {"fleeting0"};
Patrick Venture5f59c0f2018-11-11 12:55:14 -080058 double setpoint = 10.0;
Patrick Venture3349ef22018-06-12 14:09:29 -070059 ec::pidinfo initial;
60
Patrick Venture563a3562018-10-30 09:31:26 -070061 std::unique_ptr<PIDController> p = ThermalController::createThermalPid(
James Feist734f9532018-11-15 12:13:18 -080062 &z, "therm1", inputs, setpoint, initial, ThermalType::margin);
Patrick Venture3349ef22018-06-12 14:09:29 -070063 EXPECT_FALSE(p == nullptr);
64
65 EXPECT_CALL(z, getCachedValue(StrEq("fleeting0"))).WillOnce(Return(5.0));
66
Patrick Venture563a3562018-10-30 09:31:26 -070067 EXPECT_EQ(5.0, p->inputProc());
Patrick Venture3349ef22018-06-12 14:09:29 -070068}
69
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070070TEST(ThermalControllerTest, SetPtProc_BehavesAsExpected)
71{
Patrick Venture563a3562018-10-30 09:31:26 -070072 // This test just verifies inputProc behaves as expected.
Patrick Venture3349ef22018-06-12 14:09:29 -070073
74 ZoneMock z;
75
76 std::vector<std::string> inputs = {"fleeting0"};
Patrick Venture5f59c0f2018-11-11 12:55:14 -080077 double setpoint = 10.0;
Patrick Venture3349ef22018-06-12 14:09:29 -070078 ec::pidinfo initial;
79
Patrick Venture563a3562018-10-30 09:31:26 -070080 std::unique_ptr<PIDController> p = ThermalController::createThermalPid(
James Feist734f9532018-11-15 12:13:18 -080081 &z, "therm1", inputs, setpoint, initial, ThermalType::margin);
Patrick Venture3349ef22018-06-12 14:09:29 -070082 EXPECT_FALSE(p == nullptr);
83
Patrick Venture563a3562018-10-30 09:31:26 -070084 EXPECT_EQ(setpoint, p->setptProc());
Patrick Venture3349ef22018-06-12 14:09:29 -070085}
86
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070087TEST(ThermalControllerTest, OutputProc_BehavesAsExpected)
88{
James Feist734f9532018-11-15 12:13:18 -080089 // This test just verifies outputProc behaves as expected.
Patrick Venture3349ef22018-06-12 14:09:29 -070090
91 ZoneMock z;
92
93 std::vector<std::string> inputs = {"fleeting0"};
Patrick Venture5f59c0f2018-11-11 12:55:14 -080094 double setpoint = 10.0;
Patrick Venture3349ef22018-06-12 14:09:29 -070095 ec::pidinfo initial;
96
Patrick Venture563a3562018-10-30 09:31:26 -070097 std::unique_ptr<PIDController> p = ThermalController::createThermalPid(
James Feist734f9532018-11-15 12:13:18 -080098 &z, "therm1", inputs, setpoint, initial, ThermalType::margin);
Patrick Venture3349ef22018-06-12 14:09:29 -070099 EXPECT_FALSE(p == nullptr);
100
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800101 double value = 90.0;
Patrick Venture9bbf3332019-07-16 10:50:37 -0700102 EXPECT_CALL(z, addSetPoint(value));
Patrick Venture3349ef22018-06-12 14:09:29 -0700103
Patrick Venture563a3562018-10-30 09:31:26 -0700104 p->outputProc(value);
Patrick Venture3349ef22018-06-12 14:09:29 -0700105}
James Feist734f9532018-11-15 12:13:18 -0800106
107TEST(ThermalControllerTest, InputProc_MultipleInputsAbsolute)
108{
109 // This test verifies inputProc behaves as expected with multiple absolute
110 // inputs.
111
112 ZoneMock z;
113
114 std::vector<std::string> inputs = {"fleeting0", "fleeting1"};
115 double setpoint = 10.0;
116 ec::pidinfo initial;
117
118 std::unique_ptr<PIDController> p = ThermalController::createThermalPid(
119 &z, "therm1", inputs, setpoint, initial, ThermalType::absolute);
120 EXPECT_FALSE(p == nullptr);
121
122 EXPECT_CALL(z, getCachedValue(StrEq("fleeting0"))).WillOnce(Return(5.0));
123 EXPECT_CALL(z, getCachedValue(StrEq("fleeting1"))).WillOnce(Return(10.0));
124
125 EXPECT_EQ(10.0, p->inputProc());
126}
127
128TEST(ThermalControllerTest, InputProc_MultipleInputsMargin)
129{
130 // This test verifies inputProc behaves as expected with multiple margin
131 // inputs.
132
133 ZoneMock z;
134
135 std::vector<std::string> inputs = {"fleeting0", "fleeting1"};
136 double setpoint = 10.0;
137 ec::pidinfo initial;
138
139 std::unique_ptr<PIDController> p = ThermalController::createThermalPid(
140 &z, "therm1", inputs, setpoint, initial, ThermalType::margin);
141 EXPECT_FALSE(p == nullptr);
142
143 EXPECT_CALL(z, getCachedValue(StrEq("fleeting0"))).WillOnce(Return(5.0));
144 EXPECT_CALL(z, getCachedValue(StrEq("fleeting1"))).WillOnce(Return(10.0));
145
146 EXPECT_EQ(5.0, p->inputProc());
James Feist572c43d2019-01-31 15:52:22 -0800147}
148
149TEST(ThermalControllerTest, NegHysteresis_BehavesAsExpected)
150{
151
152 // This test verifies Negative hysteresis behaves as expected by
153 // crossing the setpoint and noticing readings don't change until past the
154 // hysteresis value
155
156 ZoneMock z;
157
158 std::vector<std::string> inputs = {"fleeting0"};
159 double setpoint = 10.0;
160 ec::pidinfo initial;
161 initial.negativeHysteresis = 4.0;
162
163 std::unique_ptr<PIDController> p = ThermalController::createThermalPid(
164 &z, "therm1", inputs, setpoint, initial, ThermalType::margin);
165 EXPECT_FALSE(p == nullptr);
166
167 EXPECT_CALL(z, getCachedValue(StrEq("fleeting0")))
168 .Times(3)
169 .WillOnce(Return(12.0))
170 .WillOnce(Return(9.0))
171 .WillOnce(Return(7.0));
172
Patrick Venture9bbf3332019-07-16 10:50:37 -0700173 EXPECT_CALL(z, addSetPoint(_)).Times(3);
James Feist572c43d2019-01-31 15:52:22 -0800174
175 std::vector<double> lastReadings = {12.0, 12.0, 7.0};
176 for (auto& reading : lastReadings)
177 {
178 p->process();
179 EXPECT_EQ(p->getLastInput(), reading);
180 }
181}
182
183TEST(ThermalControllerTest, PosHysteresis_BehavesAsExpected)
184{
185 // This test verifies Positive hysteresis behaves as expected by
186 // crossing the setpoint and noticing readings don't change until past the
187 // hysteresis value
188
189 ZoneMock z;
190
191 std::vector<std::string> inputs = {"fleeting0"};
192 double setpoint = 10.0;
193 ec::pidinfo initial;
194 initial.positiveHysteresis = 5.0;
195
196 std::unique_ptr<PIDController> p = ThermalController::createThermalPid(
197 &z, "therm1", inputs, setpoint, initial, ThermalType::margin);
198 EXPECT_FALSE(p == nullptr);
199
200 EXPECT_CALL(z, getCachedValue(StrEq("fleeting0")))
201 .Times(3)
202 .WillOnce(Return(8.0))
203 .WillOnce(Return(13.0))
204 .WillOnce(Return(14.0));
205
Patrick Venture9bbf3332019-07-16 10:50:37 -0700206 EXPECT_CALL(z, addSetPoint(_)).Times(3);
James Feist572c43d2019-01-31 15:52:22 -0800207
208 std::vector<double> lastReadings = {8.0, 8.0, 14.0};
209 for (auto& reading : lastReadings)
210 {
211 p->process();
212 EXPECT_EQ(p->getLastInput(), reading);
213 }
James Feist734f9532018-11-15 12:13:18 -0800214}