A list of robots running on ROS2

A list of robots running on ROS2

Here you will find a list of robots that you can use for learning ROS2 or build robotics products on top of them based on ROS2.

By robots running ROS2, I mean robots that publish their control and sensor topics in ROS2 topics. Let’s call this type of robots ROS2 Native.

I’m not including here robots that publish in ROS1 and then, there is a node that subscribes to those ROS1 topics and republishes in ROS2 format. Let’s call this type of robots ROS2 Hybrid.

So here a list of ROS2 Native robots:

Turtlebot 3, by Robotis

This was the first commercial robot able to run ROS2 on it. Turtlebot3 was designed to run ROS1 by default, so if you buy it, it will come with ROS1 pre-installed. However, the guys from Robotis quickly prepared a tutorial about how to make your Turtlebot3 run ROS2. The full tutorial is here.

Hands-on With TurtleBot 3, a Powerful Little Robot for Learning ROS - IEEE  Spectrum

ROSbot, by Husarion.

Again, this robot default system is ROS1 but Husarion has released a full tutorial about how to change the default ROS version to ROS2. The full tutorial is here.

Husarion

Rover Zero, by Rover Robotics

Same procedure as the previous 2. Default system is ROS1 but Rover provides a git repo with the code and instructions about how to install the ROS2 drivers for that robot. Check it out here.

Hadabot, by Hadabot

This is the first ROS2 native robot I have seen. It is a very simple robot with the main purpose of learn ROS2 while applying it to the real robot. Basically, just a pair of wheels with encoders, more than enough for learning basic ROS2. Check it out here.

Running Robot Operating System, ROS2, from the Web Browser with Docker –  Full-Stack Feed

e-puck 2, by GCTronic.

The e-puck 2 robot is a very small and cool robot with lots of sensors. The e-puck 2, from conception, it has nothing to do with ROS. However, recently Cyberbotics company created a ROS2 driver for that robot so we can now use this excellent robot with our favorite ROS. Here the drivers with tutorials.

e-puck v.2 – RoadNarrows Robotics

Open Manipulator, by Robotis

This is the only arm robot running ROS2 that I’m aware of. It is produced by the creators of the Turtlebot3 and, again, the robot boots initially with ROS1. However, the Robotis team have created a detailed tutorial about how to set this manipulator work with ROS2. Find the tutorial here.

OpenManipulator

…and that is all that I’ve found!

Guys, I see a potential business here… just saying…

The reason behind so few ROS2 robots

I think that one of the reasons why there are no more ROS2 robots is because ROS1 ones can make the deal even with ROS2. I mean, you can have a ROS1 robot running all your drivers to the robot hardware in ROS1 format, and then use the ROS1_bridge to interface that with ROS2 programs (that is, republishing in ROS2 format).

In case you want to learn how to do the ROS2 republishing of ROS1 topics, take our ROS2 Basics course were we teach how to use the ROS1_bridge to mix ROS1 and ROS2 programs on a single robot, among many other things of ROS2.

If you have a robot that runs on ROS2 and can be bought (not experimental ones), let me know so I can add it to the list.

ROS2 Tutorials #7: How to create a ROS2 Custom Message

ROS2 Tutorials #7: How to create a ROS2 Custom Message

In this episode of ROS2 Tutorials Series, you will learn how to create a simple ROS2 custom message for Topics.

We are going to use a ROS2 (Dashing) installation for this, but you don’t need to install ROS, as we will use the ROS Development Studio (ROSDS), an online platform that provides access to ROS2 and ROS2 computers and other powerful ROS tools within a browser!

Now let’s get to work without further ado!

Step 1: Create a Project (ROSject) on ROSDS

Head to http://rosds.online and create a project with a similar configuration as the one shown below. You can change the details as you like, but please ensure you select “Ubuntu 18.04 + ROS2 Dashing” under “Configuration”.

Create a ROS2 project for Dashing

Once done with that, open your ROSject. This might take a few moments, please be patient.

Step 2: Source the ROS2 workspace

Once the ROSject is open, head to the Tools menu and pick the Shell tool (a terminal) and run the following command to source the workspace:

user:~$ source /opt/ros/dashing/setup.bash
ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~$

If you get that ROS_DISTRO warning, just ignore it.

