Image subscriber experiment oop.cpp 20210414

From wikidb
Jump to: navigation, search
// Image Subscriber Demo - Object Oriented Version
//
// Based on Writing a simple publisher and subscriber (C++)
//https://docs.ros.org/en/foxy/Tutorials/Writing-A-Simple-Cpp-Publisher-And-Subscriber.html

#include <memory>
#include "rclcpp/rclcpp.hpp"
#include "sensor_msgs/msg/image.hpp"
using std::placeholders::_1;

// ===================================================================================
// MinimalImageSubscriber class definition

class MinimalImageSubscriber : public rclcpp::Node
{
public:
  // ---------------------------------------------------------------------------------
  // constructor - create the subscriber
  MinimalImageSubscriber(): Node("Image_Subscriber_OOP_Test")
  {
    // Set the quality of service
    rclcpp::QoS video_qos(10);
    video_qos.keep_last(10);
    video_qos.best_effort();
    video_qos.durability_volatile();

    // Subscribe to image topics
    subscription_ = this->create_subscription<sensor_msgs::msg::Image>(
       "image", 10, std::bind(&MinimalImageSubscriber::topic_callback, this, _1));
  }

private:
  //----------------------------------------------------------------------------------
  // image callback function
  void topic_callback(const sensor_msgs::msg::Image::SharedPtr msg) const
  {
    RCLCPP_INFO(this->get_logger(), "I See: " +  msg->encoding);
  }

  // --------------------------------------------------------------------------------
  // Subscriber insance variable
  rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr subscription_;
};


// ===================================================================================
// main
int main(int argc, char * argv[])
{
  // Act on the parameters and other stuff
  rclcpp::init(argc, argv);

  // Create the ROS node and start it up
  rclcpp::spin(std::make_shared<MinimalImageSubscriber>());

  rclcpp::shutdown();
  return 0;
}