[Getting started with ROS2] ROS2 creates a workspace

        Hello everyone, I am Brother Hu. Starting today, I will take a while to switch myself from ROS1 to ROS2. In the last few articles, we have learned about many basic concepts in ROS 2. Starting today, we will start step by step. Use the characteristics of ROS2 to develop programming.

        A workspace is a directory containing ROS 2 packages. Before using ROS 2, it is necessary to source the ROS 2 installation workspace in the terminal you plan to use. This makes packages for ROS 2 available in that terminal. You also have the option to source an "overlay" - a secondary workspace where you can add new packages without disturbing existing ROS 2 workspaces you've sourced".

1.1 Source ROS 2 environment

source /opt/ros/eloquent/setup.bash

1.2 Create a working directory

Best practice is to create a new directory for each new workspace. The name is not important, but it is helpful to let it indicate the purpose of the workspace. Let's choose the directory name dev_ws for the "development workspace":

mkdir -p ~/dev_ws/src
cd ~/dev_ws/src

Another best practice is to put any package in your workspace into the src directory. The code above creates a src directory in dev_ws, then cds to it.

1.3 clone a simple test project

Before cloning, make sure you are still in the dev_ws/src directory. In later sections of , you'll create your own package, but for now you'll use an existing package to practice putting your workspace together. The existing packages we use are from the ros_tutorials repository (repo). If you read the "Beginners: CLI Tools" tutorial, you will be familiar with one of the packages in this repo, turtlesim.

入口: GitHub - ros/ros_tutorials: Code used in tutorials found on ROS wiki

 Note the "Branch" dropdown on the left above the directory listing. When cloning this repo, add the -b parameter followed by the branch corresponding to the ROS2 release.

In the dev_ws/src directory, run the following command for the distribution you are using:

git clone https://github.com/ros/ros_tutorials.git -b eloquent-devel

 We can enter the directory of this package and see all downloaded subpackages

         The first three packages are ignored; turtlesim is the only actual ROS 2 package in this repo. You've now populated the workspace with a sample package, but it's not yet a fully functional workspace. The dependencies need to be resolved and the workspace built first.

1.4 Resolve dependencies

        Before building your workspace, you need to resolve package dependencies. You may already have all dependencies, and if you don't want your build to fail due to missing dependencies after a long wait, it's best practice to check dependencies every time.

Run the following command from the root of the workspace (dev_ws):

cd ~/dev_ws
rosdep install -i --from-path src --rosdistro eloquent -y

If the following error message appears:

 

Continue to use the Xiaoyu one-click installation that was prompted to use during the previous installation.

 

 After completion, continue the previous command to build dependencies, and you will be successful.

 If you already have all dependencies, the console will return:

#All required rosdeps installed successfully

Packages declare their dependencies in the package.xml file. The above command iterates through these declarations and installs the missing ones. You can learn more about rosdep in another tutorial later.

1.5 Use colcon to compile the project

From the root directory of the workspace (dev_ws), the package can now be built with the following command:

#进入根目录
cd ~/dev_ws
#编译
colcon build

There will be a prompt during the compilation process, because we have installed this project before, and now when compiling, there will be a prompt to pay attention. This is also the difference from the previous ROS1. The concept of coverage can be used in ROS2.

1. colcon buildYou can also bring some parameters, this is for sharing:

  • --packages-up-toCompile only the specified package, all its dependencies, but not the entire workspace (saves time)

  • --symlink-installAvoid needing to rebuild every time you adjust your python script

  • --event-handlers console_direct+Displays the console output when building (otherwise it can be found in the "log" directory)

After the build is complete, enter ls in the workspace root directory (~/dev_ws), and you will see that colcon has created a new directory:

 The install directory is where the workspace's installation files reside, which you can use to source code overrides. This is very different from ROS1.

1.6 Source environment variable coverage (the overlay)

Note: Before Source, it is very important to open a new terminal, separate from the terminal where the workspace was built. Complications may arise if the Source overlay is built in the same terminal as it is built, or also where the Source overlay is built.

In a new terminal, make your main ROS 2 environment the "base layer" so you can build overlays on top of it:

#新终端,不是构建终端 设置环境变量
source /opt/ros/eloquent/setup.bash
#进入工作根目录
cd ~/dev_ws
#执行覆盖
. install/local_setup.bash

Executing local_setup will only add packages available on your system to your environment. local_setup is provided to allow the use of these two workspaces. So getting the settings for the main ROS 2 installation, as you did just now, and then getting the local_setup overridden by dev_ws is the same as getting the settings for dev_w, since that includes the underlying environment that created it. If there are many, overlapping coverage, some problems may arise.

Now, you can run the package you just compiled in this terminal:

ros2 run turtlesim turtlesim_node

 But how do you know that this is the running package and not the one that comes with the system?

You can modify turtlesim in your own package to see the effect:

 You can see that the local ones take precedence over the system ones.

I added a sentence to print at the beginning of the startup file:

cd ~/dev_ws/src/ros_tutorials/turtlesim/src

vim turtlesim.cpp

 

1.7 Verification of the official method for modifying the window title

In order to verify above, whose priority is given, I added the printing, and the official method of modifying the window name, I will also share it with you:

cd ~/dev_ws/src/ros_tutorials/turtlesim/src

 

vim turtle_frame.cpp

 

 Recompile:

#进入根目录
cd ~/dev_ws
#编译
colcon build

When you're done, go ahead and test with a new terminal:

Let's use a new terminal to compare with the one that comes with the operating system:

 

The above is what I want to share today. Error correction, questions, communication: [email protected]

Guess you like

Origin blog.csdn.net/cau_weiyuhu/article/details/128766960