Sharing c++ instance in two ros node

asked 2020-09-18 07:27:16 -0500

YY gravatar image

updated 2020-09-18 15:00:35 -0500

Hello everyone,

I recently got a problem while using ros:

Now: Let's say we have two robots, each robot has one ROS node. They need the same map for working. And the map is a instance of c++ class. Currently the map will be intialized both in robot_1 and robot_2, but it seems unefficient to me, because if I have 1000 robots, then the map need to be intialized in each robot.

Goal: it will be nice, if map class only need to be initialized once, and then shared between robots. With ros msg or srv seems not possible for me, since there are a lot of functions included in „Map“ class.

Thanks guys in advance! :)

edit retag flag offensive close merge delete


Have you looked at the map server? ROS has facilities to load a single map and let multiple subscribers it.

JackB gravatar image JackB  ( 2020-09-18 14:37:19 -0500 )edit

Hi @JackB, thanks, I just had a look. It seems map_server need to use ROS specific map format. In my usecase, the "map" class has different format than the ROS map and it has more information. It's very similar to the openDrive format. . So it seems to me hard to use the map_server, please correct me if I understand it wrong. :)

YY gravatar image YY  ( 2020-09-18 14:46:47 -0500 )edit

Ah, well that is a little more complicated :) It looks like that format is an xml description file, which is from what I see, not natively supported by ROS. You may be able to define a custom message type link text but it seems like it will get pretty complex pretty quick. Question for you, why do you run both robots on the same node? Should you not have one node for robot1 and one node for robot2?

JackB gravatar image JackB  ( 2020-09-18 14:55:57 -0500 )edit

Hi @JackB, you are right, the "map" is not natively supported by ROS, that's way I created a C++ wrapper for the map information. Two robots has two different nodes, maybe I didn't explain it clearly, but they are in different nodes. And I currently cannot find a way to share memory or object in two ros nodes.

YY gravatar image YY  ( 2020-09-18 14:59:56 -0500 )edit

Ah now I think I understand your problem. You should consider ROS nodelets link text. I am not an expert on this, but my understanding is that with nodelets your nodes share a common process space. This means that they share the same heap (I think), which means you can share memory between different nodes. ROS formalizes this idea with "zero copy pointer passing between publish and subscribe calls within the same node". Does this sound closer to what you are looking for?

JackB gravatar image JackB  ( 2020-09-18 15:11:00 -0500 )edit

I know this work for ROS messages, but not sure about sharing memory "manually" outside of the ROS interface. If you can't get your information into a ROS message maybe this won't help you?

JackB gravatar image JackB  ( 2020-09-18 15:13:46 -0500 )edit

Hi @JackB, thanks for your help again. I just had a look to the nodelets. It seems to me that I can still only pass the ROS msg but via c++ pointer (nodelet) instead of TCP (node).

The plugin seems something I need, not sure.

YY gravatar image YY  ( 2020-09-19 03:39:40 -0500 )edit

Yes I think you can only share the data with ROS msg ConstPtr which is a limitation. I look forward to hearing about how you solve this problem if you do.

JackB gravatar image JackB  ( 2020-09-19 10:45:29 -0500 )edit