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


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