Skip to content

Commit d669c37

Browse files
committed
Fix tests
1 parent 050c129 commit d669c37

4 files changed

Lines changed: 75 additions & 63 deletions

File tree

control_toolbox/include/control_toolbox/pid.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ struct AntiWindupStrategy
7676
};
7777

7878
AntiWindupStrategy()
79-
: type(UNDEFINED),
80-
i_min(std::numeric_limits<double>::quiet_NaN()),
81-
i_max(std::numeric_limits<double>::quiet_NaN()),
79+
: type(NONE),
80+
i_max(std::numeric_limits<double>::infinity()),
81+
i_min(-std::numeric_limits<double>::infinity()),
8282
tracking_time_constant(0.0),
8383
error_deadband(std::numeric_limits<double>::epsilon())
8484
{
@@ -159,8 +159,8 @@ struct AntiWindupStrategy
159159
}
160160

161161
Value type = UNDEFINED;
162-
double i_min = std::numeric_limits<double>::quiet_NaN(); /**< Minimum allowable integral term. */
163-
double i_max = std::numeric_limits<double>::quiet_NaN(); /**< Maximum allowable integral term. */
162+
double i_max = std::numeric_limits<double>::infinity(); /**< Maximum allowable integral term. */
163+
double i_min = -std::numeric_limits<double>::infinity(); /**< Minimum allowable integral term. */
164164

165165
// tracking_time_constant Specifies the tracking time constant for the 'back_calculation'
166166
// strategy. If set to 0.0 a recommended default value will be applied.

control_toolbox/test/pid_ros_parameters_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ TEST(PidParametersTest, GetParametersFromParams)
575575

576576
TestablePidROS pid(node, "", "", false);
577577

578-
ASSERT_FALSE(pid.initialize_from_ros_parameters());
578+
ASSERT_TRUE(pid.initialize_from_ros_parameters());
579579

580580
rclcpp::Parameter param_p;
581581
ASSERT_TRUE(node->get_parameter("p", param_p));
@@ -591,11 +591,11 @@ TEST(PidParametersTest, GetParametersFromParams)
591591

592592
rclcpp::Parameter param_i_clamp_max;
593593
ASSERT_TRUE(node->get_parameter("i_clamp_max", param_i_clamp_max));
594-
ASSERT_TRUE(std::isnan(param_i_clamp_max.get_value<double>()));
594+
ASSERT_TRUE(std::isinf(param_i_clamp_max.get_value<double>()));
595595

596596
rclcpp::Parameter param_i_clamp_min;
597597
ASSERT_TRUE(node->get_parameter("i_clamp_min", param_i_clamp_min));
598-
ASSERT_TRUE(std::isnan(param_i_clamp_min.get_value<double>()));
598+
ASSERT_TRUE(std::isinf(param_i_clamp_min.get_value<double>()));
599599

600600
rclcpp::Parameter param_u_clamp_max;
601601
ASSERT_TRUE(node->get_parameter("u_clamp_max", param_u_clamp_max));

control_toolbox/test/pid_ros_publisher_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ TEST(PidPublisherTest, PublishTest)
5959
"/pid_state", rclcpp::SensorDataQoS(), state_callback);
6060

6161
double command = pid_ros.compute_command(-0.5, rclcpp::Duration(1, 0));
62-
EXPECT_EQ(-1.5, command);
62+
EXPECT_EQ(-1.0, command);
6363

6464
// wait for callback
6565
for (size_t i = 0; i < ATTEMPTS && !callback_called; ++i)
@@ -97,7 +97,7 @@ TEST(PidPublisherTest, PublishTest_start_deactivated)
9797
"/pid_state", rclcpp::SensorDataQoS(), state_callback);
9898

9999
double command = pid_ros.compute_command(-0.5, rclcpp::Duration(1, 0));
100-
EXPECT_EQ(-1.5, command);
100+
EXPECT_EQ(-1.0, command);
101101

102102
// wait for callback
103103
for (size_t i = 0; i < ATTEMPTS && !callback_called; ++i)
@@ -166,7 +166,7 @@ TEST(PidPublisherTest, PublishTest_prefix)
166166
"/global/pid_state", rclcpp::SensorDataQoS(), state_callback);
167167

