外观
踩坑Robotics Stack Exchange2024-08-16
Python launch 中无法获取 LaunchArgument 值
本文由 AI 根据公开内容整理,可能有误,请以原文为准:Getting the value of LaunchArgument inside python Launch file(Robotics Stack Exchange #104340)
状态:已解决
问题
提问者想在 Python launch 文件内部获取 DeclareLaunchArgument 的运行时值,但 LaunchArgument 变量本身无法直接取值,LaunchConfiguration.perform(LaunchContext) 又需要运行时 LaunchContext。
环境
ROS 2
解决办法
- 定义
OpaqueFunction(function=launch_setup),让launch_setup(context)接收正确的运行时context。 - 在
launch_setup中使用LaunchConfiguration('config').perform(context)获取运行时参数值。 - 根据该值执行后续逻辑,并返回要启动的
Node等 action。 - 在
generate_launch_description()中创建LaunchDescription(launch_args),再用ld.add_action(opfunc)加入OpaqueFunction。
原问题作者:Stanislav Svědiroh;采纳答案作者:Stanislav Svědiroh。原内容依 CC BY-SA 4.0 授权,本页摘要同样依 CC BY-SA 4.0 授权。
来源
本页事实对应的公开页面,以原文为准。