3

I'm working with ROS melodic and Gazebo 9.9.0 on an Ubuntu 18.04.2 LTS.

I want to get two boolean parameters from command line. To do it, I have this code:

int main(int argc, char **argv)
{
  bool doDiagonalSteps = false;

  bool checkDiagonalCornerUnBlocked = false;
  /**
   * We must call one of the versions of ros::init() before using any other
   * part of the ROS system.
   */
  ros::init(argc, argv, "astar_controller");

  /**
   * NodeHandle is the main access point to communications with the ROS system.
   */
  ros::NodeHandle n;

  /**
   * Get parameters (if there are parameters to get.)
   */
  if (!n.getParam("diagonalSteps", doDiagonalSteps))
    doDiagonalSteps = false;
  else
  {
    std::cout << "Diagonal steps: " <<  std::endl;
  }


  if (!n.getParam("checkDiagonalStepCorner", checkDiagonalCornerUnBlocked))
    checkDiagonalCornerUnBlocked = false;
  else
  {
    std::cout << "Diagonal Corner: " << checkDiagonalCornerUnBlocked << std::endl;
  }

When I run this command on terminal:

rosrun nav_astar_nodes astar_controller _diagonalSteps:=true _checkDiagonalStepCorner:=false

I don't get anything on the output.

How can I get a boolean parameter from command line?

Benyamin Jafari
  • 242
  • 2
  • 12
VansFannel
  • 221
  • 1
  • 8
  • and BTW: you can use nh.param("diagonalSteps", doDiagonalSteps, false); to use a default parameter. (http://wiki.ros.org/roscpp/Overview/Parameter%20Server#Getting_Parameters) – FooTheBar Jul 29 '19 at 07:15

1 Answers1

2

When you create the node handle, you need to set it to the private namespace:

ros::NodeHandle n("~");

Then the call to n.getParam("diagonalSteps", doDiagonalSteps) will look for the parameter in the namespace /astar_controller (the name of your node) where it was set when you passed an under-scored variable to it from the command line, instead of the global namespace which is what happens by default.

adamconkey
  • 360
  • 2
  • 10