4 extract ISBH and ISBD messages from AP_Logging files and produce FFT plots 6 from __future__
import print_function
14 from argparse
import ArgumentParser
15 parser = ArgumentParser(description=__doc__)
16 parser.add_argument(
"--condition", default=
None, help=
"select packets by condition")
17 parser.add_argument(
"logs", metavar=
"LOG", nargs=
"+")
19 args = parser.parse_args()
21 from pymavlink
import mavutil
24 '''display fft for raw ACC data in logfile''' 26 '''object to store data about a single FFT plot''' 27 class PlotData(object):
28 def __init__(self, ffth):
31 self.sensor_type = ffth.type
32 self.instance = ffth.instance
33 self.sample_rate_hz = ffth.smp_rate
34 self.multiplier = ffth.mul
42 def add_fftd(self, fftd):
43 if fftd.N != self.fftnum:
44 print(
"Skipping ISBD with wrong fftnum (%u vs %u)\n" % (fftd.fftnum, self.fftnum))
47 print(
"Skipping ISBD(%u) for ISBH(%u) with holes in it" % (fftd.seqno, self.fftnum))
49 if fftd.seqno != self.seqno+1:
50 print(
"ISBH(%u) has holes in it" % fftd.N)
54 self.data[
"X"].extend(fftd.x)
55 self.data[
"Y"].extend(fftd.y)
56 self.data[
"Z"].extend(fftd.z)
59 if self.sensor_type == 0:
61 elif self.sensor_type == 1:
64 return "?Unknown Sensor Type?" 70 return "%s[%u]" % (self.prefix(), self.instance)
72 print(
"Processing log %s" % filename)
73 mlog = mavutil.mavlink_connection(filename)
78 start_time = time.time()
80 m = mlog.recv_match(condition=args.condition)
84 if msgcount % 1000 == 0:
86 msg_type = m.get_type()
87 if msg_type ==
"ISBH":
88 if plotdata
is not None:
90 things_to_plot.append(plotdata)
92 plotdata = PlotData(m)
95 if msg_type ==
"ISBD":
97 print(
"Skipping ISBD outside ISBH (fftnum=%u)\n" % m.N)
101 print(
"", file=sys.stderr)
102 time_delta = time.time() - start_time
103 print(
"%us messages %u messages/second %u kB/second" % (msgcount, msgcount/time_delta, os.stat(filename).st_size/time_delta))
104 print(
"Extracted %u fft data sets" % len(things_to_plot), file=sys.stderr)
111 for thing_to_plot
in things_to_plot:
112 for axis
in [
"X",
"Y",
"Z" ]:
113 d = numpy.array(thing_to_plot.data[axis])/
float(thing_to_plot.multiplier)
115 print(
"No data?!?!?!")
118 avg = numpy.sum(d) / len(d)
120 d_fft = numpy.fft.rfft(d)
121 if thing_to_plot.tag()
not in sum_fft:
122 sum_fft[thing_to_plot.tag()] = {
127 sum_fft[thing_to_plot.tag()][axis] = numpy.add(sum_fft[thing_to_plot.tag()][axis], d_fft)
129 freq = numpy.fft.rfftfreq(len(d), 1.0/thing_to_plot.sample_rate_hz)
130 freqmap[thing_to_plot.tag()] = freq
132 for sensor
in sum_fft:
133 print(
"Sensor: %s" %
str(sensor))
134 pylab.figure(
str(sensor))
135 for axis
in [
"X",
"Y",
"Z" ]:
136 pylab.plot(freqmap[sensor], numpy.abs(sum_fft[sensor][axis]/count), label=axis)
137 pylab.legend(loc=
'upper right')
140 for filename
in args.logs: