Robotics

ROS

What is ROS?

ROS (Robot Operating System) is a flexible framework for writing robot software. It provides tools, libraries, and conventions that simplify the task of creating complex and robust robot behavior across a wide variety of robotic platforms.


When to use ROS?

  • Actuator
  • Controller
  • Sensor

Installation

ROS 2 VersionOperating System
ROS 2 Jazzy JaliscoUbuntu Linux 24.04, Windows 10
ROS 2 Humble HawksbillUbuntu Linux 22.04, Windows 10

Set Locale

$ locale  # check for UTF-8

$ sudo apt update && sudo apt install locales
$ sudo locale-gen en_US en_US.UTF-8
$ sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
$ export LANG=en_US.UTF-8

$ locale  # verify settings

Setup Sources

You will need to add the ROS 2 apt repository to your system.

First ensure that the Ubuntu Universe repository is enabled.

$ sudo apt install software-properties-common
$ sudo add-apt-repository universe

Now add the ROS 2 GPG key with apt.

$ sudo apt update && sudo apt install curl -y
$ sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key \
-o /usr/share/keyrings/ros-archive-keyring.gpg

Then add the repository to your sources list.

$ echo "deb [arch=$(dpkg --print-architecture) \
signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] \
http://packages.ros.org/ros2/ubuntu $(. \
/etc/os-release && echo $UBUNTU_CODENAME) main" \
| sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null

Install ROS 2

First of all, no matter which ROS 2 version you need, run this command first:

$ sudo apt update
$ sudo apt upgrade

Development tools: Compilers and other tools to build ROS packages

$ sudo apt install -y ros-dev-tools

ROS 2 Jazzy for Ubuntu 24.04

Desktop Install (Recommended): ROS, RViz, demos, tutorials.

$ sudo apt install -y ros-jazzy-desktop
# or
$ sudo apt install -y ros-jazzy-desktop-full

ROS 2 Humble for Ubuntu 22.04

$ sudo apt install -y ros-humble-desktop
# or
$ sudo apt install -y ros-humble-desktop-full

ROS-Base Install (Bare Bones): Communication libraries, message packages, command line tools. No GUI tools.

ROS 2 Jazzy for Ubuntu 24.04

$ sudo apt install -y ros-jazzy-ros-base

ROS 2 Humble for Ubuntu 22.04

$ sudo apt install -y ros-humble-ros-base

Uninstall

$ sudo apt remove ~nros-* && sudo apt autoremove

CLI Commands

If you need get a specific command help, for example:

$ ros2 pkg create -h

ROS 2 has a comprehensive set of command-line tools accessible through the ros2 command. Here's a list of the main ROS 2 commands:

Core Commands:

  • ros2 pkg: Package management commands
  • ros2 node: Commands for ROS 2 nodes
  • ros2 topic: Commands for ROS 2 topics
  • ros2 service: Commands for ROS 2 services
  • ros2 action: Commands for ROS 2 actions
  • ros2 interface: Commands for ROS 2 interfaces
  • ros2 param: Commands for ROS 2 parameters
  • ros2 launch: Launch ROS 2 nodes from launch files
  • ros2 run: Run ROS 2 executables
  • ros2 daemon: ROS 2 daemon commands
  • ros2 component: Commands for ROS 2 components
  • ros2 lifecycle: Commands for lifecycle nodes

Development Commands:

  • ros2 doctor: Check ROS 2 setup and identify issues
  • ros2 wtf: Alternative name for doctor command
  • ros2 msg: Commands for ROS 2 message definitions
  • ros2 srv: Commands for ROS 2 service definitions
  • ros2 test: Run ROS 2 tests
  • ros2 bag: Commands for ROS 2 bags (data recording/playback)

Build System:

  • ros2 pkg create: Create a new ROS 2 package
  • ros2 pkg executables: List package executables
  • ros2 pkg list: List installed packages
  • ros2 pkg prefix: Output the prefix path of a package
  • ros2 pkg xml: Output the XML of a package

Examples

If you installed ros-humble-desktop above you can try some examples. In one terminal, source the setup file and then run a C++ talker:

$ source /opt/ros/humble/setup.bash
$ ros2 run demo_nodes_cpp talker

In another terminal source the setup file and then run a Python listener:

$ source /opt/ros/humble/setup.bash
$ ros2 run demo_nodes_py listener

You should see the talker saying that it’s Publishing messages and the listener saying I heard those messages. This verifies both the C++ and Python APIs are working properly. Hooray!


Environment Setup

Source the Setup Script

You will need to run this command on every new shell you open to have access to the ROS 2 commands, like so:

$ source /opt/ros/humble/setup.bash
#or
$ source /opt/ros/jazzy/setup.bash

If you don’t want to have to source the setup file every time you open a new shell (skipping task 1), then you can add the command to your shell startup script:

$ echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc

Check environment:

$ printenv | grep -i ROS

URDF

To view URDF files in ROS 2, you'll need to install the following packages:

Option 1: Install the urdf_tutorial package

$ sudo apt install ros-humble-urdf-tutorial -y
$ ros2 launch urdf_tutorial display.launch.py model:=/path/to/robot.urdf

Option 2: Launch manually without urdf_tutorial

If you don't want to install urdf_tutorial, you can start the nodes manually:

# Terminal 1 - Robot state publisher with your URDF
$ ros2 run robot_state_publisher robot_state_publisher --ros-args -p robot_description:="$(cat /path/to/robot.urdf)"

# Terminal 2 - Joint state publisher GUI (for interactive joints)
$ ros2 run joint_state_publisher_gui joint_state_publisher_gui

# Terminal 3 - RViz for visualization
$ ros2 run rviz2 rviz2

In RViz:

  • Add a RobotModel display
  • Set the Robot Description topic to /robot_description
  • Set the fixed frame to your robot's base frame (often base_link)

Option 3: Create your own simple launch file

from launch import LaunchDescription
from launch_ros.actions import Node
from launch.substitutions import Command
import os

def generate_launch_description():
    urdf_file = '/path/to/robot.urdf'
    
    return LaunchDescription([
        Node(
            package='robot_state_publisher',
            executable='robot_state_publisher',
            parameters=[{'robot_description': Command(['cat ', urdf_file])}]
        ),
        Node(
            package='joint_state_publisher_gui',
            executable='joint_state_publisher_gui'
        ),
        Node(
            package='rviz2',
            executable='rviz2'
        )
    ])

Then launch with:

$ ros2 launch view_urdf.launch.py

Troubleshooting

curl: (7) Failed to connect to raw.githubusercontent.com port 443 after 11 ms: Connection refused

$ sudo nano /etc/systemd/resolved.conf
[Resolve]
DNS=9.9.9.9 8.8.8.8
FallbackDNS=1.1.1.1
$ sudo systemctl restart systemd-resolved
Previous
Poppy Humanoid