How to set params? [closed]

asked 2014-06-17 21:25:22 -0600

Luis Ruiz gravatar image

updated 2014-06-17 23:00:15 -0600

Hello, I am following the tutorials, but I cannot make my program receive params, it is a very simple one:

#include <ros/ros.h>
#include <iostream> // System libraries
#include <string.h>
#include <stdio.h>

int main(int argc, char** argv)
{
  // Init the node
  ros::init(argc, argv, "paramTest");
  ros::NodeHandle nh_;

  // Declare variables that can be modified by launch file or command line.
  std::string string1;
  int int1;
  int int2;

  // Initialize node parameters from launch file or command line.
  nh_.param<std::string>("string1", string1, "Hello");
  nh_.param<int>("int1", int1, 1 );
  nh_.param<int>("int2", int2, 2 );

  ROS_INFO( "int1 = %d", int1 );
  int int3;
  if (nh_.getParam("int1", int3))
      ROS_INFO( "int3 = %d", int3 );
  else
      ROS_ERROR( "Failed to retrieve parameter" );

return 0;
} // main

I run 'roscore' in one window, then in a new one:

rosrun ros_test paramTest _int1:="4"

I got

[ INFO] [1403057842.577851408]: int1 = 1
[ERROR] [1403057842.578736057]: Failed to retrieve parameter

Not only the value is not changed, the variable is not found :/

What am I missing???

Thanks.

[EDIT]

I had to create a handler to the node namespace with ("~") :P.

  // Initialize node parameters from launch file or command line.
  ros::NodeHandle nhl_("~");

  ROS_INFO( "int1 = %d", int1 );

  int int2;
  if (nhl_.getParam("int1", int2))
      ROS_INFO( "int2 = %d", int2 );
  else
      ROS_ERROR( "int1 not in launch file" );

return 0;
} // main
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Luis Ruiz
close date 2014-06-17 23:01:48.036308