You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By using Roslibjs to subscribe to topics using CBOR compression, I'm having a Rosbridge error with some custom messages. I can subscribe to standard messages with the compression, but not some of my custom ones, particularly when containing vectors of other custom messages.
Library Version: Last commit of humble branch.
ROS Version: Humble
Platform / OS: Ubuntu 22.04
Expected Behavior
All messages arriving through Roslibjs on the wep application, when CBOR compression is requested.
Actual Behavior
The messages don't arrive, and Rosbridge prints the following error:
[rosbridge_websocket-4] for slot, slot_type in msg.get_fields_and_field_types().items():
[rosbridge_websocket-4] AttributeError: 'numpy.ndarray' object has no attribute 'get_fields_and_field_types'
I don't know where this numpy.array comes from since my nodes are C++. I'm guessing is some conversion to Python on the message generators in between.
Possible solution
I was able to got it to work, but I'm unsure of the solution. Or, if the problem is caused by another completely different issue and this workaround is not the best approach.
On cbor_conversion.py, I added to the beginning of extract_cbor_values function:
ifisinstance(msg, np.ndarray):
returnmsg.tolist()
And imported numpy. The final result being:
# (...)importnumpyasnp# (...)defextract_cbor_values(msg):
"""Extract a dictionary of CBOR-friendly values from a ROS message. Primitive values will be casted to specific Python primitives. Typed arrays will be tagged and packed into byte arrays. """ifisinstance(msg, np.ndarray):
returnmsg.tolist()
out= {}
forslot, slot_typeinmsg.get_fields_and_field_types().items():
val=getattr(msg, slot)
# stringifslot_typeinSTRING_TYPES:
out[slot] =str(val)
# boolelifslot_typeinBOOL_TYPES:
out[slot] =bool(val)
# integerselifslot_typeinINT_TYPES:
out[slot] =int(val)
# floatselifslot_typeinFLOAT_TYPES:
out[slot] =float(val)
# time/durationelifslot_typeinTIME_TYPES:
out[slot] = {
"sec": int(val.sec),
"nanosec": int(val.nanosec),
}
# byte arrayelifslot_typeinBYTESTREAM_TYPES:
out[slot] =bytes(val)
# bool arrayelifslot_typeinBOOL_ARRAY_TYPES:
out[slot] = [bool(i) foriinval]
elifslot_typeinSTRING_ARRAY_TYPES:
out[slot] = [str(i) foriinval]
# numeric arrayselifslot_typeinTAGGED_ARRAY_FORMATS:
tag, fmt=TAGGED_ARRAY_FORMATS[slot_type]
fmt_to_length=fmt.format(len(val))
packed=struct.pack(fmt_to_length, *val)
out[slot] =Tag(tag=tag, value=packed)
# array of messageseliftype(val) inLIST_TYPES:
out[slot] = [extract_cbor_values(i) foriinval]
# messageelse:
out[slot] =extract_cbor_values(val)
returnout
It seems to be working for every message I request, and my custom messages are being processed much faster with the compression='cbor' flag now.
Any tips or suggestions?
Thanks!
The text was updated successfully, but these errors were encountered:
Because the function does check if the types are lists of primitive types, and then recursively calls itself on individual elements.
I'd also first confirm under which conditions the data becomes a NumPy array here, and see where there is a fixable bug before adding this special case.
The test_msgs here is just a dummy name for my interface package, but this is basically its structure.
I'd also first confirm under which conditions the data becomes a NumPy array here, and see where there is a fixable bug before adding this special case.
I don't know where it becomes a numpy.array, the messages are generated with rosidl_generate_interfaces.
From what I can see in message_conversion.py that has the default extract_values / extract_json_values function, the list_types definition includes np.ndarray, deals with it in the code and converts to list.
Description
By using
Roslibjs
to subscribe to topics usingCBOR
compression, I'm having aRosbridge
error with some custom messages. I can subscribe to standard messages with the compression, but not some of my custom ones, particularly when containing vectors of other custom messages.humble
branch.Humble
Ubuntu 22.04
Expected Behavior
All messages arriving through
Roslibjs
on the wep application, whenCBOR
compression is requested.Actual Behavior
The messages don't arrive, and
Rosbridge
prints the following error:I don't know where this
numpy.array
comes from since my nodes areC++
. I'm guessing is some conversion toPython
on the message generators in between.Possible solution
I was able to got it to work, but I'm unsure of the solution. Or, if the problem is caused by another completely different issue and this workaround is not the best approach.
On
cbor_conversion.py
, I added to the beginning ofextract_cbor_values
function:And imported numpy. The final result being:
It seems to be working for every message I request, and my custom messages are being processed much faster with the
compression='cbor'
flag now.Any tips or suggestions?
Thanks!
The text was updated successfully, but these errors were encountered: