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

How to get the message text back to the main function

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

D0l0RES gravatar image
void MyFunc(const std_msgs::String::ConstPtr& msg)
{
  //code
}
....
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

Comments

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 -0500 )edit
D0l0RES gravatar image D0l0RES  ( 2018-11-07 06:26:31 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

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

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.

or

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

Good luck!

edit flag offensive delete link more

Comments

2

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

gvdhoorn gravatar image gvdhoorn  ( 2018-11-07 08:03:35 -0500 )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 -0500 )edit

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

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

Question Tools

1 follower

Stats

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

Seen: 395 times

Last updated: Nov 07 '18