Skip to content

Commit 93bd4bd

Browse files
authored
Use TF prefix helper for diff drive controller (#1997)
1 parent 489ec36 commit 93bd4bd

File tree

5 files changed

+52
-80
lines changed

5 files changed

+52
-80
lines changed

diff_drive_controller/src/diff_drive_controller.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <utility>
2323
#include <vector>
2424

25+
#include "controller_interface/tf_prefix.hpp"
2526
#include "diff_drive_controller/diff_drive_controller.hpp"
2627
#include "hardware_interface/types/hardware_interface_type_values.hpp"
2728
#include "lifecycle_msgs/msg/state.hpp"
@@ -410,37 +411,52 @@ controller_interface::CallbackReturn DiffDriveController::on_configure(
410411
}
411412
});
412413

414+
// deprecation warning if tf_frame_prefix_enable set to non-default value
415+
const bool default_tf_frame_prefix_enable = true;
416+
if (params_.tf_frame_prefix_enable != default_tf_frame_prefix_enable)
417+
{
418+
RCLCPP_WARN(
419+
get_node()->get_logger(),
420+
"Parameter 'tf_frame_prefix_enable' is DEPRECATED and set to a non-default value (%s). "
421+
"Please migrate to 'tf_frame_prefix'.",
422+
params_.tf_frame_prefix_enable ? "true" : "false");
423+
}
424+
413425
// initialize odometry publisher and message
414426
odometry_publisher_ = get_node()->create_publisher<nav_msgs::msg::Odometry>(
415427
DEFAULT_ODOMETRY_TOPIC, rclcpp::SystemDefaultsQoS());
416428
realtime_odometry_publisher_ =
417429
std::make_shared<realtime_tools::RealtimePublisher<nav_msgs::msg::Odometry>>(
418430
odometry_publisher_);
419431

420-
// Append the tf prefix if there is one
432+
// resolve prefix: substitute tilde (~) with the namespace if contains and normalize slashes (/)
421433
std::string tf_prefix = "";
422434
if (params_.tf_frame_prefix_enable)
423435
{
424436
if (params_.tf_frame_prefix != "")
425437
{
426-
tf_prefix = params_.tf_frame_prefix;
438+
tf_prefix = controller_interface::resolve_tf_prefix(
439+
params_.tf_frame_prefix, get_node()->get_namespace());
427440
}
428441
else
429442
{
430-
tf_prefix = std::string(get_node()->get_namespace());
431-
}
443+
RCLCPP_WARN(
444+
get_node()->get_logger(),
445+
"Please use tilde ('~') character in 'tf_frame_prefix' as it replaced with node namespace");
432446

433-
// Make sure prefix does not start with '/' and always ends with '/'
434-
if (tf_prefix.back() != '/')
435-
{
436-
tf_prefix = tf_prefix + "/";
437-
}
438-
if (tf_prefix.front() == '/')
439-
{
440-
tf_prefix.erase(0, 1);
447+
tf_prefix = std::string(get_node()->get_namespace());
448+
if (tf_prefix.back() != '/')
449+
{
450+
tf_prefix = tf_prefix + "/";
451+
}
452+
if (tf_prefix.front() == '/')
453+
{
454+
tf_prefix.erase(0, 1);
455+
}
441456
}
442457
}
443458

459+
// prepend resolved TF prefix to frame ids
444460
const auto odom_frame_id = tf_prefix + params_.odom_frame_id;
445461
const auto base_frame_id = tf_prefix + params_.base_frame_id;
446462

