Ros Note
Ros Note
md 2024-08-20
Table of Content
1. Introduction
2. ROS Package Structure
3. Introduction to Build Tool: Catkin
4. Creating ROS Packages
Creating From Scratch
Installing Pre-existing Packages
From GitHub
From Apt
5. Using ROS Packages
6. Creating Launch Files
7. Resources
Introduction
Software in ROS is organized in packages. A package might contain ROS nodes, a ROS-independent library,
a dataset, configuration files, a third-party piece of software, or anything else that logically constitutes a
useful module. The goal of these packages it to provide this useful functionality in an easy-to-consume
manner so that software can be easily reused. In general, ROS packages follow a "Goldilocks" principle:
enough functionality to be useful, but not too much that the package is heavyweight and difficult to use
from other software.
package.xml: Contains metadata for the package (e.g., package name, maintainer's information,
dependencies).
msg: Contains custom ROS message types for that package (if created).
1/8
Lecture-4.md 2024-08-20
config: Contains necessary config(.yaml) files for the package (if needed).
urdf: Contains robot description files that contain information for constructing a 3D visualization of
a robot using STL files (if created).
meshes: Contains the necessary STL files needed to construct a 3D visualization of a robot (if
created).
scripts: Contains executable python files for the nodes designed in Python.
my_package/
├── CMakeLists.txt
├── package.xml
├── src/
│ └── my_package_node.cpp
├── include/
│ └── my_package/
│ └── my_package_node.h
├── launch/
│ └── my_package.launch
├── msg/
│ └── MyMessage.msg
├── srv/
│ └── MyService.srv
├── action/
│ └── MyAction.action
├── config/
│ └── my_package.yaml
├── urdf/
│ └── my_robot.urdf
├── meshes/
│ └── my_robot_part.stl
├── scripts/
│ └── my_script.py
└── README.md
2/8
Lecture-4.md 2024-08-20
CMake is a popular build system that is widely used for compiling C++ code. However, ROS supports
multiple language support, mainly C++ and Python. To compile executable code from multiple files written
in multiple languages and seamlessly use them together, ROS uses a custom build tool called Catkin.
Catkin combines CMake macros and Python scripts to provide some functionality on top of CMake's normal
workflow. Catkin was designed for better distribution of packages, better cross-compiling support, and
better portability.
$ cd ~/
$ mkdir tutorial_ws
$ cd tutorial_ws
$ mkdir src
$ catkin_make
Note: You have to run catkin_make at the workspace folder every time you add or create a new
package. To streamline this process and better manage the packages, we can use catkin_tools
commands instead of running catkin_make. To install catkin_tools, follow the instructions here.
Then, you can run the following commands:
$ cd ~/
$ mkdir tutorial_ws
$ cd tutorial_ws
$ mkdir src
$ catkin build
Note: You should use either catkin_make or catkin build for building packages, not both.
To create a ROS package from scratch, run the catkin_create_pkg command on the terminal at the
tutorial_ws/src directory. The syntax for the command is as follows:
Here, the package_name is the name you want to give to your package, and dependency1,... are the
packages that will be used in your package as dependencies.
3/8
Lecture-4.md 2024-08-20
Now you should see your package being built, and the package directory should look something like this:
tutorial_ws
|--- build
|--- devel
|--- src
|--- tutorial_pkg
|--- CMakeLists.txt
|--- package.xml
|--- src
If you decide to add/remove dependencies to your package, you will need to update your package.xml
file. Add the dependencies as <depend> and <exec_depend> in the package.xml file. For Python nodes,
you will need to add that dependency as <exec_depend>. For example:
For C++ nodes, you will need to add dependencies as <build_depend> and/or <exec_depend> based on
what type of dependency that package is.
There are many useful ROS packages out there that can be installed into a workspace. We will be covering
how you can install an external ROS package using GitHub and Apt.
From GitHub
All you have to do is clone the package repository into the src forder of your workspace, and run
catkin_make as usual to build the package.
4/8
Lecture-4.md 2024-08-20
$ cd ..
$ catkin_make
Note: Generally, the ROS packages are organized in Git branches according to the ROS versions. If
you're installing from the official ROS Github repo (https://github.com/ros), make sure to check out
the branch that contains the ROS version your device is running. If you're checking out other
repositories, make sure to download the package folder and paste the folder in the src folder of
your workspace and build the package.
From Apt
For the purpose of this lecture, we are going to install the Turtlesim package by running the command:
Once that's done, we need to source the setup.bash file of the workspace in order to use the nodes in
the package. We need to repeat this process every time we switch to a new terminal.
$ cd ~/tutorial_ws
$ source devel/setup.bash
We also need to source the setup.bash file for ROS every time we switch to a new terminal.
5/8
Lecture-4.md 2024-08-20
Note: Sourcing ROS every time for a new terminal is a cumbersome process. To avoid doing that, we
can write this command in the ~/.bashrc file, so that every time a new terminal is created, the
sourcing is done automatically.
Open the ~/.bashrc file with a text editor and write down the command in the file.
Save the file, and now every time a terminal opens, the setup.bash file for ROS will already be
sourced.
Once all the sourcing is done, we can use rosrun command to run a node from the sourced packages.
For C++ nodes, we do not need to mention the extension of file name when running a node.
We can also check if a certain package exists that can be used using the rospack find command.
If the package exists and is usable, you should see the directory of the package. If the package is not found,
the package either does not exist, or it is not sourced.
<?xml version="1.0"?>
<launch>
<!-- "arg": used for setting variables within launch files -->
<arg name="my_arg" default="my_value"/>
<arg name="my_other_arg" default="true"/>
6/8
Lecture-4.md 2024-08-20
<!-- "include": lets us run other launch files within a launch file
-->
<include file="$(find
ros_package)/launch/some_other_launch_file.launch">
<!-- args can be passed into nodes and launch files -->
<arg name="my_arg" value="some_value"/>
</include>
</group>
</launch>
7/8
Lecture-4.md 2024-08-20
Save the launch file as <filename>.launch. Launch files must be put in the launch folder in your
package.
After doing that, you have to source your terminal, and use roslaunch command to run a launch file.
$ cd ~/tutorial_ws
$ source devel/setup.bash
$ roslaunch tutorial_pkg <filename>.launch
Resources
GitHub repository for the package built in the tutorial: https://github.com/shawbicc/tutorial_pkg.git
Follow along the recorded lecture on ROS Packages for hands-on tutorials and examples on how to create
ROS packages and launch files. Be sure to try the codes yourself. Some of the files used in this tutorial are
already provided in a GitHub repository. It is highly encouraged to get used to the GitHub workflow and use
that with your projects as well.
8/8