Skip to content

Commit 6544bb3

Browse files
mini-1235mergify[bot]
authored andcommitted
Update subscription callback signatures (#6005)
Signed-off-by: Maurice <[email protected]> (cherry picked from commit bf46139) # 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
1 parent 10f54fb commit 6544bb3

File tree

5 files changed

+80
-4
lines changed

5 files changed

+80
-4
lines changed

source/Tutorials/Advanced/Recording-A-Bag-From-Your-Own-Node-CPP.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ Inside the ``ros2_ws/src/bag_recorder_nodes/src`` directory, create a new file c
9696

9797
writer_->open("my_bag");
9898

99+
<<<<<<< HEAD
100+
=======
101+
auto subscription_callback_lambda = [this](std::shared_ptr<const rclcpp::SerializedMessage> msg){
102+
rclcpp::Time time_stamp = this->now();
103+
104+
writer_->write(msg, "chatter", "std_msgs/msg/String", time_stamp);
105+
};
106+
107+
>>>>>>> bf46139 (Update subscription callback signatures (#6005))
99108
subscription_ = create_subscription<std_msgs::msg::String>(
100109
"chatter", 10, std::bind(&SimpleBagRecorder::topic_callback, this, _1));
101110
}
@@ -146,6 +155,15 @@ We will write data to the bag in the callback.
146155

147156
.. code-block:: C++
148157

158+
<<<<<<< HEAD
159+
=======
160+
auto subscription_callback_lambda = [this](std::shared_ptr<const rclcpp::SerializedMessage> msg){
161+
rclcpp::Time time_stamp = this->now();
162+
163+
writer_->write(msg, "chatter", "std_msgs/msg/String", time_stamp);
164+
};
165+
166+
>>>>>>> bf46139 (Update subscription callback signatures (#6005))
149167
subscription_ = create_subscription<std_msgs::msg::String>(
150168
"chatter", 10, std::bind(&SimpleBagRecorder::topic_callback, this, _1));
151169

@@ -158,8 +176,12 @@ We do this for two reasons.
158176

159177
.. code-block:: C++
160178

179+
<<<<<<< HEAD
161180
void topic_callback(std::shared_ptr<rclcpp::SerializedMessage> msg) const
162181
{
182+
=======
183+
auto subscription_callback_lambda = [this](std::shared_ptr<const rclcpp::SerializedMessage> msg){
184+
>>>>>>> bf46139 (Update subscription callback signatures (#6005))
163185

164186
Within the subscription callback, the first thing to do is determine the time stamp to use for the stored message.
165187
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.

source/Tutorials/Advanced/Simulators/Webots/Code/MyRobotDriver.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,21 @@ void MyRobotDriver::init(
2525

2626
cmd_vel_subscription_ = node->create_subscription<geometry_msgs::msg::Twist>(
2727
"/cmd_vel", rclcpp::SensorDataQoS().reliable(),
28+
<<<<<<< HEAD
2829
std::bind(&MyRobotDriver::cmdVelCallback, this, std::placeholders::_1));
2930
}
3031

3132
void MyRobotDriver::cmdVelCallback(
3233
const geometry_msgs::msg::Twist::SharedPtr msg) {
3334
cmd_vel_msg.linear = msg->linear;
3435
cmd_vel_msg.angular = msg->angular;
36+
=======
37+
[this](const geometry_msgs::msg::Twist::ConstSharedPtr msg){
38+
this->cmd_vel_msg.linear = msg->linear;
39+
this->cmd_vel_msg.angular = msg->angular;
40+
}
41+
);
42+
>>>>>>> bf46139 (Update subscription callback signatures (#6005))
3543
}
3644

3745
void MyRobotDriver::step() {

source/Tutorials/Advanced/Simulators/Webots/Code/ObstacleAvoider.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,36 @@ ObstacleAvoider::ObstacleAvoider() : Node("obstacle_avoider") {
77

88
left_sensor_sub_ = create_subscription<sensor_msgs::msg::Range>(
99
"/left_sensor", 1,
10+
<<<<<<< HEAD
1011
std::bind(&ObstacleAvoider::leftSensorCallback, this,
1112
std::placeholders::_1));
1213

1314
right_sensor_sub_ = create_subscription<sensor_msgs::msg::Range>(
1415
"/right_sensor", 1,
1516
std::bind(&ObstacleAvoider::rightSensorCallback, this,
1617
std::placeholders::_1));
18+
=======
19+
[this](const sensor_msgs::msg::Range::ConstSharedPtr msg){
20+
return this->leftSensorCallback(msg);
21+
}
22+
);
23+
24+
right_sensor_sub_ = create_subscription<sensor_msgs::msg::Range>(
25+
"/right_sensor", 1,
26+
[this](const sensor_msgs::msg::Range::ConstSharedPtr msg){
27+
return this->rightSensorCallback(msg);
28+
}
29+
);
30+
>>>>>>> bf46139 (Update subscription callback signatures (#6005))
1731
}
1832

1933
void ObstacleAvoider::leftSensorCallback(
20-
const sensor_msgs::msg::Range::SharedPtr msg) {
34+
const sensor_msgs::msg::Range::ConstSharedPtr msg) {
2135
left_sensor_value = msg->range;
2236
}
2337

2438
void ObstacleAvoider::rightSensorCallback(
25-
const sensor_msgs::msg::Range::SharedPtr msg) {
39+
const sensor_msgs::msg::Range::ConstSharedPtr msg) {
2640
right_sensor_value = msg->range;
2741

2842
auto command_message = std::make_unique<geometry_msgs::msg::Twist>();

source/Tutorials/Advanced/Simulators/Webots/Code/ObstacleAvoider.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class ObstacleAvoider : public rclcpp::Node {
99
explicit ObstacleAvoider();
1010

1111
private:
12-
void leftSensorCallback(const sensor_msgs::msg::Range::SharedPtr msg);
13-
void rightSensorCallback(const sensor_msgs::msg::Range::SharedPtr msg);
12+
void leftSensorCallback(const sensor_msgs::msg::Range::ConstSharedPtr msg);
13+
void rightSensorCallback(const sensor_msgs::msg::Range::ConstSharedPtr msg);
1414

1515
rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr publisher_;
1616
rclcpp::Subscription<sensor_msgs::msg::Range>::SharedPtr left_sensor_sub_;

source/Tutorials/Intermediate/Tf2/Writing-A-Tf2-Broadcaster-Cpp.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,39 @@ Open the file using your preferred text editor.
101101
stream << "/" << turtlename_.c_str() << "/pose";
102102
std::string topic_name = stream.str();
103103

104+
<<<<<<< HEAD
104105
subscription_ = this->create_subscription<turtlesim::msg::Pose>(
106+
=======
107+
auto handle_turtle_pose = [this](const std::shared_ptr<const turtlesim_msgs::msg::Pose> msg){
108+
geometry_msgs::msg::TransformStamped t;
109+
110+
// Read message content and assign it to
111+
// corresponding tf variables
112+
t.header.stamp = this->get_clock()->now();
113+
t.header.frame_id = "world";
114+
t.child_frame_id = turtlename_.c_str();
115+
116+
// Turtle only exists in 2D, thus we get x and y translation
117+
// coordinates from the message and set the z coordinate to 0
118+
t.transform.translation.x = msg->x;
119+
t.transform.translation.y = msg->y;
120+
t.transform.translation.z = 0.0;
121+
122+
// For the same reason, turtle can only rotate around one axis
123+
// and this why we set rotation in x and y to 0 and obtain
124+
// rotation in z axis from the message
125+
tf2::Quaternion q;
126+
q.setRPY(0, 0, msg->theta);
127+
t.transform.rotation.x = q.x();
128+
t.transform.rotation.y = q.y();
129+
t.transform.rotation.z = q.z();
130+
t.transform.rotation.w = q.w();
131+
132+
// Send the transformation
133+
tf_broadcaster_->sendTransform(t);
134+
};
135+
subscription_ = this->create_subscription<turtlesim_msgs::msg::Pose>(
136+
>>>>>>> bf46139 (Update subscription callback signatures (#6005))
105137
topic_name, 10,
106138
std::bind(&FramePublisher::handle_turtle_pose, this, std::placeholders::_1));
107139
}

0 commit comments

Comments
 (0)