mavfft.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python
  2. '''
  3. fit best estimate of magnetometer offsets
  4. '''
  5. from __future__ import print_function
  6. import numpy
  7. import pylab
  8. from argparse import ArgumentParser
  9. parser = ArgumentParser(description=__doc__)
  10. parser.add_argument("--condition", default=None, help="select packets by condition")
  11. parser.add_argument("--sample-length", type=int, default=0, help="number of samples to run FFT over")
  12. parser.add_argument("logs", metavar="LOG", nargs="+")
  13. args = parser.parse_args()
  14. from pymavlink import mavutil
  15. def fft(logfile):
  16. '''display fft for raw ACC data in logfile'''
  17. print("Processing log %s" % filename)
  18. mlog = mavutil.mavlink_connection(filename)
  19. data = {'ACC1.rate' : 1000,
  20. 'ACC2.rate' : 1600,
  21. 'ACC3.rate' : 1000,
  22. 'GYR1.rate' : 1000,
  23. 'GYR2.rate' : 800,
  24. 'GYR3.rate' : 1000}
  25. for acc in ['ACC1','ACC2','ACC3']:
  26. for ax in ['AccX', 'AccY', 'AccZ']:
  27. data[acc+'.'+ax] = []
  28. for gyr in ['GYR1','GYR2','GYR3']:
  29. for ax in ['GyrX', 'GyrY', 'GyrZ']:
  30. data[gyr+'.'+ax] = []
  31. # now gather all the data
  32. while True:
  33. m = mlog.recv_match(condition=args.condition)
  34. if m is None:
  35. break
  36. type = m.get_type()
  37. if type.startswith("ACC"):
  38. data[type+'.AccX'].append(m.AccX)
  39. data[type+'.AccY'].append(m.AccY)
  40. data[type+'.AccZ'].append(m.AccZ)
  41. if type.startswith("GYR"):
  42. data[type+'.GyrX'].append(m.GyrX)
  43. data[type+'.GyrY'].append(m.GyrY)
  44. data[type+'.GyrZ'].append(m.GyrZ)
  45. print("Extracted %u data points" % len(data['ACC1.AccX']))
  46. for msg in ['ACC1', 'ACC2', 'ACC3', 'GYR1', 'GYR2', 'GYR3']:
  47. pylab.figure()
  48. if msg.startswith('ACC'):
  49. prefix = 'Acc'
  50. else:
  51. prefix = 'Gyr'
  52. for axis in ['X', 'Y', 'Z']:
  53. field = msg + '.' + prefix + axis
  54. d = data[field]
  55. if args.sample_length != 0:
  56. d = d[0:args.sample_length]
  57. d = numpy.array(d)
  58. if len(d) == 0:
  59. continue
  60. avg = numpy.sum(d) / len(d)
  61. d -= avg
  62. d_fft = numpy.fft.rfft(d)
  63. freq = numpy.fft.rfftfreq(len(d), 1.0 / data[msg+'.rate'])
  64. pylab.plot( freq, numpy.abs(d_fft), label=field )
  65. pylab.legend(loc='upper right')
  66. for filename in args.logs:
  67. fft(filename)
  68. pylab.show()