168168
double command = pid_ros.compute_command(-0.5, rclcpp::Duration(1, 0));
169-
EXPECT_EQ(-1.5, command);
169+
EXPECT_EQ(-1.0, command);
170170

171171
// wait for callback
172172
for (size_t i = 0; i < ATTEMPTS && !callback_called; ++i)
@@ -204,7 +204,7 @@ TEST(PidPublisherTest, PublishTest_local_prefix)
204204
"~/local/pid_state", rclcpp::SensorDataQoS(), state_callback);
205205

206206
double command = pid_ros.compute_command(-0.5, rclcpp::Duration(1, 0));
207-
EXPECT_EQ(-1.5, command);
207+
EXPECT_EQ(-1.0, command);
208208

209209
// wait for callback
210210
for (size_t i = 0; i < ATTEMPTS && !callback_called; ++i)
@@ -246,7 +246,7 @@ TEST(PidPublisherTest, PublishTestLifecycle)
246246
"/pid_state", rclcpp::SensorDataQoS(), state_callback);
247247

248248
double command = pid_ros.compute_command(-0.5, rclcpp::Duration(1, 0));
249-
EXPECT_EQ(-1.5, command);
249+
EXPECT_EQ(-1.0, command);
250250

251251
// wait for callback
252252
for (size_t i = 0; i < ATTEMPTS && !callback_called; ++i)

control_toolbox/test/pid_tests.cpp

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ TEST(ParameterTest, integrationBackCalculationZeroGainTest)
208208
// AntiWindupStrategy antiwindup_strat);
209209
AntiWindupStrategy antiwindup_strat;
210210
antiwindup_strat.type = AntiWindupStrategy::BACK_CALCULATION;
211+
antiwindup_strat.i_max = 1.0;
212+
antiwindup_strat.i_min = -1.0;
211213
antiwindup_strat.tracking_time_constant = 0.0; // Set to 0.0 to use the default value
212214
Pid pid(0.0, 0.0, 0.0, 20.0, -20.0, antiwindup_strat);
213215

@@ -258,6 +260,8 @@ TEST(ParameterTest, integrationConditionalIntegrationZeroGainTest)
258260
// AntiWindupStrategy antiwindup_strat);
259261
AntiWindupStrategy antiwindup_strat;
260262
antiwindup_strat.type = AntiWindupStrategy::CONDITIONAL_INTEGRATION;
263+
antiwindup_strat.i_max = 1.0;
264+
antiwindup_strat.i_min = -1.0;
261265
Pid pid(0.0, 0.0, 0.0, 20.0, -20.0, antiwindup_strat);
262266

263267
double cmd = 0.0;
@@ -296,22 +300,6 @@ TEST(ParameterTest, integrationConditionalIntegrationZeroGainTest)
296300
EXPECT_EQ(0.0, cmd);
297301
}
298302

299-
TEST(ParameterTest, ITermBadIBoundsTestConstructor)
300-
{
301-
RecordProperty(
302-
"description",
303-
"This test checks if an error is thrown for bad i_bounds specification (i.e. "
304-
"i_min > i_max).");
305-
306-
AntiWindupStrategy antiwindup_strat;
307-
antiwindup_strat.type = AntiWindupStrategy::NONE;
308-
antiwindup_strat.i_max = -1.0;
309-
antiwindup_strat.i_min = 1.0;
310-
311-
// Check that the output is not a non-sense if i-bounds are bad, i.e. i_min > i_max
312-
EXPECT_THROW(Pid pid(1.0, 1.0, 1.0, 1.0, -1.0, antiwindup_strat), std::invalid_argument);
313-
}
314-
315303
TEST(ParameterTest, ITermBadIBoundsTest)
316304
{
317305
RecordProperty(
@@ -349,7 +337,7 @@ TEST(ParameterTest, integrationAntiwindupTest)
349337
antiwindup_strat.type = AntiWindupStrategy::BACK_CALCULATION;
350338
antiwindup_strat.i_max = 1.0;
351339
antiwindup_strat.i_min = -1.0;
352-
340+
antiwindup_strat.tracking_time_constant = 1.0;
353341
const double u_max = std::numeric_limits<double>::infinity();
354342
const double u_min = -std::numeric_limits<double>::infinity();
355343

@@ -358,16 +346,16 @@ TEST(ParameterTest, integrationAntiwindupTest)
358346
double cmd = 0.0;
359347

360348
cmd = pid.compute_command(-1.0, 1.0);
361-
EXPECT_NEAR(-1.0, cmd, EPS);
349+
EXPECT_NEAR(0.0, cmd, EPS);
362350

363351
cmd = pid.compute_command(-1.0, 1.0);
364352
EXPECT_NEAR(-1.0, cmd, EPS);
365353

366354
cmd = pid.compute_command(0.5, 1.0);
367-
EXPECT_NEAR(0.0, cmd, EPS);
368-
369-
cmd = pid.compute_command(-1.0, 1.0);
370355
EXPECT_NEAR(-1.0, cmd, EPS);
356+
357+
cmd = pid.compute_command(0.0, 1.0);
358+
EXPECT_NEAR(0.0, cmd, EPS);
371359
}
372360

373361
TEST(ParameterTest, gainSettingCopyPIDTest)
@@ -521,8 +509,11 @@ TEST(CommandTest, proportionalOnlyTest)
521509
"This test checks that a command is computed correctly using the proportional contribution "
522510
"only.");
523511

