4 #include <gtest/gtest.h> 8 #include <boost/math/special_functions/fpclassify.hpp> 12 TEST(ParameterTest, ITermBadIBoundsTest)
14 RecordProperty(
"description",
"This test checks that the integral contribution is robust to bad i_bounds specification (i.e. i_min > i_max).");
17 Pid pid(1.0, 1.0, 1.0, -1.0, 1.0);
23 EXPECT_FALSE(boost::math::isinf(ie));
24 EXPECT_FALSE(boost::math::isnan(cmd));
28 EXPECT_FALSE(boost::math::isinf(ie));
29 EXPECT_FALSE(boost::math::isnan(cmd));
32 TEST(ParameterTest, integrationClampTest)
34 RecordProperty(
"description",
"This test succeeds if the integral contribution is clamped when the integral gain is non-zero.");
36 Pid pid(0.0, 1.0, 0.0, 1.0, -1.0);
50 TEST(ParameterTest, integrationClampZeroGainTest)
52 RecordProperty(
"description",
"This test succeeds if the integral contribution is clamped when the integral gain is zero. If the integral contribution is not clamped while it is disabled, it can cause sudden jumps to the minimum or maximum bound in control command when re-enabled.");
57 Pid pid(0.0, i_gain, 0.0, i_max, i_min);
64 EXPECT_LE(i_min, cmd);
65 EXPECT_LE(cmd, i_max);
69 EXPECT_LE(i_min, cmd);
70 EXPECT_LE(cmd, i_max);
74 TEST(ParameterTest, integrationAntiwindupTest)
76 RecordProperty(
"description",
"This test succeeds if the integral error is prevented from winding up when i_gain > 0");
81 Pid pid(0.0, i_gain, 0.0, i_max, i_min,
true);
99 TEST(ParameterTest, negativeIntegrationAntiwindupTest)
101 RecordProperty(
"description",
"This test succeeds if the integral error is prevented from winding up when i_gain < 0");
103 double i_gain = -2.5;
106 Pid pid(0.0, i_gain, 0.0, i_max, i_min,
true);
112 EXPECT_EQ(-0.2, cmd);
115 EXPECT_EQ(-0.2, cmd);
118 EXPECT_EQ(-0.075, cmd);
121 EXPECT_EQ(-0.2, cmd);
124 TEST(ParameterTest, gainSettingCopyPIDTest)
126 RecordProperty(
"description",
"This test succeeds if a PID object has its gain set at different points in time then the values are get-ed and still remain the same, as well as when PID is copied.");
129 double p_gain = rand() % 100;
130 double i_gain = rand() % 100;
131 double d_gain = rand() % 100;
132 double i_max = rand() % 100;
133 double i_min = -1 * rand() % 100;
134 bool antiwindup =
false;
137 Pid pid1(p_gain, i_gain, d_gain, i_max, i_min, antiwindup);
140 double p_gain_return, i_gain_return, d_gain_return, i_max_return, i_min_return;
141 bool antiwindup_return;
142 pid1.
getGains(p_gain_return, i_gain_return, d_gain_return, i_max_return, i_min_return, antiwindup_return);
144 EXPECT_EQ(p_gain, p_gain_return);
145 EXPECT_EQ(i_gain, i_gain_return);
146 EXPECT_EQ(d_gain, d_gain_return);
147 EXPECT_EQ(i_max, i_max_return);
148 EXPECT_EQ(i_min, i_min_return);
149 EXPECT_EQ(antiwindup, antiwindup_return);
154 p_gain = rand() % 100;
155 i_gain = rand() % 100;
156 d_gain = rand() % 100;
157 i_max = rand() % 100;
158 i_min = -1 * rand() % 100;
159 pid1.
setGains(p_gain, i_gain, d_gain, i_max, i_min, antiwindup);
165 EXPECT_EQ(i_max, g1.
i_max_);
166 EXPECT_EQ(i_min, g1.
i_min_);
182 pid2.
getGains(p_gain_return, i_gain_return, d_gain_return, i_max_return, i_min_return, antiwindup_return);
184 EXPECT_EQ(p_gain, p_gain_return);
185 EXPECT_EQ(i_gain, i_gain_return);
186 EXPECT_EQ(d_gain, d_gain_return);
187 EXPECT_EQ(i_max, i_max_return);
188 EXPECT_EQ(i_min, i_min_return);
189 EXPECT_EQ(antiwindup, antiwindup_return);
192 double pe2, ie2, de2;
202 pid3.
getGains(p_gain_return, i_gain_return, d_gain_return, i_max_return, i_min_return, antiwindup_return);
204 EXPECT_EQ(p_gain, p_gain_return);
205 EXPECT_EQ(i_gain, i_gain_return);
206 EXPECT_EQ(d_gain, d_gain_return);
207 EXPECT_EQ(i_max, i_max_return);
208 EXPECT_EQ(i_min, i_min_return);
209 EXPECT_EQ(antiwindup, antiwindup_return);
212 double pe3, ie3, de3;
221 double pe1, ie1, de1;
228 EXPECT_EQ(0.0, cmd1);
231 TEST(CommandTest, proportionalOnlyTest)
233 RecordProperty(
"description",
"This test checks that a command is computed correctly using the proportional contribution only.");
236 Pid pid(1.0, 0.0, 0.0, 0.0, 0.0);
242 EXPECT_EQ(-0.5, cmd);
247 EXPECT_EQ(-0.5, cmd);
252 EXPECT_EQ(-1.0, cmd);
260 TEST(CommandTest, resetWithInitialValuesTest)
262 RecordProperty(
"description",
"This test checks that resetting PID with initial i_error & d_error values work.");
264 Pid pid(1.0, 1.0, 1.0, 5.0, -5.0);
277 EXPECT_EQ(-1.5, cmd);
286 TEST(CommandTest, integralOnlyTest)
288 RecordProperty(
"description",
"This test checks that a command is computed correctly using the integral contribution only (ATTENTION: this test depends on the integration scheme).");
291 Pid pid(0.0, 1.0, 0.0, 5.0, -5.0);
297 EXPECT_EQ(-0.5, cmd);
302 EXPECT_EQ(-1.0, cmd);
307 EXPECT_EQ(-1.0, cmd);
311 EXPECT_EQ(-1.0, cmd);
319 TEST(CommandTest, derivativeOnlyTest)
321 RecordProperty(
"description",
"This test checks that a command is computed correctly using the derivative contribution only with own differentiation (ATTENTION: this test depends on the differentiation scheme).");
324 Pid pid(0.0, 0.0, 1.0, 0.0, 0.0);
330 EXPECT_EQ(-0.5, cmd);
345 EXPECT_EQ(-0.5, cmd);
353 TEST(CommandTest, completePIDTest)
355 RecordProperty(
"description",
"This test checks that a command is computed correctly using a complete PID controller (ATTENTION: this test depends on the integral and differentiation schemes).");
357 Pid pid(1.0, 1.0, 1.0, 5.0, -5.0);
364 EXPECT_EQ(-1.5, cmd);
369 EXPECT_EQ(-1.5, cmd);
374 EXPECT_EQ(-3.5, cmd);
377 int main(
int argc,
char** argv) {
378 testing::InitGoogleTest(&argc, argv);
379 return RUN_ALL_TESTS();
int main(int argc, char **argv)
TEST(ParameterTest, ITermBadIBoundsTest)