LP-551 Changes from review, change names and fix typos.
[librepilot.git] / python / examples / example_readlog.py
blob56bbad901dac112a084d05e1e741ef0410273920
1 ##
2 ##############################################################################
4 # @file example_readlog.py
5 # @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017.
6 # The OpenPilot Team, http://www.openpilot.org Copyright (C) 2011.
7 # @brief Base classes for python UAVObject
8 #
9 # @see The GNU Public License (GPL) Version 3
11 #############################################################################/
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 3 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 # for more details.
23 # You should have received a copy of the GNU General Public License along
24 # with this program; if not, write to the Free Software Foundation, Inc.,
25 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 import logging
31 import serial
32 import traceback
33 import sys
35 from librepilot.uavtalk.uavobject import *
36 from librepilot.uavtalk.uavtalk import *
37 from librepilot.uavtalk.objectManager import *
38 from librepilot.uavtalk.connectionManager import *
39 from example import UavtalkDemo
42 def _hex02(value):
43 return "%02X" % value
45 class UavtalkLogDemo(UavtalkDemo):
47 def __init__(self):
48 self.nbUpdates = 0
49 self.lastRateCalc = time.time()
50 self.updateRate = 0
51 self.objMan = None
52 self.connMan = None
54 def setup(self, filename):
55 print "Opening File \"%s\"" % filename
56 file = open(filename,"rb")
57 if file == None:
58 raise IOError("Failed to open file")
60 print "Creating UavTalk"
61 self.uavTalk = UavTalk(None, filename)
63 print "Starting ObjectManager"
64 self.objMan = ObjManager(self.uavTalk)
65 self.objMan.importDefinitions()
67 print "Starting UavTalk"
68 self.uavTalk.start()
70 def printUsage():
71 appName = os.path.basename(sys.argv[0])
72 print
73 print "usage:"
74 print " %s filename " % appName
75 print
76 print " for example: %s /tmp/OP-2015-04-28_23-16-33.opl" % appName
77 print
79 if __name__ == '__main__':
81 if len(sys.argv) !=2:
82 print "ERROR: Incorrect number of arguments"
83 print len(sys.argv)
84 printUsage()
85 sys.exit(2)
87 script, filename = sys.argv
88 if not os.path.exists(sys.argv[1]):
89 sys.exit('ERROR: Database %s was not found!' % sys.argv[1])
91 # Log everything, and send it to stderr.
92 logging.basicConfig(level=logging.INFO)
94 try:
95 demo = UavtalkLogDemo()
96 demo.setup(filename)
98 while True:
99 demo.objMan.AttitudeState.waitUpdate()
100 demo._onAttitudeUpdate(demo.objMan.AttitudeState)
102 except KeyboardInterrupt:
103 pass
104 except Exception,e:
105 print
106 print "An error occured: ", e
107 print
108 traceback.print_exc()
110 print