From 6544bb3b9b7c03edd61e72149c1734585286dc88 Mon Sep 17 00:00:00 2001 From: Maurice Alexander Purnawan Date: Mon, 8 Dec 2025 16:53:05 +0700 Subject: [PATCH] Update subscription callback signatures (#6005) Signed-off-by: Maurice (cherry picked from commit bf46139324dca80e5ae5c100f97d2c4608d957a3) # Conflicts: # source/Tutorials/Advanced/Recording-A-Bag-From-Your-Own-Node-CPP.rst # source/Tutorials/Advanced/Simulators/Webots/Code/MyRobotDriver.cpp # source/Tutorials/Advanced/Simulators/Webots/Code/ObstacleAvoider.cpp # source/Tutorials/Intermediate/Tf2/Writing-A-Tf2-Broadcaster-Cpp.rst --- ...Recording-A-Bag-From-Your-Own-Node-CPP.rst | 22 +++++++++++++ .../Simulators/Webots/Code/MyRobotDriver.cpp | 8 +++++ .../Webots/Code/ObstacleAvoider.cpp | 18 +++++++++-- .../Webots/Code/ObstacleAvoider.hpp | 4 +-- .../Tf2/Writing-A-Tf2-Broadcaster-Cpp.rst | 32 +++++++++++++++++++ 5 files changed, 80 insertions(+), 4 deletions(-) diff --git a/source/Tutorials/Advanced/Recording-A-Bag-From-Your-Own-Node-CPP.rst b/source/Tutorials/Advanced/Recording-A-Bag-From-Your-Own-Node-CPP.rst index 91e907ba9aa..6956f374ff9 100644 --- a/source/Tutorials/Advanced/Recording-A-Bag-From-Your-Own-Node-CPP.rst +++ b/source/Tutorials/Advanced/Recording-A-Bag-From-Your-Own-Node-CPP.rst @@ -96,6 +96,15 @@ Inside the ``ros2_ws/src/bag_recorder_nodes/src`` directory, create a new file c writer_->open("my_bag"); +<<<<<<< HEAD +======= + auto subscription_callback_lambda = [this](std::shared_ptr msg){ + rclcpp::Time time_stamp = this->now(); + + writer_->write(msg, "chatter", "std_msgs/msg/String", time_stamp); + }; + +>>>>>>> bf46139 (Update subscription callback signatures (#6005)) subscription_ = create_subscription( "chatter", 10, std::bind(&SimpleBagRecorder::topic_callback, this, _1)); } @@ -146,6 +155,15 @@ We will write data to the bag in the callback. .. code-block:: C++ +<<<<<<< HEAD +======= + auto subscription_callback_lambda = [this](std::shared_ptr msg){ + rclcpp::Time time_stamp = this->now(); + + writer_->write(msg, "chatter", "std_msgs/msg/String", time_stamp); + }; + +>>>>>>> bf46139 (Update subscription callback signatures (#6005)) subscription_ = create_subscription( "chatter", 10, std::bind(&SimpleBagRecorder::topic_callback, this, _1)); @@ -158,8 +176,12 @@ We do this for two reasons. .. code-block:: C++ +<<<<<<< HEAD void topic_callback(std::shared_ptr msg) const { +======= + auto subscription_callback_lambda = [this](std::shared_ptr msg){ +>>>>>>> bf46139 (Update subscription callback signatures (#6005)) Within the subscription callback, the first thing to do is determine the time stamp to use for the stored message. This can be anything appropriate to your data, but two common values are the time at which the data was produced, if known, and the time it is received. diff --git a/source/Tutorials/Advanced/Simulators/Webots/Code/MyRobotDriver.cpp b/source/Tutorials/Advanced/Simulators/Webots/Code/MyRobotDriver.cpp index 56fdf7a64a5..dfc34226b97 100644 --- a/source/Tutorials/Advanced/Simulators/Webots/Code/MyRobotDriver.cpp +++ b/source/Tutorials/Advanced/Simulators/Webots/Code/MyRobotDriver.cpp @@ -25,6 +25,7 @@ void MyRobotDriver::init( cmd_vel_subscription_ = node->create_subscription( "/cmd_vel", rclcpp::SensorDataQoS().reliable(), +<<<<<<< HEAD std::bind(&MyRobotDriver::cmdVelCallback, this, std::placeholders::_1)); } @@ -32,6 +33,13 @@ void MyRobotDriver::cmdVelCallback( const geometry_msgs::msg::Twist::SharedPtr msg) { cmd_vel_msg.linear = msg->linear; cmd_vel_msg.angular = msg->angular; +======= + [this](const geometry_msgs::msg::Twist::ConstSharedPtr msg){ + this->cmd_vel_msg.linear = msg->linear; + this->cmd_vel_msg.angular = msg->angular; + } + ); +>>>>>>> bf46139 (Update subscription callback signatures (#6005)) } void MyRobotDriver::step() { diff --git a/source/Tutorials/Advanced/Simulators/Webots/Code/ObstacleAvoider.cpp b/source/Tutorials/Advanced/Simulators/Webots/Code/ObstacleAvoider.cpp index b584975e63b..61e72d6ec0d 100644 --- a/source/Tutorials/Advanced/Simulators/Webots/Code/ObstacleAvoider.cpp +++ b/source/Tutorials/Advanced/Simulators/Webots/Code/ObstacleAvoider.cpp @@ -7,6 +7,7 @@ ObstacleAvoider::ObstacleAvoider() : Node("obstacle_avoider") { left_sensor_sub_ = create_subscription( "/left_sensor", 1, +<<<<<<< HEAD std::bind(&ObstacleAvoider::leftSensorCallback, this, std::placeholders::_1)); @@ -14,15 +15,28 @@ ObstacleAvoider::ObstacleAvoider() : Node("obstacle_avoider") { "/right_sensor", 1, std::bind(&ObstacleAvoider::rightSensorCallback, this, std::placeholders::_1)); +======= + [this](const sensor_msgs::msg::Range::ConstSharedPtr msg){ + return this->leftSensorCallback(msg); + } + ); + + right_sensor_sub_ = create_subscription( + "/right_sensor", 1, + [this](const sensor_msgs::msg::Range::ConstSharedPtr msg){ + return this->rightSensorCallback(msg); + } + ); +>>>>>>> bf46139 (Update subscription callback signatures (#6005)) } void ObstacleAvoider::leftSensorCallback( - const sensor_msgs::msg::Range::SharedPtr msg) { + const sensor_msgs::msg::Range::ConstSharedPtr msg) { left_sensor_value = msg->range; } void ObstacleAvoider::rightSensorCallback( - const sensor_msgs::msg::Range::SharedPtr msg) { + const sensor_msgs::msg::Range::ConstSharedPtr msg) { right_sensor_value = msg->range; auto command_message = std::make_unique(); diff --git a/source/Tutorials/Advanced/Simulators/Webots/Code/ObstacleAvoider.hpp b/source/Tutorials/Advanced/Simulators/Webots/Code/ObstacleAvoider.hpp index 51c2b3e8703..61838aaaed9 100644 --- a/source/Tutorials/Advanced/Simulators/Webots/Code/ObstacleAvoider.hpp +++ b/source/Tutorials/Advanced/Simulators/Webots/Code/ObstacleAvoider.hpp @@ -9,8 +9,8 @@ class ObstacleAvoider : public rclcpp::Node { explicit ObstacleAvoider(); private: - void leftSensorCallback(const sensor_msgs::msg::Range::SharedPtr msg); - void rightSensorCallback(const sensor_msgs::msg::Range::SharedPtr msg); + void leftSensorCallback(const sensor_msgs::msg::Range::ConstSharedPtr msg); + void rightSensorCallback(const sensor_msgs::msg::Range::ConstSharedPtr msg); rclcpp::Publisher::SharedPtr publisher_; rclcpp::Subscription::SharedPtr left_sensor_sub_; diff --git a/source/Tutorials/Intermediate/Tf2/Writing-A-Tf2-Broadcaster-Cpp.rst b/source/Tutorials/Intermediate/Tf2/Writing-A-Tf2-Broadcaster-Cpp.rst index e3b05c4a316..e855943cc35 100644 --- a/source/Tutorials/Intermediate/Tf2/Writing-A-Tf2-Broadcaster-Cpp.rst +++ b/source/Tutorials/Intermediate/Tf2/Writing-A-Tf2-Broadcaster-Cpp.rst @@ -101,7 +101,39 @@ Open the file using your preferred text editor. stream << "/" << turtlename_.c_str() << "/pose"; std::string topic_name = stream.str(); +<<<<<<< HEAD subscription_ = this->create_subscription( +======= + auto handle_turtle_pose = [this](const std::shared_ptr msg){ + geometry_msgs::msg::TransformStamped t; + + // Read message content and assign it to + // corresponding tf variables + t.header.stamp = this->get_clock()->now(); + t.header.frame_id = "world"; + t.child_frame_id = turtlename_.c_str(); + + // Turtle only exists in 2D, thus we get x and y translation + // coordinates from the message and set the z coordinate to 0 + t.transform.translation.x = msg->x; + t.transform.translation.y = msg->y; + t.transform.translation.z = 0.0; + + // For the same reason, turtle can only rotate around one axis + // and this why we set rotation in x and y to 0 and obtain + // rotation in z axis from the message + tf2::Quaternion q; + q.setRPY(0, 0, msg->theta); + t.transform.rotation.x = q.x(); + t.transform.rotation.y = q.y(); + t.transform.rotation.z = q.z(); + t.transform.rotation.w = q.w(); + + // Send the transformation + tf_broadcaster_->sendTransform(t); + }; + subscription_ = this->create_subscription( +>>>>>>> bf46139 (Update subscription callback signatures (#6005)) topic_name, 10, std::bind(&FramePublisher::handle_turtle_pose, this, std::placeholders::_1)); }