[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_make
the 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:
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.bash
this 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
.
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 -/.bashrc
The 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
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.
You can see that there is already the function package just created in the src directory:
after clicking in, you can see that the function package has been initialized:
these are the files that will be generated for us by default. CMakeLists
Both files and package
documents are very important.
CMakeLists
The file will place some compilation options for the feature package.
package
Some 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.
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 useenv
the command to find all environment variables in the system. Used toenv|grep ros
find all environment variables related to ros.
Among them:ROS_PACKAGE_PATH=/home/geroge/catkin_ws/src:/opt/ros/melodic/share
displayed 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 createdlearning_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/src
corresponding 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
You can see that he has found the function package in the second path.