提取rosbag中的RTK信息,整理成TUM格式

功能:提取rosbag中的RTK信息,整理成tum格式,便于SLAM精度评估
使用:python2 rtk2tum.py -b [rosbag文件] -o [输出tum格式文件名] -p []topic名称]
示例:python2 rtk2tum.py -b bag_file.bag -o out_file.tum -p /topic/subtopic

# coding:utf-8 
import math
import sys
import os
import pandas as pd
import argparse

# 功能:提取rosbag中的RTK信息,整理成tum格式,便于SLAM精度评估
# 使用:python2 rtk2tum.py -b [rosbag文件] -o [输出tum格式文件名] -p []topic名称]
# 示例:python2 rtk2tum.py -b bag_file.bag -o out_file.tum -p /topic/subtopic

def to_xyz_3(M_lat, M_lon, M_alt, O_lat, O_lon, O_alt):
    Ea = 6378137  # 赤道半径
    Eb = 6356725  # 极半径
    M_lat = math.radians(M_lat)
    M_lon = math.radians(M_lon)
    O_lat = math.radians(O_lat)
    O_lon = math.radians(O_lon)
    Ec = Ea * (1 - (Ea - Eb) / Ea * ((math.sin(M_lat)) ** 2)) + M_alt
    Ed = Ec * math.cos(M_lat)
    d_lat = M_lat - O_lat
    d_lon = M_lon - O_lon
    x = d_lat * Ec
   # coding:utf-8 
    y = d_lon * Ed
    z = M_alt - O_alt
    return x, y, z

def write_data(path, data):
    with open(path,'a+') as f:
        i = 0
        for item in data:
            # tum格式要求行尾没有空格
            if i != 7:
                f.write(str(item)+" ")
            else:
                 f.write(str(item)+"\n")
            i += 1        
        f.close()

# 参数
#setup the argument list
parser = argparse.ArgumentParser(description='turn RTK data into xyz')
parser.add_argument('-b',  metavar='--bag',  help='rosbag with rtk information')
parser.add_argument('-o', metavar='--output_file',  help='tum file %(default)s')
parser.add_argument('-p', metavar='--RTK topic name', default="/dji_osdk_ros/rtk_position", help='tum file %(default)s')

args = parser.parse_args()
bagfile = args.b
# 如果不指明输出文件,则与输入文件同名
if args.o is None:
    args.o = args.b.rstrip(".bag")+".tum"
outfile = args.o
#print help if no argument is specified
if len(sys.argv)<2:
    parser.print_help()
    sys.exit(0)
print "bag file is " + bagfile
print "output file is " + outfile
# 删掉输出文件,防止不明追加
os.system("rm -f "+outfile)

# 将rosbag转为临时csv文件
os.system('rostopic echo -b ' + bagfile + ' -p ' + args.p +' > temp.csv')

# 读取csv临时文件
df = pd.read_csv('temp.csv')
# 原点经纬度
O_lat = df["field.latitude"][0]
O_lon = df["field.longitude"][0]
O_alt = df["field.altitude"][0] 
for i in range(len(df)):
    result = []
    time = df["field.header.stamp"][i]
    result.append(time)
    M_lat = df["field.latitude"][i]
    M_lon = df["field.longitude"][i]
    M_alt = df["field.altitude"][i] 
    x, y, z = to_xyz_3(M_lat, M_lon, M_alt, O_lat, O_lon, O_alt)
    result.append(x)
    result.append(y)
    result.append(z)
    result.append(0)
    result.append(0)
    result.append(0)
    result.append(0)
    write_data(outfile, result)



# 删除临时文件
os.system('rm -f temp.csv')
print "Transform finished!"



  • 5
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奋豆者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值