512+
AntiWindupStrategy antiwindup_strat;
513+
antiwindup_strat.type = AntiWindupStrategy::NONE;
514+
524515
// Set only proportional gain
525-
Pid pid(1.0, 0.0, 0.0, 0.0, 0.0);
516+
Pid pid(1.0, 0.0, 0.0, 10.0, -10.0, antiwindup_strat);
526517
double cmd = 0.0;
527518

528519
// If initial error = 0, p-gain = 1, dt = 1
@@ -553,19 +544,22 @@ TEST(CommandTest, integralOnlyTest)
553544
"This test checks that a command is computed correctly using the integral contribution only "
554545
"(ATTENTION: this test depends on the integration scheme).");
555546

547+
AntiWindupStrategy antiwindup_strat;
548+
antiwindup_strat.type = AntiWindupStrategy::NONE;
549+
556550
// Set only integral gains with enough limits to test behavior
557-
Pid pid(0.0, 1.0, 0.0, 5.0, -5.0);
551+
Pid pid(0.0, 1.0, 0.0, 5.0, -5.0, antiwindup_strat);
558552
double cmd = 0.0;
559553

560554
// If initial error = 0, i-gain = 1, dt = 1
561555
cmd = pid.compute_command(-0.5, 1.0);
562-
// Then expect command = error
563-
EXPECT_EQ(-0.5, cmd);
556+
// Then expect command = 0.0
557+
EXPECT_EQ(0.0, cmd);
564558

565559
// If call again with same arguments
566560
cmd = pid.compute_command(-0.5, 1.0);
567561
// Then expect the integral part to double the command
568-
EXPECT_EQ(-1.0, cmd);
562+
EXPECT_EQ(-0.5, cmd);
569563

570564
// Call again with no error
571565
cmd = pid.compute_command(0.0, 1.0);
@@ -579,30 +573,30 @@ TEST(CommandTest, integralOnlyTest)
579573
// Finally call again with positive error to see if the command changes in the opposite direction
580574
cmd = pid.compute_command(1.0, 1.0);
581575
// Expect that the command is cleared since error = -1 * previous command, i-gain = 1, dt = 1
582-
EXPECT_EQ(0.0, cmd);
576+
EXPECT_EQ(-1.0, cmd);
583577

584578
// If initial error = 0, i-gain = 1, dt = 1
585-
cmd = pid.compute_command(-0.5, 1.0);
579+
cmd = pid.compute_command(0.0, 1.0);
586580
// Then expect command = error
587-
EXPECT_EQ(-0.5, cmd);
581+
EXPECT_EQ(0.0, cmd);
588582
// after reset without argument (save_i_term=false)
589583
// we expect the command to be 0 if update is called error = 0
590584
pid.reset();
591-
cmd = pid.compute_command(0.0, 1.0);
585+
cmd = pid.compute_command(0.5, 1.0);
592586
EXPECT_EQ(0.0, cmd);
593587

594588
// If initial error = 0, i-gain = 1, dt = 1
595-
cmd = pid.compute_command(-0.5, 1.0);
589+
cmd = pid.compute_command(0.0, 1.0);
596590
// Then expect command = error
597-
EXPECT_EQ(-0.5, cmd);
591+
EXPECT_EQ(0.5, cmd);
598592
// after reset with argument (save_i_term=false)
599593
// we expect the command to be 0 if update is called error = 0
600594
pid.reset(false);
601-
cmd = pid.compute_command(0.0, 1.0);
595+
cmd = pid.compute_command(-0.5, 1.0);
602596
EXPECT_EQ(0.0, cmd);
603597

