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

How to get the message from the Subscriber Function

asked 2018-01-18 09:15:02 -0500

Slevin Kelevra gravatar image

updated 2018-01-18 13:03:04 -0500

Hi, I am relatively new to ROS and have a question regarding Publisher and Subscriber.

To make this easy I use the Code from the ROS Tutorial "Writing a Simple Publisher and Subscriber (C++)". I can get everything to run and I can see the published messages through the "listener" node.

My question now is, how can I get the messages from the "chatterCallback" function back into my main function because I want to use it with Data from the main. I looked into the documentation of "subscribe" but I cant get it to work.

Maybe I cant see the obvious solution because I didn't programmed for a while now.

Used Code: http://wiki.ros.org/ROS/Tutorials/Wri...

#include "ros/ros.h"
#include "std_msgs/String.h"

void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
  ROS_INFO("I heard: [%s]", msg->data.c_str());
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "listener");

  ros::NodeHandle n;

  ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

  //here I want access to the message from chatterCallback

  ros::spin();

  return 0;
}

Thank you for the answers.

edit retag flag offensive close merge delete

Comments

Welcome! What does

I cant get it to work

mean? What have you tried (post your code please)?

jayess gravatar image jayess  ( 2018-01-18 11:24:40 -0500 )edit

I edited the post.

Slevin Kelevra gravatar image Slevin Kelevra  ( 2018-01-18 13:03:35 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-01-18 13:31:08 -0500

jayess gravatar image

What you can do is create a global variable to store the data in. For example

#include "ros/ros.h"
#include "std_msgs/String.h"

std_msgs::String str_var; // global variable

void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
  str_var.data = msg->data; // save the data in the callback
  ROS_INFO("I heard: [%s]", msg->data.c_str());
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "listener");

  ros::NodeHandle n;

  ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

  //here I want access to the message from chatterCallback

  ros::spin();

  return 0;
}

Ideally, you'd create a class for this and save the data to a class variable instead of a global one.

Note that I haven't compiled the code so there may be some errors, but the main idea is there.

edit flag offensive delete link more

Comments

Note btw that this (as all examples given on questions about 'using msgs in main') diverges from the purely event-based style of control-flow that asynchronous systems such as ROS pub-sub make possible/are proponents of.

Attempting to treat it as a synchronous, sampling, periodic while-loop is ..

gvdhoorn gravatar image gvdhoorn  ( 2018-01-18 13:57:05 -0500 )edit

.. obviously possible, but may lead to unexpected behaviour and even subtle bugs.

See this ROSCon17 presentation about something similar.

gvdhoorn gravatar image gvdhoorn  ( 2018-01-18 13:58:32 -0500 )edit

I knew that I could use a global variable but I learned that global variables should be avoided if possible.

I try your idea with the class tomorrow but it sounds promissing.

To the problem with the asynchronous system: would it be a solution when I build a queue in the class to accumulate all ...

Slevin Kelevra gravatar image Slevin Kelevra  ( 2018-01-18 14:56:19 -0500 )edit

...messages till I get them from the main function?

Slevin Kelevra gravatar image Slevin Kelevra  ( 2018-01-18 14:56:42 -0500 )edit

I understand that there could be errors if no message is received and the code in main calls the message. However, I don't see how this problem is solved using a class variable instead of a global variable.

Also, what is the most ideal way to solve this problem? I am sure it is quite common for people to want to access messages from a topic, manipulate them and publish the output to another topic. There must be a standard way to do this.

thejose gravatar image thejose  ( 2020-08-14 09:59:11 -0500 )edit

I am sure it is quite common for people to want to access messages from a topic, manipulate them and publish the output to another topic. There must be a standard way to do this.

Yes:

  1. store them in a work-queue or a message_filters/Cache and have a separate thread process the queue, or
  2. do the work in the callback and publish as soon as you have processed the message
gvdhoorn gravatar image gvdhoorn  ( 2020-08-14 12:53:28 -0500 )edit

Question Tools

Stats

Asked: 2018-01-18 09:15:02 -0500

Seen: 5,315 times

Last updated: Jan 18 '18