Step 3: Create a ROS2 C++ package in your ROS2 workspace

We are creating a C++ package (ament_cmake) and adding dependencies rclcpp (ROS C++ client) and std_msgs (in-built standard messages type, on which our new message will be based).

user:~$ mkdir -p ros2_ws/src
user:~$ cd ros2_ws/src
user:~/ros2_ws/src$
user:~/ros2_ws/src$ ros2 pkg create ros2_msg --build-type ament_cmake --dependencies rclcpp std_msgs
going to create a new package
package name: ros2_msg
destination directory: /home/user/ros2_ws/src
package format: 3
version: 0.0.0
description: TODO: Package description
maintainer: ['user <user@todo.todo>']
licenses: ['TODO: License declaration']
build type: ament_cmake
dependencies: ['rclcpp', 'std_msgs']
creating folder ./ros2_msg
creating ./ros2_msg/package.xml
creating source and include folder
creating folder ./ros2_msg/src
creating folder ./ros2_msg/include/ros2_msg
creating ./ros2_msg/CMakeLists.txt
user:~/ros2_ws/src$

Step 3: Create the definition of the ROS2 custom message

user:~/ros2_ws/src$ cd ros2_msg
user:~/ros2_ws/src/ros2_msg$ mkdir -p msg
user:~/ros2_ws/src/ros2_msg$ cd msg/
user:~/ros2_ws/src/ros2_msg/msg$ touch MyMsg.msg

Pick the “IDE” tool from the Tools menu, locate the MyMsg.msg file, and paste in the following code:

int32 day
string month
int32 year

Next, open up the CMakeList.txt file in the IDE and add the required dependencies:

  1. Add these two dependencies under “#find package”, right after the three that are already there:
    • find_package(builtin_interfaces REQUIRED)
      find_package(rosidl_default_generators REQUIRED)

  2. Add the following function to CMakeList.txt.This function tells the ROS system to generate a new message from the specified message definition, using the specified interface.
    • rosidl_generate_interfaces(new_msg
        "msg/MyMsg.msg"
        DEPENDENCIES builtin_interfaces
      )
      

      Add custom function to CMakeLists.txt

Next, open up the package.xml file in the IDE and make the following changes:

  1. Change the package format from “2” to “3”, if not already “3” (in this case it was already 3):
    • Check package format is "3"
  2. Add the dependencies that complement the ones we have in CMakeList.txt:
    • <build_depend>builtin_interfaces</build_depend>
      <build_depend>rosidl_default_generators</build_depend>
      <exec_depend>builtin_interfaces</exec_depend>
      <exec_depend>rosidl_default_runtime</exec_depend>
      
      <member_of_group>rosidl_interface_packages</member_of_group>

      Add dependencies to package.xml

Step 4: Compile the package

Whew, a couple of ingredients had to be added there; now it is time to compile the message and taste the “soup”!

Get back to the terminal and run the following commands:

user:~$ cd ~/ros2_ws
user:~/ros2_ws$ colcon build --symlink-install
Starting >>> ros2_msg
Finished <<< ros2_msg [14.1s]
Summary: 1 package finished [14.3s]
user:~/ros2_ws$ source install/setup.bash
ROS_DISTRO was set to 'dashing' before. Please make sure that the environment does not mix paths from different distributions.
ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~/ros2_ws$

Step 5: Find and test the new custom message:

user:~/ros2_ws$ ros2 msg list | grep MyMsg
ros2_msg/msg/MyMsg

user:~/ros2_ws$ ros2 msg show ros2_msg/msg/MyMsg
int32 day
string month
int32 year
user:~/ros2_ws$ ros2 topic pub /test_topic ros2_msg/MyMsg "{day: '7', month: 'October', year: '2019'}"
publisher: beginning loop
publishing #1: ros2_msg.msg.MyMsg(day=7, month='October', year=2019)

publishing #2: ros2_msg.msg.MyMsg(day=7, month='October', year=2019)

publishing #3: ros2_msg.msg.MyMsg(day=7, month='October', year=2019)

Pick another Shell from the Tools menu and check the message being published (PS: you need to source ROS Dashing and the workspace first):

