0
$\begingroup$

Rosanswers logo

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


Originally posted by NOVA1323 on ROS Answers with karma: 3 on 2022-02-11

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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>

Originally posted by jarvisschultz with karma: 9031 on 2022-02-11

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by NOVA1323 on 2022-02-12:
Thanks, I always thought that the first string was just a placeholder or message.

$\endgroup$

Your Answer

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