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

How to get the message text back to the main function

asked 2018-11-07 06:06:19 -0600

D0l0RES gravatar image
void MyFunc(const std_msgs::String::ConstPtr& msg)
ros::Subscriber sub = n.subscribe("shatter", 1000, chatterCallback);

standard subscription and reading data from the theme. But I have a question is it possible to implement reading from another topic that the message was in the same main function

edit retag flag offensive close merge delete


I don't quite understand your question. Are you trying to subscribe to two different topics at the same time and access the messages from these two topics inside the main loop of your node?

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-11-07 06:23:41 -0600 )edit
D0l0RES gravatar image D0l0RES  ( 2018-11-07 06:26:31 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-11-07 07:01:00 -0600

You have two choices.

(Easiest) Either make a global variable that will hold the contents of your message, so that every function can have access to it.


(More formal) Create a class and pass the necessary variables to your desired callbacks.

Good luck!

edit flag offensive delete link more



Or (recommended, although more complex and requires msgs with Headers): use message_filters/Cache.

gvdhoorn gravatar image gvdhoorn  ( 2018-11-07 08:03:35 -0600 )edit

I had completely missed that alternative. Nice one! Looks a bit of an overkill, but it's very elegant!

gstavrinos gravatar image gstavrinos  ( 2018-11-08 02:53:50 -0600 )edit

@gvdhoorn, @gstavrinos and get the message itself from the cache variable

D0l0RES gravatar image D0l0RES  ( 2018-11-08 03:52:36 -0600 )edit

Question Tools

1 follower


Asked: 2018-11-07 06:06:19 -0600

Seen: 392 times

Last updated: Nov 07 '18