Skip to content

Commit

Permalink
tools: added extract_batch_imu.py
Browse files Browse the repository at this point in the history
  • Loading branch information
tridge committed Sep 20, 2023
1 parent b1e16d1 commit b758310
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions tools/extract_batch_imu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env python3

'''
extract ISBH and ISBD messages from AP_Logging files into a bin GYR and ACC log
'''
from pymavlink import DFReader
from argparse import ArgumentParser

parser = ArgumentParser('Parse bin log into GYR+ACC bin log')
parser.add_argument("inlog")
parser.add_argument("outlog")

args = parser.parse_args()

from pymavlink import mavutil

class DF_logger:
'''write to a DF format log'''
def __init__(self, filename):
self.outf = open(filename,'wb')
self.outf.write(bytes([0]))
self.outf.flush()
self.mlog = DFReader.DFReader_binary(filename)
self.outf.seek(0)
self.formats = {}

def write(self, name, fmt, fields, *args):
if not name in self.formats:
self.formats[name] = self.mlog.add_format(DFReader.DFFormat(0, name, 0, fmt, fields))
self.outf.write(self.mlog.make_format_msgbuf(self.formats[name]))
self.outf.write(self.mlog.make_msgbuf(self.formats[name], args))
self.outf.flush()

def extract_batch(inlog, outlog):
print("Processing log %s" % inlog)
mlog = mavutil.mavlink_connection(inlog)
dflog = DF_logger(outlog)

ISBH = None
count = 0

while True:
m = mlog.recv_match(type=['ISBH','ISBD'])
if m is None:
break
msg_type = m.get_type()
if msg_type == "ISBH":
ISBH = m
count += 1
elif msg_type == "ISBD" and ISBH is not None:
if m.N != ISBH.N:
continue
stype = 'ACC' if ISBH.type == 0 else 'GYR'
stype += '%u' % ISBH.instance
sample_us = int(1.0e6 / ISBH.smp_rate)
tbase = ISBH.SampleUS + (m.seqno * 32) * sample_us
mul = 1.0/ISBH.mul
for i in range(32):
dflog.write(stype, 'Qfff', 'TimeUS,X,Y,Z',
tbase+i*sample_us,
m.x[i]*mul, m.y[i]*mul, m.z[i]*mul)

print("Extracted %u frames" % count)
extract_batch(args.inlog, args.outlog)

0 comments on commit b758310

Please sign in to comment.