Configuring ROS Environment , Navigating ROS Filesystem and Creating ROS package

Mayank SharmaMayank Sharma
7 min read

Before starting these tutorials please complete installation

configuring ROS Environment:

If you are ever having problems finding or using your ROS packages make sure that you have your environment properly setup. A good way to check is to ensure that environment variables like ROS_ROOT and ROS_PACKAGE_PATH are set :

printenv | grep ROS

If they are not then you might need to 'source' some setup.*sh files.

If you just installed ROS from apt on Ubuntu then you will have setup.*sh files in '/opt/ros/<distro>/', and you could source them like so:

$ source /opt/ros/<distro>/setup.bash

Using the short name of your ROS distribution instead of <distro>

If you installed ROS Melodic, that would be:

source /opt/ros/melodic/setup.bash

You will need to run this command on every new shell you open to have access to the ROS commands, unless you add this line to your .bashrc.

Create a ROS Workspace

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/
catkin_make

but when you run catkin_make command then you may get error

ImportError: "from catkin_pkg.package import parse_package" failed: No module named 'catkin_pkg'
Make sure that you have installed "catkin_pkg", it is up to date and on the PYTHONPATH.
CMake Error at /opt/ros/melodic/share/catkin/cmake/safe_execute_process.cmake:11 (message):
  execute_process(/usr/bin/python3
  "/opt/ros/melodic/share/catkin/cmake/parse_package_xml.py"
  "/opt/ros/melodic/share/catkin/cmake/../package.xml"
  "/home/vboxuser/catkin_ws/build/catkin/catkin_generated/version/package.cmake")
  returned error code 1
Call Stack (most recent call first):
  /opt/ros/melodic/share/catkin/cmake/catkin_package_xml.cmake:74 (safe_execute_process)
  /opt/ros/melodic/share/catkin/cmake/all.cmake:168 (_catkin_package_xml)
  /opt/ros/melodic/share/catkin/cmake/catkinConfig.cmake:20 (include)
  CMakeLists.txt:58 (find_package)


-- Configuring incomplete, errors occurred!
See also "/home/vboxuser/catkin_ws/build/CMakeFiles/CMakeOutput.log".
See also "/home/vboxuser/catkin_ws/build/CMakeFiles/CMakeError.log".
Makefile:320: recipe for target 'cmake_check_build_system' failed
make: *** [cmake_check_build_system] Error 1
Invoking "make cmake_check_build_system" failed

To resolve this error we have to install some package which are:

sudo apt-get install python3-catkin-pkg-modules

After installing catkin_pkg, try running catkin_make again in your catkin_ws:

cd ~/catkin_ws
catkin_make

The catkin_make command is a convenience tool for working with catkin workspaces. Running it the first time in your workspace, it will create a CMakeLists.txt link in your 'src' folder.

Python 3 users in ROS Melodic and earlier: note, if you are building ROS from source to achieve Python 3 compatibility, and have setup your system appropriately (ie: have the Python 3 versions of all the required ROS Python packages installed, such as catkin) the first catkin_make command in a clean catkin_workspace must be:

catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python3

This will configure catkin_make in python 3. You may then proceed to use just catkin_make for subsequent builds.

Additionally, if you look in your current directory you should now have a 'build' and 'devel' folder. Inside the 'devel' folder you can see that there are now several setup.*sh files. Sourcing any of these files will overlay this workspace on top of your environment.

To make sure your workspace is properly overlayed by the setup script, make sure ROS_PACKAGE_PATH environment variable includes the directory you're in.

echo $ROS_PACKAGE_PATH

Navigating the ROS Filesystem

Code is spread across many ROS packages. Navigating with command-line tools such as ls and cd can be very tedious which is why ROS provides tools to help you.

Using rospack

rospack allows you to get information about packages. In this tutorial, we are only going to cover the find option, which returns the path to package.

rospack find [package_name]

#example
rospack find roscpp

Using roscd

roscd is a part of the rosbash suite. It allows you to change directory (cd) directly to a package or a stack.

roscd <package-or-stack>[/subdir]

roscd roscpp

Subdirectories

roscd can also move to a subdirectory of a package or stack.

roscd roscpp/cmake
pwd

roscd log

roscd log will take you to the folder where ROS stores log files. Note that if you have not run any ROS programs yet, this will yield an error saying that it does not yet exist.

If you have run some ROS program before, try:

roscd log

Using rosls

rosls is a part of the rosbash suite. It allows you to ls directly in a package by name rather than by absolute path.

Usage:

rosls roscpp_tutorials

Tab Completion

It can get tedious to type out an entire package name. In the previous example, roscpp_tutorials is a fairly long name. Luckily, some ROS tools support TAB COMPLETION.

After pushing the TAB key, the command line should fill out the rest.

You may have noticed a pattern with the naming of the ROS tools:

  • rospack = ros + package

  • roscd = ros + cd

  • rosls = ros + ls

Creating a ROS Package

catkin package contains package.xml file, CMakeLists.txt and also Each package must have its own folder

  •     my_package/
          CMakeLists.txt
          package.xml
    

Packages in a catkin Workspace

workspace_folder/        -- WORKSPACE
  src/                   -- SOURCE SPACE
    CMakeLists.txt       -- 'Toplevel' CMake file, provided by catkin
    package_1/
      CMakeLists.txt     -- CMakeLists.txt file for package_1
      package.xml        -- Package manifest for package_1
    ...
    package_n/
      CMakeLists.txt     -- CMakeLists.txt file for package_n
      package.xml        -- Package manifest for package_n

Creating a catkin Package

first create the catkin workspace and move into it

cd ~/catkin_ws/src

Now use the catkin_create_pkg script to create a new package called 'beginner_tutorials' which depends on std_msgs, roscpp, and rospy:

catkin_create_pkg beginner_tutorials std_msgs rospy roscpp

This will create a beginner_tutorials folder which contains a package.xml and a CMakeLists.txt, which have been partially filled out with the information you gave catkin_create_pkg.

catkin_create_pkg requires that you give it a package_name and optionally a list of dependencies on which that package depends:

# This is an example, do not try to run this
# catkin_create_pkg <package_name> [depend1] [depend2] [depend3]

Building a catkin workspace and sourcing the setup file

cd ~/catkin_ws
catkin_make

After the workspace has been built it has created a similar structure in the devel subfolder as you usually find under /opt/ros/$ROSDISTRO_NAME.

To add the workspace to your ROS environment you need to source the generated setup file:

. ~/catkin_ws/devel/setup.bash

package dependencies

1 First-order dependencies :

When using catkin_create_pkg earlier, a few package dependencies were provided. These first-order dependencies can now be reviewed with the rospack tool.

rospack depends1 beginner_tutorials

As you can see, rospack lists the same dependencies that were used as arguments when running catkin_create_pkg. These dependencies for a package are stored in the package.xml file:

roscd beginner_tutorials
cat package.xml

2 Indirect dependencies

In many cases, a dependency will also have its own dependencies. For instance, rospy has other dependencies.

 rospack depends1 rospy

A package can have quite a few indirect dependencies. Luckily rospack can recursively determine all nested dependencies.

rospack depends beginner_tutorials

Customizing Your Package

This part of the tutorial will look at each file generated by catkin_create_pkg and describe, line by line, each component of those files and how you can customize them for your package.

Customizing the package.xml

First update the description tag:

Change the description to anything you like, but by convention the first sentence should be short while covering the scope of the package. If it is hard to describe the package in a single sentence then it might need to be broken up.

vim package.xml

Maintainer tags : we have to do the changes in the package.xml file

 <maintainer email="you@yourdomain.tld">Your Name</maintainer>

This is a required and important tag for the package.xml because it lets others know who to contact about the package.

license tags

You should choose a license and fill it in here. Some common open source licenses are BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, and LGPLv3.

dependencies tags

All of our listed dependencies have been added as a build_depend for us, in addition to the default buildtool_depend on catkin. In this case we want all of our specified dependencies to be available at build and run time, so we'll add a exec_depend tag for each of them as well.

lets build our package

Building Packages

As long as all of the system dependencies of your package are installed, we can now build your new package.

I hope you guys enjoys the blog do share and like it

want to connect with me ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

My Contact Info:

๐Ÿ“ฉEmail:-mayank07082001@gmail.com

LinkedIn:-linkedin.com/in/mayank-sharma-devops

20
Subscribe to my newsletter

Read articles from Mayank Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mayank Sharma
Mayank Sharma

๐Ÿ”น๐Ÿ”น Enter the world of Maya Hey everyone This is Mayank Sharma , a disciplined ,openminded ,enthusiastic & highly dedicated guy for achieving the excellence in life. For me learning is a never ending process, I believe in ๐‚๐จ๐ง๐ญ๐ข๐ง๐ฎ๐จ๐ฎ๐ฌ ๐‹๐ž๐š๐ซ๐ง๐ข๐ง๐  & ๐‚๐จ๐ง๐ญ๐ข๐ง๐ฎ๐จ๐ฎ๐ฌ ๐ƒ๐ž๐ฏ๐ž๐ฅ๐จ๐ฉ๐ฆ๐ž๐ง๐ญ . My goal is to be a successful Professional Engineer by hard work through my knowledge, skills & commitment which will help me grow, explore and excel along with a great team . With a natural curiosity and a talent for problem-solving, I am constantly exploring new ways to innovate the things and improve processes. Mail me ๐Ÿ‘‰ mayank07082001@gmail.com ๐Ÿ”น๐Ÿ”น I am persuing B.Tech in information technology with the specialisation of Artificial Intelligence and robotic from M.I.T.S Gwalior (M.P) . I'm passionate about learning cutting edge technologies & implement them to solve real world problems. I have devoted my time for them & gained experience in the following tools & technologies . ๐Ÿ“Œ ๐Ž๐ฉ๐ž๐ซ๐š๐ญ๐ข๐ง๐  ๐’๐ฒ๐ฌ๐ญ๐ž๐ฆ : Wษชษดแด…แดแดกs, Cแด‡ษดแด›OS, Rแด‡แด…Hแด€แด›,Fedora,Ubuntu ๐Ÿ“Œ ๐‹๐š๐ง๐ ๐ฎ๐š๐ ๐ž๐ฌ : Pสแด›สœแดษด, Sสœแด‡สŸสŸ Sแด„ส€ษชแด˜แด›ษชษดษข, c++, java ๐Ÿ“Œ ๐‚๐จ๐ง๐ญ๐š๐ข๐ง๐ž๐ซ ๐„๐ง๐ ๐ข๐ง๐ž : Dแดแด„แด‹แด‡ส€, CRI-O, openshift ๐Ÿ“Œ ๐‚๐จ๐ง๐ญ๐š๐ข๐ง๐ž๐ซ ๐Œ๐š๐ง๐š๐ ๐ž๐ฆ๐ž๐ง๐ญ ๐“๐จ๐จ๐ฅ๐ฌ : Kแดœส™แด‡ส€ษดแด‡แด›แด‡s ๐Ÿ“Œ ๐€๐ฎ๐ญ๐จ๐ฆ๐š๐ญ๐ข๐จ๐ง ๐“๐จ๐จ๐ฅ๐ฌ : Aษดsษชส™สŸแด‡, Gษชแด›/Gษชแด›Hแดœส™ ๐Ÿ“Œ ๐‚๐ฅ๐จ๐ฎ๐ ๐‚๐จ๐ฆ๐ฉ๐ฎ๐ญ๐ข๐ง๐  ๐๐ฅ๐š๐ญ๐Ÿ๐จ๐ซ๐ฆ : AWS(EC2,s3,EBs,vpc,loadbalancers,Autosg,rds,cloudwatch,route53, lamda , EFS, snapshot, serverless, EKS) ๐Ÿ“ŒDATABASE : MongoDB, Mysql Also I have worked on Wแด‡ส™ Dแด‡แด แด‡สŸแดแด˜แดแด‡ษดแด›. I enjoy working on new ideas and helping others in these areas. ๐Ÿ”น๐Ÿ”น Apart from being a technology enthusiast, travelling & photography has remained my constant passion. I always keep a right attitude & try to maintain positive vibe in every situation. I also spend time in playing Cricket & Football. Hence I understand the importance of team play & responsibility of a team player very well. Hobbies: โœจI am very interested in cyber security . whatever I see I try to find out the loopholes in it and then try to hack it or trying that how can i breach / break the security โœจ I love to see cricket (IPL) also very much interested in playing cricket . โœจI also love watching various tv and movie series, Top3 is Money heist, Gangs of wasseypur and comedy movies(like hera pheri) and college related webseries etc.