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

Command line argument passing in rosrun

asked 2018-07-30 06:20:28 -0600

kk2105 gravatar image

Hi All,

I am trying to pass the command line arguments while running

  rosrun <package> <node> <parameter>

So that a particular block of code could be executed based on the parameter.

Below is the approach.

     int main(int argc, char *argv[])
     {  
            std::string param;
            ros::init(argc, argv, "node_name");
            ros::NodeHandle nh("~");
            ROS_INFO("Got parameter : %s", param.c_str());

            if(nh.getParam("blue", param))
            {
                 blue();    //Run program blue
            }
            else if(nh.getParam("green", param))
            {
                green();
            }
            else
            {
                 cout << "Don't run anything !! " << endl;
            }
            return 0;
       }

Also when I try to print the value of param it shows nothing.

Can anybody help me to fix this please.

Thank you, KK

edit retag flag offensive close merge delete

5 Answers

Sort by ยป oldest newest most voted
4

answered 2018-07-31 04:29:11 -0600

kk2105 gravatar image

updated 2018-08-03 01:32:51 -0600

I was able to get the expected result by updating the code as given below.

 int main(int argc, char *argv[])
 {  
       ros::init(argc, argv, "node_name");
       std::string param;
       ros::NodeHandle nh("~");
       std::string check;
       nh.getParam("param", check);
       cout << check << endl;
       ROS_INFO("Got parameter : %s", check.c_str());

        if(check.compare("blue") == 0)
        {
          cout << check << endl;
          blue();
        }
         else if(check.compare("greeen") == 0)
        {
          cout << check << endl;
          green();
        }
        else
        {
          cout << "Control has come out !!! " << endl;
        }  
        return 0;
  }

command to run

 rosrun package node_name _param:=blue
 rosrun package node_name _param:=green
edit flag offensive delete link more
5

answered 2019-04-12 03:14:10 -0600

KOOYOO gravatar image

it only works when you write this ros::NodeHandle nh("~");

edit flag offensive delete link more
4

answered 2018-08-03 08:19:46 -0600

Just to complement, I think your original code is a bit confusing. You are expecting to have a parameter with the type "string", instead of expecting a specific value for a given parameter.

It's important to pay attention to the return of nh.getParam(). For example, if you run with _blue:=true or _blue:=1, you have false return because it expects to fill a string variable, not a boolean or an integer. But if you run _blue:=some_text, you have true return, because the type matches.

Just to complement, you can remove the parameter param or you can use it and remove check, it gets cleaner. I tried in my local environment and it worked like that:

#include<ros/ros.h>
#include<iostream>

using namespace std;

 int main(int argc, char *argv[])
 {  
    std::string param;
    ros::init(argc, argv, "node_name");
    ros::NodeHandle nh("~");
    nh.getParam("param", param);
    ROS_INFO("Got parameter : %s", param.c_str());

    if(param.compare("blue") == 0)
    {
        cout << "blue " << endl;
    }
    else if(param.compare("green") == 0)
    {
        cout << "green " << endl;
    }
    else
    {
        cout << "Don't run anything !! " << endl;
    }
    return 0;
}
edit flag offensive delete link more

Comments

@marcoarruda Thanks for your response. Could you please let me know in this case how you would be mentioning the parameter while executing the rosrun ?

For example:

 rosrun my_package my_nodename _param:=blue  ??
kk2105 gravatar image kk2105  ( 2018-08-03 12:39:31 -0600 )edit
1

Yes, that's exactly how I am running it.You can check in this video I recorded https://www.youtube.com/watch?v=CQKZE...

marcoarruda gravatar image marcoarruda  ( 2018-08-03 13:02:20 -0600 )edit

@marcoarruda Thanks man. Gone through the video, it was really helpful. In your case instead of using a separate variable name, you have used the same "param", correct ?

kk2105 gravatar image kk2105  ( 2018-08-03 13:25:38 -0600 )edit

Exactly! As far as I understood, you don't need two parameters, but a single one is enough to choose a behavior in your algorithm (blue, green, yellow, etc.) Furthermore, you can change the type of param you want to use. If you have only 2 options, you can use boolean. For more options, integer..

marcoarruda gravatar image marcoarruda  ( 2018-08-03 13:34:33 -0600 )edit

@marcoarruda I had more than 5 cases, so I used if else to compare strings. I have posted the approach I used as answer in this,

kk2105 gravatar image kk2105  ( 2018-08-03 13:37:41 -0600 )edit
1

answered 2018-07-30 07:23:56 -0600

According to rosrun documentation you can do following:

rosrun my_package my_node _my_param:=value

Concrete in your case to run "blue" code should be:

rosrun my_package my_node _blue:=true
edit flag offensive delete link more

Comments

@destogl Thanks for the swift response. I will try and update you.

kk2105 gravatar image kk2105  ( 2018-07-30 07:29:38 -0600 )edit

@destogl .. Unfortunately the given method did not work.

kk2105 gravatar image kk2105  ( 2018-07-30 08:28:39 -0600 )edit

In your code snippet above you're printing out the value of Param before it is being set, so it will always be empty. If you move the ROS_INFO statement inside the first if then it should show the parameter value.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-07-30 10:09:30 -0600 )edit

@PeteBlackerThe3rd . Thanks for the response. I was able to fix the issue and got the expected results. Will be posting the answers.

kk2105 gravatar image kk2105  ( 2018-07-31 04:23:16 -0600 )edit
1

answered 2018-10-01 08:47:55 -0600

SS6141 gravatar image

Add _ (underscore) before the parameter name to make it identify and remap as a variable inside the node. Adding nothing will help the ROS to identify it as a topic and remap the same.

Syntax

rosrun package node _variable:=value

rosrun package node topic:=/topic_name

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2018-07-30 06:20:28 -0600

Seen: 36,660 times

Last updated: Apr 12 '19