ROS2 Tutorial 01 Create a workspace

1. What is a workspace?

In the process of developing a robot, the code, parameters, scripts and other files of a certain robot are managed in a folder. This folder is the workspace.
Just like creating a project in vscode

2. The difference between ROS1 and ROS2 workspaces

ROS1

ROS1 used to use the Catkin compilation system. After catkin compilation, four spaces will be generated:
src. Code space: used to store the code build of all functional packages. Compilation space: used to store the cache and intermediate files devel
generated by the compilation process. Development space: used Installation space
for placing the executable file generated by compilation
: It is not necessary. It can be generated with catkin_make install in the working directory. The executable file can be installed into this space.

ROS2

ROS2 is compiled using colcon. The four folders generated after compilation are
src. Code space: used to store the codes, scripts, etc. of all functional packages.
Build compilation space: used to store the cache and intermediate files generated by the compilation process.
Install installation space: used Used to place the executable files and script logs generated by compilation.
Log space: a space used to save logs such as warnings and errors during compilation and running.

3. Create a workspace

ROS official tutorial on creating a workspace

1. Create a workspace folder

mkdir -p ~/ROSLearningWorkspace/src
#mkdir -p中的 -p为-parent意思,如果该路径下有父目录不存在,则一并创建父目录
#“~”默认为系统根目录
cd ~/ROSLearningWorkspace/src

2. Update package dependencies

When you put function packages shared by others into the workspace, these function packages will have more or less dependent files. At this time, you need to update the dependency packages of these function packages.

first update

When updating for the first time, you need

sudo apt install -y python3-pip
#安装pip3
sudo pip3 install rosdepc
#通过pip3安装rosdepc,rosdepc是中国区适配的rosdep
sudo rosdepc init
#rosdepc初始化
rosdepc update
#rosdepc更新
cd ~/ROSLearningWorkspace
#到工作空间目录下
rosdepc install -i --from-path src --rosdistro humble -y
#安装/更新依赖,此处版本为ROS2 Humble LTS

Normal update

When updating later, just

sudo rosdepc init
#rosdepc初始化
rosdepc update
#rosdepc更新
cd ~/ROSLearningWorkspace
#到工作空间目录下
rosdepc install -i --from-path src --rosdistro humble -y
#安装/更新依赖,此处版本为ROS2 Humble LTS

When the corresponding dependencies are installed for the packages in the workspace, you will get the following feedback

All required rosdeps installed successfully

3. Compile workspace

After updating the dependencies, you can compile the workspace

Install the compilation tool colcon

When there is no colcon, you need to obtain colcon first.

sudo apt update
sudo apt upgrade
sudo apt install python3-colcon-ros

Start compiling

cd ~/ROSLearningWorkspace
#到工作空间根目录下
colcon build
#colcon build 将编译文件夹下的所有源码并完成相应配置

Advanced

1. When you need to compile a package separately, add the suffix –packages-select to save time.

colcon build --packages-select package_name

2. If you want the python script to be modified, you no longer need to recompile it every time.
Warning: There may be code incompatibility.

colcon build --symlink-install

After colcon is compiled successfully, the corresponding executable file will be generated in the install directory of the workspace. Because python is an interpreted language, a copy will be copied to the install. ROS will execute the contents of the install. If it is not restarted every time Compilation, the copy version in install executed by ROS will not be updated.

4. Set the environment variables of the workspace

The source command is usually used to re-execute the newly modified initialization file so that it takes effect immediately without having to log out and log in again.
Execute setup.bash to set the environment variables of the workspace to facilitate calling newly generated files.

. install/setup.bash
# “.”为运行

Difference from ros1

It needs to be different from the method in ros1.
This is the method in ros1. Please do not use:

source devel/setup.bash

Automatically set environment variables

Through further settings, this environment variable can automatically take effect every time you open the terminal.
The directory of the workspace can be viewed with the pwd command to view the current directory path.
Here WORKSPACE needs to be replaced with the directory of the workspace that needs to be set.

echo "source WORKSPACE/install/setup.bash" >> ~/.bashrc

example:

echo "source /home/mtbotpc/ROSLearningWorkspace/install/setup.bash" >> ~/.bashrc

Guess you like

Origin blog.csdn.net/m0_56661101/article/details/124877689#comments_28987665