0 简介
介绍编写ROS2(C++语言)软件包的步骤;
0.1 前置条件
参考x.1,和x.2,安装ROS2和编译工具;
1 创建ROS2软件包
以下的指令,创建一个名为mtuav-sns-radar-ros2
的ROS2软件包,使用ament_cmake
作为构建系统,许可证类型为Apache-2.0,并包含一个名为radar_node
的节点;
mkdir -p ~/ros2_ws/src
cd ~/ros2_ws/src
ros2 pkg create --build-type ament_cmake --license Apache-2.0 --node-name radar_node mtuav-sns-radar-ros2
可以通过ros2 pkg create --help
,查看初始化ROS2软件包的可用选项:
~$ ros2 pkg create --help
usage: ros2 pkg create [-h] [--package-format {2,3}] [--description DESCRIPTION] [--license LICENSE]
[--destination-directory DESTINATION_DIRECTORY] [--build-type {cmake,ament_cmake,ament_python}]
[--dependencies DEPENDENCIES [DEPENDENCIES ...]] [--maintainer-email MAINTAINER_EMAIL]
[--maintainer-name MAINTAINER_NAME] [--node-name NODE_NAME] [--library-name LIBRARY_NAME]
package_name
Create a new ROS 2 package
positional arguments:
package_name The package name
options:
-h, --help show this help message and exit
--package-format {2,3}, --package_format {2,3}
The package.xml format.
--description DESCRIPTION
The description given in the package.xml
--license LICENSE The license attached to this package; this can be an arbitrary string, but a LICENSE file will only be
generated if it is one of the supported licenses (pass '?' to get a list)
--destination-directory DESTINATION_DIRECTORY
Directory where to create the package directory
--build-type {cmake,ament_cmake,ament_python}
The build type to process the package with
--dependencies DEPENDENCIES [DEPENDENCIES ...]
list of dependencies
--maintainer-email MAINTAINER_EMAIL
email address of the maintainer of this package
--maintainer-name MAINTAINER_NAME
name of the maintainer of this package
--node-name NODE_NAME
name of the empty executable
--library-name LIBRARY_NAME
name of the empty library
2 编译ROS2软件包
# Release版本
(cd ~/ros2_ws/ && colcon build --cmake-args -DCMAKE_BUILD_TYPE=Release --packages-select mtuav-sns-radar-ros2)
# Debug版本
(cd ~/ros2_ws/ && colcon build --cmake-args -DCMAKE_BUILD_TYPE=Debug --packages-select mtuav-sns-radar-ros2)
3 使用gdb调试debug版本的程序
3.1 启动gdb时传入参数
gdb --args ~/ros2_ws/build/mtuav-sns-radar-ros2/radar_node /dev/ttyUSB0 921600
3.2 启动gdb后设置参数
gdb ~/ros2_ws/build/mtuav-sns-radar-ros2/radar_node
# 之后在gdb中设置参数
set args /dev/ttyUSB0 921600
4 添加依赖
4.1 创建ROS2软件包时,指定依赖
如果你在创建ROS2软件包时,已经知道需要的全部或者部分依赖,那么,你可以通过传入--dependencies
选项指定依赖:
ros2 pkg create --dependencies rclcpp sensor_msgs message_filters --build-type ament_cmake --license Apache-2.0 --node-name radar_node mtuav-sns-radar-ros2
和不指定依赖相比,文件变动如下:
mtuav-sns-radar-ros2/package.xml
mtuav-sns-radar-ros2/CMakeLists.txt
4.2 创建ROS2软件包后,添加依赖
参考4.1,以添加pcl_msgs为例;
在package.xml,增加:
在CMakeLists.txt,增加:
find_package(pcl_msgs REQUIRED)
# ...
# 在定义radar_node之后
ament_target_dependencies(radar_node
# ...
"pcl_msgs"
)
这种使用ament_target_dependencies
添加第三方依赖的方式,并不局限于ROS2的功能软件包,对于能够使用find_package
找到的第三方依赖,都可以尝试使用;
可以认为ament_target_dependencies
实现了以下功能:
find_package(PCL REQUIRED common)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
ament_target_dependencies
是官方推荐的方式去添加依赖项。它将使依赖项的库、头文件和自身的依赖项被正常找到;
x 参考文档
x.1 Ubuntu (Debian packages) — ROS 2 Documentation: Iron documentation
x.2 Using colcon to build packages — ROS 2 Documentation: Iron documentation
x.3 Creating a package — ROS 2 Documentation: Iron documentation
x.4 ROS 2编写包并用colcon编译_ros2 cmakelists 规则-CSDN博客
x.5 详细分析一个ROS2 CMakeLists.txt文件 | 首飞的博客
x.6 How do I run a program with commandline arguments using GDB within a Bash script? - Stack Overflow
x.7 io features related to pcap will be disabled · Issue #2651 · PointCloudLibrary/pcl
标签:name,package,--,C++,radar,ROS2,软件包,ros2 From: https://www.cnblogs.com/xiaochou/p/18140351