0
$\begingroup$

I am trying to declare two string parameters vid and pid for a serial communication node in ROS2. However, when I use the following launch file to pass my parameters:

<launch>
  <node pkg="serial" exec="serial">
    <param name="vid" type="str" value="0483"/>
    <param name="pid" type="str" value="5740"/>
  </node>
</launch>

I got the following error:

[serial-1] terminate called after throwing an instance of 'rclcpp::exceptions::InvalidParameterTypeException'
[serial-1]   what():  parameter 'vid' has invalid type: Wrong parameter type, parameter {vid} is of type {string}, setting it to {double} is not allowed.

It seems like my type specification was never respected by roslaunch.

According to the official documentation it seems like ROS2 removed support for explicit type declaration. I want to understand the rationale behind this change and find out possible alternative approaches (without moving away from xml format).


Update: Add C++ source code for reproducibility

class SerialNode: public rclcpp::Node {
  SerialNode(): Node("serial") {
    declare_parameter<std::string>("pid", "");
    declare_parameter<std::string>("vid", "");
    declare_parameter<int>("baud", 115200);
    const auto vid = get_parameter("vid").as_string();
    const auto pid = get_parameter("pid").as_string();
    const auto baud = get_parameter("baud").as_int();
  }
  /* irrelevant code omitted ... */
}

Update:

  1. The python version of launch file also produces identical error.
  2. I added a letter x in front of my values (i.e. x0483 and x5740 instead of 0483 and 5740), then the parameters are successfully passed as strings. I think this is a very strong proof that my code is not causing this error.
[serial-1] No serial port found for device x0483:x5740
$\endgroup$

1 Answer 1

1
$\begingroup$

Can you update your launch file like this? I think there is a problem on your XML file.

from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    return LaunchDescription([
        Node(
            package='serial',  
            executable='serial',
            name='serial',
            output='screen',
            parameters=[
                {'vid': 0x0483},  # 'vid' parameter
                {'pid': 0x5740},  # 'pid' parameter
            ]
        ),
    ])
$\endgroup$
12
  • $\begingroup$ I've checked it multiple times. I've updated my question and added my C++ code. $\endgroup$ Commented Sep 17 at 8:56
  • $\begingroup$ Here is the funny part: roslaunch interprets 0483 as "double" just because of the preceding 0. It treats 5740 as "int", and in case vid or pid contains a letter a - f (since they are hex strings) roslaunch would treat them as string instead. I feel totally out of control of its funny behavior. $\endgroup$ Commented Sep 17 at 9:08
  • $\begingroup$ Can you use launch.py instead of launch.xml? I have updated my reply on code, please check it. $\endgroup$ Commented Sep 17 at 17:16
  • 1
    $\begingroup$ Yeah I am aware of this solution, but I think roslaunch should be better than this. The fix is not elegant and induces unnecessary burden on developers. Imagine a system which previously worked fine got crashed just because a string parameter being changed to only digits... $\endgroup$ Commented Sep 17 at 21:28
  • 1
    $\begingroup$ I've opened an issue at ros2/launch, not sure if it's the correct location. $\endgroup$ Commented Sep 17 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.