diff_drive_controller/src/diff_drive_controller_parameter.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ diff_drive_controller:
4949
tf_frame_prefix_enable: {
5050
type: bool,
5151
default_value: true,
52-
description: "Enables or disables appending tf_prefix to tf frame id's.",
52+
description: "Deprecated: this parameter will be removed in a future release. Use 'tf_frame_prefix' instead.",
5353
}
5454
tf_frame_prefix: {
5555
type: string,

diff_drive_controller/test/test_diff_drive_controller.cpp

Lines changed: 13 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,10 @@ class TestableDiffDriveController : public diff_drive_controller::DiffDriveContr
7474
return realtime_odometry_publisher_;
7575
}
7676
// Declare these tests as friends so we can access odometry_message_
77-
FRIEND_TEST(TestDiffDriveController, configure_succeeds_tf_test_prefix_false_no_namespace);
78-
FRIEND_TEST(TestDiffDriveController, configure_succeeds_tf_test_prefix_true_no_namespace);
79-
FRIEND_TEST(TestDiffDriveController, configure_succeeds_tf_blank_prefix_true_no_namespace);
80-
FRIEND_TEST(TestDiffDriveController, configure_succeeds_tf_test_prefix_false_set_namespace);
81-
FRIEND_TEST(TestDiffDriveController, configure_succeeds_tf_test_prefix_true_set_namespace);
82-
FRIEND_TEST(TestDiffDriveController, configure_succeeds_tf_blank_prefix_true_set_namespace);
77+
FRIEND_TEST(TestDiffDriveController, configure_succeeds_tf_prefix_no_namespace);
78+
FRIEND_TEST(TestDiffDriveController, configure_succeeds_tf_blank_prefix_no_namespace);
79+
FRIEND_TEST(TestDiffDriveController, configure_succeeds_tf_prefix_set_namespace);
80+
FRIEND_TEST(TestDiffDriveController, configure_succeeds_tf_tilde_prefix_set_namespace);
8381
// Declare these tests as friends so we can access controller_->reference_interfaces_
8482
FRIEND_TEST(TestDiffDriveController, chainable_controller_unchained_mode);
8583
FRIEND_TEST(TestDiffDriveController, chainable_controller_chained_mode);
@@ -310,29 +308,7 @@ TEST_F(
310308
EXPECT_EQ(cmd_if_conf.type, controller_interface::interface_configuration_type::INDIVIDUAL);
311309
}
312310

