Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added type adaptation to Image Publisher raw transport #301

Open
wants to merge 1 commit into
base: rolling
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion image_transport/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ find_package(sensor_msgs REQUIRED)
# Build image_transport library
add_library(${PROJECT_NAME}
src/camera_common.cpp
src/publisher.cpp
src/plugin_publisher.cpp
src/subscriber.cpp
src/single_subscriber_publisher.cpp
src/camera_publisher.cpp
Expand Down
9 changes: 0 additions & 9 deletions image_transport/default_plugins.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
<library path="image_transport_plugins">
<class
name="image_transport/raw_pub"
type="image_transport::RawPublisher"
base_class_type="image_transport::PublisherPlugin">
<description>
This is the default publisher. It publishes the Image as-is on the base topic.
</description>
</class>

<class
name="image_transport/raw_sub"
type="image_transport::RawSubscriber"
Expand Down
37 changes: 37 additions & 0 deletions image_transport/include/image_transport/image_transport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@

#include <functional>
#include <memory>
#include <utility>
#include <string>
#include <vector>

#include "loader_fwds.hpp"
#include "rclcpp/node.hpp"

#include "image_transport/camera_publisher.hpp"
Expand All @@ -46,6 +48,11 @@
namespace image_transport
{

using Publisher = PublisherBase<sensor_msgs::msg::Image>;

PubLoaderPtr getPubLoader();
SubLoaderPtr getSubLoader();

/*!
* \brief Advertise an image topic, free function version.
*/
Expand All @@ -56,6 +63,21 @@ Publisher create_publisher(
rmw_qos_profile_t custom_qos = rmw_qos_profile_default,
rclcpp::PublisherOptions options = rclcpp::PublisherOptions());

template<
typename MessageT,
typename AllocatorT = std::allocator<void>,
typename PublisherT = PublisherBase<MessageT, AllocatorT>>
std::shared_ptr<PublisherT>
IMAGE_TRANSPORT_PUBLIC
create_type_adapted_publisher(
rclcpp::Node * node,
const std::string & base_topic,
rmw_qos_profile_t custom_qos = rmw_qos_profile_default,
rclcpp::PublisherOptions options = rclcpp::PublisherOptions())
{
return std::make_shared<PublisherT>(node, base_topic, getPubLoader(), custom_qos, options);
}

/**
* \brief Subscribe to an image topic, free function version.
*/
Expand All @@ -68,6 +90,21 @@ Subscriber create_subscription(
rmw_qos_profile_t custom_qos = rmw_qos_profile_default,
rclcpp::SubscriptionOptions options = rclcpp::SubscriptionOptions());

template<typename AdapterT, typename CallbackT>
typename rclcpp::Subscription<AdapterT>::SharedPtr create_type_adapted_subscription(
rclcpp::Node * node,
const std::string & base_topic,
CallbackT && callback,
rmw_qos_profile_t custom_qos = rmw_qos_profile_default,
rclcpp::SubscriptionOptions options = rclcpp::SubscriptionOptions())
{
auto qos = rclcpp::QoS(rclcpp::QoSInitialization::from_rmw(custom_qos), custom_qos);
return node->create_subscription<AdapterT>(
base_topic, qos,
std::forward<CallbackT>(callback),
options);
}

/*!
* \brief Advertise a camera, free function version.
*/
Expand Down
125 changes: 125 additions & 0 deletions image_transport/include/image_transport/plugin_publisher.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright (c) 2009, Willow Garage, Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the Willow Garage nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#ifndef IMAGE_TRANSPORT__PLUGIN_PUBLISHER_HPP_
#define IMAGE_TRANSPORT__PLUGIN_PUBLISHER_HPP_

#include <memory>
#include <string>

#include "rclcpp/macros.hpp"
#include "rclcpp/node.hpp"

#include "sensor_msgs/msg/image.hpp"

#include "image_transport/exception.hpp"
#include "image_transport/loader_fwds.hpp"
#include "image_transport/single_subscriber_publisher.hpp"
#include "image_transport/visibility_control.hpp"

namespace image_transport
{

/**
* \brief Manages advertisements of multiple transport options on an Image topic.
*
* Publisher is a drop-in replacement for ros::Publisher when publishing
* Image topics. In a minimally built environment, they behave the same; however,
* Publisher is extensible via plugins to publish alternative representations of
* the image on related subtopics. This is especially useful for limiting bandwidth and
* latency over a network connection, when you might (for example) use the theora plugin
* to transport the images as streamed video. All topics are published only on demand
* (i.e. if there are subscribers).
*
* A Publisher should always be created through a call to ImageTransport::advertise(),
* or copied from one that was.
* Once all copies of a specific Publisher go out of scope, any subscriber callbacks
* associated with that handle will stop being called. Once all Publisher for a
* given base topic go out of scope the topic (and all subtopics) will be unadvertised.
*/
class PluginPublisher
{
public:
IMAGE_TRANSPORT_PUBLIC
PluginPublisher() = default;

IMAGE_TRANSPORT_PUBLIC
PluginPublisher(
rclcpp::Node * nh,
const std::string & image_topic,
PubLoaderPtr loader,
rmw_qos_profile_t custom_qos,
rclcpp::PublisherOptions options = rclcpp::PublisherOptions());

/*!
* \brief Returns the number of subscribers that are currently connected to
* this Publisher.
*
* Returns the total number of subscribers to all advertised topics.
*/
IMAGE_TRANSPORT_PUBLIC
size_t getNumSubscribers() const;

/*!
* \brief Publish an image on the topics associated with this Publisher.
*/
IMAGE_TRANSPORT_PUBLIC
void publish(const sensor_msgs::msg::Image & message) const;

/*!
* \brief Publish an image on the topics associated with this Publisher.
*/
IMAGE_TRANSPORT_PUBLIC
void publish(const sensor_msgs::msg::Image::ConstSharedPtr & message) const;

/*!
* \brief Shutdown the advertisements associated with this Publisher.
*/
IMAGE_TRANSPORT_PUBLIC
void shutdown();

IMAGE_TRANSPORT_PUBLIC
operator void *() const;

IMAGE_TRANSPORT_PUBLIC
bool operator<(const PluginPublisher & rhs) const {return impl_ < rhs.impl_;}

IMAGE_TRANSPORT_PUBLIC
bool operator!=(const PluginPublisher & rhs) const {return impl_ != rhs.impl_;}

IMAGE_TRANSPORT_PUBLIC
bool operator==(const PluginPublisher & rhs) const {return impl_ == rhs.impl_;}

private:
struct Impl;
std::shared_ptr<Impl> impl_;
};

} // namespace image_transport

#endif // IMAGE_TRANSPORT__PLUGIN_PUBLISHER_HPP_
Loading