user:~$ source /opt/ros/dashing/setup.bash
ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~$ source ros2_ws/install/setup.bash
ROS_DISTRO was set to 'dashing' before. Please make sure that the environment does not mix paths from different distributions.
ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~$ ros2 topic list
/parameter_events
/rosout
/test_topic
user:~$ ros2 topic echo /test_topic
day: 7
month: October
year: 2019
---
day: 7
month: October
year: 2019
---
day: 7
month: October
year: 2019
---
day: 7
month: October
year: 2019
---
day: 7
month: October
year: 2019
---
day: 7
month: October
year: 2019
---
^Cuser:~$

Great, it ran! We’re done here!!

Extra 1: ROSject link

Get the ROSject containing all code used in the post in the following link: http://www.rosject.io/l/c3f3784/

Extra 2: Video

Prefer to watch a video demonstrating the steps above? We have one for you below!

Related Resources

Feedback

Did you like this post? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS or ROS2 topics, please let us know in the comments area and we will do a video or post about it 🙂

Post edited by Bayode Aderinola

#ROS2 #ROS #ROS2tutorials #Robot

ROS2 Tutorials #8: How to communicate between ROS1 & ROS2 with ros1_bridge

ROS2 Tutorials #8: How to communicate between ROS1 & ROS2 with ros1_bridge

In this post, you will learn how to communicate between ROS1 & ROS2 with ros1_bridge. You will see how to set up the ros1_bridge package in order to send ROS2 commands to control a TurtleBot2 simulation running in ROS1 Melodic.

Step 1: Grab a copy of the Project (ROSject) on ROSDS

Click here to get your copy of the project used in this post.

Step 2: Launch the TurtleBot2 simulation from the Simulations menu

Launch a TB2 Simulation

Now you should see a Simulation window pop up with the TurtleBot2 simulation.

TurtleBot2 Simulation

Step 2: Source the workspace for ROS1 Melodic

Pick a Shell (terminal tool) from the Tools menu and run the following command:

user:~$ source /opt/ros/melodic/setup.bash
user:~$

Step 3: Source the workspace for ROS2 Dashing

In the same Shell used in Step 2, run the following command:

user:~$ source /opt/ros/dashing/local_setup.bash
ROS_DISTRO was set to 'melodic' before. Please make sure that theenvironment does not mix paths from different distributions.
user:~$

Step 3: Ensure ROS_MASTER_URI is pointing in the right direction:

Still on the same shell, run the following:

user:~$ echo $ROS_MASTER_URI
http://master:11311
user:~$ export ROS_MASTER_URI=http://master:11311
user:~$

Step 4: Communicate between ROS1 & ROS2 with ros1_bridge: start the ros_bridge

Now, start the ros1_bridge and put this terminal on one side – we’re done with it for now.

user:~$ ros2 run ros1_bridge dynamic_bridge
Created 2 to 1 bridge for service /gazebo/clear_body_wrenches
Created 2 to 1 bridge for service /gazebo/clear_joint_forces
Created 2 to 1 bridge for service /gazebo/delete_light
Created 2 to 1 bridge for service /gazebo/delete_model
Created 2 to 1 bridge for service /gazebo/get_joint_properties
Created 2 to 1 bridge for service /gazebo/get_light_properties
Created 2 to 1 bridge for service /gazebo/get_link_properties
...

Step 5: Control the ROS1 TurtleBot2 Simulation from ROS2

Fire up another Shell from the Tools menu and publish to /cmd_vel.

user:~$ source /opt/ros/dashing/local_setup.bash
ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~$ ros2 topic pub /cmd_vel geometry_msgs/Twist "linear:
  x: 0.5
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 0.0"
publisher: beginning loop
publishing #1: geometry_msgs.msg.Twist(linear=geometry_msgs.msg.Vector3(x=0.5, y=0.0, z=0.0), angular=geometry_msgs.msg.Vector3(x=0.0, y=0.0, z=0.0))

In the shell where you’re running the ros1_bridge, you should see messages similar to the following:

created 1to2 bridge for topic '/rosout' with ROS 1 type 'rosgraph_msgs/Log' and ROS 2 type 'rcl_interfaces/msg/Log'
created 2to1 bridge for topic '/cmd_vel' with ROS 2 type 'geometry_msgs/msg/Twist' and ROS 1 type ''
created 2to1 bridge for topic '/rosout' with ROS 2 type 'rcl_interfaces/msg/Log' and ROS 1 type 'rosgraph_msgs/Log'
[INFO] [ros_bridge]: Passing message from ROS 2 geometry_msgs/msg/Twist to ROS 1 geometry_msgs/Twist (showing msg only once per type)

