ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Taking arguments via command line for launch files.

asked 2022-02-11 06:13:18 -0500

NOVA1323 gravatar image

I am trying to pass a value to a parameter in a node via the launch file.

I created a basic node that would print the parameter it read or it would give an error saying no parameter.

even though I pass a parameter the node is not able to read it.

I have added the code of the launch file and node below.

Launch File:

<launch>

<arg name="station" />

<node pkg="param_from_launch" type="trial" name="trial">
  <param name="param_1" value="$(arg station)"/>
</node>

</launch>

C++ Node:

#include "ros/ros.h"
#include <iostream>

int main(int argc, char **argv)
{
  // Set up ROS.
    ros::init(argc, argv, "trial");
    ros::NodeHandle n("~");   // private namespace

    int param_1;

    n.param<int>("param_from_launch", param_1, 0);
    if(n.param("param_from_launch", param_1)){
        ROS_INFO("param_from_launch: %d", param_1);
    }
    else{
        ROS_ERROR("NO PARAM");
    }
    // Let ROS handle all callbacks.
    ros::spin();

    return 0;
}  // end main()

running the node separately gives me the expected output: param_from_launch: 0. but when running the launch file I get [ERROR] [1644580954.583154879]: NO PARAM

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2022-02-11 09:07:19 -0500

I see two basic issues with the example posted in the question:

  1. The <param> tag in the launch file set's the parameter name to param_1. So we will have a parameter /trial/param_1 with a value determined by the station arg to the launch file. However, the node tries to read a parameter called /trial/param_from_launch.
  2. You have not set the output attribute for the node. So even if your node did work as intended you wouldn't see the output messages you're hoping to see (except the calls to ROS_ERROR).

What if you change your launch file to:

<launch>

<arg name="station" />

<node pkg="roscpp_tutorials" type="trial" name="trial" output="screen">
  <param name="param_from_launch" value="$(arg station)" type="int" />
</node>

</launch>
edit flag offensive delete link more

Comments

Thanks, I always thought that the first string was just a placeholder or message.

NOVA1323 gravatar image NOVA1323  ( 2022-02-12 00:16:01 -0500 )edit
0

answered 2022-02-11 07:10:22 -0500

Ranjit Kathiriya gravatar image

running the node separately gives me the expected output: param_from_launch: 0

Because your launch file the peram you are passing is in string formate and the code file the variable and all is in int form.

for example: Your launch file:

<launch>

<arg name="station" />

<node pkg="param_from_launch" type="trial" name="trial">
  <param name="param_1" value="$(arg station)"/>
</node>

</launch>

and your CPP file:

std::string s;
n.param<std::string>("my_param", s, "default_value");
edit flag offensive delete link more

Comments

1

FWIW, it's definitely possible to set integer parameters through a launch file.

jarvisschultz gravatar image jarvisschultz  ( 2022-02-11 09:01:34 -0500 )edit

as @jarvisschultz said you can set integer parameters infact you can also define what kind of parameter you would like using type for eg: <param name="my_param" value="my_value" type="int" />

NOVA1323 gravatar image NOVA1323  ( 2022-02-12 00:13:03 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-02-11 06:13:18 -0500

Seen: 68 times

Last updated: Feb 11 '22