ros2使用launch动态传参

Camill

头文件

首先ROS2launch使用Python编写

必要的头文件

1
2
3
4
5
6
7
import os
import launch

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node

launch参数

1
2
3
def generate_launch_description():
correct_factor_vx_arg = DeclareLaunchArgument('correct_factor_vx', default_value='0.898',
description='correct factor vx, e.g. 0.9')

加上参数服务器传参

1
2
3
4
5
6
7
8
9
10
11
12
13
def generate_launch_description():
correct_factor_vx_arg = DeclareLaunchArgument('correct_factor_vx', default_value='0.898',
description='correct factor vx, e.g. 0.9')

# 添加以下
originbot_base_node = Node(
package='originbot_base',
executable='originbot_base',
output='screen',
emulate_tty=True,
parameters=[{
'correct_factor_vx': LaunchConfiguration('correct_factor_vx'),
}])

return这些参数和执行文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def generate_launch_description():
correct_factor_vx_arg = DeclareLaunchArgument('correct_factor_vx', default_value='0.898',
description='correct factor vx, e.g. 0.9')

originbot_base_node = Node(
package='originbot_base',
executable='originbot_base',
output='screen',
emulate_tty=True,
parameters=[{
'correct_factor_vx': LaunchConfiguration('correct_factor_vx'),
}])

# 添加以下
return LaunchDescription([
correct_factor_vx_arg,
originbot_base_node,
])

命令行传参

1
$ ros2 launch  learning_launch parameters.launch.py correct_factor_vx_arg:=0.777

示例

执行文件(c++)

foxy中如下:

1
2
3
float correct_factor_vx_ = 1.0;
this->declare_parameter("correct_factor_vx"); //声明及获取线速度校正参数
this->get_parameter_or<float>("correct_factor_vx", correct_factor_vx_, 1.0);

humble如下:

1
float correct_factor_vx_ = this->declare_parameter("correct_factor_vx", 1.0);
  • 标题: ros2使用launch动态传参
  • 作者: Camill
  • 创建于 : 2024-02-04 15:08:00
  • 更新于 : 2024-02-23 19:00:00
  • 链接: https://camill.love/note/ros2-launch/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论