Package in ROS1
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:
1 2 3 |
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:
1 2 3 4 |
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:
1 |
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:
1 2 |
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:
1 2 |
cd ~/catkin_ws/script chmod +x [FILE_NAME.py] |
In order to run a Python node in a package, use the following command:
1 2 |
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:
1 |
~/catkin_ws/src/[PACKAGE_FOLDER] |
or alternatively you can use the following command:
1 |
roscd [PACKAGE_NAME] |