Ask Your Question

Access all the fields in ROS msg

asked 2017-03-22 07:07:11 -0500

jonagf gravatar image

updated 2017-03-22 09:14:48 -0500

Thomas D gravatar image

This is probably a very simple and stupid question, but I searched everywhere, and maybe because it's so simple I can't find an answer.

I created a msg like this (and maybe Im gonna add a string later as well)

int64 intensity 
int64 location
int64 duration

And now I'm trying to give values to each of this variables and publish them in a topic. I did the follow:

// (dont know how I'll do if I had the string, but thats not the problem **yet**)
ros::Publisher chatter_pub = n.advertise<std_msgs::Int64>("chatter", 1000);
std_msgs::Int64 msg;

Now the question comes here. In the ros publisher tutorials, we use, and as I searched online I found that data was the name of our variable. However, I did not understand that. How can I give values to every field of my message? I thought on doing something like this, but I know its not correct... = 15; = 100; = 37;

//ROS_INFO("intensity %d, local %d, dura %d ",,,;

Thank you!!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-03-22 09:21:23 -0500

Thomas D gravatar image

updated 2017-03-22 09:23:52 -0500

If you created a custom message then you need to create a Publisher that uses that custom message type. Right now your publisher is using a std_msgs/Int64 type which only has a single field named data.

Let's say your custom message is in a package called my_custom_msgs and that the file is named Custom.msg. Then you would need to declare your publisher like

ros::NodeHandle n;
ros:Publisher pub = n.advertise<my_custom_msgs::Custom>("chatter", 1000);

Now you can declare a message using the custom data type, populate each of the fields in your message, and publish.

my_custom_msgs::Custom msg;
msg.intensity = 15;
msg.location = 100;
msg.duration = 37;

Later, if you change your message to include a string so that it becomes

int64 intensity 
int64 location
int64 duration
string note

Then you will create the publisher the same way, but populating the fields and publishing will be

my_custom_msgs::Custom msg;
msg.intensity = 15;
msg.location = 100;
msg.duration = 37;
msg.note = "Hello";
edit flag offensive delete link more


Thank you very much! It was very useful! The package part was missing in my head, now it makes sense!

jonagf gravatar image jonagf  ( 2017-03-22 12:20:52 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-03-22 07:06:11 -0500

Seen: 2,169 times

Last updated: Mar 22 '17