Skip to content

Commit

Permalink
replace separate Image and CameraInfo publishers with combined image_…
Browse files Browse the repository at this point in the history
…transport::CameraPublisher
  • Loading branch information
jschornak-bdai committed Nov 16, 2023
1 parent ae80926 commit 46ddf33
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#pragma once

#include <image_transport/camera_publisher.hpp>
#include <image_transport/image_transport.hpp>
#include <rclcpp/node.hpp>
#include <sensor_msgs/msg/image.hpp>
#include <spot_driver_cpp/interfaces/publisher_interface_base.hpp>
Expand Down Expand Up @@ -42,10 +44,10 @@ class RclcppPublisherInterface : public PublisherInterfaceBase {
/** @brief rclcpp node use to create the publishers. */
std::shared_ptr<rclcpp::Node> node_;

/** @brief Map between image topic names and image publishers. */
std::unordered_map<std::string, std::shared_ptr<rclcpp::Publisher<sensor_msgs::msg::Image>>> image_publishers_;
/** @brief ImageTransport instance used to create CameraPublishers. */
image_transport::ImageTransport image_transport_;

/** @brief Map between camera info topic names and camera info publishers. */
std::unordered_map<std::string, std::shared_ptr<rclcpp::Publisher<sensor_msgs::msg::CameraInfo>>> info_publishers_;
/** @brief Map between image topic names and camera publishers. */
std::unordered_map<std::string, image_transport::CameraPublisher> publishers_;
};
} // namespace spot_ros2
35 changes: 8 additions & 27 deletions spot_driver_cpp/src/interfaces/rclcpp_publisher_interface.cpp
Original file line number Diff line number Diff line change
@@ -1,60 +1,41 @@
// Copyright (c) 2023 Boston Dynamics AI Institute LLC. All rights reserved.

#include <rclcpp/qos.hpp>
#include <spot_driver_cpp/interfaces/rclcpp_publisher_interface.hpp>
#include <tl_expected/expected.hpp>

namespace {
constexpr auto kPublisherHistoryDepth = 1;

constexpr auto kImageTopicSuffix = "image";
constexpr auto kCameraInfoTopicSuffix = "camera_info";
} // namespace

namespace spot_ros2 {

RclcppPublisherInterface::RclcppPublisherInterface(const std::shared_ptr<rclcpp::Node>& node) : node_{node} {}
RclcppPublisherInterface::RclcppPublisherInterface(const std::shared_ptr<rclcpp::Node>& node)
: node_{node}, image_transport_{node_} {}

void RclcppPublisherInterface::createPublishers(const std::set<ImageSource>& image_sources) {
image_publishers_.clear();
info_publishers_.clear();
publishers_.clear();

for (const auto& image_source : image_sources) {
// Since these topic names do not have a leading `/` character, they will be published within the namespace of the
// node, which should match the name of the robot. For example, the topic for the front left RGB camera will
// ultimately appear as `/MyRobotName/camera/frontleft/image`.
const auto topic_name_base = toRosTopic(image_source);

const auto image_topic_name = topic_name_base + "/" + kImageTopicSuffix;

image_publishers_.try_emplace(image_topic_name,
node_->create_publisher<sensor_msgs::msg::Image>(
image_topic_name, rclcpp::QoS(rclcpp::KeepLast(kPublisherHistoryDepth))));

const auto info_topic_name = topic_name_base + "/" + kCameraInfoTopicSuffix;
info_publishers_.try_emplace(info_topic_name,
node_->create_publisher<sensor_msgs::msg::CameraInfo>(
info_topic_name, rclcpp::QoS(rclcpp::KeepLast(kPublisherHistoryDepth))));
const auto image_topic_name = toRosTopic(image_source) + "/" + kImageTopicSuffix;
publishers_.try_emplace(image_topic_name,
image_transport_.advertiseCamera(image_topic_name, kPublisherHistoryDepth));
}
}

tl::expected<void, std::string> RclcppPublisherInterface::publish(
const std::map<ImageSource, ImageWithCameraInfo>& images) {
for (const auto& [image_source, image_data] : images) {
const auto topic_name_base = toRosTopic(image_source);
const auto image_topic_name = topic_name_base + "/" + kImageTopicSuffix;
const auto info_topic_name = topic_name_base + "/" + kCameraInfoTopicSuffix;
const auto image_topic_name = toRosTopic(image_source) + "/" + kImageTopicSuffix;

try {
image_publishers_.at(image_topic_name)->publish(image_data.image);
publishers_.at(image_topic_name).publish(image_data.image, image_data.info);
} catch (const std::out_of_range& e) {
return tl::make_unexpected("No publisher exists for image topic `" + image_topic_name + "`.");
}
try {
info_publishers_.at(info_topic_name)->publish(image_data.info);
} catch (const std::out_of_range& e) {
return tl::make_unexpected("No publisher exists for camera info topic`" + info_topic_name + "`.");
}
}

return {};
Expand Down

0 comments on commit 46ddf33

Please sign in to comment.