ROS 2

General Tips:

You save yourself soo much trouble just working with Ubuntu and not in some shell or trying to make it work on another OS. It’s possible but I ran into a lot of trouble that I fixed by switching to Ubuntu.

ROS2_DOMAIN_ID Set to be able to communicate between Ros2 packages on a subdomain that has multiple ROS2 packages communicating without them interfering with each other.

Install ROS2:

Depending on the Ubuntu version:

ROS 2 Humble Hawksbill : Ubuntu 22.04

ROS 2 Galactic : Ubuntu 20.04

ROS 2 Foxy : Ubuntu 20.04

ROS 2 Dashing : Ubuntu 18.04

Make sure you do all the steps in the above link before continuing.

Check if everything went well by running:

$ source /opt/ros/humble/setup.bash
$ ros2

This should return the help overview of Ros2.

On Opening a new terminal, before running ROS2 or any packages make sure you run the setup file:

$ source /opt/ros/humble/setup.bash

Make sure the setup.{shell} is your current shell. To check what shell you use run the following:

$ echo $SHELL

Whenever you run into:

ros2: command not found

Make sure you run the setup file.

$ source /opt/ros/humble/setup.bash

If you work a lot with Ros2, it’s a good idea to put it into your ~/.bashrc file

How to create a new ROS2 package

Example

Install colcon:

$ sudo sh -c 'echo "deb [arch=amd64,arm64] http://repo.ros2.org/ubuntu/main `lsb_release -cs` main" > /etc/apt/sources.list.d/ros2-latest.list'
$ curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -
$ sudo apt update
$ sudo apt install python3-colcon-common-extensions

To create a new package initially:

$ ros2 pkg create --build-type ament_python [name]

This will create a lot of files for you initially.

You write your custom python code; classes deriving from the Node parent class:

class MinimalPublisher(Node):

Add dependencies to the package.xml

Add entry point into the setup.py:

entry_points={ 'console_scripts': [ 'talker = py_pubsub.publisher_member_function:main', ], },

And run the Node depending on the entry points:

$ ros2 run py_pubsub talker