Building ROS2 packages
Setup
Setting up the ROS2 Package Workspace
first create the folder for the package usual it is called packagename_ws
where _ws
stands for then you enter the directory and create another directory called src
then run the colcon build
command.
install
folder and add the setup.bash
source command to your ~/.bashrc
Creating a Python Packages
Setting Up
Go to the workspace folder that was created in the last step and open the src
folder. In this folder run this command.
add your code in to the folder with the __init__.py
. This is usually in the folder src/{package_name}/{package_name}
Programming
create a python file for your first node.
Now add it to the setup.py file in the entry_points
section.
it is setup with the name of the ROS2 command first then the equals followed by the package name, python file name that is in the same folder as the __init__.py
, and finally the function name to call, usual main
.
The final step is building the script and testing it.
Building
go the the {package_name}_ws
folder with the src
and install
folder in it. Then run the colcon build
command.
next run the source ~/.bashrc
command to reload the workspace source file and the other source files.
To help improve the build speed we can also use --packages-select
to only build a specific package.
Simplifying Build (specific to python)
since we are using python and it is interpreted we can simplify the build process when working buy using the symlink command to avoid building.
then just like before run the source ~/.bashrc
command to refresh everything and after that you will never have to worry about building or refreshing again.
Creating a C++ Packages
Go to the workspace folder that was created in the last step and open the src
folder. In this folder run this command.
next create a file in {package_name}/src
and call it node_name.cpp
Next open the program menu on VSCode using CTRL + SHIFT + P
. Select the C/C++: Edit Configurations (JSON)
. Make Sure the c_cpp_properties.json
file has the "/opt/ros/humble/include/**"
include