313-
TEST_F(TestDiffDriveController, configure_succeeds_tf_test_prefix_false_no_namespace)
314-
{
315-
std::string odom_id = "odom";
316-
std::string base_link_id = "base_link";
317-
std::string frame_prefix = "test_prefix";
318-
319-
ASSERT_EQ(
320-
InitController(
321-
left_wheel_names, right_wheel_names,
322-
{rclcpp::Parameter("tf_frame_prefix_enable", rclcpp::ParameterValue(false)),
323-
rclcpp::Parameter("tf_frame_prefix", rclcpp::ParameterValue(frame_prefix)),
324-
rclcpp::Parameter("odom_frame_id", rclcpp::ParameterValue(odom_id)),
325-
rclcpp::Parameter("base_frame_id", rclcpp::ParameterValue(base_link_id))}),
326-
controller_interface::return_type::OK);
327-
328-
ASSERT_EQ(controller_->on_configure(rclcpp_lifecycle::State()), CallbackReturn::SUCCESS);
329-
330-
/* tf_frame_prefix_enable is false so no modifications to the frame id's */
331-
ASSERT_EQ(controller_->odometry_message_.header.frame_id, odom_id);
332-
ASSERT_EQ(controller_->odometry_message_.child_frame_id, base_link_id);
333-
}
334-
335-
TEST_F(TestDiffDriveController, configure_succeeds_tf_test_prefix_true_no_namespace)
311+
TEST_F(TestDiffDriveController, configure_succeeds_tf_prefix_no_namespace)
336312
{
337313
std::string odom_id = "odom";
338314
std::string base_link_id = "base_link";
@@ -349,13 +325,12 @@ TEST_F(TestDiffDriveController, configure_succeeds_tf_test_prefix_true_no_namesp
349325

350326
ASSERT_EQ(controller_->on_configure(rclcpp_lifecycle::State()), CallbackReturn::SUCCESS);
351327

352-
/* tf_frame_prefix_enable is true and frame_prefix is not blank so should be appended to the frame
353-
* id's */
328+
// frame_prefix is not blank so should be prepended to the frame id's
354329
ASSERT_EQ(controller_->odometry_message_.header.frame_id, frame_prefix + "/" + odom_id);
355330
ASSERT_EQ(controller_->odometry_message_.child_frame_id, frame_prefix + "/" + base_link_id);
356331
}
357332

358-
TEST_F(TestDiffDriveController, configure_succeeds_tf_blank_prefix_true_no_namespace)
333+
TEST_F(TestDiffDriveController, configure_succeeds_tf_blank_prefix_no_namespace)
359334
{
360335
std::string odom_id = "odom";
361336
std::string base_link_id = "base_link";
@@ -372,38 +347,12 @@ TEST_F(TestDiffDriveController, configure_succeeds_tf_blank_prefix_true_no_names
372347

373348
ASSERT_EQ(controller_->on_configure(rclcpp_lifecycle::State()), CallbackReturn::SUCCESS);
374349

375-
/* tf_frame_prefix_enable is true but frame_prefix is blank so should not be appended to the frame
376-
* id's */
377-
ASSERT_EQ(controller_->odometry_message_.header.frame_id, odom_id);
378-
ASSERT_EQ(controller_->odometry_message_.child_frame_id, base_link_id);
379-
}
380-
381-
TEST_F(TestDiffDriveController, configure_succeeds_tf_test_prefix_false_set_namespace)
382-
{
383-
std::string test_namespace = "/test_namespace";
384-
385-
std::string odom_id = "odom";
386-
std::string base_link_id = "base_link";
387-
std::string frame_prefix = "test_prefix";
388-
389-
ASSERT_EQ(
390-
InitController(
391-
left_wheel_names, right_wheel_names,
392-
{rclcpp::Parameter("tf_frame_prefix_enable", rclcpp::ParameterValue(false)),
393-
rclcpp::Parameter("tf_frame_prefix", rclcpp::ParameterValue(frame_prefix)),
394-
rclcpp::Parameter("odom_frame_id", rclcpp::ParameterValue(odom_id)),
395-
rclcpp::Parameter("base_frame_id", rclcpp::ParameterValue(base_link_id))},
396-
test_namespace),
397-
controller_interface::return_type::OK);
398-
399-
ASSERT_EQ(controller_->on_configure(rclcpp_lifecycle::State()), CallbackReturn::SUCCESS);
400-
401-
/* tf_frame_prefix_enable is false so no modifications to the frame id's */
350+
// frame_prefix is blank so nothing added to the frame id's
402351
ASSERT_EQ(controller_->odometry_message_.header.frame_id, odom_id);
403352
ASSERT_EQ(controller_->odometry_message_.child_frame_id, base_link_id);
404353
}
405354

406-
TEST_F(TestDiffDriveController, configure_succeeds_tf_test_prefix_true_set_namespace)
355+
TEST_F(TestDiffDriveController, configure_succeeds_tf_prefix_set_namespace)
407356
{
408357
std::string test_namespace = "/test_namespace";
409358

@@ -423,18 +372,17 @@ TEST_F(TestDiffDriveController, configure_succeeds_tf_test_prefix_true_set_names
423372

424373
ASSERT_EQ(controller_->on_configure(rclcpp_lifecycle::State()), CallbackReturn::SUCCESS);
425374

426-
/* tf_frame_prefix_enable is true and frame_prefix is not blank so should be appended to the frame
427-
* id's instead of the namespace*/
375+
// frame_prefix is not blank so should be prepended to the frame id's instead of the namespace
428376
ASSERT_EQ(controller_->odometry_message_.header.frame_id, frame_prefix + "/" + odom_id);
429377
ASSERT_EQ(controller_->odometry_message_.child_frame_id, frame_prefix + "/" + base_link_id);
430378
}
431379

432-
TEST_F(TestDiffDriveController, configure_succeeds_tf_blank_prefix_true_set_namespace)
380+
TEST_F(TestDiffDriveController, configure_succeeds_tf_tilde_prefix_set_namespace)
433381
{
434382
std::string test_namespace = "/test_namespace";
435383
std::string odom_id = "odom";
436384
std::string base_link_id = "base_link";
437-
std::string frame_prefix = "";
385+
std::string frame_prefix = "~";
438386

439387
ASSERT_EQ(
440388
InitController(
@@ -448,9 +396,8 @@ TEST_F(TestDiffDriveController, configure_succeeds_tf_blank_prefix_true_set_name
448396

449397
ASSERT_EQ(controller_->on_configure(rclcpp_lifecycle::State()), CallbackReturn::SUCCESS);
450398

399+
// frame_prefix has tilde (~) character so node namespace should be prepended to the frame id's
451400
std::string ns_prefix = test_namespace.erase(0, 1) + "/";
452-
/* tf_frame_prefix_enable is true but frame_prefix is blank so namespace should be appended to the
453-
* frame id's */
454401
ASSERT_EQ(controller_->odometry_message_.header.frame_id, ns_prefix + odom_id);
455402
ASSERT_EQ(controller_->odometry_message_.child_frame_id, ns_prefix + base_link_id);
456403
}

doc/migration.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Migration Guides: Kilted Kaiju to Lyrical Luth
44
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
55
This list summarizes important changes between Kilted Kaiju (previous) and Lyrical Luth (current) releases, where changes to user code might be necessary.
66

7-
87
effort_controllers
98
*****************************
109
* ``effort_controllers/JointGroupEffortController`` is deprecated. Use :ref:`forward_command_controller <forward_command_controller_userdoc>` instead by adding the ``interface_name`` parameter and set it to ``effort``. (`#1913 <https://github.com/ros-controls/ros2_controllers/pull/1913>`_).
@@ -16,3 +15,8 @@ position_controllers
1615
velocity_controllers
1716
*****************************
1817
* ``velocity_controllers/JointGroupVelocityController`` is deprecated. Use :ref:`forward_command_controller <forward_command_controller_userdoc>` instead by adding the ``interface_name`` parameter and set it to ``velocity``. (`#1913 <https://github.com/ros-controls/ros2_controllers/pull/1913>`_).
18+
19+
diff_drive_controller
20+
*****************************
21+
* Instead of using ``tf_frame_prefix_enable:=false``, set an empty ``tf_frame_prefix:=""`` parameter instead. (`#1997 <https://github.com/ros-controls/ros2_controllers/pull/1997>`_).
22+
* For using node namespace as tf prefix: Set ``tf_frame_prefix:="~"``, where the ("~") character is substituted with node namespace. (`#1997 <https://github.com/ros-controls/ros2_controllers/pull/1997>`_).

doc/release_notes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ force_torque_sensor_broadcaster
99
*******************************
1010
* Added support for transforming Wrench messages to a given list of target frames. This is useful when applications need force/torque data in their preferred coordinate frames. (`#2021 <https://github.com/ros-controls/ros2_controllers/pull/2021/files>`__).
1111

12+
diff_drive_controller
13+
*****************************
14+
* Parameter ``tf_frame_prefix_enable`` got deprecated and will be removed in a future release (`#1997 <https://github.com/ros-controls/ros2_controllers/pull/1997>`_).
15+
* Now any tilde ("~") character in ``tf_frame_prefix`` is substituted with node namespace. (`#1997 <https://github.com/ros-controls/ros2_controllers/pull/1997>`_).
16+
1217
joint_trajectory_controller
1318
***************************
1419
* Fill in 0 velocities and accelerations into point before trajectories if the state interfaces

0 commit comments

Comments
 (0)