And, of course, you should see that your robot started to move!

And that was it 😉 . We just controlled a simulation started in ROS1 with ROS2, using the ros1_bridge.

Extra: Video

Prefer to watch a video demonstrating the steps above? We have one for you below! PS: the video uses another simulation, a Parrot Drone, but the concepts are the same.

Related Resources

Feedback

Did you like this post? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS or ROS2 topics, please let us know in the comments area and we will do a video or post about it 🙂

Post edited by Bayode Aderinola

#ROS2 #ROS #ROS2tutorials #Robot

ROS2 Tutorials #6: How to create a ROS2 launch file [NEW]

ROS2 Tutorials #6: How to create a ROS2 launch file [NEW]

About

In this post, you will learn how to create a ROS2 launch file.

The problem: we have a ROS2 C++ package but it has no launch file. Yes, we can run the code of this package using ros2 run..., but we also want to be able to run it using ros2 launch...

Let’s get to work right away and solve this problem!

Step 1: Get the package without a launch file

Remember we have a package…get a copy of the ROSject containing this package using this link: http://www.rosject.io/l/bce4ffd/.

If you would like to learn how we created this ROSject and package, please see this post: https://www.theconstruct.ai/ros2-tutorials-create-a-ros2-package-cpp/.

Step 2: Recompile the package

Open the ROSject. This might take a few moments, please be patient.

Once the ROSject opens, fire up a Shell from the Tools menu and run the following commands to recompile the package.

user:~$ source /opt/ros/crystal/setup.bash
ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~$ cd ros2_ws
user:~/ros2_ws$ colcon build --symlink-install
Starting >>> ros2_cpp_pkg
 
Finished <<< ros2_cpp_pkg [21.9s]
 
Summary: 1 package finished [22.0s]
user:~/ros2_ws$ source install/setup.bash # source the workspace
ROS_DISTRO was set to 'crystal' before. Please make sure that the environment does not mix paths from different distributions. 
ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~/ros2_ws$

Step 3: Create a ROS2 launch file for this package

Create a launch directory within the package and create a launch file within the directory.

On the Shell, run:

user:~/ros2_ws$ cd src/ros2_cpp_pkg/
user:~/ros2_ws/src/ros2_cpp_pkg$ mkdir launch
user:~/ros2_ws/src/ros2_cpp_pkg$ cd launch && touch ros2_cpp_code.launch.py
user:~/ros2_ws/src/ros2_cpp_pkg$

Fire up an IDE from the Tools menu, find the launch file just created and paste the following code into it:

"""Launch the cpp_code executable in this package"""

from launch import LaunchDescription
import launch_ros.actions


def generate_launch_description():
    return LaunchDescription([
        launch_ros.actions.Node(
            # the name of the executable is set in CMakeLists.txt, towards the end of
            # the file, in add_executable(...) and the directives following it
            package='ros2_cpp_pkg', node_executable='cpp_code', output='screen'),
    ])

Now, we need to tell ROS to recognize the launch file. Open up  CMakeLists.txt in the IDE and add the following lines at the bottom of it.

# install the launch directory
install(DIRECTORY
  launch
  DESTINATION share/${PROJECT_NAME}/
)

Don’t forget to save all the changes!!

Step 4: Compile and source the workspace again.

Again? Yes, again! We need to.

user:~/ros2_ws/src/ros2_cpp_pkg/launch$ cd /home/user/ros2_ws
user:~/ros2_ws$ colcon build --symlink-install
Starting >>> ros2_cpp_pkg
Finished <<< ros2_cpp_pkg [3.35s]

Summary: 1 package finished [3.48s]
user:~/ros2_ws$ source install/setup.bash # source the workspace
user:~/ros2_ws$

Step 5: Launch the package using the new launch file and be happy!

You’ve been working very hard, time to eat the fruit of your labor!

The format for the command is ros2 launch <package_name> <launch_file_name>.

We are launching the C++ code in src/ros2_cpp_code.cpp, and the output should be something like this:

