Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'numpy.ndarray' object has no attribute 'get_fields_and_field_types' #3

Open
Miker2808 opened this issue May 15, 2022 · 8 comments

Comments

@Miker2808
Copy link

When attempting to run with my rosbag I generated from a simulation I received this error, tried both in ubuntu 20 and windows 10
Working on ROS2 Foxy.

`
['/simulation/d9/gps', '/simulation/d9/imu/acceleration', '/simulation/d9/imu/orientation', '/simulation/d9/imu/orientation/rate', '/simulation/d9/imu/velocity']
Traceback (most recent call last):
File "/home/simteam/.local/bin/ros2bag-convert", line 8, in
sys.exit(main())
File "/home/simteam/.local/lib/python3.8/site-packages/ros2bag_convert/main.py", line 14, in main
data = read_bag.read_from_all_topics(file_url,True)
File "/home/simteam/.local/lib/python3.8/site-packages/ros2bag_convert/ros2bag_convert/read_bag.py", line 210, in read_from_all_topics
timestamps, messages = read_from_topic(bag_file, topic_names[i], print_out)
File "/home/simteam/.local/lib/python3.8/site-packages/ros2bag_convert/ros2bag_convert/read_bag.py", line 183, in read_from_topic
dic_data = message_converter.convert_ros_message_to_dictionary(msg)
File "/home/simteam/.local/lib/python3.8/site-packages/ros2bag_convert/ros2bag_convert/message_converter.py", line 243, in convert_ros_message_to_dictionary
dictionary[field_name] = _convert_from_ros_type(field_type, field_value)
File "/home/simteam/.local/lib/python3.8/site-packages/ros2bag_convert/ros2bag_convert/message_converter.py", line 259, in _convert_from_ros_type
field_value = convert_ros_message_to_dictionary(field_value)
File "/home/simteam/.local/lib/python3.8/site-packages/ros2bag_convert/ros2bag_convert/message_converter.py", line 240, in convert_ros_message_to_dictionary
message_fields = message.get_fields_and_field_types()
AttributeError: 'numpy.ndarray' object has no attribute 'get_fields_and_field_types'

`

@ferasterkawi
Copy link

The same problem. Did you find a solution?

@Miker2808
Copy link
Author

Afraid I did not, decided to use a solution that doesn't require ros2 bag.
from my digging it seems related to the code screwing up with "rosidl" somewhere

@MarioAlmelaUMH
Copy link

MarioAlmelaUMH commented Jul 17, 2023

Even after the merging, I'm still getting this error. Has anyone made any more advances?

Tank you very much.

@FeiduoduoPeng
Copy link

Did you guys finish the probelem?

@andrewTianxiaolu
Copy link

Guys this problem is caused by the "message_conventer.py". Here is the resolution of this problem:
mehmetkillioglu/ros2_message_converter#3 (comment)

def _is_ros_binary_type(field_type):
""" Checks if the field is a binary array one, fixed size or not
list(bytearray(de(encoded_data)))
_is_ros_binary_type("uint8")
>>> False
_is_ros_binary_type("uint8[]")
>>> True
_is_ros_binary_type("uint8[3]")
>>> True
_is_ros_binary_type("char")
>>> False
_is_ros_binary_type("char[]")
>>> True
_is_ros_binary_type("char[3]")
>>> True
_is_ros_binary_type("octet")
>>> True
"""
return field_type.startswith('uint8[') or field_type.startswith('char[') or field_type.startswith('octet')

Add the octet type here.

@MarioAlmelaUMH
Copy link

Hi. I've added the octet type:
octet

However, I'm still getting a similar error:
error

I'm using a whole bunch of custom message types, so I wonder if I should add more clauses besides uint8, char and octet. Do you know how can I check which ones to add?

Thank you in advance.

@gcgloven
Copy link

you can try modify the convert_ros_message_to_dictionary function to:

def convert_ros_message_to_dictionary(message):
    """
    Takes in a ROS message and returns a Python dictionary.

    Example:
        ros_message = std_msgs.msg.String(data="Hello, Robot")
        dict_message = convert_ros_message_to_dictionary(ros_message)
    """
    dictionary = {}
    if hasattr(message, 'get_fields_and_field_types'):
        message_fields = message.get_fields_and_field_types()
        for (field_name, field_type) in message_fields.items():
            field_value = getattr(message, field_name)
            dictionary[field_name] = _convert_from_ros_type(field_type, field_value)
    else:
        dictionary = None

    return dictionary

Also, add create csv if not exist function to save_csv_file.py:

def save_csv_file(data, csv_file_name, version=0, print_out=False):
    """ Save data to a csv_file_name (use it after 'read_from_all_topics').
    """

    # Create csv file
    import os

    if not os.path.exists(os.path.dirname(csv_file_name)):
        os.makedirs(os.path.dirname(csv_file_name))

    ...................................

@lidarmansiwon
Copy link

This is a package that modified the code based on the above advice. You can use git clone as it is.

https://github.com/lidarmansiwon/ros2bag_convert_foxy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants