Skip to content
  • Home
  • About the Blog
  • About the Author
  • Sitemap

Abdur Rosyid's Blog

Just a few notes on mechanical engineering and robotics

Package in ROS1

July 8, 2021 by Abdur Rosyid

A ROS package is a wrapper or container of a piece of code. By this definition, you can write any C++ or Python code and then wrap it in a ROS package in order to use it in ROS. There are two types of packages in ROS: 1) packages already built in ROS and 2) user packages. The packages of the former type are commonly available as Debian installers (binary) and source files. They consist of: 1) core ROS packages which are installed when you install ROS and 2) additional ROS packages developed to provide some capabilities. The user packages are packages created by the users. A user package can contain one or more nodes. The nodes can be created by using C++ or Python. A node created by using C++ needs to be compiled, hence compiling needs to be run every time a change/modification is made to the code. On the other hand, a node created by using Python does not need to be compiled. You just only need to make sure that the permission status of the Python file is “executable”.

A good practice in creating a package is to make it independent of other packages if possible. I said “if possible” because in some cases a package necessarily dependent on another package.

To create a user package in ROS1, do the following steps:

Step 1: Create a ROS package by using the following command:

cd ~/catkin_ws/src
catkin_create_pkg [PACKAGE_NAME] [DEPENDENCY_PKG_1] [DEPENDENCY_PKG_N]
e.g. catkin_create_pkg my_ros_pkg std_msgs roscpp

This command will create a package folder inside src folder. There are two generated files and two generated folders inside the package folder: package.xml, CMakeLists.txt, and two folders namely: src and include.

  • package.xml: an XML file containing information about the package and dependency packages.
  • CMakeLists.txt: build configuration file.
  • src: folder in which we put .cpp code(s).
  • include: folder in which we put additional C++ code(s) needed by .cpp code(s) in src folder.

Step 2: Modify package.xml file

The dependency packages written in package.xml are those mentioned in the catkin_create_pkg command. If you want to modify the package name and add or modify the dependency packages , you can do that manually on the package.xml file.

Step 3: Modify CMakeLists.txt file

At the minimum, add the following two lines:

add_executable(node_name src/node_name.cpp)
e.g. add_executable(hello_world_node src/hello_world_node.cpp)
target_link_libraries(node_name ${catkin_LIBRARIES})
e.g. target_link_libraries(hello_world_node ${catkin_LIBRARIES})

The “add_executable” is used to define the C++ node(s) to be created. If you need to create more than one C++ node, you should have the same number of “add_executable”.

Step 4: Write your C++ code in “src” folder (and “include” folder if necessary) inside your package folder. You can also do this later.

Step 5: Compile/build by using the following command:

cd ~/catkin_ws && catkin_make

Until here you already have your package compiled.

If you want to write a node in Python, a good practice is to make a folder called “script” inside your package folder and then you can write your Python code (.py file) inside this folder. You also need to add “rospy” in the list of dependency packages in package.xml.

Running a node of a package

In order to run a C++ node in a package, use the following command:

rosrun [PACKAGE_NAME] [NODE_NAME]
e.g. rosrun my_ros_pkg my_node

Notice that you can only run a node after you run ROS Master, i.e. by running “roscore” command, unless you run your node through roslaunch (in this case you need to call your node in the roslaunch file.

If you have a node written in Python (.py file), set the permission of the Python file to “executable” by using “chmod +x” command. Suppose your Python file is inside “script” folder:

cd ~/catkin_ws/script
chmod +x [FILE_NAME.py]

In order to run a Python node in a package, use the following command:

rosrun [PACKAGE_NAME] [PYTHON_NODE_FILE_NAME.py]
e.g. rosrun my_ros_pkg my_node.py

Opening a package source folder

If you want to open your package folder, you can simply go there manually by using cd command:

~/catkin_ws/src/[PACKAGE_FOLDER] 

or alternatively you can use the following command:

roscd [PACKAGE_NAME]

Post navigation

Previous Post:

How to establish ROS1 network between multiple machines

Next Post:

ROS1 Topic, Publisher, and Subscriber

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • STEM 101
  • Robotics
  • Kinematics
  • Dynamics
  • Control
  • Robot Operating System (ROS)
  • Robot Operating System (ROS2)
  • Software Development
  • Mechanics of Materials
  • Finite Element Analysis
  • Fluid Mechanics
  • Thermodynamics

Recent Posts

  • Pull Request on Github
  • Basics of Git and Github
  • Conda vs Docker
  • A Conda Cheat Sheet
  • Installing NVIDIA GPU Driver on Ubuntu

Archives

  • June 2025
  • July 2021
  • June 2021
  • March 2021
  • September 2020
  • April 2020
  • January 2015
  • April 2014
  • March 2014
  • March 2012
  • February 2012
  • June 2011
  • March 2008
© 2026 Abdur Rosyid's Blog | WordPress Theme by Superbthemes