Ask Your Question
0

rebuild bagfile from csv with Error: 'list' object has no attribute 'header'

asked 2019-05-11 12:07:17 -0600

xj yang gravatar image

updated 2019-05-11 12:09:41 -0600

Hello, I am working on rebuilding bagfile from csv file, and I am stuck at the tf part. As the structure of the tf is like this:

image description

Here is what I have done, with the 'list' object has no attribute 'header' error:

import csv
import rospy
import rosbag
from tf.msg import tfMessage

with rosbag.Bag('rebuild.bag', 'w') as bag:
    with open('tf.csv', 'r') as csvFile:
        reader = csv.reader(csvFile)
        for row in reader:
            tf_msg = [tfMessage().transforms]
            tf_msg[0].header.seq = int(row[1])

Sorry this looks more like a python question, I cannot figure out how to initialize a tfMessage instance...

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2019-05-11 18:48:11 -0600

xj yang gravatar image

updated 2019-05-11 18:49:46 -0600

So to solve this part, first manuall create a class according to tfMessage().transforms[0] structure, and then include this class in the tfMessage().transforms emply list (be aware of the variables' type):

import csv
import rospy
import rosbag
from tf.msg import tfMessage

class tf_message:
    class header:
        seq = []
        class stamp():
            secs = []
            nsecs = []
        frame_id = []
    child_frame_id = []
    class transform():
        class translation():
            x = []
            y = []
            z = []
        class rotation():
            x = []
            y = []
            z = []
            w = []

    def __init__(self,_seq,_secs,_nsecs,
                _frame_id,_child_frame_id,
                _trans_x,_trans_y,_trans_z,
                _rot_x,_rot_y,_rot_z,_rot_w):
        self.header.seq = _seq
        self.header.stamp.secs = _secs
        self.header.stamp.nsecs = _nsecs
        self.header.frame_id = _frame_id
        self.child_frame_id = _child_frame_id
        self.transform.translation.x = _trans_x
        self.transform.translation.y = _trans_y
        self.transform.translation.z = _trans_z
        self.transform.rotation.x = _rot_x
        self.transform.rotation.y = _rot_y
        self.transform.rotation.z = _rot_z
        self.transform.rotation.w = _rot_w

with rosbag.Bag('rebuild.bag', 'w') as bag:
    with open('tf.csv', 'r') as csvFile:
        reader = csv.reader(csvFile)
        for row in reader:
            tf_msg = tfMessage()
            sample_message = tf_message(_seq, _secs, _nsecs,
                                        _frame_id, _child_frame_id,
                                        _trans_x, _trans_y, _trans_z,
                                        _rot_x, _rot_y, _rot_z, _rot_w)
            tf_msg.transforms = [sample_message]
edit flag offensive delete link more
0

answered 2019-05-11 14:00:00 -0600

jayess gravatar image

transforms is a list so you can't access the header attribute the way you're trying. Try

tf_msg[0][0].header.seq = int(row[0])

instead.

edit flag offensive delete link more

Comments

Also I guess there is another problem with initializing this list, I tried your way and got the error:

list index out of range

which I guess this list is still empty when assigning value to tf_msg[0][0].header.seq?

xj yang gravatar imagexj yang ( 2019-05-11 16:37:38 -0600 )edit

What you could try is to create the list outside of the loop, create a new message in the loop, and then push that message to the list. I'm on a mobile phone so I can't really write this up right now but can when I get back to a computer.

jayess gravatar imagejayess ( 2019-05-11 16:48:36 -0600 )edit

Sorry I am not sure about how to do this, I feel like to initialize the list outside of the loop may still be stuck at initializing a tfMessage().transforms list?

xj yang gravatar imagexj yang ( 2019-05-11 17:15:23 -0600 )edit

okay it's working, thank you for the idea!

xj yang gravatar imagexj yang ( 2019-05-11 18:41:26 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2019-05-11 12:07:17 -0600

Seen: 68 times

Last updated: May 11