AccelSearch.py 4.5 KB

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