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

Attempting to publish and subscribe to individual portions of an advertised topic

asked 2011-11-05 20:42:59 -0500

AlgorithmSeeker gravatar image

updated 2011-11-05 23:40:18 -0500

Hey guys. I've just started using ROS a couple of days back. I'm trying to write a publisher(talker) that will take certain parts of a topic and publish them, and a subscriber that can take whatever this talker is publishing. I know this question sounds a bit vague, but can you help me? I've read the tutorials but I still can't use that to my purpose.

Let's say we have some topic '/XXXX/DATA'. I want to extract, say, '/XXX/DATA.acc_x' using the publisher and send it to the subscriber. Is this even possible?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2011-11-05 21:41:48 -0500

makokal gravatar image

Yes it is possible, The data that is available on the topic is specified by messages. In you case I assume you define some message that contains some element like

float64 acc_x

for instance. Then you subscribe to this topic and just access the data element via the callback say

void dataCallback(const SomePkg::SomeMsgTypeConstPtr msg) { float accx = msg->acc_x; }

And you are set. You can go ahead and do anything with it afterwards.

edit flag offensive delete link more

Comments

You can also publish in the callback too.
tfoote gravatar image tfoote  ( 2011-11-16 09:12:22 -0500 )edit
0

answered 2011-11-05 22:10:16 -0500

AlgorithmSeeker gravatar image

Thanks for that, makokal. However, I am still encountering problems in accessing the data. Let me explain my problem more clearly.

I have a certain robot which is producing data under several topics, one of which includes this /XXX/Data. Now /XXX/Data has several members in its definition, for example acc_x, acc_y, acc_z, angle_roll, angle_pitch, angle_yaw etc. What I'm trying to do, is to write a listener that will extract specifically acc_x, which is of int16 type. To access this acc_x, I have tried including the header file, e.g. "information.h" that specifically contains the structure that is publishing /XXX/Data, and this structure contains acc_x, acc_y etc. Then, in my listener, I tried to create an object of the type of the structure I mentioned above. But it's not working.

edit flag offensive delete link more
0

answered 2011-11-06 00:30:18 -0500

AlgorithmSeeker gravatar image

updated 2011-11-06 00:33:49 -0500

Let me be even more specific. See this link 'Writing a simple Publisher and Subscriber in C++': http://www.ros.org/wiki/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29

Now this is part of the code written for the tutorial on how to write a Publisher.

while (ros::ok())

{ /** * This is a message object. You stuff it with data, and then publish it. */ std_msgs::String msg;

std::stringstream ss;
ss << "hello world " << count;
msg.data = ss.str();

ROS_INFO("%s", msg.data.c_str());

Notice how a string object 'ss' is created, and "Hello World" is assigned to 'ss'. Now what I want is, instead of assigning 'Hello World' to this string object, I want to assign information that is being published by the topic /XXX/DATA

I hope my question is clear.

edit flag offensive delete link more

Comments

the easiest way is to just subscribe to the topic and buffer the information on some variable and then use in the other publisher.
makokal gravatar image makokal  ( 2011-11-06 05:04:19 -0500 )edit

Question Tools

Stats

Asked: 2011-11-05 20:42:59 -0500

Seen: 352 times

Last updated: Nov 06 '11