本文仅用于个人学习记录。
.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
...
需要注意:
- 可能需要对yaml文件权限进行修改,否则可能会报错:
“Error: Error opening YAML file, at ./src/parser.c:271, at ./src/rcl/arguments.c:415”
- 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

1697

被折叠的 条评论
为什么被折叠?



