Creating New Packages in ROS - arieldo/MorBot GitHub Wiki
This tutorial will guide you through the process of creating new packages in ROS (Robot Operating System), a flexible framework for writing robot software. The tutorial aims to provide a clear explanation of the steps required to create and manage ROS packages.
- Introduction to ROS Packages
- Creating a New ROS Package
- Building the Package
- Adding Nodes and Launch Files
In ROS, a package is the primary unit for organizing software. It contains source code, configuration files, and other resources necessary for building and running a specific application. Packages allow developers to distribute and reuse their code easily.
To create a new ROS package, follow these steps:
- Open a terminal window.
- Navigate to the
src
directory of your ROS workspace, e.g.,~/MorBot/morbot_ros/morbot_ws/src
:
cd ~/MorBot/morbot_ros/morbot_ws/src
- Use the
catkin_create_pkg
command to create a new package. Replaceyour_package_name
with the desired name for your package, anddependency_1
,dependency_2
, ... with the required dependencies:
catkin_create_pkg your_package_name dependency_1 dependency_2 ...
For example, to create a package named morbot_control
with the rospy
,roscpp
, std_msgs
and sensor_msgs
dependencies, run the following command:
catkin_create_pkg morbot_control rospy roscpp std_msgs sensor_msgs
- The new package will be created inside the
src
directory of your workspace. The package will contain several files and directories, including:
-
CMakeLists.txt
: The CMake build file for the package. -
package.xml
: The package manifest, which contains metadata about the package, such as its name, version, description, and dependencies.
To build your newly created ROS package, follow these steps:
- Navigate to the root of your ROS workspace:
cd ~/MorBot/morbot_ros/morbot_ws
- Run the
catkin_make
command:
catkin_make
- After the build is successful, source the workspace's
setup.bash
file to make the new package available in your ROS environment:
source devel/setup.bash
After creating and building your ROS package, you can add nodes and launch files to implement the desired functionality. Nodes are executable files that perform specific tasks, while launch files help start multiple nodes and set parameters simultaneously.
A ROS node is a fundamental component in the ROS framework, which is a flexible and open-source platform for developing robotics software. A node represents a single, standalone process that performs a specific function in a robot's system, such as controlling a sensor, actuator, or algorithm. Nodes communicate with each other by exchanging messages through a publish-subscribe mechanism called "topics," enabling them to work together in a distributed manner. This modular design allows for easy integration, scalability, and reusability of software components in robotics applications.
To add a new node to your package, follow these steps:
- Add a new directory named
scripts
inside your package directory (~/morbot_ws/src/your_package_name
):
cd ~/MorBot/morbot_ros/morbot_ws/src/your_package_name
mkdir scripts
For example, the package named morbot_control
that we created earlier :
cd ~/MorBot/morbot_ros/morbot_ws/src/morbot_control
mkdir scripts
- Create a new source file (e.g., Python) inside the
scripts
directory. For example, create a Python file namedmy_node.py
:
cd scripts
touch my_node.py
sudo chmod 777 my_node.py
- Open the source file and write the code for your node:
#!/usr/bin/env python
import rospy
print("My Cool ROS python Node!!")
If you ONLY use python nodes pass the Cpp nodes
to Both Python and CPP
C++ nodes are written in C++ and are compiled into a shared library (.so) file. To add a new C++ node to your package, follow these steps:
- Add a new directory named
src
inside your package directory (~/morbot_ws/src/your_package_name
):
cd ~/MorBot/morbot_ros/morbot_ws/src/your_package_name
mkdir src
For example, the package named morbot_control
that we created earlier :
cd ~/MorBot/morbot_ros/morbot_ws/src/morbot_control
mkdir src
- Create a new source file (e.g., Cpp) inside the
src
directory. For example, create a Cpp file namedmy_node.cpp
:
cd src
touch my_node.cpp
-
Open the source file and write the code for your node, following the ROS programming guidelines and using the appropriate ROS libraries.
-
Modify the
CMakeLists.txt
file in your package directory to include the new node during the build process. For example, if you created a C++ node, add the following lines:
add_executable(my_node nodes/my_node.cpp)
target_link_libraries(my_node ${catkin_LIBRARIES})
- Build your package again using the catkin_make command:
cd ~/MorBot/morbot_ros/morbot_ws
catkin_make
- Source the workspace's setup.bash file to make the new node available in your ROS environment:
source devel/setup.bash
ROS launch files are XML files used to configure and start multiple nodes, parameters, and other settings required for a specific robot application. They provide a convenient way to start a complex system with a single command. Launch files typically have a ".launch" extension and use the roslaunch command-line tool to execute them. They simplify the process of starting and managing multiple nodes, allowing for greater flexibility and organization in robot software development.
To add a new launch file to your package, follow these steps:
- Add a new directory named
launch
inside your package directory (~/morbot_ws/src/your_package_name
):
cd ~/MorBot/morbot_ros/morbot_ws/src/your_package_name
mkdir launch
For example, the package named morbot_control
that we created earlier :
cd ~/MorBot/morbot_ros/morbot_ws/src/morbot_control
mkdir launch
- Create a new launch file inside the
launch
directory. For example, create a launch file namedmy_launch_file.launch
:
cd launch
touch my_launch_file.launch
- Open the launch file and write the XML code to configure the nodes and parameters you want to launch. For example, to launch the my_node you created earlier:
<launch>
<node name="my_node" pkg="your_package_name" type="my_node.py" output="screen" />
</launch>
Replace your_package_name with the name of your package. The name attribute specifies the name of the node, while the pkg attribute points to the package that contains the node. The type attribute indicates the executable name, which should match the name given in the CMakeLists.txt file.
For example, the package named morbot_control
that we created earlier :
<launch>
<node name="my_node" pkg="morbot_control" type="my_node.py" output="screen" />
</launch>
-
Save the launch file and close the editor.
-
Build your package again using the catkin_make command:
cd ~/MorBot/morbot_ros/morbot_ws
catkin_make
- Source the workspace's setup.bash file to make the new launch file available in your ROS environment:
source devel/setup.bash
- Run the launch file using the roslaunch command:
roslaunch your_package_name my_launch_file.launch
For example, the package named morbot_control
that we created earlier :
roslaunch morbot_control my_launch_file.launch
Now, you have successfully added nodes and launch files to your ROS package. You can continue to develop your package by adding more nodes, launch files, or other resources as needed. Remember to build your package and source the setup.bash file after making changes.
-
Learn Writing a Simple Publisher and Subscriber in Python for ROS PubSubPy
-
Build your first MorBot Demo with ROS-Workshop