Skip to content

Latest commit

 

History

History
220 lines (142 loc) · 9.09 KB

File metadata and controls

220 lines (142 loc) · 9.09 KB

Tutorial 1: Creating Workspace and Packages

This tutorial explains how to create your workspace and create packages inside a workspace.

Table of contents

Foreword

Before getting started with this, it is assumed that you have a working ROS installation and have gone through the following files

You may check if you have correctly sourced the ROS installation using

echo $ROS_DISTRO

And if everything is right, you must see noetic (or whatever distro you installed).

Workspace

As explained earlier, a workspace is a directory on your system which contains all the ROS related, user developed content. This repository is created as a workspace on a local machine and pushed to the cloud.

More about workspaces here

To create your own workspace, follow the following instructions

  1. Create a folder and a src folder inside it

    This can be done using the mkdir command

    mkdir -p ~/ros_workspaces/learning_ws/src

    I prefer making a parent folder which contains different workspaces (sometimes I feel the need to work on completely different projects in ROS which need to be separate). The ros_workspaces folder acts as this directory. In roswiki tutorials, you may find that instead of ros_workspaces/learning_ws the workspace folder is mentioned as catkin_ws. You can use whatever name you want to, just make sure you remember it.

  2. Go to your workspace folder

    For us, the workspace folder is ros_workspaces/learning_ws. It will be whatever you chose in step 1. You need to cd into it.

    cd ~/ros_workspaces/learning_ws

    For the rest of this repository, it is assumed that unless mentioned otherwise, you're running all commands from this folder.

  3. Build your workspace for the first time

    After navigating into the workspace folder, run the catkin_make command

    catkin_make

    This command will build all the packages inside an existing workspace. However, if an empty workspace is used, it will create the files necessary for the workspace.

roswiki on Creating a ROS workspace using Catkin here

After build

After the first build, a few things can be noted

  1. In the src folder inside your workspace, a file called CMakeLists.txt would have appeared. This file is actually a soft copy of a template build file. Under no ordinary circumstances, you are to modify this file. All the build configurations that packages require are done through CMakeLists.txt file inside the package folders and not this one.

  2. Two new folders named build and devel would have formed.

    1. The build folder is used by CMake and catkin to keep their cache information. This folder aids in building everything in the package.

      More about the build space here

    2. The devel folder is used to store built targets (executables, library headers, etc.). This is useful for ROS to locate them.

      More about devel space here

    • In case you want to rebuild everything from ground up, you can delete the build and devel folders (you may use rm -r) and then again run catkin_make in the workspace folder.
  3. Source your workspace: Inside the devel folder, there must be files named setup.*. In order for ROS to know where the workspace is, you need to source the workspace. Without this, packages inside your workspace shall not be discoverable.

    To confirm this, you may first run

    echo $ROS_PACKAGE_PATH

    If you do not have any other workspace sourced, you must see only /opt/ros/noetic/share in the output (installation path). Let us source the workspace that we just created

    source ~/ros_workspaces/learning_ws/devel/setup.bash

    And now run

    echo $ROS_PACKAGE_PATH

    Now, in the output, we can also see the src folder of the workspace that we created. If you use the default bash shell, then you must source setup.bash. If you're using another shell, say zsh, sourcing setup.zsh may be a better idea.

    • The above source command will have to be executed every time you open a new terminal. Otherwise, the $ROS_PACKAGE_PATH variable would not be updated. Therefore, it is a good idea to have the source line in the ~/.bashrc file, so that every time you open a new terminal, the workspace is sourced also (similar to what you may have done by sourcing the ROS installation file when installing ROS).

      Run the following command

      echo "source ~/ros_workspaces/learning_ws/devel/setup.bash" >> ~/.bashrc

      Note that you may use ~/.bashrc if you use bash. If you're using, say zsh, you might want to use the command

      echo "source ~/ros_workspaces/learning_ws/devel/setup.zsh" >> ~/.zshrc

Packages

A package is used to contain all the things made for a particular purpose inside a single folder. These folders are stored inside the src folder of the workspace.

More about packages here

To create your own package, you must first have the following information in mind

  • Package name: An appropriate name for your package. For the purpose of this tutorial, let it be cpp_basic_nodes.
  • Dependencies: They are other packages and libraries on which your package depends to build and run everything inside it. You can even add or remove them after creating the package. However, it's better to have the known ones set up with creation. For this tutorial, we'll use the following dependencies
    • roscpp: Since this is a C++ package, it uses the C++ client library.

To create a package, follow these steps

  1. Make sure that you are in the src folder of your workspace and that the workspace and ROS is properly sourced.

    cd ~/ros_workspaces/learning_ws/src
  2. Run the following command to create a package using catkin_create_pkg

    catkin_create_pkg cpp_basic_nodes roscpp

This must have create a folder named cpp_basic_nodes and the folder must have

  • CMakeLists.txt file: This is to manage how things in the package are built

    More about the CMakeLists.txt file here

  • package.xml file: For managing the manifesto and information about the package

    More about the package.xml file here

Building

After the package is created, you might want to build it (even if its empty, just to see if everything is fine). To do that, first cd into the workspace folder

cd ~/ros_workspaces/learning_ws/

Now run the following

catkin_make

You must see a message in the output mentioning that your package cpp_basic_nodes was found (under traversing N packages in topological order).

To selectively build packages, you may use the --pkg tag with catkin_make.

Tools

Some tools that are useful in handling packages

rospack

The rospack tool can be used to get information about packages. For example, to get the basic dependencies of a package, you may run

rospack depends1 cpp_basic_nodes

To get all dependencies, you may use

rospack depends cpp_basic_nodes

You can use rospack help to get more information about the tool.

More about the rospack tool here

rosls and roscd

The rosls tool is used to list the contents of a package (irrespective of the directory you're in).

rosls cpp_basic_nodes

Will list the contents of the cpp_basic_nodes package.

The roscd tool is used to cd into a package directory

roscd cpp_basic_nodes

Will get you into the directory ~/ros_workspaces/learning_ws/src/cpp_basic_nodes (if you did everything according to this tutorial)

Conclusion

In this tutorial, you learned how to make your own workspace and create packages inside the workspace. You also learned how to build the packages in a workspace.

Reference

  • Tutorials on roswiki
    • Creating a workspace here
    • Creating a package here
    • Navigating the ROS filesystem here