ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

With image transport, you don't actually have to manage all the publishers yourself. The ImageTransport object will do that for you. As for your question though... You can create a shared pointer with an empty deleter function to be able to pass to Image Transport:

class CamPublisher_ : public rclcpp::Node
{
protected:
    rclcpp::Node::SharedPtr node_handle_;
    image_transport::ImageTransport image_transport_;
    image_transport::Publisher image_publisher_;
    ...
public:
    CamPublisher_() :
            Node("camera"),
            // create the shared_ptr node handle for this node.
            node_handle_(std::shared_ptr<CamPublisher_>(this, [](auto *) {})),
            image_transport_(node_handle_),
            image_publisher_(image_transport.advertise("image", 10)
    {
        ....
    }
    void convert_and_publish(const cv::Mat& frame)
    {
        sensor_msgs::msg::Image msg;
        ....
        image_publisher_->publish(msg);
    }
}