日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

將rosbag中的imu數(shù)據(jù)轉(zhuǎn)成txt文件

作者:王不偏 更新時(shí)間: 2022-09-25 編程語(yǔ)言

將名為imu_20220923的rosbag類(lèi)型數(shù)據(jù)包中imu話(huà)題的數(shù)據(jù)使用命令

rostopic echo -b imu_20220923.bag /imu > imu_angle.txt

轉(zhuǎn)成名為imu_angle.txt的txt文件,結(jié)果如下:在這里插入圖片描述
現(xiàn)在想要單獨(dú)將其中的x,y,時(shí)間戳信息提取出來(lái)做成excel表格,使用代碼

#!/usr/bin/python2.6  
# -*- coding: utf-8 -*-  
import os
import shutil
import xlwt
row=0
######################字符串分割為字符函數(shù)#############################
def split_str(s):
  return [ch for ch in s]

#################################
root = '/home/jyy/下載/python腳本/'
file_path = os.path.join(root, 'imu_angle.txt')


temp_x = []
temp_y = []
temp_sec = []
temp_nsec = []
if os.path.isfile(file_path):
    with open(file_path,'r') as f:
        for line in f.readlines():  # 讀取每行數(shù)據(jù)
            data = line.split('/t/n')  # 按空格劃分每行數(shù)據(jù)
            #print("line=",line)  # split_str
            # print("data=",data)  # split_str
            #print("line.split[0]=  ",line.split(':')[0])  
            #print("line.split[1]=  ",line.split(':')[1]
  ######################進(jìn)行數(shù)據(jù)分類(lèi)和提取#########

    # 按 ':' 劃分每行數(shù)據(jù),結(jié)果保存在字符串?dāng)?shù)組中,每行劃分為包含倆個(gè)元素的字符串?dāng)?shù)組,
    #數(shù)組的第一個(gè)元素是數(shù)據(jù)標(biāo)識(shí),用來(lái)判斷,第二個(gè)元素就是對(duì)應(yīng)的數(shù)據(jù)。
    #使用數(shù)組的第一個(gè)元素(數(shù)據(jù)標(biāo)識(shí))判斷分類(lèi)
            if( line.split(':')[0] =='    secs'):     #!!!!注意標(biāo)識(shí)符前面的空格也要加上!!!!
                print("split_line.x=",line.split(':')[1]) 
                temp_sec.append((line.split(':')[1]))  #將數(shù)組的第二個(gè)元素保存到臨時(shí)列表中
            if( line.split(':')[0] =='    nsecs'):  #    secs#    nsecs#  y
                print("split_line.x=",line.split(':')[1]) 
                temp_nsec.append((line.split(':')[1]))  #添加到臨時(shí)列表中 
            if( line.split(':')[0] =='  x'):  #!!!!注意標(biāo)識(shí)符前面的空格也要加上!!!!
                print("split_line.x=",line.split(':')[1]) 
                temp_x.append((line.split(':')[1]))  #添加到臨時(shí)列表中
            if( line.split(':')[0] =='  y'):  #    secs#    nsecs#  y
                print("split_line.x=",line.split(':')[1]) 
                temp_y.append((line.split(':')[1]))  #添加到臨時(shí)列表中 
   ######################分類(lèi)好的數(shù)據(jù)保存在一個(gè)單獨(dú)的txt文件中#########

fh1 = open('sec.txt', 'w')
fh2 = open('nsec.txt', 'w')
fh3= open('only_x.txt', 'w')
fh4 = open('only_y.txt', 'w')
for data1 in temp_sec:   #時(shí)間戳的秒數(shù)據(jù)保存在一個(gè)單獨(dú)的txt文件中
    fh1.write(data1)
    print("fh1.write.temp_sec=",data1)
fh1.close()

for data2 in temp_nsec:   #時(shí)間戳的納秒數(shù)據(jù)保存在一個(gè)單獨(dú)的txt文件中
    fh2.write(data2)
    print("fh2.write.temp_nsec=",data2)
fh2.close()

for data3 in temp_x:  #imu的x保存在一個(gè)單獨(dú)的txt文件中
    fh3.write(data3)
    print("fh3.write.temp_x=",data3)
fh3.close()
for data4 in temp_y:   #imu的y保存在一個(gè)單獨(dú)的txt文件中
    fh4.write(data4)
    print("fh4.write.temp_y=",data4)               
fh4.close()

                

                

執(zhí)行代碼生成4getxt文件在這里插入圖片描述

其中nsec.txt中提取到的數(shù)據(jù)結(jié)果如下:在這里插入圖片描述
然后在windows中使用excel整合到一個(gè)表格就行。

原文鏈接:https://blog.csdn.net/weixin_50508111/article/details/127028916

欄目分類(lèi)
最近更新