# rclpy.exceptions.ParameterNotSetException：参数 'my_param' 未设置，但通过 --ros-args -p 提供了覆盖

- **ID:** `ros2/rclpy-parameter-override-not-applied`
- **领域:** ros2
- **类别:** config_error
- **错误码:** `RCLPY_PARAM_OVERRIDE_001`
- **验证级别:** ai_generated
- **修复率:** 87%

## 根因

参数覆盖在节点声明之前提供，或者节点使用不同的名称/类型声明参数，导致覆盖被忽略。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Humble | active | — | — |
| Iron | active | — | — |
| Jazzy | active | — | — |

## 解决方案

1. ```
   Declare the parameter with a default value before using it in the node: self.declare_parameter('my_param', 'default_value'). Then ensure the override is passed in the launch file after the node is started: <node pkg="my_pkg" exec="my_node" name="my_node"><param name="my_param" value="override_value"/></node>
   ```
2. ```
   Use the 'allow_undeclared_parameters' option in the node's constructor: node = rclpy.create_node('my_node', allow_undeclared_parameters=True). Then overrides will be applied regardless of declaration order.
   ```
3. ```
   Move the parameter declaration to the beginning of the __init__ method, before any other node operations, and use the same name as in the override.
   ```

## 无效尝试

- **Add more --ros-args -p arguments with different parameter names** — The issue is timing or naming, not the number of overrides; adding more won't fix the mismatch. (90% 失败率)
- **Use a YAML file instead of command-line -p arguments** — YAML files have the same timing and naming constraints; the root cause remains. (80% 失败率)
- **Set the parameter in the node's constructor before calling super().__init__()** — Parameters must be declared after the node is initialized; doing so before can cause undefined behavior. (85% 失败率)