604598
// If initial error = 0, i-gain = 1, dt = 1
605-
cmd = pid.compute_command(-0.5, 1.0);
599+
cmd = pid.compute_command(0.0, 1.0);
606600
// Then expect command = error
607601
EXPECT_EQ(-0.5, cmd);
608602
// after reset with save_i_term=true
@@ -619,8 +613,11 @@ TEST(CommandTest, derivativeOnlyTest)
619613
"This test checks that a command is computed correctly using the derivative contribution only "
620614
"with own differentiation (ATTENTION: this test depends on the differentiation scheme).");
621615

616+
AntiWindupStrategy antiwindup_strat;
617+
antiwindup_strat.type = AntiWindupStrategy::NONE;
618+
622619
// Set only derivative gain
623-
Pid pid(0.0, 0.0, 1.0, 0.0, 0.0);
620+
Pid pid(0.0, 0.0, 1.0, 10.0, -10.0, antiwindup_strat);
624621
double cmd = 0.0;
625622

626623
// If initial error = 0, d-gain = 1, dt = 1
@@ -656,24 +653,29 @@ TEST(CommandTest, completePIDTest)
656653
"This test checks that a command is computed correctly using a complete PID controller "
657654
"(ATTENTION: this test depends on the integral and differentiation schemes).");
658655

659-
Pid pid(1.0, 1.0, 1.0, 5.0, -5.0);
656+
AntiWindupStrategy antiwindup_strat;
657+
antiwindup_strat.type = AntiWindupStrategy::NONE;
658+
antiwindup_strat.i_max = 10.0;
659+
antiwindup_strat.i_min = -10.0;
660+
661+
Pid pid(1.0, 1.0, 1.0, 5.0, -5.0, antiwindup_strat);
660662
double cmd = 0.0;
661663

662664
// All contributions are tested, here few tests check that they sum up correctly
663665
// If initial error = 0, all gains = 1, dt = 1
664666
cmd = pid.compute_command(-0.5, 1.0);
665-
// Then expect command = 3x error
666-
EXPECT_EQ(-1.5, cmd);
667+
// Then expect command = -1.0
668+
EXPECT_EQ(-1.0, cmd);
667669

668670
// If call again with same arguments, no error change, but integration do its part
669671
cmd = pid.compute_command(-0.5, 1.0);
670-
// Then expect command = 3x error again
671-
EXPECT_EQ(-1.5, cmd);
672+
// Then expect command = -1.0
673+
EXPECT_EQ(-1.0, cmd);
672674

673675
// If call again increasing the error
674-
cmd = pid.compute_command(-1.0, 1.0);
676+
cmd = pid.compute_command(0.0, 1.0);
675677
// Then expect command equals to p = -1, i = -2.0 (i.e. - 0.5 - 0.5 - 1.0), d = -0.5
676-
EXPECT_EQ(-3.5, cmd);
678+
EXPECT_EQ(-0.5, cmd);
677679
}
678680

679681
TEST(CommandTest, backCalculationPIDTest)
@@ -688,6 +690,8 @@ TEST(CommandTest, backCalculationPIDTest)
688690
// Setting u_max = 5.0 and u_min = -5.0 to test clamping
689691
AntiWindupStrategy antiwindup_strat;
690692
antiwindup_strat.type = AntiWindupStrategy::BACK_CALCULATION;
693+
antiwindup_strat.i_max = 10.0;
694+
antiwindup_strat.i_min = -10.0;
691695
antiwindup_strat.tracking_time_constant = 1.0; // Set to 0.0 to use the default value
692696
Pid pid(0.0, 1.0, 0.0, 5.0, -5.0, antiwindup_strat);
693697

@@ -749,6 +753,8 @@ TEST(CommandTest, conditionalIntegrationPIDTest)
749753
// Setting u_max = 5.0 and u_min = -5.0 to test clamping
750754
AntiWindupStrategy antiwindup_strat;
751755
antiwindup_strat.type = AntiWindupStrategy::CONDITIONAL_INTEGRATION;
756+
antiwindup_strat.i_max = 10.0;
757+
antiwindup_strat.i_min = -10.0;
752758
antiwindup_strat.tracking_time_constant = 1.0;
753759
Pid pid(0.0, 1.0, 0.0, 5.0, -5.0, antiwindup_strat);
754760

