rosidl
rosidl copied to clipboard
deserialization fails when float is NaN
Example Python code generated by rosidl for float type field (in /opt/ros/galactic/lib/python3.8/site-packages/sensor_msgs/msg/_battery_state.py):
@capacity.setter
def capacity(self, value):
if __debug__:
assert \
isinstance(value, float), \
"The 'capacity' field must be of type 'float'"
self._capacity = value
The above assertion fails when the float value is ".nan".
File "/opt/ros/humble/local/lib/python3.10/dist-packages/sensor_msgs/msg/_battery_state.py", line 469, in capacity
assert value >= -3.402823e+38 and value <= 3.402823e+38, \
AssertionError: The 'capacity' field must be a float in [-3.402823e+38, 3.402823e+38]
Here is an example message (which failed to de-serialize) captured by "ros2 topic echo" command:
---
header:
stamp:
sec: 1663358019
nanosec: 115042374
frame_id: system
voltage: 16.0
temperature: 30.649999618530273
current: -4.269999980926514
charge: 30.20001220703125
capacity: .nan
design_capacity: 65.4000244140625
percentage: 0.7099999785423279
power_supply_status: 2
power_supply_health: 1
power_supply_technology: 2
present: true
cell_voltage:
- 3.5510001182556152
- 3.5930001735687256
cell_temperature:
- 29.700000762939453
- 31.600000381469727
location: ''
serial_number: '0'
Workaround was found to the problem; running Python with the -O flag, which sets __debug__
to false, disabling the assertion.
It would be better to fix the assertion, allowing the value to be NaN.
NaN is a valid value according to the comment at https://github.com/ros2/common_interfaces/blob/rolling/sensor_msgs/msg/BatteryState.msg#L39 : "(If unmeasured NaN)" .