user:~/ros2_ws$ ros2 launch ros2_cpp_pkg ros2_cpp_code.launch.py
[INFO] [launch]: process[cpp_code-1]: started with pid [13071]
[INFO] [ObiWan]: Help me Obi-Wan Kenobi, you're my only hope
[INFO] [launch]: process[cpp_code-1]: process has finished cleanly
user:~/ros2_ws$

Sweet! Done!!

Extra 1: ROSject link

Get the ROSject containing all code used in the post in the following link: http://www.rosject.io/l/bcf8985/.

Extra 2: ROS2 Full Course for Beginners

Extra 3: Video

Prefer to watch a video demonstrating the steps above? We have one for you below!

Related Resources

ROS2 Basics

Feedback

Did you like this post? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS topics, please let us know in the comments area and we will do a video/post about it 🙂

ROS2 Tutorials #5: How to create a ROS2 Package for Python

ROS2 Tutorials #5: How to create a ROS2 Package for Python

About

In this post, you will learn how to create a simple ROS2 package for Python. You don’t need a ROS2 installation for this as we will use the ROS Development Studio (ROSDS), an online platform that provides access to ROS (1 or 2) computers and other powerful ROS tools within a browser!

PS: If you have ROS2 installed locally on your machine, you can skip Step 1.

Step 1: Create a Project (ROSject) on ROSDS

Head to http://rosds.online and create a project with a similar configuration as the one shown below. You can change the details as you like, but please ensure you select “Ubuntu 18.04 + ROS2 Crystal” under “Configuration”.

Once done with that, open your ROSject. This might take a few moments, please be patient.

Step 2: Source the ROS2 workspace

Once the ROSject is open, head to the Tools menu and pick the Shell tool (if on your local PC just fire up a terminal) and run the following command to source the workspace:

user:~$ source /opt/ros/crystal/setup.bash
ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~$

If you get that ROS_DISTRO warning, just ignore it.

Step 3: Create a ROS2 Python package in your ROS2 workspace

The syntax for creating a ROS2 Python package is ros2 pkg create <package_name>.

In the same terminal as in Step 2, change to your ROS2 workspace src directory and create a package there:

user:~$ cd ros2_ws/src
user:~/ros2_ws/src$ ros2 pkg create ros2_demo_py
going to create a new package
package name: ros2_demo_py
destination directory: /home/user/ros2_ws/src
package format: 2
version: 0.0.0
description: TODO: Package description
maintainer: ['user <user@todo.todo>']
licenses: ['TODO: License declaration']
build type: ament_cmake
dependencies: []
creating folder ./ros2_demo_py
creating ./ros2_demo_py/package.xml
creating source and include folder
creating folder ./ros2_demo_py/src
creating folder ./ros2_demo_py/include/ros2_demo_py
creating ./ros2_demo_py/CMakeLists.txt
user:~/ros2_ws/src$

Step 4: Delete CMakeLists.txt , create setup.py and setup.cfg and edit package.xml

Unlike ROS1, ROS2 Python packages don’t use CMakeList.txt, but a new setup.py file. Let’s create one here. In the Shell:

user:~/ros2_ws/src$ cd ros2_demo_py/
user:~/ros2_ws/src/ros2_demo_py$ rm CMakeLists.txt
user:~/ros2_ws/src/ros2_demo_py$ touch setup.py
user:~/ros2_ws/src/ros2_demo_py$ touch setup.cfg
user:~/ros2_ws/src/ros2_demo_py$

Fire up the IDE, locate the setup.py file and paste in the following code:

from setuptools import setup

package_name = 'ros2_demo_py'

setup(
    name=package_name,
    version='0.7.0',
    packages=[package_name],
    install_requires=['setuptools'],
    zip_safe=True,
    author='You',
    author_email='you@youremail.com',
    maintainer='YourFirstname Lastname',
    maintainer_email='your@youremail.com',
    keywords=['ROS'],
    classifiers=[
        'Intended Audience :: Developers',
        'License :: OSI Approved :: Apache Software License',
        'Programming Language :: Python',
        'Topic :: Software Development',
    ],
    description='A simple ROS2 Python package',
    license='Apache License, Version 2.0',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'demo = ros2_demo_py.demo:main'
        ],
    },
)

