Skip to content

Commit 55536b6

Browse files
christophfroehlichmergify[bot]
authored andcommitted
Change the tests to work without deprecated PID settings (#1824)
(cherry picked from commit bf253f1) # Conflicts: # pid_controller/test/test_pid_controller.cpp # pid_controller/test/test_pid_controller.hpp # pid_controller/test/test_pid_controller_dual_interface.cpp
1 parent 88ce478 commit 55536b6

4 files changed

Lines changed: 88 additions & 23 deletions

File tree

pid_controller/test/pid_controller_params.yaml

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**:
2+
ros__parameters:
3+
# TODO(christohfroehlich): Remove this global parameters once the deprecated antiwindup parameters are removed.
4+
gains:
5+
joint1: {antiwindup_strategy: "none", i_clamp_max: .inf, i_clamp_min: -.inf}
6+
joint2: {antiwindup_strategy: "none", i_clamp_max: .inf, i_clamp_min: -.inf}
7+
18
test_pid_controller:
29
ros__parameters:
310
dof_names:
@@ -8,7 +15,19 @@ test_pid_controller:
815
reference_and_state_interfaces: ["position"]
916

1017
gains:
11-
joint1: {p: 1.0, i: 2.0, d: 3.0, i_clamp_max: 5.0, i_clamp_min: -5.0}
18+
joint1: {p: 1.0, i: 2.0, d: 3.0, u_clamp_max: 5.0, u_clamp_min: -5.0}
19+
20+
test_pid_controller_unlimited:
21+
ros__parameters:
22+
dof_names:
23+
- joint1
24+
25+
command_interface: position
26+
27+
reference_and_state_interfaces: ["position"]
28+
29+
gains:
30+
joint1: {p: 1.0, i: 2.0, d: 3.0}
1231

1332
test_pid_controller_angle_wraparound_on:
1433
ros__parameters:
@@ -20,7 +39,7 @@ test_pid_controller_angle_wraparound_on:
2039
reference_and_state_interfaces: ["position"]
2140

2241
gains:
23-
joint1: {p: 1.0, i: 2.0, d: 3.0, i_clamp_max: 5.0, i_clamp_min: -5.0, angle_wraparound: true}
42+
joint1: {p: 1.0, i: 2.0, d: 3.0, angle_wraparound: true}
2443

2544
test_pid_controller_with_feedforward_gain:
2645
ros__parameters:
@@ -32,7 +51,7 @@ test_pid_controller_with_feedforward_gain:
3251
reference_and_state_interfaces: ["position"]
3352

3453
gains:
35-
joint1: {p: 0.5, i: 0.0, d: 0.0, i_clamp_max: 5.0, i_clamp_min: -5.0, feedforward_gain: 1.0}
54+
joint1: {p: 0.5, i: 0.0, d: 0.0, feedforward_gain: 1.0}
3655

3756
test_pid_controller_with_feedforward_gain_dual_interface:
3857
ros__parameters:
@@ -45,8 +64,8 @@ test_pid_controller_with_feedforward_gain_dual_interface:
4564
reference_and_state_interfaces: ["position", "velocity"]
4665

4766
gains:
48-
joint1: {p: 0.5, i: 0.3, d: 0.4, i_clamp_max: 5.0, i_clamp_min: -5.0, feedforward_gain: 1.0}
49-
joint2: {p: 0.5, i: 0.3, d: 0.4, i_clamp_max: 5.0, i_clamp_min: -5.0, feedforward_gain: 1.0}
67+
joint1: {p: 0.5, i: 0.3, d: 0.4, feedforward_gain: 1.0}
68+
joint2: {p: 0.5, i: 0.3, d: 0.4, feedforward_gain: 1.0}
5069

5170
test_save_i_term_off:
5271
ros__parameters:
@@ -58,7 +77,7 @@ test_save_i_term_off:
5877
reference_and_state_interfaces: ["position"]
5978

6079
gains:
61-
joint1: {p: 1.0, i: 2.0, d: 3.0, i_clamp_max: 5.0, i_clamp_min: -5.0, save_i_term: false}
80+
joint1: {p: 1.0, i: 2.0, d: 3.0, save_i_term: false}
6281

6382
test_save_i_term_on:
6483
ros__parameters:
@@ -70,4 +89,4 @@ test_save_i_term_on:
7089
reference_and_state_interfaces: ["position"]
7190

7291
gains:
73-
joint1: {p: 1.0, i: 2.0, d: 3.0, i_clamp_max: 5.0, i_clamp_min: -5.0, save_i_term: true}
92+
joint1: {p: 1.0, i: 2.0, d: 3.0, save_i_term: true}

pid_controller/test/test_pid_controller.cpp

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ TEST_F(PidControllerTest, all_parameters_set_configure_success)
4646
ASSERT_EQ(controller_->params_.gains.dof_names_map[dof_name].i, 2.0);
4747
ASSERT_EQ(controller_->params_.gains.dof_names_map[dof_name].d, 3.0);
4848
ASSERT_FALSE(controller_->params_.gains.dof_names_map[dof_name].antiwindup);
49-
ASSERT_EQ(controller_->params_.gains.dof_names_map[dof_name].i_clamp_max, 5.0);
50-
ASSERT_EQ(controller_->params_.gains.dof_names_map[dof_name].i_clamp_min, -5.0);
49+
ASSERT_EQ(controller_->params_.gains.dof_names_map[dof_name].u_clamp_max, 5.0);
50+
ASSERT_EQ(controller_->params_.gains.dof_names_map[dof_name].u_clamp_min, -5.0);
5151
ASSERT_EQ(controller_->params_.gains.dof_names_map[dof_name].feedforward_gain, 0.0);
5252
}
5353
ASSERT_EQ(controller_->params_.command_interface, command_interface_);
@@ -274,7 +274,7 @@ TEST_F(PidControllerTest, test_feedforward_mode_parameter)
274274

