Implement shared memory data publishing between nodelets
I have a set of 3 nodelets running which publish and subscribe to topics amongst themselves. To optimize the process I want to use the shared memory functionality for zero-copy data being shared between nodelets through publish and subscribe. I tried following this example. The example uses a very basic data type for std_msgs::String::ConstPtr
. I am using a custom defined message and I get a compile time error saying that I cannot assign values to boost::shared_ptr.
For e.g. if my message type is the following (MyMessage.msg)-
Header header
int dataA
string dataB
In my publish method I do something like this -
void publishTopic()
{
std::string str("my message");
MyMessage::ConstPtr msg;
msg->dataA = 5;
msg->dataB = str;
myPublisher.publish(msg);
}
Lines msg->dataA = 5
and msg->dataB = str
are giving me the errors. I know I'm doing something wrong with how I'm assigning values to pointers, but cannot think of a solution off the top of my head. Any help will be appreciated.
Can you please also refer a documentation for using boost::shared_ptr in ROS if any exists?
Asked by ajain on 2014-12-23 14:05:20 UTC
Answers
Your problem is that you're using const
pointers. const
pointers cannot be mutated, which is why you are getting errors.
When you are generating a message for publishing it needs to be a standard shared pointer. When you receive it in your callback it will be a const
pointer or else it will be copied.
Note: When you publish a message with a shared pointer you're not allowed to modify it after it has been sent as other parts of the process might be accessing it.
For information on boost pointers I suggest the boost documentation: http://www.boost.org/doc/libs/1_57_0/libs/smart_ptr/shared_ptr.htm Or general reference sites like stack overflow: http://stackoverflow.com/questions/569775/smart-pointers-boost-explained
Asked by tfoote on 2014-12-23 18:12:46 UTC
Comments
Thanks a lot for a quick answer. It works if I instantiate msg
as boost::shared_ptr<MyMessage> msg
.
Asked by ajain on 2014-12-23 19:01:01 UTC
Comments