Locate the setup.cfg file and paste in the following code:

[develop]
script-dir=$base/lib/ros2_demo_py
[install]
install-scripts=$base/lib/ros2_demo_py

Locate package.xml and replace the contents with this snippet:

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="2">
  <name>ros2_demo_py</name>
  <version>0.7.3</version>
  <description>A simple ROS2 Python package</description>

  <maintainer email="sloretz@openrobotics.org">Shane Loretz</maintainer>
  <license>Apache License 2.0</license>

  <exec_depend>rclpy</exec_depend>
  <exec_depend>std_msgs</exec_depend>

  <!-- These test dependencies are optional
  Their purpose is to make sure that the code passes the linters -->
  <test_depend>ament_copyright</test_depend>
  <test_depend>ament_flake8</test_depend>
  <test_depend>ament_pep257</test_depend>
  <test_depend>python3-pytest</test_depend>

  <export>
    <build_type>ament_python</build_type>
  </export>
</package>

Step 5: Create the Python code

In the Shell, run the following commands:

user:~/ros2_ws/src/ros2_demo_py$ mkdir ros2_demo_py && cd ros2_demo_py
user:~/ros2_ws/src/ros2_demo_py/ros2_demo_py$ touch demo.py
user:~/ros2_ws/src/ros2_demo_py/ros2_demo_py$ touch __init__.py
user:~/ros2_ws/src/ros2_demo_py/ros2_demo_py$

We created the __init__.py file so that the ros2_demo_py folder could be recognized as a python module directory.

Locate the demo.py file, open it in the IDE and paste in the following code:

# Copyright 2016 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import rclpy
from rclpy.node import Node

from std_msgs.msg import String


class MinimalPublisher(Node):

    def __init__(self):
        super().__init__('minimal_publisher')
        self.publisher_ = self.create_publisher(String, 'topic')
        timer_period = 0.5  # seconds
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.i = 0

    def timer_callback(self):
        msg = String()
        msg.data = 'Hello World: %d' % self.i
        self.publisher_.publish(msg)
        self.get_logger().info('Publishing: "%s"' % msg.data)
        self.i += 1


def main(args=None):
    rclpy.init(args=args)

    minimal_publisher = MinimalPublisher()

    rclpy.spin(minimal_publisher)

    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    minimal_publisher.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

Step 6: Compile the package and source the workspace

We have created all the files needed. Now let’s compile.

user:~/ros2_ws/src/ros2_demo_py/ros2_demo_py$ cd /home/user/ros2_ws/
user:~/ros2_ws$ colcon build --symlink-install

Starting >>> ros2_demo_py
Finished <<< ros2_demo_py [0.78s]

Summary: 1 package finished [0.90s]
user:~/ros2_ws$ source install/setup.bash # source the workspace
user:~/ros2_ws$

Step 7: Test-run the package

Here comes the moment of truth…will it run?

user:~/ros2_ws$ ros2 run ros2_demo_py demo
[INFO] [minimal_publisher]: Publishing: "Hello World: 0"
[INFO] [minimal_publisher]: Publishing: "Hello World: 1"
[INFO] [minimal_publisher]: Publishing: "Hello World: 2"
[INFO] [minimal_publisher]: Publishing: "Hello World: 3"
[INFO] [minimal_publisher]: Publishing: "Hello World: 4"
...

Great, it ran! We’re done here!!

Extra 1: ROSject link

Get the ROSject containing all code used in the post in the following link: http://www.rosject.io/l/bd0cfbd/

Extra 2: Video

Prefer to watch a video demonstrating the steps above? We have one for you below!

Related Resources

Feedback

Did you like this post? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS topics, please let us know in the comments area and we will do a video or post about it ?

 

Code edited by Bayode Aderinola

ROS2 Tutorials #4: How to create a ROS2 Package for C++ [NEW]

ROS2 Tutorials #4: How to create a ROS2 Package for C++ [NEW]

About

In this post, you will learn how to create a simple ROS2 package for C++. You don’t need a ROS2 installation for this, as we will use the ROS Development Studio (ROSDS), an online platform that provides access to ROS (1 or 2) computers and other powerful ROS tools within a browser!

PS: If you have ROS2 installed locally on your machine, you can skip Step 1.

