mavflighttime.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python
  2. '''
  3. work out total flight time for a mavlink log
  4. '''
  5. from __future__ import print_function
  6. import time
  7. import glob
  8. from argparse import ArgumentParser
  9. parser = ArgumentParser(description=__doc__)
  10. parser.add_argument("--condition", default=None, help="condition for packets")
  11. parser.add_argument("--groundspeed", type=float, default=3.0, help="groundspeed threshold")
  12. parser.add_argument("logs", metavar="LOG", nargs="+")
  13. args = parser.parse_args()
  14. from pymavlink import mavutil
  15. from pymavlink.mavextra import distance_two
  16. def flight_time(logfile):
  17. '''work out flight time for a log file'''
  18. print("Processing log %s" % filename)
  19. mlog = mavutil.mavlink_connection(filename)
  20. in_air = False
  21. start_time = 0.0
  22. total_time = 0.0
  23. total_dist = 0.0
  24. t = None
  25. last_msg = None
  26. last_time_usec = None
  27. while True:
  28. m = mlog.recv_match(type=['GPS','GPS_RAW_INT'], condition=args.condition)
  29. if m is None:
  30. if in_air:
  31. total_time += time.mktime(t) - start_time
  32. if total_time > 0:
  33. print("Flight time : %u:%02u" % (int(total_time)/60, int(total_time)%60))
  34. return (total_time, total_dist)
  35. if m.get_type() == 'GPS_RAW_INT':
  36. groundspeed = m.vel*0.01
  37. status = m.fix_type
  38. time_usec = m.time_usec
  39. else:
  40. groundspeed = m.Spd
  41. status = m.Status
  42. time_usec = m.TimeUS
  43. if status < 3:
  44. continue
  45. t = time.localtime(m._timestamp)
  46. if groundspeed > args.groundspeed and not in_air:
  47. print("In air at %s (percent %.0f%% groundspeed %.1f)" % (time.asctime(t), mlog.percent, groundspeed))
  48. in_air = True
  49. start_time = time.mktime(t)
  50. elif groundspeed < args.groundspeed and in_air:
  51. print("On ground at %s (percent %.1f%% groundspeed %.1f time=%.1f seconds)" % (
  52. time.asctime(t), mlog.percent, groundspeed, time.mktime(t) - start_time))
  53. in_air = False
  54. total_time += time.mktime(t) - start_time
  55. if last_msg is None or time_usec > last_time_usec or time_usec+30e6 < last_time_usec:
  56. if last_msg is not None:
  57. total_dist += distance_two(last_msg, m)
  58. last_msg = m
  59. last_time_usec = time_usec
  60. return (total_time, total_dist)
  61. total_time = 0.0
  62. total_dist = 0.0
  63. for filename in args.logs:
  64. for f in glob.glob(filename):
  65. (ftime, fdist) = flight_time(f)
  66. total_time += ftime
  67. total_dist += fdist
  68. print("Total time in air: %u:%02u" % (int(total_time)//60, int(total_time)%60))
  69. print("Total distance travelled: %.1f meters" % total_dist)