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

- **ID:** `ros2/parameter-declare-not-allowed-during-callback`
- **领域:** ros2
- **类别:** runtime_error
- **错误码:** `PARAM-4001`
- **验证级别:** ai_generated
- **修复率:** 93%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| ROS2 Humble Hawksbill | active | — | — |
| ROS2 Iron Irwini | active | — | — |
| ROS2 Jazzy Jalisco | active | — | — |

## 解决方案

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.
   ```

## 无效尝试

- **** — 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. (50% 失败率)
- **** — 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. (60% 失败率)