Step 1: Create a Project (ROSject) on ROSDS

Head to http://rosds.online and create a project with a similar configuration as the one shown below. You can change the details as you like, but please ensure you select “Ubuntu 18.04 + ROS2 Crystal” under “Configuration”.

Once done with that, open your ROSject. This might take a few moments, please be patient.

Step 2: Source the ROS2 workspace

Once the ROSject is open, head to the Tools menu and pick the Shell tool (if on your local PC just fire up a terminal) and run the following command to source the workspace:

user:~$ source /opt/ros/crystal/setup.bash
ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~$

If you get that ROS_DISTRO warning, just ignore it.

Step 3: Create a ROS2 package in your ROS2 workspace

The syntax for creating a ROS2 package is ros2 pkg create <package_name> --build-type <build_type> --dependencies <dependencies_separated_by_single_space>.

In the same terminal as in Step 2, change to your ROS2 workspace src directory and create a package there:

user:~$ cd ros2_ws/src
user:~/ros2_ws/src$ ros2 pkg create ros2_cpp_pkg --build-type ament_cmake --dependencies rclcpp
going to create a new package
package name: ros2_cpp_pkg
destination directory: /home/user/ros2_ws/src
package format: 2
version: 0.0.0
description: TODO: Package description
maintainer: ['user <user@todo.todo>']
licenses: ['TODO: License declaration']
build type: ament_cmake
dependencies: ['rclcpp']
creating folder ./ros2_cpp_pkg
creating ./ros2_cpp_pkg/package.xml
creating source and include folder
creating folder ./ros2_cpp_pkg/src
creating folder ./ros2_cpp_pkg/include/ros2_cpp_pkg
creating ./ros2_cpp_pkg/CMakeLists.txt
user:~/ros2_ws/src$

Step 4: Create C++ code inside the package

Create a file named ros2_cpp_code.cpp inside the new package:

user:~/ros2_ws/src$ cd ros2_cpp_pkg/src
user:~/ros2_ws/src/ros2_cpp_pkg/src$ touch ros2_cpp_code.cpp
user:~/ros2_ws/src/ros2_cpp_pkg/src$

Pick the IDE from the Tools menu, locate the C++ file and paste in the following code:

#include "rclcpp/rclcpp.hpp"

int main(int argc, char *argv[]) {
  rclcpp::init(argc, argv);
  auto node = rclcpp::Node::make_shared("ObiWan");

  RCLCPP_INFO(node->get_logger(),
              "Help me Obi-Wan Kenobi, you're my only hope");

  rclcpp::shutdown();
  return 0;
}

In the IDE, open up the CMakeLists.txt and paste the following line at the bottom of the file. We are doing this to ensure the C++ file will be properly detected and compiled.

add_executable(cpp_code src/ros2_cpp_code.cpp)
ament_target_dependencies(cpp_code rclcpp)

install(TARGETS
    cpp_code
    DESTINATION lib/${PROJECT_NAME}
)

Step 5: Compile and test-run the package

Now your package is ready to compile…so compile it and source the workspace!

user:~/ros2_ws/src/ros2_cpp_pkg/src$ cd /home/user/ros2_ws
user:~/ros2_ws$ colcon build --symlink-install
Starting >>> ros2_cpp_pkg

Finished <<< ros2_cpp_pkg [21.9s]

Summary: 1 package finished [22.0s]
user:~/ros2_ws$ source install/setup.bash # source the workspace
ROS_DISTRO was set to 'crystal' before. Please make sure that the environment does not mix paths from different distributions. ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~/ros2_ws$

Great. Now test-run it.  You should have the string mentioned in the C++ code printed to the terminal output:

user:~/ros2_ws$ ros2 run ros2_cpp_pkg cpp_code
[INFO] [ObiWan]: Help me Obi-Wan Kenobi, you're my only hope
user:~/ros2_ws$

Done!

Extra 1: ROSject link

Get the ROSject containing all code used in the post in the following link: http://www.rosject.io/l/bce4ffd/!

Extra 2: Video

Prefer to watch a video demonstrating the steps above? We have one for you below!

Related Resources

Feedback

Did you like this post? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS topics, please let us know in the comments area and we will do a video or post about it 🙂

Pin It on Pinterest