[ROS Lecture 1] 1. Create a workspace

1. Work space

1.src:

Space to place the source code of all function packages

2.build:

All intermediate files generated during compilation (less used)

3.devel:

Stores all compiled executable files and configuration scripts for environment variables (commonly used)

4.install:

It is a bit repetitive with devel. It stores compiled executable files. In ros2, install and devel are merged into the same folder.

2. Create a workspace

1. Compilation of workspace

Depends on catkin_makethe tool, its bottom layer is also based on cmake, but it is just a little encapsulated based on cmake.
Compiling in the newly created workspace will not compile any code (because the code has not been written yet), but will only generate some fixed files:
Insert image description here

2. Configure environment variables:

Configure the path of the ROS function package so that ROS can find the location of the corresponding environment package.
You can use source devel/setup.bashthis sentence to configure environment variables. Under the current interruption, enter this command to tell the ros system that the corresponding function package is in the catkin_ws workspace.
However, this command only takes effect in the current terminal and will become invalid when another terminal is opened.
Therefore, it is recommended to put this command in the terminal's configuration file so that you don't have to run this command every time you open it. The location of the configuration file is in the root directory of home and is a file script named .bashrc.
Use the following command: vi
Open the script and add the following command (i.e. environment variable, path name of the function package) to the last line: source ~/catkin_ws/devel/setup.bash
After saving, run the command: source -/.bashrcThe configuration just now will take effect in the terminal.

  • You can use the echo command to print out the environment variables in the current ros system:echo $ROS_PACKAGE_PATH

Insert image description here
You can see that there are currently two environment variable paths, one was just added, and the other was added after installing the ros system. If the terminal wants to find the corresponding command method or file of ros, it needs to go to the second path to find it, because the second path It is the location where the ros system is installed.

3. Create function packages

You can specifically write relevant code in the function package, and you must also have dependencies on various packages. These specific dependency packages can be written out when creating a function package.
As shown in the figure below, the corresponding feedback indicates that the function package has been created.
Insert image description here

You can see that there is already the function package just created in the src directory:
Insert image description here
after clicking in, you can see that the function package has been initialized:
Insert image description here
these are the files that will be generated for us by default. CMakeListsBoth files and packagedocuments are very important.
CMakeListsThe file will place some compilation options for the feature package.
packageSome specific information describing the function package will be placed, such as the version number, which other function packages the function package specifically depends on, etc.
Then you can go back to the root directory of the workspace, recompile the workspace, and you will see the compilation feedback that the package has been found.
Insert image description here

Function packages with the same name cannot exist in the same workspace, but function packages with the same name can exist in different workspaces.
But if there are really function packages with the same name in different workspaces, when ros runs a certain function package, what is the order in which the function packages with the same name are run? This requires using the overlaying mechanism of ros. That is, the coverage of the workspace.
You can use envthe command to find all environment variables in the system. Used toenv|grep ros find all environment variables related to ros.
Insert image description here
Among them: ROS_PACKAGE_PATH=/home/geroge/catkin_ws/src:/opt/ros/melodic/sharedisplayed are the two paths that ros will search for when searching for function packages. When ros wants to search for a function package, it will start searching from these two paths in sequence. For example, if it finds the function package just created learning_ros, if it finds the function package in the first path , it can end the search and run the function package in the function package. home/geroge/catkin_ws/srccorresponding node.
The path of the workspace is recorded in the ROS PACKAGE PATH environment variable in turn. The
newly set path will be automatically placed at the front end in the ROS PACKAGE PATH.
When running, ROS will first search whether the specified function package exists in the front-end workspace.
If it does not exist, , search for other workspaces in sequence backwards

[Example]
First install a roscpp-turtorial package, and then let’s see how it finds this package:

sudo apt-get install ros-melodic-roscpp-tutorials

Then we start looking for this package:

 rospack find roscpp_tutorials 

Insert image description here
You can see that he has found the function package in the second path.

Guess you like

Origin blog.csdn.net/Gorege__Hu/article/details/131982608