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

How to make a copy of all data contained within a ROS topic message?

asked 2014-04-01 17:36:49 -0600

mysteriousmonkey29 gravatar image

updated 2014-04-04 10:45:05 -0600

ahendrix gravatar image

Hello, I'm trying to write a node that subscribes to the kinect data from multiple turtlebots, then concatenates/averages the data, and republishes the resulting single data stream to rviz as part of a larger program intended to map a room cooperatively with multiple turtlebots at once.

I've already written a node that subscribes to kinect data from multiple turtlebots, but now am struggling with the republishing. I have several callback methods that are call every time a message is published on a certain topic, and within each of these callback methods, I would like to copy all the data contained within the message to a temporary global variable which can then be accessed in the main class. However, the only way I can think of to do this is to go through and manually copy each individual piece of data from the message to the global variable. Is there a simpler way to do this? For example, is there a command that I could use to create a copy of each message within each callback function, which I could call and then just modify whatever parts I wanted to modify, instead of having to manually copy each data piece?

Edit: Yes, I am using C++. Here is the odometry (location/heading) callback function. RIght now it just prints out the relevant data:

void OdomCallback(const nav_msgs::Odometry locator)
  ROS_INFO("Position: %f,%f", locator.pose.pose.position.x,locator.pose.pose.position.y);
  ROS_INFO("Heading: linear:%f,angular:%f", locator.twist.twist.linear.x,locator.twist.twist.angular.z);

So you're saying that in order to copy the message I would add in a line like this? nav_msgs::Odometry globalLocationVariable=locator;

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2014-04-02 03:09:02 -0600

demmeln gravatar image

Are you using C++? You can simply copy the data into a new object? Like

FooMsg copy = callbackMsg;

It would look a bit different if you use pointer instead of references in your callbacks. Can you edit your question to include some sample code (at least one of the callbacks)?

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2014-04-01 17:36:49 -0600

Seen: 5,820 times

Last updated: Apr 04 '14