1 # -*- coding: utf-8 -*-
3 # Get IMU, GPS, misc info from serial logs (or from a file)
4 # Copyright (C) 2015 Olivier Matz <zer0@droids-corp.org>
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 INT = "([-+]?[0-9][0-9]*)"
27 FLOAT = "([-+]?[0-9]*\.?[0-9]+)"
36 self.prev_reftime = None
40 def open_serial(self, filename = "/dev/ttyUSB0", baudrate = 57600):
41 self.f = serial.Serial(port = filename, baudrate = baudrate)
45 def open_file(self, filename):
46 self.f = open(filename)
49 self.prev_reftime = None
52 def update_state(self):
53 # read from file/serial
54 while len(self.data) < 16384:
55 data = self.f.read(1024)
60 lines = self.data.split("\n")
62 # try to match imu/gps infos for all lines
66 m = re.match(".*IMU received %s %s %s"%(INT, INT, INT), l)
68 self.roll, self.pitch, self.yaw = (
69 map(lambda x: float(x)*(math.pi/18000), m.groups()))
72 m = re.match("%s "%INT +
73 "\| gyro %s %s %s " % (FLOAT, FLOAT, FLOAT) +
74 "\| accel %s %s %s " % (FLOAT, FLOAT, FLOAT) +
75 "\| magnet %s %s %s " % (FLOAT, FLOAT, FLOAT) +
76 "\| angles %s %s %s"% (FLOAT, FLOAT, FLOAT), l)
78 cur_time = float(m.groups()[0]) / 1000.
79 # first time, init prev_time and prev_reftime
80 if self.realtime == False and self.prev_time == None:
81 self.prev_time = cur_time
82 self.prev_reftime = time.time()
83 # if it's a replay, check current time first
84 if self.realtime == False:
86 if cur_time - self.prev_time > time.time() - self.prev_reftime:
90 map(lambda x: float(x), m.groups()[10:13]))
91 self.roll = math.degrees(roll) + 180.
92 self.pitch = math.degrees(pitch)
93 self.yaw = math.degrees(yaw)
96 self.data = reduce(lambda x,y: x+"\n"+y, lines, "")