@@ -802,10 +808,16 @@ TEST(CommandTest, timeArgumentTest)
802808
{
803809
RecordProperty("description", "Tests different dt argument type methods.");
804810

805-
Pid pid1(1.0, 1.0, 1.0, 5.0, -5.0);
806-
Pid pid2(1.0, 1.0, 1.0, 5.0, -5.0);
807-
Pid pid3(1.0, 1.0, 1.0, 5.0, -5.0);
808-
Pid pid4(1.0, 1.0, 1.0, 5.0, -5.0);
811+
AntiWindupStrategy antiwindup_strat;
812+
antiwindup_strat.type = AntiWindupStrategy::NONE;
813+
antiwindup_strat.i_max = 10.0;
814+
antiwindup_strat.i_min = -10.0;
815+
antiwindup_strat.tracking_time_constant = 1.0;
816+
817+
Pid pid1(1.0, 1.0, 1.0, 5.0, -5.0, antiwindup_strat);
818+
Pid pid2(1.0, 1.0, 1.0, 5.0, -5.0, antiwindup_strat);
819+
Pid pid3(1.0, 1.0, 1.0, 5.0, -5.0, antiwindup_strat);
820+
Pid pid4(1.0, 1.0, 1.0, 5.0, -5.0, antiwindup_strat);
809821

810822
// call without error_dt, dt is used to calculate error_dt
811823
auto cmd1 = pid1.compute_command(-0.5, 1.0);
@@ -816,7 +828,7 @@ TEST(CommandTest, timeArgumentTest)
816828

817829
// If initial error = 0, all gains = 1, dt = 1
818830
// Then expect command = 3x error
819-
EXPECT_EQ(-1.5, cmd1);
831+
EXPECT_EQ(-1.0, cmd1);
820832
EXPECT_EQ(cmd1, cmd2);
821833
EXPECT_EQ(cmd1, cmd3);
822834
EXPECT_EQ(cmd1, cmd4);
@@ -826,15 +838,15 @@ TEST(CommandTest, timeArgumentTest)
826838
cmd2 = pid2.compute_command(-0.5, 0.0, dt);
827839
cmd3 = pid3.compute_command(-0.5, 0.0, dt.nanoseconds());
828840
cmd4 = pid4.compute_command(-0.5, 0.0, std::chrono::nanoseconds(1s));
829-
EXPECT_EQ(-1.5, cmd1);
841+
EXPECT_EQ(-1.0, cmd1);
830842
EXPECT_EQ(cmd1, cmd2);
831843
EXPECT_EQ(cmd1, cmd3);
832844
EXPECT_EQ(cmd1, cmd4);
833845
cmd1 = pid1.compute_command(-0.5, 0.0, 1.0);
834846
cmd2 = pid2.compute_command(-0.5, 0.0, dt);
835847
cmd3 = pid3.compute_command(-0.5, 0.0, dt.nanoseconds());
836848
cmd4 = pid4.compute_command(-0.5, 0.0, std::chrono::nanoseconds(1s));
837-
EXPECT_EQ(-2.0, cmd1);
849+
EXPECT_EQ(-1.5, cmd1);
838850
EXPECT_EQ(cmd1, cmd2);
839851
EXPECT_EQ(cmd1, cmd3);
840852
EXPECT_EQ(cmd1, cmd4);
@@ -844,7 +856,7 @@ TEST(CommandTest, timeArgumentTest)
844856
pid1.get_current_pid_errors(pe, ie1, de);
845857
cmd1 = pid1.compute_command(-0.5, 0.0, 0.0);
846858
pid1.get_current_pid_errors(pe, ie2, de);
847-
EXPECT_EQ(-2.0, cmd1);
859+
EXPECT_EQ(-1.5, cmd1);
848860
EXPECT_EQ(ie1, ie2);
849861
// should throw if called with negative dt
850862
EXPECT_THROW(cmd1 = pid1.compute_command(-0.5, 0.0, -1.0), std::invalid_argument);

0 commit comments

Comments
 (0)