Workspace
Before writing code in ROS, it is important to ensure that the code is in an appropriate location. The typical location for ROS code is as follows:
~/workspace/src/package/src/executable.cpp
A workspace is a unit that is compiled at once, and it is necessary to organize your ROS code. You can create as many workspaces as you need and place them in any location that is convenient for you. In this guide, we will create a workspace named "blog_ws" in the home directory:
mkdir -p ~/workspace_name/src
Make sure to create a "src" folder within the workspace. After creating the workspace, it is necessary to source the setup file:
source /opt/ros/noetic/setup.bash
This command is used to make the terminal recognize ROS. It should be executed in each new terminal where you want to use ROS.
Package
A package is a set of source files that is responsible for a particular function, such as organizing the shape of the robot, moving the robot, or mapping the surrounding environment. To create a package, change the directory to the "src" folder inside the workspace and use the following command:
catkin_create_pkg package_name dependency1 dependency2
You can name the package as you wish. In this guide, we will name it "blog_pkg". Dependencies 1 and 2 are other package names referenced by this package. If you are using C++, it is recommended to add the package "roscpp" as the default dependency, and any other packages as needed.
Compile
Once the package is created, it can be compiled by changing the directory to the workspace and executing the following command:
cd ..
catkin_make
If the compilation is successful, you will see two new folders created in the workspace folder. This indicates that the basic setup is complete, and you can begin writing code within the package.
Congrats!
Creating a workspace and package in ROS is a simple process that allows for organization and efficient code management. By following this guide, you should be able to set up your own workspace and package in ROS with ease.
Check out my next article about topics, nodes, messages, and services HERE.