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

How to connect remote roscore with python in runtime

asked 2019-08-13 21:42:06 -0500

ugluo gravatar image

The following is the implementation using roscpp:

std::map<std::string, std::string=""> remappings;

remappings["__master"] = master_url;

remappings["__hostname"] = host_url;

ros::init(remappings, "node name");

parement "master_url" and "host_url" come from UI

So how to implement using python?

edit retag flag offensive close merge delete

Comments

Can you please clarify: What are you trying to achieve? What have you tried? What do you expect to happen and what happens instead? And please make sure your code is correctly formatted.

ct2034 gravatar image ct2034  ( 2019-08-14 03:19:51 -0500 )edit

I want to connect remote roscore with python, The above code implements the function of connecting to a remote roscore using roscpp.

ugluo gravatar image ugluo  ( 2019-08-14 03:28:28 -0500 )edit

roscpp: std::map<std::string, std::string=""> remappings; remappings["__master"] = master_url; remappings["__hostname"] = host_url; ros::init(remappings, "node name");

ugluo gravatar image ugluo  ( 2019-08-14 03:32:41 -0500 )edit

the rospy project come from catkin_create_qt_pkg

catkin_create_qt_pkg test_qt

the code in 'src/qnode.cpp' lline 57-59

ugluo gravatar image ugluo  ( 2019-08-14 04:38:44 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2019-08-14 03:41:31 -0500

ct2034 gravatar image

updated 2019-08-15 03:56:00 -0500

I am not familiar with the remapping within the code. The normal way is: You have to set the ROS_MASTER_URI and ROS_HOSTNAMEenvironment variable in the terminal according to https://wiki.ros.org/ROS/NetworkSetup. Then run your python code with rosrun and it will connect to the remote roscore.

edit flag offensive delete link more

Comments

Thanks I know this solution,but I want to implement it in code like roscpp, the parameters come from the UI I have tried to use os.system("shell commond") but failed,like this:

os.system('export ROS_MASTER_URI=http://192.168.0.91:11311 ; echo $ROS_MASTER_URI')
os.system('echo $ROS_MASTER_URI')
ugluo gravatar image ugluo  ( 2019-08-14 04:30:18 -0500 )edit
ct2034 gravatar image ct2034  ( 2019-08-14 05:02:09 -0500 )edit

Thanks, the function of os.putenv() can't set the environment on my own running python,just like the way "launch terminal 、set environment and run command", my rusty code in link text

ugluo gravatar image ugluo  ( 2019-08-15 02:22:30 -0500 )edit

please try os.putenv("ROS_MASTER_URI", "http://192.168.0.91:11311") (and the same for ROS_HOSTNAME) then you can check with print(os.environ). This seems to be also how init_nodegets the params (https://docs.ros.org/melodic/api/rosp... - line 317)

ct2034 gravatar image ct2034  ( 2019-08-15 03:51:02 -0500 )edit

Thank you serserly ,the problem is already solved.

i try to use os.putenv

os.putenv("ROS_MASTER_URI", "http://192.168.0.91:11311")
print(os.environ)
self.node =  rospy.init_node('leap_one_gazebo_control', anonymous=True)
print(os.environ)

then output:

http://localhost:11311
http://localhost:11311

so i print the type(os.environ) and Force modify data of the dictionary

os.environ['ROS_MASTER_URI'] = 'http://192.168.0.91:11311'
os.environ['ROS_MASTER_IP'] = '192.168.0.91'

tne output

http://192.168.0.91:11311
http://192.168.0.91:11311
ugluo gravatar image ugluo  ( 2019-08-15 04:18:55 -0500 )edit

ok, cool. Can you please accept my answer then (click the little check mark)

ct2034 gravatar image ct2034  ( 2019-08-15 04:20:29 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2019-08-13 21:42:06 -0500

Seen: 861 times

Last updated: Aug 15 '19