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

object pointer as ROS message

asked 2015-05-05 11:59:35 -0500

Yianni gravatar image

updated 2015-05-05 12:02:51 -0500

Is it possible to pass an object pointer as a ROS message? Below a MWE, that doesn't even get to compile with error message:

talker.cpp:20:12: error: invalid conversion from ‘Foo*’ to ‘std_msgs::UInt64_<std::allocator<void> >::_data_type {aka long unsigned int}’ [-fpermissive]
msg.data = &foo;
         ^

MWE:

#include <iostream>
#include "ros/ros.h"
#include "std_msgs/UInt64.h"

class Foo
{
   public:
      int count;
};

int main(int argc, char **argv)
{
  ros::init(argc, argv, "talker");
  ros::NodeHandle n;
  ros::Publisher chatter_pub = n.advertise<std_msgs::UInt64>("chatter", 1000);
  ros::Rate loop_rate(10);
  Foo foo;
  foo.count = 0;
  std_msgs::UInt64 msg;
  msg.data = &foo;
  while (ros::ok())
  {
    chatter_pub.publish(msg);
    ros::spinOnce();
    loop_rate.sleep();
    ++foo.count;
  }
  return 0;
}

Might be a silly question give that the pointer is of the object type pointer, but all I need to do is pass the integer hex memory address to another node (and hopefully create a same object pointer that points to the same address). Possibly, a dangerous thing to do but it will suffice. Any help appreciated.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2015-05-05 12:52:47 -0500

ahendrix gravatar image

You can't do this.

The OS will run each process in its own memory space, and this will prevent and kind of memory-sharing between processes. Even if your program compiled and ran, it would not work; you'd either get a segmentation fault or silently corrupt memory.

Instead, you should create a custom message type for the data you're trying to pass between nodes, and transmit the entire data structure from one node to another. (This will also work even if your nodes aren't running on the same computer).

edit flag offensive delete link more

Comments

1

@ahendrix is correct. I'd add that if you are concerned about the memory requirements of having two copies of the object or the communication bandwidth requirements, nodelets could be a useful solution for you: http://wiki.ros.org/nodelet

jarvisschultz gravatar image jarvisschultz  ( 2015-05-05 13:22:02 -0500 )edit

@ahendrix@jarvisschultz Thanks. Yes, makes sense as there would be I guess nasty security loopholes then. Unfortunately, it is not a memory issue but rather trying to get openni2_launch with openni2_tracker to work together, only one can open the xtion with the other getting the device descriptor.

Yianni gravatar image Yianni  ( 2015-05-06 04:14:46 -0500 )edit

Please just ask a new question about using the openni2_launch and openni2_tracker together.

ahendrix gravatar image ahendrix  ( 2015-05-06 11:37:26 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2015-05-05 11:59:35 -0500

Seen: 3,367 times

Last updated: May 05 '15