ROS2 launch文件与参数设置

本文详细介绍如何在ROS2项目中使用launch文件启动C++节点,并从yaml配置文件中读取订阅topic。涉及launch.py编写、参数设置、CMakeLists.txt修改,以及如何在源代码中获取和使用yaml文件中的参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ROS2 launch文件与参数设置

在ros2创建的C++语言的package中,通过launch文件启动多个节点,并从yaml文件中读取订阅的topic名称。

项目架构
working_space/
|
└───build/
|
└───install/
|
└───log/
|
└───src/
	└───package_1/
	|	|
	|	└───config/
	|	|	└───***.rviz
	|	|	└───***.yaml
	|	|
    |   └───include/
	|	|
    |   └───package_1/
	|	|	└───***.h
	|	|	└───***.hpp
	|	|
    |   └───launch/
	|	|	└───***.launch.py
	|	|
	|	└───src/
	|	|	└───***.cpp
	|	|	└───***.cpp
	|	|
	|	└───CMakeLists.txt
	|	└───package.xml
	|
	└───package_2/
	|
	└───package_3/


launch文件编写
from ament_index_python.packages import get_package_share_directory
import launch
import launch.actions
import launch.substitutions
import launch_ros.actions
import os.path

def generate_launch_description():
    rviz_config_dir = os.path.join(get_package_share_directory('sensor_fusion'), 'config', 'fusion.rviz')
    para_dir = os.path.join(get_package_share_directory('sensor_fusion'), 'config', 'sensor_fusion_subscribed_topic.yaml')
    return launch.LaunchDescription([
        launch.actions.DeclareLaunchArgument(
            'node_prefix',
            # if launch file's prefix is "_lanuch.py", change '.' into '_'
            default_value=[launch.substitutions.EnvironmentVariable('USER'), '.'],
            description='Prefix for node names'
        ),
        launch_ros.actions.Node(
            package='rviz2',
            node_namespace='rviz2',
            node_executable='rviz2',
            arguments=['-d', rviz_config_dir]
        ),
        launch_ros.actions.Node(
            package='kitti_pub',
            node_namespace='kitti_pub',
            node_executable='kitti_pub',
            output='screen'
        ),
        launch_ros.actions.Node(
            package='sensor_fusion',
            node_namespace='sensor_fusion',
            node_executable='sensor_fusion',
            parameters=[para_dir],
            output='screen'
        )
    ])
yaml文件编写
# namespace
sensor_fusion:
  # node_name
  sensor_fusion:
    # parameters
    ros__parameters:
      # topic's name will be namespace setted in the launch file plus original publisher topic.
      point_cloud_topic: "/kitti_pub/kitti_points"
      image_topic: "/kitti_pub/kitti_cam02"
      detect_box2d_topic: "/kitti_pub/yolo_det"
CMakeLists.txt文件修改

ament_package() 前面加上以下内容,才能在share文件夹中找到launch文件和config文件。

install(DIRECTORY
  launch
  DESTINATION share/${PROJECT_NAME}/
)
install(DIRECTORY
  config
  DESTINATION share/${PROJECT_NAME}/
)
源文件内读取yaml文件参数并初始化subscriber
// Same type with the parameters in yaml to read the params
string point_cloud_topic, image_topic, detect_box2d_topic;
// Declare and get parameters from yaml or use alternative parameters
this->declare_parameter<string>("point_cloud_topic", "/kitti_pub/kitti_points");
this->declare_parameter<string>("image_topic", "/kitti_pub/kitti_cam02");
this->declare_parameter<string>("detect_box2d_topic", "/kitti_pub/yolo_det");
this->get_parameter_or<string>("point_cloud_topic", point_cloud_topic, "/kitti_pub/kitti_points");
this->get_parameter_or<string>("image_topic", image_topic, "/kitti_pub/kitti_cam02");
this->get_parameter_or<string>("detect_box2d_topic", detect_box2d_topic, "/kitti_pub/yolo_det");
// Declare subscribers
message_filters::Subscriber<sensor_msgs::msg::PointCloud2> pcl_sub;
message_filters::Subscriber<sensor_msgs::msg::Image> img_sub;
message_filters::Subscriber<darknet_ros_msgs::msg::BoundingBoxes> det_sub;
// Initialize subscribers
pcl_sub.subscribe(this, point_cloud_topic);
img_sub.subscribe(this, image_topic);
det_sub.subscribe(this, detect_box2d_topic);

参考链接:

https://docs.ros.org/en/dashing/Tutorials/Launch-Files/Creating-Launch-Files.html

https://docs.ros.org/en/dashing/Tutorials/Launch-system.html

https://blog.csdn.net/qq_33399315/article/details/106863743?spm=1001.2014.3001.5501

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值