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

ROS2 QoS callback function

asked 2020-02-25 06:26:57 -0600

Sapodilla gravatar image

Hi everyone,

First of all, I just learned about ROS2 so I am not so familiar with it. Also, I apologize for my bad English.

I am now working on how to implement QoS in my subscription. I have a little bit experience on eProsima FastRTPS and I did manage to run publisher and subscriber with QoS policies. Thus, I expected ROS2 works quite the same way. Here I want to have a deadline policy with a callback function. However, I were lost while trying to set up the callback because in FastRTPS, it has publisher/subscriber listener, which handles all the event callback functions. In ROS2, I do not know where and how to put or configure callback function. I had a look in SubscriptionEventCallbacks class but it did not work (maybe I set it wrongly). After searching on internet, I have not had any clue yet.

Below is a part of my code trying to configure QoS (I am also not expert on C++ and I am learning it, so feel free to put any comment):

Listener(): Node("Listener_node") // Constructor
rclcpp::QoS qos_profile(10);
std::chrono::milliseconds deadline_time(1000);
Subscription = this->create_subscription<std_msgs::msg::String>("topic",qos_profile,std::bind(&Listener::callback,this,std::placeholder::_1));

Any recommendation, comment or hint is welcomed!


edit retag flag offensive close merge delete


Can you concisely state what your issue is? Your code sample below looks very reasonable.

stevemacenski gravatar image stevemacenski  ( 2020-02-25 10:54:04 -0600 )edit

Hi Steven, sorry for my late response. I already found the solution for my issue a few second before typing this comment :D. To make it a bit more clearly, I want to have a callback function whenever a deadline is missed. After spending some time to going through ROS2 doxygen, it finally works for me :). I will put the answer for those who come across this problem like me.

Sapodilla gravatar image Sapodilla  ( 2020-02-26 09:30:25 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-02-26 09:35:33 -0600

Sapodilla gravatar image

To whoever struggles the same problem as me, for having your callback function for QoS, specify the callback function in rclcpp::SubscriptionOptions.

Good luck!

edit flag offensive delete link more


Hello Can you let me know how did you use rclcpp::SubscriptionOptions in the code?

Gunika gravatar image Gunika  ( 2020-12-18 11:10:31 -0600 )edit

Can you please let me know how did you use the callback in rclcpp::SubscriptionOptions? This is my code for the subscriber: rclcpp::QoS qos_profile(10); std::chrono::milliseconds deadline_time(1000); qos_profile.deadline(deadline_time); rclcpp::SubscriptionOptions subscription_options; subscription_options.event_callbacks.deadline_callback = -> void { std::cout << "Connection Lost"; };

  subscription_ = this->create_subscription<std_msgs::msg::String>(
  "topic", 10,subscription_options,qos_profile,std::bind(&MinimalSubscriber::topic_callback, this, _1));

Publisher: { rclcpp::QoS qos_profile(10); std::chrono::milliseconds deadline_time(1000); qos_profile.deadline(deadline_time); publisher_ = this->create_publisher<std_msgs::msg::string>("topic", 10,qos_profile); timer_ = this->create_wall_timer( 500ms, std::bind(&MinimalPublisher::t

Gunika gravatar image Gunika  ( 2020-12-19 10:05:59 -0600 )edit

Question Tools



Asked: 2020-02-25 06:26:57 -0600

Seen: 1,672 times

Last updated: Feb 26 '20