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

set ROS_MASTER_URI directly in code

asked 2014-02-20 12:36:45 -0500

major gravatar image

updated 2016-10-24 08:59:21 -0500

ngrennan gravatar image

is it Possible to set ROS_MASTER_URI directly in c++ code without use of environment variables????

I use a big program with needs a lot time of initialisation, and I builded roslibs wichthe program uses but I like to change sometimes the masteruri,the hostname without to setting new env-vars and reload the whole programm. It would be nice if I colud set them as argument in ros::init or so. Thus the programm could change dynamically the server without restart.

(want to change ROS_IP,ROS_HOSTNAME too)

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2014-02-21 07:58:13 -0500

ahendrix gravatar image

ROS does not allow you to change the master URI at runtime. The internal hooks to change the master URI, subscribe and publish topics to the new master don't exist.

If you want your node to communicate with different topics or a different ROS master at runtime, I suggest you take a look at some of the topic and multimaster tools that can be used to forward topics between different ROS masters:

edit flag offensive delete link more


What about the parameters in ros::init() is it possible the config it once for the whole session without use of environment variables??

major gravatar image major  ( 2014-02-22 04:44:02 -0500 )edit

Sure; just run your node as 'rosrun my_pkg my_node __master:=http://foo:11311' or './my_node __master:=http://foo:11311' using the remapping arguments you found. Or, collect the appropriate environment variables into two rc files, and source the appropriate file before your run.

ahendrix gravatar image ahendrix  ( 2014-02-22 08:19:29 -0500 )edit

and with the function ros::shutdown() is it possible to call ros::init(...) with pass arguments for the first ros_master the after doing some stuff call ros::shutdown() and then to start ros::init again with another ros_master and doing stuff on that one ??? (i dont want to do stuff on both simultanously just want to change it in runtime for longer periods without shuting down the progranm)

major gravatar image major  ( 2014-02-24 02:57:17 -0500 )edit

I don't think anyone has tried to use the ROS API that way before. I suspect you'll run into some weird corner cases and bugs.

ahendrix gravatar image ahendrix  ( 2014-02-24 06:42:18 -0500 )edit

answered 2014-02-20 23:38:41 -0500

major gravatar image

I found a way by my own but could not verify it: it is possible to pass arguments to ros::init through command line:

I m not using rosrun and start some nodes by my own and will try to pass a special sting to ros::init(argc, argv, "my_node_name");

with arguments like std::string my_master = "__master="

and call ros::init(1, my_master, "my_node_name");

edit flag offensive delete link more


Did you manage to change the ROS node Master_URI in runtime ? Please give a feedback gravatar image  ( 2018-10-30 04:03:00 -0500 )edit

Question Tools


Asked: 2014-02-20 12:36:45 -0500

Seen: 3,877 times

Last updated: Feb 21 '14