1
$\begingroup$

Rosanswers logo

Hi everyone,

i would like to get the ros message defintion file as a string in c++. Similar to how it is done in python ...

Where I just have to call:

rosmsg.get_msg_text("sensor_msgs/Joy")

and get the following output:

std_msgs/Header header
  uint32 seq
  time stamp
  string frame_id
float32[] axes
int32[] buttons

The message type is not known before compile time ... Thus, everything I have is a string in the following format

std::string strType = "sensor_msgs/Joy"; // or something else like = "std_msgs/String" ...

so i need a method or a function which takes in a string, defining the package and the message type ... and want to get out a string with the complete message definition ...

But like it is done in python, not only the content of sensor_msgs/msg/Joy.msg, which is:

Header header           # timestamp in the header is the time the data is received
float32[] axes          # the axes measurements from a joystick
int32[] buttons         # the buttons measurements from a joystick

I need the extracted format of all nested message definitions like presented in the first example ...

I think this is also possible somehow in roscpp, but I could not figure it out yet, thanks for any suggestions ...

Cheers ...


Originally posted by andre__ on ROS Answers with karma: 11 on 2012-12-12

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Just use ros::message_traits::Definition<Type>::value(). Following your example, to print the definition of sensor_msgs/Joy to the console, simply write:

std::cout << ros::message_traits::Definition<sensor_msgs::Joy>::value() << std::endl

This assumes you have the appropriate includes.


Originally posted by Miguel S. with karma: 1114 on 2012-12-12

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by Mehdi. on 2017-09-13:
For this to be possible, all the message headers that will eventually be used should be available at compile time. I think an approach using strings only would be way more flexible and can cover all kinds of messages.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.