Skip to content

Commit

Permalink
properly remap compressed topics (#851)
Browse files Browse the repository at this point in the history
## Before:

Pushing into namespace is broken, only image_raw changes (camera_info
and transport topics should change):
```
ros2 run image_publisher image_publisher_node --ros-args -p filename:=test.png -r image_raw:=foo/image_raw
---
ros2 topic list
/camera_info
/foo/image_raw
/image_raw/compressed
/image_raw/compressedDepth
/image_raw/theora
```

## After:

Pushing into namespace now works:
```
ros2 run image_publisher image_publisher_node --ros-args -p filename:=test.png -r image_raw:=foo/image_raw
---
ros2 topic list
/foo/camera_info
/foo/image_raw
/foo/image_raw/compressed
/foo/image_raw/compressedDepth
/foo/image_raw/theora
```
  • Loading branch information
mikeferguson authored Jan 18, 2024
1 parent 335e878 commit 9ba7d61
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 17 deletions.
6 changes: 5 additions & 1 deletion image_publisher/src/image_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ using namespace std::chrono_literals;
ImagePublisher::ImagePublisher(const rclcpp::NodeOptions & options)
: rclcpp::Node("ImagePublisher", options)
{
pub_ = image_transport::create_camera_publisher(this, "image_raw");
// For compressed topics to remap appropriately, we need to pass a
// fully expanded and remapped topic name to image_transport
auto node_base = this->get_node_base_interface();
std::string topic_name = node_base->resolve_topic_or_service_name("image_raw", false);
pub_ = image_transport::create_camera_publisher(this, topic_name);

flip_horizontal_ = this->declare_parameter("flip_horizontal", false);
flip_vertical_ = this->declare_parameter("flip_vertical", false);
Expand Down
10 changes: 7 additions & 3 deletions image_view/src/disparity_view_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@ namespace image_view
DisparityViewNode::DisparityViewNode(const rclcpp::NodeOptions & options)
: rclcpp::Node("disparity_view_node", options)
{
std::string topic =
rclcpp::expand_topic_or_service_name("image", this->get_name(), this->get_namespace());
// For compressed topics to remap appropriately, we need to pass a
// fully expanded and remapped topic name to image_transport
auto node_base = this->get_node_base_interface();
std::string topic = node_base->resolve_topic_or_service_name("image", false);

if (topic == "image") {
auto topics = this->get_topic_names_and_types();

if (topics.find(topic) == topics.end()) {
RCLCPP_WARN(
this->get_logger(), "Topic 'image' has not been remapped! Typical command-line usage:\n"
"\t$ ros2 run image_view disparity_view --ros-args -r image:=<disparity image topic>");
Expand Down
8 changes: 5 additions & 3 deletions image_view/src/extract_images_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ ExtractImagesNode::ExtractImagesNode(const rclcpp::NodeOptions & options)
: rclcpp::Node("extract_images_node", options),
filename_format_(""), count_(0), _time(this->now())
{
auto topic = rclcpp::expand_topic_or_service_name(
"image", this->get_name(), this->get_namespace());
// For compressed topics to remap appropriately, we need to pass a
// fully expanded and remapped topic name to image_transport
auto node_base = this->get_node_base_interface();
std::string topic = node_base->resolve_topic_or_service_name("image", false);

this->declare_parameter<std::string>("transport", std::string("raw"));
std::string transport = this->get_parameter("transport").as_string();
Expand All @@ -83,7 +85,7 @@ ExtractImagesNode::ExtractImagesNode(const rclcpp::NodeOptions & options)

auto topics = this->get_topic_names_and_types();

if (topics.find(topic) != topics.end()) {
if (topics.find(topic) == topics.end()) {
RCLCPP_WARN(
this->get_logger(), "extract_images: image has not been remapped! "
"Typical command-line usage:\n\t$ ros2 run image_view extract_images "
Expand Down
6 changes: 4 additions & 2 deletions image_view/src/image_saver_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ namespace image_view
ImageSaverNode::ImageSaverNode(const rclcpp::NodeOptions & options)
: rclcpp::Node("image_saver_node", options)
{
auto topic = rclcpp::expand_topic_or_service_name(
"image", this->get_name(), this->get_namespace());
// For compressed topics to remap appropriately, we need to pass a
// fully expanded and remapped topic name to image_transport
auto node_base = this->get_node_base_interface();
std::string topic = node_base->resolve_topic_or_service_name("image", false);

// Useful when CameraInfo is being published
cam_sub_ = image_transport::create_camera_subscription(
Expand Down
8 changes: 5 additions & 3 deletions image_view/src/image_view_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ ImageViewNode::ImageViewNode(const rclcpp::NodeOptions & options)
auto transport = this->declare_parameter("image_transport", "raw");
RCLCPP_INFO(this->get_logger(), "Using transport \"%s\"", transport.c_str());

std::string topic = rclcpp::expand_topic_or_service_name(
"image", this->get_name(), this->get_namespace());
// For compressed topics to remap appropriately, we need to pass a
// fully expanded and remapped topic name to image_transport
auto node_base = this->get_node_base_interface();
std::string topic = node_base->resolve_topic_or_service_name("image", false);

image_transport::TransportHints hints(this, transport);
pub_ = this->create_publisher<sensor_msgs::msg::Image>("output", 1);
Expand All @@ -118,7 +120,7 @@ ImageViewNode::ImageViewNode(const rclcpp::NodeOptions & options)

auto topics = this->get_topic_names_and_types();

if (topics.find(topic) != topics.end()) {
if (topics.find(topic) == topics.end()) {
RCLCPP_WARN(
this->get_logger(), "Topic 'image' has not been remapped! "
"Typical command-line usage:\n"
Expand Down
7 changes: 4 additions & 3 deletions image_view/src/stereo_view_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ StereoViewNode::StereoViewNode(const rclcpp::NodeOptions & options)
cv::setMouseCallback("right", &StereoViewNode::mouseCb, this);
cv::setMouseCallback("disparity", &StereoViewNode::mouseCb, this);

// Resolve topic names
std::string stereo_ns = rclcpp::expand_topic_or_service_name(
"stereo", this->get_name(), this->get_namespace());
// For compressed topics to remap appropriately, we need to pass a
// fully expanded and remapped topic name to image_transport
auto node_base = this->get_node_base_interface();
std::string stereo_ns = node_base->resolve_topic_or_service_name("stereo", false);

std::string left_topic = rclcpp::expand_topic_or_service_name(
stereo_ns + "/left" + rclcpp::expand_topic_or_service_name(
Expand Down
7 changes: 5 additions & 2 deletions image_view/src/video_recorder_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ VideoRecorderNode::VideoRecorderNode(const rclcpp::NodeOptions & options)
rclcpp::shutdown();
}

auto topic = rclcpp::expand_topic_or_service_name(
"image", this->get_name(), this->get_namespace());
// For compressed topics to remap appropriately, we need to pass a
// fully expanded and remapped topic name to image_transport
auto node_base = this->get_node_base_interface();
std::string topic = node_base->resolve_topic_or_service_name("image", false);

sub_image = image_transport::create_subscription(
this, topic, std::bind(&VideoRecorderNode::callback, this, std::placeholders::_1), "raw");

Expand Down

0 comments on commit 9ba7d61

Please sign in to comment.