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

ROS Namespace related confusion

asked 2019-03-08 05:11:06 -0500

ravijoshi gravatar image

I am developing a ROS Package on ROS Indigo in Ubuntu 14.04 LTS OS.

The package contains a launch file with the following content:

<launch>
  <arg name="host" default="172.17.69.137"/>
  <arg name="port" default="1357"/>
  <arg name="timeout" default="1000"/>

  <arg name="model" default="$(find my_package)/files/robot.urdf"/>
  <param name="robot_description" command="$(find xacro)/xacro --inorder $(arg model)"/>
  <param name="use_gui" value="true"/>
  <node name="robot_state_publisher" pkg="robot_state_publisher" type="state_publisher"/>

  <node name="receiver" pkg="my_package" type="receiver" output="screen">
    <param name="host" value="$(arg host)" />
    <param name="port" value="$(arg port)" />
    <param name="timeout" value="$(arg timeout)" />
  </node>
</launch>

The CPP source file uses the above parameters as shown below:

ros::init(argc, argv, "receive_potentio", ros::init_options::AnonymousName);

ros::NodeHandle nh;
ros::Publisher jointStatePub = nh.advertise<sensor_msgs::JointState>("joint_states", 1);

std::string host;
int timeout, port;

// the following doesn't work. However when relative is
// used, i.e. ros::NodeHandle nh("~") it works!
nh.getParam("host", host);
nh.getParam("port", port);
nh.getParam("timeout", timeout);

The absolute namespace defined in this way i.e., ros::NodeHandle nh doesn't get params nh.getParam("host", host) . But when relative is used ros::NodeHandle nh("~"), param nh.getParam("host", host) works. However in this case ros::Publisher jointStatePub doesn't work!

I think I am missing something obvious here. How do I make both of the work together?

edit retag flag offensive close merge delete

Comments

However in this case ros::Publisher jointStatePub doesn't work!

How does a Publisher "not work" exactly?

gvdhoorn gravatar image gvdhoorn  ( 2019-03-08 05:14:20 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
4

answered 2019-03-08 05:18:12 -0500

gvdhoorn gravatar image

updated 2019-03-08 05:20:50 -0500

<node name="receiver" pkg="my_package" type="receiver" output="screen">
  <param name="host" value="$(arg host)" />
  <param name="port" value="$(arg port)" />
  <param name="timeout" value="$(arg timeout)" />
</node>

Here, you're setting host, port and timeout as private parameters of the receiver node.

But here:

ros::NodeHandle nh;
[..]
nh.getParam("host", host);
nh.getParam("port", port);
nh.getParam("timeout", timeout);

You're trying to access those parameters as if they're "public" (ie: exist in some other namespace than the node's namespace).

That won't work.

// the following doesn't work. However when relative is
// used, i.e. ros::NodeHandle nh("~") it works!

And that makes sense, as ~ has a special meaning as a namespace name (which is what the first argument in that particular ros::NodeHandle ctor is): it refers to the private namespace of the node in which the NodeHandle is created.

So now you're reading private parameters from the private namespace. And that succeeds.

See also wiki/Parameter Server.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2019-03-08 05:11:06 -0500

Seen: 304 times

Last updated: Mar 08 '19