4 convert a MAVLink tlog file to a MATLab mfile 6 from __future__
import print_function
7 from builtins
import range
11 from pymavlink
import mavutil
14 '''convert a tlog to a .m file''' 16 print(
"Processing %s" % filename)
18 mlog = mavutil.mavlink_connection(filename, dialect=args.dialect, zero_time_base=
True)
27 types = types.split(
',')
30 (head, tail) = os.path.split(filename)
31 basename =
'.'.join(tail.split(
'.')[:-1])
32 mfilename = re.sub(
'[\.\-\+\*]',
'_', basename) +
'.m' 34 if re.match(
'^[a-zA-z]', mfilename)
is None:
35 mfilename =
'm_' + mfilename
38 mfilename = os.path.join(head, mfilename)
39 print(
"Creating %s" % mfilename)
41 f = open(mfilename,
"w")
46 m = mlog.recv_match(condition=args.condition)
50 if types
is not None and m.get_type()
not in types:
52 if m.get_type() ==
'BAD_DATA':
55 fieldnames = m._fieldnames
57 if mtype
in [
'FMT',
'PARM']:
60 if mtype
not in type_counters:
61 type_counters[mtype] = 0
62 f.write(
"%s.columns = {'timestamp'" % mtype)
63 for field
in fieldnames:
64 val = getattr(m, field)
65 if not isinstance(val, str):
66 if type(val)
is not list:
67 f.write(
",'%s'" % field)
69 for i
in range(0, len(val)):
70 f.write(
",'%s%d'" % (field, i + 1))
73 type_counters[mtype] += 1
74 f.write(
"%s.data(%u,:) = [%f" % (mtype, type_counters[mtype], m._timestamp))
75 for field
in m._fieldnames:
76 val = getattr(m, field)
77 if not isinstance(val, str):
78 if type(val)
is not list:
79 f.write(
",%.20g" % val)
81 for i
in range(0, len(val)):
82 f.write(
",%.20g" % val[i])
86 from argparse
import ArgumentParser
87 parser = ArgumentParser(description=__doc__)
89 parser.add_argument(
"--condition", default=
None, help=
"select packets by condition")
90 parser.add_argument(
"-o",
"--output", default=
None, help=
"output filename")
91 parser.add_argument(
"--types", default=
None, help=
"types of messages (comma separated)")
92 parser.add_argument(
"--dialect", default=
"ardupilotmega", help=
"MAVLink dialect")
93 parser.add_argument(
"logs", metavar=
"LOG", nargs=
"+")
94 args = parser.parse_args()
97 for filename
in args.logs: