[ROS Q&A] 146 – Command line argument passing in rosrun

[ROS Q&A] 146 - Command line argument passing in rosrun

Written by Marco Arruda

03/08/2018

 

In this video we are going to explore how to pass an argument through command line using rosrun.

It can be a bit confusing, but we are going to explain the difference of the return of the method depending of the type of the value you pass to the argument.

 

Step 1. Create a project in ROS Development Studio(ROSDS)

ROSDS helps you follow our tutorial in a fast pace without dealing without setting up an environment locally. If you haven’t had an account yet, you can create a free account here. Let’s call the project rosrun_param.

Step 2. Create package

We’ll create a package for our code with the roscpp and std_msgs dependency.

cd ~/catkin_ws/src
catkin_create_pkg my_pkg roscpp std_msgs

Under the my_pkg/src folder, create a my_pkg_node.cpp file with the code from the question.

#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("~");
  ROS_INFO("Got parameter : %s", param.c_str());

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

To compile the code, we have to modify the CMakeLists.txt file. Please uncomment the add_executable and target_link_libraries in the build part.

Then we can compile the code with

cd ~/catkin_ws
catkin_make

After compile, we can run the executable with

rosrun my_pkg my_pkg_node

The output is just don’t run anything because we didn’t pass any argument yet.

As an example, we can pass arguments like the following

rosrun my_pkg my_pkg_node _blue:=text

Then the output is blue.

However, the code doesn’t quite make sense. Let’s modify it.

#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;
}

Now if you pass blue or green as arguments, it will print the color. If you pass something else, it will run nothing.

Want to learn more?

If you are interested in ROS and want to learn more about it, please check our ROS Basics C++ Course.

 

Edit by: Tony Huang

RELATED LINKS

ROS Development Studio (RDS)
Robot Ignite Academy
ROS Basics C++ Course

 


Feedback

Did you like this video? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS topics, please let us know on the comments area and we will do a video about it.

Topics:
Masterclass 2023 batch2 blog banner

Check Out These Related Posts

129. ros2ai

129. ros2ai

I would like to dedicate this episode to all the ROS Developers who believe that ChatGPT or...

read more

0 Comments

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This