ROS | rosparameter server used in C++

When I was writing roslaunch file, and insert a few parameters, which is always a double-type parameter can not be used rosparam get.

ROS official tutorial Using Parameters in roscpp did not solve my problem.

The logic is simple laucn file, is to role_name, and detect_rangethese two parameters to gap_selectorgo use. However, I can only get to role_nameit has failed to get detect_rangevalue.

<!-- -->
<launch>
  <arg name="role_name" default="hero" />
  <arg name="detect_range" default="30.0" />

  <node pkg="gap_selector" type="gap_selector" name="gap_selector" output="screen">
    <param name="role_name" value="$(arg role_name)" />
    <param name="detect_range" value="$(arg detect_range)" />
  </node>

</launch>

The same logic is no problem in python files actually, only a problem in C ++ to write rosnode in.

Toss about an hour, check a number of issues related to the Internet, or return to the beginning of the official tutorial found the answer. In 1.2 param()part very humble heart to such a sentence:

Sometimes the compiler requires a hint for the string type.

So the solution is, in the launch file in the "express" its type:

<!-- -->
<launch>
  <arg name="role_name" default="hero" />
  <arg name="detect_range" default="30.0" />

  <node pkg="gap_selector" type="gap_selector" name="gap_selector" output="screen">
    <param name="role_name" value="$(arg role_name)" />
    <param name="detect_range" type="double" value="$(arg detect_range)" />
  </node>

</launch>

But if the official tutorial with examples, only explicit type in c ++ code can not solve the problem.

  string temp;
  n.param<string>("detect_range", temp, "50");
  cout << "-----------------------------" << temp << endl;

Output is 50 instead of 30.0:

-----------------------------50

Then the corresponding in c ++ Comparative secure worded as follows:

int main(int argc, char **argv) {
  ros::init(argc, argv, "gap_selector");
  ros::NodeHandle n("~");

  double detect_range;
  if (n.getParam("detect_range", detect_range)) {
    ROS_INFO("Got param: %f", detect_range);
  } else {
    detect_range = 50.0;
    ROS_ERROR("Failed to get param 'detect_range'");
  }
  ...
}

Guess you like

Origin www.cnblogs.com/casperwin/p/11432918.html