ros2 learning 05 function package definition

What is a feature pack?

Each robot may have many functions, such as movement control, visual perception, autonomous navigation, etc. Here, the code for each function is placed in a separate file, which is defined as a function package. Each function package can run independently

So is it okay if I say I don’t want to separate? sure

The definition of function packages in ros is similar to the concept of microservices in other traditional system services. For example, if you want to develop an e-commerce system, the system has user services, product services, order services, evaluation services, etc.

Create feature package

How to create a function package in ROS2? We can use this command:

ros2 pkg create --build-type <build-type> <package_name>

Detailed explanation of the command to create a function package:

  • pkg: Indicates functions related to the function package;
  • create: indicates creating a function package;
  • build-type: Indicates whether the newly created function package is C++ or Python. If C++ or C is used, then it will be followed by ament_cmake. If Python is used, it will be followed by ament_python;
  • package_name: The name of the new function package.

For example, create C++ and Python versions of function packages in the terminal:

cd ~/dev_ws/src

Create a C++ function package example

ros2 pkg create --build-type ament_cmake learning_pkg_c

Create a Python function package example

ros2 pkg create --build-type ament_python learning_pkg_python

Compile function package

In the created function package, we can continue to write the code. Afterwards, we need to compile and configure environment variables to run normally:

cd ~/dev_ws
colcon build   # 编译工作空间所有功能包
source install/local_setup.bash

Function package structure

Function packages are not ordinary folders, so how to determine whether a folder is a function package? Let's analyze the structure of the two newly created function packages just now.

C++ function package

First, let’s take a look at the C++ type function package. There must be two files in it: package.xml and CMakerLists.txt.

Insert image description here
The main content of the package.xml file is as follows, including the copyright description of the function package and the declaration of various dependencies.

Insert image description here
The CMakeLists.txt file is a compilation rule. C++ code needs to be compiled to run, so you must set how to compile in this file, using CMake syntax.

Insert image description here

Python function package

The C++ function package needs to compile the source code into an executable file, but the Python language is analytical and does not require compilation, so there will be some differences, but there will also be these two files: package.xml and setup.py.

Insert image description here
The main content of the package.xml file is the same as the C++ version function package, including the copyright description of the function package and various dependency statements.

Insert image description here

The setup.py file also contains some copyright information. In addition, there is also a program entry configured by "entry_points". In the subsequent programming explanation, we will introduce how to use it.

Insert image description here

Guess you like

Origin blog.csdn.net/hai411741962/article/details/135099564