PARAM-4001 ros2 runtime_error ai_generated true

rclcpp.exceptions.ParameterAlreadyDeclaredException: 参数 'my_param' 已经被声明

rclcpp.exceptions.ParameterAlreadyDeclaredException: parameter 'my_param' has already been declared

ID: ros2/parameter-declare-not-allowed-during-callback

其他格式: JSON · Markdown 中文 · English
93%修复率
89%置信度
1证据数
2023-12-05首次发现

版本兼容性

版本状态引入弃用备注
ROS2 Humble Hawksbill active
ROS2 Iron Irwini active
ROS2 Jazzy Jalisco active

根因分析

节点尝试声明一个已经声明过的参数,通常是由于在同一个节点中两次调用 `declare_parameter`,或者因为该参数已被父类自动声明(例如 `use_sim_time`)。

English

A node attempts to declare a parameter that was already declared, often due to calling `declare_parameter` twice in the same node, or because the parameter was automatically declared by a parent class (e.g., `use_sim_time`).

generic

官方文档

https://docs.ros.org/en/humble/Tutorials/Intermediate/Parameters/Understanding-ROS2-Parameters.html

解决方案

  1. Check if the parameter already exists before declaring, using `has_parameter` in C++ or Python. This is especially important when inheriting from nodes that declare common parameters like 'use_sim_time'.
  2. If the parameter is declared by a parent class (e.g., from `rclcpp::Node`), avoid redeclaring it. Instead, set its value via the node options or parameter overrides.

无效尝试

常见但无效的做法:

  1. 50% 失败

    Wrapping the declare_parameter call in a try-except block to ignore the exception may cause the parameter to have an unintended default value, leading to subtle bugs.

  2. 60% 失败

    Changing the parameter name to a different string (e.g., 'my_param_2') avoids the error but doesn't fix the duplicate declaration; it may also break other code expecting the original name.