【学习总结】ROS2的launch文件配置

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

本文仅用于个人学习记录。

.launch.py

ROS2的launch文件是python格式,如下:

import launch
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch_ros.actions import Node
from launch.substitutions import LaunchConfiguration
from launch.conditions import IfCondition
import os

def generate_launch_description():
    # Declare the launch argument
    rviz_arg = DeclareLaunchArgument('rviz', default_value='true', description='Launch RViz')

    # Absolute paths to configuration files
    mapping_yaml_path = '/home/larry/p2v_slam_ws/src/p2v_slam_ros2/config/office.yaml'  # Use your actual absolute path
    rviz_config_path = '/home/larry/p2v_slam_ws/src/p2v_slam_ros2/launch/rviz.rviz'  # Use your actual absolute path

    print(f"Mapping YAML path: {mapping_yaml_path}")

    # Node for lio_node
    lio_node = Node(
        package='p2v',
        executable='lio_node',
        name='lio_node',
        output='screen',
        parameters=[mapping_yaml_path]
    )

    # Node for rviz (only if the rviz argument is true)
    rviz_node = Node(
        package='rviz2',
        executable='rviz2',
        name='rviz',
        output='screen',
        arguments=['-d', rviz_config_path],
        condition=IfCondition(LaunchConfiguration('rviz'))
    )

    return LaunchDescription([
        rviz_arg,  # Declare the argument to launch RViz
        lio_node,  # Launch lio_node from p2v package
        rviz_node   # Conditionally launch RViz if 'rviz' argument is true
    ])

载入yaml文件

需要配置参数文件。yaml参数文件和ros1不同的是,需要在最开始加入节点名称,然后下一级是ros__parameters,接下来是其他参数。如下:

lio_node:
	ros__parameters:
		lidar_topic: /livox/lidar
		imu_topic: /livox/imu
		map_frame: lidar
		...

需要注意:

  1. 可能需要对yaml文件权限进行修改,否则可能会报错:

“Error: Error opening YAML file, at ./src/parser.c:271, at ./src/rcl/arguments.c:415”

  1. yaml格式需要注意第一行是node名称,第二行是ros__parameters,不按照格式,会报错:

Error: Cannot have a value before ros__parameters at line 1, at ./src/parse.c:806, at ./src/rcl/arguments.c:415

启动指令

ros2 launch <node> xxx.launch.py

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值