ros2 runtime_error ai_generated true

rclpy.exceptions.ParameterAlreadyDeclaredException: Parameter 'use_sim_time' has already been declared

ID: ros2/parameter-already-declared

Also available as: JSON · Markdown
92%Fix Rate
90%Confidence
3Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2 active

Root Cause

declare_parameter() called twice for the same parameter name. Common when mixing automatic and manual declaration.

generic

Workarounds

  1. 92% success Use has_parameter() before declare_parameter()
    if not self.has_parameter('my_param'): self.declare_parameter('my_param', default_value)
  2. 95% success Declare all parameters once in __init__ and use get_parameter() elsewhere
  3. 85% success Set automatically_declare_parameters_from_overrides=False if declaring manually

Dead Ends

Common approaches that don't work:

  1. Catch the exception and ignore it 70% fail

    Silently uses the first declaration value; later code may expect a different default

  2. Undeclare then re-declare the parameter 65% fail

    Causes a brief period where the parameter does not exist; external observers may see inconsistent state