Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filterROSArgs function in init.cpp #694

Merged
merged 1 commit into from
Nov 9, 2015

Conversation

Karsten1987
Copy link
Contributor

Allows a pre-filtering of ROS keywords before calling ros::init().

This is an alternative to ros::this_node::getName() [0], since this requires to call ros::init(argc, argv, "node_name") beforehand and forcing to specify a node name.
ros::filterROSArgs can be called wo/ init for filter various keywords such as __name or __ns.

$ rosrun hello_world hello_world __name:=foo
// hello_world.cpp
int main( int argc, char** argv )
{ 
  std::string node_name = ros::filterROSArgs(argc, argv, "__name");
  if (!node_name.empty())
    ros::init(argc, argv, node_name);
  else
    ros::init(argc, argv, "bar");

  return 0;
}

In case the desired argument is not found, an empty string is returned.

@ros-pull-request-builder
Copy link
Member

Can one of the admins verify this patch?

@wjwwood
Copy link
Member

wjwwood commented Nov 3, 2015

I guess you'll be using this for some other ROS parameter or in some other way? The reason I ask is that this code is equivalent to your snippet above:

// hello_world.cpp
int main( int argc, char** argv )
{ 
  ros::init(argc, argv, "bar");

  return 0;
}

If you run it without a __name:= argument it will be called bar, but if you call it with __name:=foo it will be called foo.

@dirk-thomas
Copy link
Member

Can you please describe your use case?

Beside that I am not sure if filterROSArgs is a suitable name for the function. Since it doesn't really filter anything and only checks for one specific argument I would suggest calling it getROSArg instead.

@Karsten1987
Copy link
Contributor Author

@wjwwood I might be wrong here, but ros::this_node::getName() resolves the complete namespace. So the example you gave would lead to /foo. Launching the executable inside a namespace bar_ns would return /bar_ns/foo as a name. Can you confirm that?

Initial motivation comes from this PR ros-naoqi/naoqi_driver#44.
Other use cases are launching the same executable for different devices (e.g. IP cameras) and adjusting their unique topic/rosparam names on runtime via a respective launch file.

In general I was wondering how you'd access the __name variable anyways which is passed by a launch file when inside your ros::init you overwrite this by a hard-coded name?

@dirk-thomas Sure, there is no problem in renaming this. You guys might find better names according to the context.

@dirk-thomas
Copy link
Member

@Karsten1987 Can you please update the function according to my previous comment.

@Karsten1987 Karsten1987 force-pushed the filter_ros_args branch 3 times, most recently from a598688 to 6c37765 Compare November 6, 2015 23:37
@dirk-thomas
Copy link
Member

@ros-pull-request-builder
Copy link
Member

Test passed.
Refer to this link for build results: http://jenkins.ros.org/job/_pull_request-indigo-ros_comm/431/

@dirk-thomas
Copy link
Member

Thanks for iterating on this. Next time please also comment on the ticket since there is no notification for pushing to a PR.

dirk-thomas added a commit that referenced this pull request Nov 9, 2015
filterROSArgs function in init.cpp
@dirk-thomas dirk-thomas merged commit bea196d into ros:indigo-devel Nov 9, 2015
rsinnet pushed a commit to MisoRobotics/ros_comm that referenced this pull request Jun 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants