Exploring ROS2 with wheeled robotic – #2 – subscribe to ROS2 laser scan matter

Exploring ROS2 with wheeled robotic – #2 –  subscribe to ROS2 laser scan matter

[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 matter utilizing ROS2 C++.

You’ll study:

  • create a node with ROS2 and C++
  • subscribe to a subject with ROS2 and C++
  • launch a ROS2 node utilizing a launch file

1 – Setup setting – Launch simulation

Earlier than the rest, be sure you have the rosject from the earlier put up, you possibly can copy it from right here.

Launch the simulation in a single webshell and in a special tab, checkout the subjects we’ve got accessible. You could 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 rclcpphttps://www.theconstructsim.com/exploring-ros2-with-wheeled-robot-2-how-to-subscribe-to-ros2-laser-scan-topic/::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));
  }

personal:
  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(), "Hi there my buddies");
  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’ll be able to have extra details about it within the reference connected, however on this put up we’re simply utilizing the default QoS offered. Remember the next parameters:

  • matter identify
  • callback technique

The callback technique must be binded, which implies it is not going to be execute on the subscriber declaration, however when the callback is known as. So we go 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 implementationhttps://www.theconstructsim.com/exploring-ros2-with-wheeled-robot-2-how-to-subscribe-to-ros2-laser-scan-topic/ of a category.

3 – Compile and run

With a view 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

With a view 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’ve got a “exhausting coded” matter identify laser_scan. No downside 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",https://www.theconstructsim.com/exploring-ros2-with-wheeled-robot-2-how-to-subscribe-to-ros2-laser-scan-topic/
        output="display screen",
        remappings=[
            ('laser_scan', '/dolly/laser_scan')
        ]
    )

    return LaunchDescription([
        reading_laser
    ])

On this launch file there may be an occasion of a node getting the executable as argument and it’s setup the remappings attribute with a purpose 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 outcome should be much like:

Associated programs & additional hyperlinks:

The put up Exploring ROS2 with wheeled robotic – #2 – subscribe to ROS2 laser scan matter appeared first on The Assemble.


The Assemble Weblog

[ad_2]

Previous Article

How Can Proxies Enhance search engine optimisation Monitoring?

Next Article

Tribological properties of carbon-containing composite supplies ready by high-pressure high-temperature synthesis: Fullerenes, Nanotubes and Carbon Nanostructures: Vol 0, No 0

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