[ad_1]
By Marco Arruda
That is the second chapter of the sequence “Exploring ROS2 with a wheeled robotic”. On this episode, you’ll discover ways to subscribe to a ROS2 subject utilizing ROS2 C++.
You’ll study:
- The right way to create a node with ROS2 and C++
- The right way to subscribe to a subject with ROS2 and C++
- The right way to launch a ROS2 node utilizing a launch file
1 – Setup setting – Launch simulation
Earlier than the rest, ensure you have the rosject from the earlier submit, you possibly can copy it from right here.
Launch the simulation in a single webshell and in a unique tab, checkout the subjects we have now obtainable. You need to get one thing much like the picture beneath:

2 – Create a ROS2 node
Our objective is to learn the laser information, so create a brand new file known as reading_laser.cpp:
contact ~/ros2_ws/src/my_package/reading_laser.cpp
And paste the content material beneath:
#embody "rclcpp/rclcpp.hpp"
#embody "sensor_msgs/msg/laser_scan.hpp"
utilizing std::placeholders::_1;
class ReadingLaser : public rclcpp::Node {
public:
ReadingLaser() : Node("reading_laser") {
auto default_qos = rclcpp::QoS(rclcpp::SystemDefaultsQoS());
subscription_ = this->create_subscription(
"laser_scan", default_qos,
std::bind(&ReadingLaser::topic_callback, this, _1));
}
non-public:
void topic_callback(const sensor_msgs::msg::LaserScan::SharedPtr _msg) {
RCLCPP_INFO(this->get_logger(), "I heard: '%f' '%f'", _msg->ranges[0],
_msg->ranges[100]);
}
rclcpp::Subscription::SharedPtr subscription_;
};
int major(int argc, char *argv[]) {
rclcpp::init(argc, argv);
auto node = std::make_shared();
RCLCPP_INFO(node->get_logger(), "Hey my mates");
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}
We’re creating a brand new class ReadingLaser that represents the node (it inherits rclcpp::Node). A very powerful about that class are the subscriber attribute and the tactic callback. Within the major perform we’re initializing the node and hold it alive (spin) whereas its ROS connection is legitimate.
The subscriber constructor expects to get a QoS, that stands for the middleware used for the high quality of service. You may have extra details about it within the reference hooked up, however on this submit we’re simply utilizing the default QoS offered. Bear in mind the next parameters:
- subject identify
- callback technique
The callback technique must be binded, which implies it won’t be execute on the subscriber declaration, however when the callback is named. So we cross the reference of the tactic and setup the this reference for the present object for use as callback, afterall the tactic itself is a generic implementation of a category.
3 – Compile and run
As a way to compile the cpp file, we should add some directions to the ~/ros2_ws/src/my_package/src/CMakeLists.txt:
- Search for discover dependencies and embody the sensor_msgs library
- Simply earlier than the set up instruction add the executable and goal its dependencies
- Append one other set up instruction for the brand new executable we’ve simply created
# discover dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(sensor_msgs REQUIRED)
...
...
add_executable(reading_laser src/reading_laser.cpp)
ament_target_dependencies(reading_laser rclcpp std_msgs sensor_msgs)
...
...
set up(TARGETS
reading_laser
DESTINATION lib/${PROJECT_NAME}/
)
Compile it:
colcon construct --symlink-install --packages-select my_package
4 – Run the node and mapping the subject
As a way to run the executable created, you should use:
ros2 run my_package reading_laser
Though the the laser values gained’t present up. That’s as a result of we have now a “arduous coded” subject identify laser_scan. No drawback in any respect, once we can map subjects utilizing launch recordsdata. Create a brand new launch file ~/ros2_ws/src/my_package/launch/reading_laser.py:
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
reading_laser = Node(
bundle="my_package",
executable="reading_laser",
output="display",
remappings=[
('laser_scan', '/dolly/laser_scan')
]
)
return LaunchDescription([
reading_laser
])
On this launch file there’s an occasion of a node getting the executable as argument and it’s setup the remappings attribute as a way to remap from laser_scan to /dolly/laser_scan.
Run the identical node utilizing the launch file this time:
ros2 launch my_package reading_laser.launch.py
Add some obstacles to the world and the end result have to be much like:

Associated programs & additional hyperlinks:

The Constructsim Weblog
[ad_2]
