AccelSearch.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 '''
4 search a set of log files for bad accel values
5 '''
6 from __future__ import print_function
7 from builtins import input
8 from builtins import range
9 
10 import sys, os
11 import zipfile
12 
13 from pymavlink import mavutil
14 
15 # extra imports for pyinstaller
16 import json
17 from pymavlink.dialects.v10 import ardupilotmega
18 
19 search_dirs = ['c:\Program Files\APM Planner',
20  'c:\Program Files\Mission Planner',
21  'c:\Program Files (x86)\APM Planner',
22  'c:\Program Files (x86)\Mission Planner']
23 results = 'SearchResults.zip'
24 email = 'Craig Elder <craig@3drobotics.com>'
25 
26 from argparse import ArgumentParser
27 parser = ArgumentParser(description=__doc__)
28 parser.add_argument("--directory", action='append', default=search_dirs, help="directories to search")
29 parser.add_argument("--post-boot", action='store_true', help="post boot only")
30 parser.add_argument("--init-only", action='store_true', help="init only")
31 parser.add_argument("--single-axis", action='store_true', help="single axis only")
32 
33 args = parser.parse_args()
34 
35 logcount = 0
36 
37 def AccelSearch(filename):
38  global logcount
39  mlog = mavutil.mavlink_connection(filename)
40  badcount = 0
41  badval = None
42  have_ok = False
43  last_t = 0
44  while True:
45  m = mlog.recv_match(type=['PARAM_VALUE','RAW_IMU'])
46  if m is None:
47  if last_t != 0:
48  logcount += 1
49  return False
50  if m.get_type() == 'PARAM_VALUE':
51  if m.param_id.startswith('INS_PRODUCT_ID'):
52  if m.param_value not in [0.0, 5.0]:
53  return False
54  if m.get_type() == 'RAW_IMU':
55  if m.time_usec < last_t:
56  have_ok = False
57  last_t = m.time_usec
58  if abs(m.xacc) >= 3000 and abs(m.yacc) > 3000 and abs(m.zacc) > 3000 and not args.single_axis:
59  if args.post_boot and not have_ok:
60  continue
61  if args.init_only and have_ok:
62  continue
63  print(have_ok, last_t, m)
64  break
65  # also look for a single axis that stays nearly constant at a large value
66  for axes in ['xacc', 'yacc', 'zacc']:
67  value1 = getattr(m, axes)
68  if abs(value1) > 2000:
69  if badval is None:
70  badcount = 1
71  badval = m
72  continue
73  value2 = getattr(badval, axes)
74  if abs(value1 - value2) < 30:
75  badcount += 1
76  badval = m
77  if badcount > 5:
78  logcount += 1
79  if args.init_only and have_ok:
80  continue
81  print(have_ok, badcount, badval, m)
82  return True
83  else:
84  badcount = 1
85  badval = m
86  if badcount == 0:
87  have_ok = True
88  if last_t != 0:
89  logcount += 1
90  return True
91 
92 found = []
93 directories = args.directory
94 
95 # allow drag and drop
96 if len(sys.argv) > 1:
97  directories = sys.argv[1:]
98 
99 filelist = []
100 
101 for d in directories:
102  if not os.path.exists(d):
103  continue
104  if os.path.isdir(d):
105  print("Searching in %s" % d)
106  for (root, dirs, files) in os.walk(d):
107  for f in files:
108  if not f.endswith('.tlog'):
109  continue
110  path = os.path.join(root, f)
111  filelist.append(path)
112  elif d.endswith('.tlog'):
113  filelist.append(d)
114 
115 for i in range(len(filelist)):
116  f = filelist[i]
117  print("Checking %s ... [found=%u logcount=%u i=%u/%u]" % (f, len(found), logcount, i, len(filelist)))
118  if AccelSearch(f):
119  found.append(f)
120 
121 
122 if len(found) == 0:
123  print("No matching files found - all OK!")
124  input('Press enter to close')
125  sys.exit(0)
126 
127 print("Creating zip file %s" % results)
128 try:
129  zip = zipfile.ZipFile(results, 'w')
130 except Exception:
131  print("Unable to create zip file %s" % results)
132  print("Please send matching files manually")
133  for f in found:
134  print('MATCHED: %s' % f)
135  input('Press enter to close')
136  sys.exit(1)
137 
138 for f in found:
139  zip.write(f, arcname=os.path.basename(f))
140 zip.close()
141 
142 print('==============================================')
143 print("Created %s with %u of %u matching logs" % (results, len(found), logcount))
144 print("Please send this file to %s" % email)
145 print('==============================================')
146 
147 input('Press enter to close')
148 sys.exit(0)


mavlink
Author(s): Lorenz Meier
autogenerated on Sun Apr 7 2019 02:06:02