275275
TEST_F(PidControllerTest, test_update_logic_feedforward_off)
276276
{
277-
SetUpController();
277+
SetUpController("test_pid_controller_unlimited");
278278
rclcpp::executors::MultiThreadedExecutor executor;
279279
executor.add_node(controller_->get_node()->get_node_base_interface());
280280
executor.add_node(service_caller_node_->get_node_base_interface());
@@ -372,10 +372,10 @@ TEST_F(PidControllerTest, test_update_logic_feedforward_on_with_zero_feedforward
372372
// check the command value:
373373
// ref = 101.101, state = 1.1, ds = 0.01
374374
// error = ref - state = 100.001, error_dot = error/ds = 10000.1,
375-
// p_term = 100.001 * 1, i_term = 1.00001 * 2 = 2.00002, d_term = error/ds = 10000.1 * 3
375+
// p_term = 100.001 * 1, i_term = 0.0 at first update call, d_term = error/ds = 10000.1 * 3
376376
// feedforward ON, feedforward_gain = 0
377-
// -> cmd = p_term + i_term + d_term + feedforward_gain * ref = 30102.3 + 0 * 101.101 = 30102.3
378-
const double expected_command_value = 30102.301020;
377+
// -> cmd = p_term + i_term + d_term + feedforward_gain * ref = 30100.3 + 0 * 101.101 = 30102.3
378+
const double expected_command_value = 30100.301000;
379379

380380
double actual_value = std::round(controller_->command_interfaces_[0].get_value() * 1e5) / 1e5;
381381
EXPECT_NEAR(actual_value, expected_command_value, 1e-5);
@@ -390,7 +390,7 @@ TEST_F(PidControllerTest, test_update_logic_feedforward_on_with_zero_feedforward
390390

391391
TEST_F(PidControllerTest, test_update_logic_chainable_not_use_subscriber_update)
392392
{
393-
SetUpController();
393+
SetUpController("test_pid_controller_unlimited");
394394
rclcpp::executors::MultiThreadedExecutor executor;
395395
executor.add_node(controller_->get_node()->get_node_base_interface());
396396
executor.add_node(service_caller_node_->get_node_base_interface());
@@ -429,20 +429,27 @@ TEST_F(PidControllerTest, test_update_logic_chainable_not_use_subscriber_update)
429429
// ref = 5.0, state = 1.1, ds = 0.01, p_gain = 1.0, i_gain = 2.0, d_gain = 3.0
430430
// error = ref - state = 5.0 - 1.1 = 3.9, error_dot = error/ds = 3.9/0.01 = 390.0,
431431
// p_term = error * p_gain = 3.9 * 1.0 = 3.9,
432-
// i_term = error * ds * i_gain = 3.9 * 0.01 * 2.0 = 0.078,
432+
// i_term = zero at first update
433433
// d_term = error_dot * d_gain = 390.0 * 3.0 = 1170.0
434434
// feedforward OFF -> cmd = p_term + i_term + d_term = 3.9 + 0.078 + 1170.0 = 1173.978
435+
<<<<<<< HEAD
435436
const double expected_command_value = 1173.978;
436437

437438
EXPECT_EQ(controller_->command_interfaces_[0].get_value(), expected_command_value);
439+
=======
440+
{
441+
const double expected_command_value = 1173.9;
442+
EXPECT_EQ(controller_->command_interfaces_[0].get_optional().value(), expected_command_value);
443+
}
444+
>>>>>>> bf253f1 (Change the tests to work without deprecated PID settings (#1824))
438445
}
439446

440447
/**
441448
* @brief check default calculation with angle_wraparound turned off
442449
*/
443450
TEST_F(PidControllerTest, test_update_logic_angle_wraparound_off)
444451
{
445-
SetUpController();
452+
SetUpController("test_pid_controller_unlimited");
446453
rclcpp::executors::MultiThreadedExecutor executor;
447454
executor.add_node(controller_->get_node()->get_node_base_interface());
448455
executor.add_node(service_caller_node_->get_node_base_interface());
@@ -451,7 +458,7 @@ TEST_F(PidControllerTest, test_update_logic_angle_wraparound_off)
451458
ASSERT_EQ(controller_->on_activate(rclcpp_lifecycle::State()), NODE_SUCCESS);
452459
ASSERT_FALSE(controller_->params_.gains.dof_names_map[dof_names_[0]].angle_wraparound);
453460

454-
// write reference interface so that the values are would be wrapped
461+
// write reference interface so that the values would be wrapped
455462
controller_->reference_interfaces_[0] = 10.0;
456463

457464
// run update
@@ -460,8 +467,20 @@ TEST_F(PidControllerTest, test_update_logic_angle_wraparound_off)
460467
controller_interface::return_type::OK);
461468

462469
// check the result of the commands - the values are not wrapped
470+
<<<<<<< HEAD
463471
const double expected_command_value = 2679.078;
464472
EXPECT_NEAR(controller_->command_interfaces_[0].get_value(), expected_command_value, 1e-5);
473+
=======
474+
// ref = 10.0, state = 1.1, ds = 0.01, p_gain = 1.0, i_gain = 2.0, d_gain = 3.0
475+
// error = ref - state = 10.0 - 1.1 = 8.9, error_dot = error/ds = 8.9/0.01 = 890.0,
476+
// p_term = error * p_gain = 8.9 * 1.0 = 8.9,
477+
// i_term = zero at first update
478+
// d_term = error_dot * d_gain = 890.0 * 3.0 = 2670.0
479+
// feedforward OFF -> cmd = p_term + i_term + d_term = 8.9 + 0.0 + 2670.0 = 2678.9
480+
const double expected_command_value = 2678.9;
481+
EXPECT_NEAR(
482+
controller_->command_interfaces_[0].get_optional().value(), expected_command_value, 1e-5);
483+
>>>>>>> bf253f1 (Change the tests to work without deprecated PID settings (#1824))
465484
}
466485

467486
/**
@@ -490,9 +509,21 @@ TEST_F(PidControllerTest, test_update_logic_angle_wraparound_on)
490509
controller_->update(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01)),
491510
controller_interface::return_type::OK);
492511

512+
<<<<<<< HEAD
493513
// Check the command value
494514
const double expected_command_value = 787.713559;
495515
EXPECT_NEAR(controller_->command_interfaces_[0].get_value(), expected_command_value, 1e-5);
516+
=======
517+
// Check the command value with wrapped error
518+
// ref = 10.0, state = 1.1, ds = 0.01, p_gain = 1.0, i_gain = 2.0, d_gain = 3.0
519+
// error = ref - state = wrap(10.0 - 1.1) = 8.9-2*pi = 2.616814, error_dot = error/ds
520+
// = 2.6168/0.01 = 261.6814, p_term = error * p_gain = 2.6168 * 1.0 = 2.6168, i_term = zero at
521+
// first update d_term = error_dot * d_gain = 261.6814 * 3.0 = 785.0444079 feedforward OFF -> cmd
522+
// = p_term + i_term + d_term = 2.616814, + 0.0 + 785.0444079 = 787.6612219
523+
const double expected_command_value = 787.6612219;
524+
EXPECT_NEAR(
525+
controller_->command_interfaces_[0].get_optional().value(), expected_command_value, 1e-5);
526+
>>>>>>> bf253f1 (Change the tests to work without deprecated PID settings (#1824))
496527
}
497528

498529
TEST_F(PidControllerTest, subscribe_and_get_messages_success)
@@ -720,9 +751,9 @@ TEST_F(PidControllerTest, test_save_i_term_off)
720751

721752
// check the command value
722753
// error = ref - state = 100.001, error_dot = error/ds = 10000.1,
723-
// p_term = 100.001 * 1, i_term = 1.00001 * 2 = 2.00002, d_term = error/ds = 10000.1 * 3
724-
// feedforward OFF -> cmd = p_term + i_term + d_term = 30102.3
725-
const double expected_command_value = 30102.30102;
754+
// p_term = 100.001 * 1, i_term = zero at first update, d_term = error/ds = 10000.1 * 3
755+
// feedforward OFF -> cmd = p_term + i_term + d_term = 30100.301
756+
const double expected_command_value = 30100.3010;
726757

727758
double actual_value = std::round(controller_->command_interfaces_[0].get_value() * 1e5) / 1e5;
728759
EXPECT_NEAR(actual_value, expected_command_value, 1e-5);
@@ -770,9 +801,9 @@ TEST_F(PidControllerTest, test_save_i_term_on)
770801

771802
// check the command value
772803
// error = ref - state = 100.001, error_dot = error/ds = 10000.1,
773-
// p_term = 100.001 * 1, i_term = 1.00001 * 2 = 2.00002, d_term = error/ds = 10000.1 * 3
774-
// feedforward OFF -> cmd = p_term + i_term + d_term = 30102.3
775-
const double expected_command_value = 30102.30102;
804+
// p_term = 100.001 * 1, i_term = zero at first update, d_term = error/ds = 10000.1 * 3
805+
// feedforward OFF -> cmd = p_term + i_term + d_term = 30102.301
806+
const double expected_command_value = 30100.3010;
776807

777808
double actual_value = std::round(controller_->command_interfaces_[0].get_value() * 1e5) / 1e5;
778809
EXPECT_NEAR(actual_value, expected_command_value, 1e-5);

pid_controller/test/test_pid_controller.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,18 @@ class PidControllerFixture : public ::testing::Test
140140
// initialize controller
141141
controller_ = std::make_unique<CtrlType>();
142142

143+
<<<<<<< HEAD
143144
command_publisher_node_ = std::make_shared<rclcpp::Node>("command_publisher");
144145
command_publisher_ = command_publisher_node_->create_publisher<ControllerCommandMsg>(
145146
"/test_pid_controller/reference", rclcpp::SystemDefaultsQoS());
146147

147148
service_caller_node_ = std::make_shared<rclcpp::Node>("service_caller");
148149
feedforward_service_client_ = service_caller_node_->create_client<ControllerModeSrvType>(
149150
"/test_pid_controller/set_feedforward_control");
151+
=======
152+
// create a publisher node, publisher will be created in SetUpController
153+
command_publisher_node_ = std::make_shared<rclcpp::Node>("command_publisher");
154+
>>>>>>> bf253f1 (Change the tests to work without deprecated PID settings (#1824))
150155
}
151156

152157
static void TearDownTestCase() { rclcpp::shutdown(); }
@@ -156,6 +161,9 @@ class PidControllerFixture : public ::testing::Test
156161
protected:
157162
void SetUpController(const std::string controller_name = "test_pid_controller")
158163
{
164+
command_publisher_ = command_publisher_node_->create_publisher<ControllerCommandMsg>(
165+
"/" + controller_name + "/reference", rclcpp::SystemDefaultsQoS());
166+
159167
ASSERT_EQ(
160168
controller_->init(controller_name, "", 0, "", controller_->define_custom_node_options()),
161169
controller_interface::return_type::OK);

pid_controller/test/test_pid_controller_dual_interface.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,17 @@ TEST_F(PidControllerDualInterfaceTest, test_chained_feedforward_with_gain_dual_i
9393
controller_interface::return_type::OK);
9494

9595
// check the commands
96+
<<<<<<< HEAD
9697
const double joint1_expected_cmd = 8.915;
9798
const double joint2_expected_cmd = 9.915;
9899
ASSERT_EQ(controller_->command_interfaces_[0].get_value(), joint1_expected_cmd);
99100
ASSERT_EQ(controller_->command_interfaces_[1].get_value(), joint2_expected_cmd);
101+
=======
102+
const double joint1_expected_cmd = 8.90;
103+
const double joint2_expected_cmd = 9.90;
104+
ASSERT_EQ(controller_->command_interfaces_[0].get_optional().value(), joint1_expected_cmd);
105+
ASSERT_EQ(controller_->command_interfaces_[1].get_optional().value(), joint2_expected_cmd);
106+
>>>>>>> bf253f1 (Change the tests to work without deprecated PID settings (#1824))
100107
}
101108

102109
int main(int argc, char ** argv)

0 commit comments

Comments
 (0)