@@ -28,6 +28,7 @@ class MessageLostListener : public rclcpp::Node
2828 explicit MessageLostListener (const rclcpp::NodeOptions & options)
2929 : Node(" MessageLostListener" , options)
3030 {
31+ // Callback that will be used when a message is received.
3132 auto callback =
3233 [this ](const sensor_msgs::msg::Image::SharedPtr msg) -> void
3334 {
@@ -38,11 +39,9 @@ class MessageLostListener : public rclcpp::Node
3839 " I heard an image. Message single trip latency: [%f]" ,
3940 diff.seconds ());
4041 };
41- // Create a subscription to the topic which can be matched with one or more compatible ROS
42- // publishers.
43- // Note that not all publishers on the same topic with the same type will be compatible:
44- // they must have compatible Quality of Service policies.
4542 rclcpp::SubscriptionOptions sub_opts;
43+ // Update the subscription options, so an event handler that will report the number of lost
44+ // messages is created together with the subscription.
4645 sub_opts.event_callbacks .message_lost_callback =
4746 [logger = this ->get_logger ()](rclcpp::QOSMessageLostInfo & info)
4847 {
@@ -52,6 +51,8 @@ class MessageLostListener : public rclcpp::Node
5251 info.total_count_change << " \n >\t Total number of messages lost: " <<
5352 info.total_count );
5453 };
54+ // Create the subscription. This will also create an event handler based on the above
55+ // subscription options.
5556 sub_ = create_subscription<sensor_msgs::msg::Image>(
5657 " message_lost_chatter" , 1 , callback, sub_opts);
5758 }
0 commit comments