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

[ROS2] publishing UInt16

asked 2020-04-10 07:10:01 -0500

jlepers gravatar image

Hi, I would like to publish a message without it being of type std_msgs::msg::String.

std_msgs::msg::UInt16 statuswoord;

auto publisher = node->create_publisher<std_msgs::msg::UInt16>("motor_info", rmw_qos_profile_default);  

auto msg =  std::make_shared<std_msgs::msg::UInt16();

msg->data = statuswoord;
publisher->publish(msg);

But I get following error:

cannot convert ‘std_msgs::msg::UInt16 {aka std_msgs::msg::UInt16_<std::allocator<void> >}’ to std_msgs::msg::UInt16_<std::allocator<void> > >::_data_type {aka short unsigned int}’ in assignment msg->data = test;

In my case I need it to be UInt16, but it might happen that I need another datatype in the future. Is there a guide somewhere on casting all the std_msgs ?

Thanks!

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2020-04-10 07:29:09 -0500

gvdhoorn gravatar image

updated 2020-04-10 07:38:38 -0500

Is there a guide somewhere on casting all the std_msgs ?

I don't believe this has anything to do with casting.

There are two issues with the code you show:

auto msg =  std::make_shared<std_msgs::msg::UInt16();

This line is missing the closing > (after UInt16), and:

std_msgs::msg::UInt16 statuswoord;
msg->data = statuswoord;

this can't work.

The field data is of type uint16 in the ROS 2 msg IDL (see here), which gets converted to a std::uint16_t for C++ (refer to About ROS 2 interfaces: Message description specification - Fields - Field types), which is a short unsigned int on amd64 and many other architectures.

The statuswoord variable is of type std_msgs::msg::UInt16.

You cannot assign a std_msgs::msg::UInt16 to a variable of type std::uint16_t.

To make this work, you'll have to change the type of statuswoord to std::uint16_t.

edit flag offensive delete link more

Comments

Note btw that this is not really a problem specific to ROS 2: in ROS 1 a similar system was used, and publishing a message from the std_msgs package, but treating it as a primitive type also didn't work.

gvdhoorn gravatar image gvdhoorn  ( 2020-04-10 07:30:16 -0500 )edit

Note also btw that using the std_msgs set of messages is somewhat discouraged. A short unsigned int carries almost no semantics, which makes it not very useful for publishing as a ROS message.

You may want to create a new message type, called StatusWord and give it a single field (of type uint16) called data (or something more descriptive). Then publish a StatusWord message.

This will make sure that whoever subscribes to your StatusWord knows how to deal with those kinds of messages, and it's immediately clear to consumers that they're not receiving just some random short unsigned int, but a bitmask encoding the status (of something).

gvdhoorn gravatar image gvdhoorn  ( 2020-04-10 07:33:53 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2020-04-10 07:10:01 -0500

Seen: 1,693 times

Last updated: Apr 10 '20