mavflightmodes.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python
  2. '''
  3. show changes in flight modes
  4. '''
  5. from __future__ import print_function
  6. import datetime
  7. import os
  8. import time
  9. from argparse import ArgumentParser
  10. parser = ArgumentParser(description=__doc__)
  11. parser.add_argument("logs", metavar="LOG", nargs="+")
  12. args = parser.parse_args()
  13. from pymavlink import mavutil
  14. def flight_modes(logfile):
  15. '''show flight modes for a log file'''
  16. print("Processing log %s" % filename)
  17. mlog = mavutil.mavlink_connection(filename)
  18. mode = ""
  19. previous_mode = ""
  20. mode_start_timestamp = -1
  21. time_in_mode = {}
  22. previous_percent = -1
  23. seconds_per_percent = -1
  24. filesize = os.path.getsize(filename)
  25. while True:
  26. m = mlog.recv_match(type=['SYS_STATUS','HEARTBEAT','MODE'],
  27. condition='MAV.flightmode!="%s"' % mlog.flightmode)
  28. if m is None:
  29. break
  30. print('%s MAV.flightmode=%-12s (MAV.timestamp=%u %u%%)' % (
  31. time.asctime(time.localtime(m._timestamp)),
  32. mlog.flightmode,
  33. m._timestamp, mlog.percent))
  34. mode = mlog.flightmode
  35. if (mode not in time_in_mode):
  36. time_in_mode[mode] = 0
  37. if (mode_start_timestamp == -1):
  38. mode_start_timestamp = m._timestamp
  39. elif (previous_mode != "" and previous_mode != mode):
  40. time_in_mode[previous_mode] = time_in_mode[previous_mode] + (m._timestamp - mode_start_timestamp)
  41. #figure out how many seconds per percentage point so I can
  42. #caculate how many seconds for the final mode
  43. if (seconds_per_percent == -1 and previous_percent != -1
  44. and previous_percent != mlog.percent):
  45. seconds_per_percent = (m._timestamp - mode_start_timestamp) / (mlog.percent - previous_percent)
  46. mode_start_timestamp = m._timestamp
  47. previous_mode = mode
  48. previous_percent = mlog.percent
  49. #put a whitespace line before the per-mode report
  50. print()
  51. print("Time per mode:")
  52. #need to get the time in the final mode
  53. if (seconds_per_percent != -1):
  54. seconds_remaining = (100.0 - previous_percent) * seconds_per_percent
  55. time_in_mode[previous_mode] = time_in_mode[previous_mode] + seconds_remaining
  56. total_flight_time = 0
  57. for key, value in time_in_mode.items():
  58. total_flight_time = total_flight_time + value
  59. for key, value in time_in_mode.items():
  60. print('%-12s %s %.2f%%' % (key, str(datetime.timedelta(seconds=int(value))), (value / total_flight_time) * 100.0))
  61. else:
  62. #can't print time in mode if only one mode during flight
  63. print(previous_mode, " 100% of flight time")
  64. for filename in args.logs:
  65. flight_modes(filename)