Skip to content

Commit

Permalink
Use const& signature for read-only sub callbacks (#2297)
Browse files Browse the repository at this point in the history
The constant reference message signatures should be preferred over the
shared pointer to constant message signatures for read-only subscriber
callbacks, as discussed in ros2/rclcpp#1598.

As such, it makes sense to migrate to said signatures in documentation.

Signed-off-by: Abrar Rahman Protyasha <[email protected]>
(cherry picked from commit e4cd3cb)

# Conflicts:
#	source/Tutorials/Custom-ROS2-Interfaces.rst
#	source/Tutorials/FastDDS-Configuration/FastDDS-Configuration.rst
#	source/Tutorials/Tf2/Writing-A-Tf2-Broadcaster-Cpp.rst
#	source/Tutorials/Writing-A-Simple-Cpp-Publisher-And-Subscriber.rst
  • Loading branch information
aprotyas authored and mergify-bot committed Feb 9, 2022
1 parent 52a8d73 commit 4275c66
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
6 changes: 5 additions & 1 deletion source/Tutorials/Custom-ROS2-Interfaces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,13 @@ Subscriber:
}

private:
<<<<<<< HEAD
void topic_callback(const tutorial_interfaces::msg::Num::SharedPtr msg) const // CHANGE
=======
void topic_callback(const tutorial_interfaces::msg::Num & msg) const // CHANGE
>>>>>>> e4cd3cb (Use `const&` signature for read-only sub callbacks (#2297))
{
RCLCPP_INFO_STREAM(this->get_logger(), "I heard: '" << msg->num << "'"); // CHANGE
RCLCPP_INFO_STREAM(this->get_logger(), "I heard: '" << msg.num << "'"); // CHANGE
}
rclcpp::Subscription<tutorial_interfaces::msg::Num>::SharedPtr subscription_; // CHANGE
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,13 @@ In a new source file named ``src/sync_async_reader.cpp`` write the following con
/**
* Actions to run every time a new message is received
*/
<<<<<<< HEAD
void topic_callback(const std_msgs::msg::String::SharedPtr msg) const
=======
void topic_callback(const std_msgs::msg::String & msg) const
>>>>>>> e4cd3cb (Use `const&` signature for read-only sub callbacks (#2297))
{
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg.data.c_str());
}
// A subscriber that listens to topic 'sync_topic'
Expand Down
16 changes: 10 additions & 6 deletions source/Tutorials/Tf2/Writing-A-Tf2-Broadcaster-Cpp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ Open the file using your preferred text editor.
}

private:
<<<<<<< HEAD
void handle_turtle_pose(const std::shared_ptr<turtlesim::msg::Pose> msg)
=======
void handle_turtle_pose(const turtlesim::msg::Pose & msg)
>>>>>>> e4cd3cb (Use `const&` signature for read-only sub callbacks (#2297))
{
rclcpp::Time now = this->get_clock()->now();
geometry_msgs::msg::TransformStamped t;
Expand All @@ -119,15 +123,15 @@ Open the file using your preferred text editor.

// 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.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);
q.setRPY(0, 0, msg.theta);
t.transform.rotation.x = q.x();
t.transform.rotation.y = q.y();
t.transform.rotation.z = q.z();
Expand Down Expand Up @@ -195,15 +199,15 @@ Here we copy the information from the 3D turtle pose into the 3D transform.

// 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.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);
q.setRPY(0, 0, msg.theta);
t.transform.rotation.x = q.x();
t.transform.rotation.y = q.y();
t.transform.rotation.z = q.z();
Expand Down
12 changes: 10 additions & 2 deletions source/Tutorials/Writing-A-Simple-Cpp-Publisher-And-Subscriber.rst
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,13 @@ Open the ``subscriber_member_function.cpp`` with your text editor.
}

private:
<<<<<<< HEAD
void topic_callback(const std_msgs::msg::String::SharedPtr msg) const
=======
void topic_callback(const std_msgs::msg::String & msg) const
>>>>>>> e4cd3cb (Use `const&` signature for read-only sub callbacks (#2297))
{
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg.data.c_str());
}
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};
Expand Down Expand Up @@ -398,9 +402,13 @@ The only field declaration in this class is the subscription.
.. code-block:: C++

private:
<<<<<<< HEAD
void topic_callback(const std_msgs::msg::String::SharedPtr msg) const
=======
void topic_callback(const std_msgs::msg::String & msg) const
>>>>>>> e4cd3cb (Use `const&` signature for read-only sub callbacks (#2297))
{
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg.data.c_str());
}
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;

Expand Down

0 comments on commit 4275c66

Please sign in to comment.