[ROS] ROS1 creates workspace and function package

1. ROS workspace

Each ROS project has a fixed and identical directory structure, that is, ROS projects are built with a directory structure. Use this folder with a fixed structure as a workspace. There are four important subdirectories in the workspace: build, devel, install, and src. Only src needs to be created manually, and the other three are automatically generated by the build command. The following The role of these four subdirectories will be explained in detail

~/workspace/ros$ tree -L 1
.
├── build
├── devel
├── install
└── src

2. Create a workspace

2.1 Create a workspace root directory

Create an empty directory as the root directory of the workspace, for example, my root directory is: ~/workspace/ros

$ mkdir -p ~/workspace/ros

2.2 Create source directory src

All source code will be put into src, enter the workspace root directory

$ cd  ~/workspace/ros

create src

$ mkdir src

2.3 Initialize the workspace

Enter the src directory and execute the command catkin_init_workspace

$ cd src/
$ catkin_init_workspace 
Creating symlink "~/workspace/ros/src/CMakeLists.txt" pointing to "/opt/ros/melodic/share/catkin/cmake/toplevel.cmake"

You can learn from the printed information that the initialization workspace is to create a soft link CMakeLists.txt pointing to ==/opt/ros/melodic/share/catkin/cmake/toplevel.cmake==, you can use cat CMakeLists.txt to view this file Content

$ cat CMakeLists.txt 
# toplevel CMakeLists.txt for a catkin workspace
# catkin/cmake/toplevel.cmake

cmake_minimum_required(VERSION 3.0.2)

project(Project)

set(CATKIN_TOPLEVEL TRUE)
略……

2.4 compile

Compilation needs to be executed in the root directory of the workspace, so return to the parent directory first

~/workspace/ros/src$ cd ..
~/workspace/ros$ ls
src

Use the catkin_make command to compile, and two directories build and devel will be created during the compilation process;
build: compilation directory, storing temporary files generated during the compilation process;
devel: development space, storing executable files, script files, and configuration files generated by compilation wait

$ catkin_make
Base path: ~/workspace/ros
Source space:~/workspace/ros/src
Build space: ~/workspace/ros/build
Devel space:~/workspace/ros/devel
Install space:~/workspace/ros/install
####
#### Running command: "cmake~/workspace/ros/src -DCATKIN_DEVEL_PREFIX=~/workspace/ros/devel -DCMAKE_INSTALL_PREFIX=~/workspace/ros/install -G Unix Makefiles" in "~/workspace/ros/build"
####
-- The C compiler identification is GNU 7.5.0
略……
-- Configuring done
-- Generating done
-- Build files have been written to: ~/workspace/ros/build
####
#### Running command: "make -j6 -l6" in "~/workspace/ros/build"
####

From the printed information, it can be seen that it is equivalent to executing the following two commands cmake and make

cmake ~/workspace/ros/src -DCATKIN_DEVEL_PREFIX=~/workspace/ros/devel -DCMAKE_INSTALL_PREFIX=~/workspace/ros/install -G Unix Makefiles
make -j6 -l6

2.5 installation

Executing the catkin_make install command will copy and install executable files, scripts, configuration files, etc. into the install directory, which is actually equivalent to executingmake installOrder

catkin_make install 
Base path:~/workspace/ros
Source space:~/workspace/ros/src
Build space: ~/workspace/ros/build
Devel space: ~/workspace/ros/devel
Install space:~/workspace/ros/install
####
#### Running command: "make cmake_check_build_system" in "~/workspace/ros/build"
####
####
#### Running command: "make install -j6 -l6" in "~/workspace/ros/build"
####
Install the project...
-- Install configuration: ""
-- Installing: ~/workspace/ros/install/_setup_util.py
略……

Many people complain that the contents of the devel and install directories are basically duplicated, so devel is removed in ROS2, and only the install directory is kept;

I guess the person who "complains" should have never done a software development project before...

So far, one line of code has not been typed, and the generated file is as follows

~/workspace/ros$ tree -L 2
.
├── build
│   ├── atomic_configure
│   ├── catkin
│   ├── catkin_generated
│   ├── CATKIN_IGNORE
│   ├── catkin_make.cache
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   ├── cmake_install.cmake
│   ├── CTestConfiguration.ini
│   ├── CTestCustom.cmake
│   ├── CTestTestfile.cmake
│   ├── gtest
│   ├── install_manifest.txt
│   ├── Makefile
│   └── test_results
├── devel
│   ├── cmake.lock
│   ├── env.sh
│   ├── lib
│   ├── local_setup.bash
│   ├── local_setup.sh
│   ├── local_setup.zsh
│   ├── setup.bash
│   ├── setup.sh
│   ├── _setup_util.py
│   └── setup.zsh
├── install
│   ├── env.sh
│   ├── local_setup.bash
│   ├── local_setup.sh
│   ├── local_setup.zsh
│   ├── setup.bash
│   ├── setup.sh
│   ├── _setup_util.py
│   └── setup.zsh
└── src
    └── CMakeLists.txt -> /opt/ros/melodic/share/catkin/cmake/toplevel.cmake

11 directories, 27 files

3. Create a function package

3.1 Command format

catkin_create_pkg   <package_name>   [depend1] [depdend2] [……]

3.2 create

When creating a function package, it needs to be executed in the src directory, and the source code file of the function package will be placed in the corresponding package folder. The src directory cannot directly put the source code file. Enter src and execute the command
catkin_create_pkg

$ cd ~/workspace/ros/src
$ catkin_create_pkg test_pkg std_msgs rospy roscpp
Created file test_pkg/CMakeLists.txt
Created file test_pkg/package.xml
Created folder test_pkg/include/test_pkg
Created folder test_pkg/src
Successfully created files in ~/workspace/ros/src/test_pkg. Please adjust the values in package.xml.

The automatically generated directory structure is as follows

:~/workspace/ros/src$ tree
.
├── CMakeLists.txt -> /opt/ros/melodic/share/catkin/cmake/toplevel.cmake
└── test_pkg
    ├── CMakeLists.txt
    ├── include
    │   └── test_pkg
    ├── package.xml
    └── src

4 directories, 3 files

3.3 compile

Return to the root directory of the workspace and execute catkin_make again

$ cd ..
$ catkin_make
略……
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 1 packages in topological order:
-- ~~  - test_pkg
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'test_pkg'
-- ==> add_subdirectory(test_pkg)
-- Configuring done
-- Generating done
略……

It can be seen that when compiling again, the function package test_pkg is compiled into

3.4 Update environment variables

Add the path of the current workspace to the environment variable, take ROS_PACKAGE_PATH as an example:
before updating the environment variable:

$ echo $ROS_PACKAGE_PATH
/opt/ros/melodic/share

Execute the update environment variable command:

$ source ~/workspace/ros/devel/setup.bash 

After executing the update environment variable:

$ echo $ROS_PACKAGE_PATH
~/workspace/ros/src:/opt/ros/melodic/share

3.5 ROS-related environment variables

You can use the command == printenv | grep ROS== to view ROS-related environment variables

$ printenv | grep ROS
ROS_ETC_DIR=/opt/ros/melodic/etc/ros
ROS_ROOT=/opt/ros/melodic/share/ros
ROS_MASTER_URI=http://localhost:11311
ROS_VERSION=1
ROS_PYTHON_VERSION=2
ROS_PACKAGE_PATH=/opt/ros/melodic/share
ROSLISP_PACKAGE_DIRECTORIES=
ROS_DISTRO=melodic

Guess you like

Origin blog.csdn.net/u010168781/article/details/130377938