mavflightmodes.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 '''
00004 show changes in flight modes
00005 '''
00006 
00007 import sys, time, datetime, os
00008 
00009 from argparse import ArgumentParser
00010 parser = ArgumentParser(description=__doc__)
00011 parser.add_argument("logs", metavar="LOG", nargs="+")
00012 
00013 args = parser.parse_args()
00014 
00015 from pymavlink import mavutil
00016 
00017 
00018 def flight_modes(logfile):
00019     '''show flight modes for a log file'''
00020     print("Processing log %s" % filename)
00021     mlog = mavutil.mavlink_connection(filename)
00022 
00023     mode = ""
00024     previous_mode = ""
00025     mode_start_timestamp = -1
00026     time_in_mode = {}
00027     previous_percent = -1
00028     seconds_per_percent = -1
00029 
00030     filesize = os.path.getsize(filename)
00031 
00032     while True:
00033         m = mlog.recv_match(type=['SYS_STATUS','HEARTBEAT','MODE'],
00034                             condition='MAV.flightmode!="%s"' % mlog.flightmode)
00035         if m is None:
00036             break
00037         print('%s MAV.flightmode=%-12s (MAV.timestamp=%u %u%%)' % (
00038             time.asctime(time.localtime(m._timestamp)),
00039             mlog.flightmode,
00040             m._timestamp, mlog.percent))
00041 
00042         mode = mlog.flightmode
00043         if (mode not in time_in_mode):
00044             time_in_mode[mode] = 0
00045 
00046         if (mode_start_timestamp == -1):
00047             mode_start_timestamp = m._timestamp
00048         elif (previous_mode != "" and previous_mode != mode):
00049             time_in_mode[previous_mode] = time_in_mode[previous_mode] + (m._timestamp - mode_start_timestamp)
00050 
00051             #figure out how many seconds per percentage point so I can
00052             #caculate how many seconds for the final mode
00053             if (seconds_per_percent == -1 and previous_percent != -1
00054                     and previous_percent != mlog.percent):
00055                 seconds_per_percent = (m._timestamp - mode_start_timestamp) / (mlog.percent - previous_percent)
00056 
00057             mode_start_timestamp = m._timestamp
00058 
00059         previous_mode = mode
00060         previous_percent = mlog.percent
00061 
00062     #put a whitespace line before the per-mode report
00063     print()
00064     print("Time per mode:")
00065 
00066     #need to get the time in the final mode
00067     if (seconds_per_percent != -1):
00068         seconds_remaining = (100.0 - previous_percent) * seconds_per_percent
00069 
00070         time_in_mode[previous_mode] = time_in_mode[previous_mode] + seconds_remaining
00071 
00072         total_flight_time = 0
00073         for key, value in time_in_mode.iteritems():
00074             total_flight_time = total_flight_time + value
00075 
00076         for key, value in time_in_mode.iteritems():
00077             print('%-12s %s %.2f%%' % (key, str(datetime.timedelta(seconds=int(value))), (value / total_flight_time) * 100.0))
00078     else:
00079         #can't print time in mode if only one mode during flight
00080         print(previous_mode, " 100% of flight time")
00081 
00082 for filename in args.logs:
00083     flight_modes(filename)


mavlink
Author(s): Lorenz Meier
autogenerated on Wed Sep 9 2015 18:06:17