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

How to use a message that I made?

asked 2012-04-13 08:15:23 -0600

BetoCC011 gravatar image

updated 2014-04-20 14:09:21 -0600

ngrennan gravatar image

I created a new message following the tutorial i found on http://www.ros.org/wiki/ROS/Tutorials/CreatingMsgAndSrv

But, how do i use it ?

I know how to use standard ros messages, like this:

// First i create the publisher
ros::Publisher fix_pub = n.advertise<sensor_msgs::NavSatFix>("fix", 1);

// Then I create the message.
sensor_msgs::NavSatFixPtr fix(new sensor_msgs::NavSatFix);

// Then, after I'm done editing, i publish the message
fix_pub.publish (fix);

But how do I do that, if I didn't make any class... I tried to find the standard messages' classes, the sensor_msgs/NavSatFix.h for instance, but i couldn't find it, can u guys help me?


So, if I want to use the message I must create another package for it? Can't I create the message inside of my package? Because when I tried as you said using my package, it didn't work :(

edit retag flag offensive close merge delete

Comments

You can create a message inside the same package. It is generally better to define all your messages in a separate package. That greatly simplifies inter-package dependencies.

joq gravatar image joq  ( 2012-04-13 09:48:06 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
19

answered 2012-04-13 09:04:23 -0600

DimitriProsser gravatar image

First, I define my message type.

# MyCustomMsg.msg

Header header
float64 data

I then add my message's package to the manifest of my C++ node.

<depend package="my_custom_msg_package" />

Then, I import it into my C++ code.

#include <my_custom_msg_package/MyCustomMsg.h>

Set up a publisher:

my_msg_pub_ = n.advertise<my_custom_msg_package::MyCustomMsg>("topic", 1);

I then instantiate an instance of that message:

my_custom_msg_package::MyCustomMsg msg;

Finally, I fill it with data and publish it.

msg.header.stamp = ros::Time::now();
msg.header.frame_id = "/world";
msg.data = 0.0;

my_msg_pub_.publish(msg);
edit flag offensive delete link more

Comments

3

In the message package's CMakeLists.txt you need rosbuild_genmsg().

joq gravatar image joq  ( 2012-04-13 09:49:14 -0600 )edit

Oh yes, I figured I'd miss something.

DimitriProsser gravatar image DimitriProsser  ( 2012-04-13 11:28:32 -0600 )edit

In the message package's CMakeLists.txt you need rosbuild_genmsg(). I did that,but still it is showing that there is an error in my CMakeLists.txt.

muin028 gravatar image muin028  ( 2012-09-18 01:37:59 -0600 )edit

Please edit your original question, adding the errors you are seeing. You should be fairly close to solving this problem.

joq gravatar image joq  ( 2012-09-18 09:07:30 -0600 )edit
2

i have problem too... when i add "#include <msg/MyCustomMsg.h>" it send me "no such file or directory"

Mohsen Hk gravatar image Mohsen Hk  ( 2013-05-31 05:21:26 -0600 )edit
3

answered 2017-02-27 01:04:08 -0600

Sangeet gravatar image

Watch this YouTube Video, I had the exactly same question.

To the point and precise ::

https://www.youtube.com/watch?v=ilRAlo5hi5o

edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2012-04-13 08:15:23 -0600

Seen: 45,040 times

Last updated: Apr 13 '12