How to publish an empty or NaN Int16?

asked 2021-05-26 02:51:15 -0500

Py gravatar image

updated 2021-05-26 02:51:55 -0500

I'm using rospy to publish some Int16 data when a sensor is connected. This works great but when the sensor cannot connect I want to publish either an empty or NaN value on the same topic.

When I try the following, the data is actually published as 0 which is misleading in the context of my application.

pub = rospy.Publisher('my_topic', Int16, queue_size=1)
my_empty_message = Int16()
pub.publish(my_empty_message)

I've played around by adding my_empty_message.data = NaN but NaN does not seem to be compatible with the message type.

How should I go about this?

edit retag flag offensive close merge delete

Comments

NaN is a floating point concept. It does not exist for integers.

Could you clarify why you want to publish messages even if there is no sensor connected?

gvdhoorn gravatar image gvdhoorn  ( 2021-05-26 03:22:54 -0500 )edit

You can choose not to publish. Alternatively, you can send a value that the sensor can't handle (for example, a negative number or a large number such as 30000) instead of NaN.

miura gravatar image miura  ( 2021-05-26 19:56:44 -0500 )edit

I want to publish an empty message to indicate that there is no sensor connection established. Even when I choose not to publish rostopic echo shows data as 0 when, to me, it ought to be blank or something equivalent to NaN.

Py gravatar image Py  ( 2021-06-12 06:19:27 -0500 )edit

I want to publish an empty message to indicate that there is no sensor connection established.

What about publishing a "sensor status message" instead + not publishing the sensor data itself when there is no connection? That's both less ambiguous as well as easily implemented.

Even when I choose not to publish rostopic echo shows data as 0 when, to me, it ought to be blank or something equivalent to NaN.

Could you describe this more? rostopic echo will not "make up" data, so if there are no messages published, it will not echo anything.

gvdhoorn gravatar image gvdhoorn  ( 2021-06-12 06:51:28 -0500 )edit

How would I go about publishing a sensor status message? I guess that means on a separate topic if it's a different message type?

For the second comment, I've realised that I was actually publishing the initialisation of my data variable, i.e. data = Int16(), in a while loop which I think explains why rostopic echo was giving me 0.

Py gravatar image Py  ( 2021-06-12 07:19:33 -0500 )edit