-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhawk.py
315 lines (241 loc) · 10.4 KB
/
hawk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import click
import subprocess
import math
import json
import sys
import os
import re
from collections import defaultdict
# Constants
OPEN_LOG_PATH = "/home/pi/Documents/army/shd/scripts/hawk/open_log.py"
#
def var_printer(**vars):
for n, v in vars.items():
print(n+':', v)
class Line:
types = defaultdict(list)
timestamps = defaultdict(list)
objs = []
def __init__(self, dic):
self.raw_dic = dic
self.metadata = dic['meta'] # Dic with type and timestamp
self.data = dic['data'] # Full data dic
self.type = self.metadata['type']
self.timestamp = self.metadata['timestamp']
Line.types[self.type].append(self)
Line.timestamps[self.timestamp].append(self)
Line.objs.append(self)
@classmethod
def get_nearest(cls, timestamp, type_name):
assert type_name in Line.types, "Required type doesn't exist"
sorted_ts = sorted(Line.timestamps.keys())
min_dif = abs(timestamp - sorted_ts[0])
prev_dif = min_dif
for ts in sorted_ts:
data = Line.timestamps[ts]
line = None
for elem in data:
if elem.type == type_name:
line = elem
break
if not line:
continue
dif = abs(timestamp-ts)
if dif < min_dif:
min_dif = dif
if dif > prev_dif:
return line
prev_dif = dif
def __repr__(self):
s = "{} - data keys: {}, (at {})".format(self.type,
', '.join(list(self.data.keys())),
self.timestamp)
return s
class PixhawkLog:
def __init__(self, filename):
self.filename = filename
self.raw_content = self.open_log(filename)
self.lines = [Line(line) for line in
self.log_output_to_json(self.raw_content)]
def get_types(self):
return self.lines[0].types.keys()
def log_output_to_json(self,out):
out = out.replace('\n',',')
out = '[' + out[:-1] + ']'
out = json.loads(out)
return out
def open_log(self, file):
interpreter_name = sys.executable
b_out = subprocess.check_output([interpreter_name, OPEN_LOG_PATH, '--format','json',file])
out = b_out.decode('utf-8')
return out
def get_time_attr(self, timestamp, attr_type):
sorted_ts = sorted(Line.timestamps.keys())
assert len(Line.timestamps), "No timestamp recorded"
assert timestamp >= sorted_ts[0], "Required timestamp is below lowest record (required: {}, lowest: {})".format(timestamp, sorted_ts[0])
assert timestamp < sorted_ts[-1], "Required timestamp is above highest record (required: {}, highest: {})".format(timestamp, sorted_ts[-1])
line = Line.get_nearest(timestamp, attr_type)
return line
def get_record_startandstop(self, fps, video_len):
cmd_lines = Line.types['CMD']
record_end = record_start = None
for line in cmd_lines:
prm1, prm2 = line.data['Prm1'], line.data['Prm2']
if prm1 == 27 and prm2 == 0:
record_end = float(line.timestamp)
if not record_end:
record_end = self.lines[-1].timestamp
record_start = record_end - video_len/fps
return record_start, record_end
class ProtrackLog:
def __init__(self, filename):
# Config
self.timestamp_format = '{:10.4f}'
self.altitude_format = '{:.2f}'
self.heading_format = '{:.2f}'
self.pitch_format = '{:.2f}'
self.roll_format = '{:.2f}'
self.coord_format = '{:.6f}'
self.line_format = '\t'.join(['time', 'altitude', 'bearing', 'depression',
'uaveast', 'heading', 'uavnorth', 'pitch', 'roll',
'horizfov', 'vertfov', 'sensortype', 'dayornight',
'azimerr', 'eleverr', 'orleft', 'orright', 'ortop',
'orbottom'])
self.filename = filename
self.attributes_dic = {
'time': self.get_time,
'altitude': self.get_altitude,
'bearing': self.get_bearing,
'depression': self.get_depression,
'uaveast': self.get_uaveast,
'heading': self.get_heading,
'uavnorth': self.get_uavnorth,
'pitch': self.get_pitch,
'roll': self.get_roll,
'horizfov': self.get_horizfov,
'vertfov': self.get_vertfov,
'sensortype': self.get_sensortype,
'dayornight': self.get_dayornight,
'azimerr': self.get_azimerr,
'eleverr': self.get_eleverr,
'orleft': self.get_orleft,
'orright': self.get_orright,
'ortop': self.get_ortop,
'orbottom': self.get_orbottom,
}
def get_time(self, ph_log, timestamp):
timestamp = self.timestamp_format.format(timestamp)
timestamp = float(timestamp)
return timestamp
def get_altitude(self, ph_log, timestamp):
gps = ph_log.get_time_attr(timestamp, 'GPS')
alt = gps.data['Alt']
alt = float(self.altitude_format.format(alt))
return alt
def get_bearing(self, ph_log, timestamp):
return 0
def get_depression(self, ph_log, timestamp):
return 0
def get_uaveast(self, ph_log, timestamp):
gps = ph_log.get_time_attr(timestamp, 'GPS')
lon = gps.data['Lng']
lon = float(self.coord_format.format(lon))
return lon
def get_heading(self, ph_log, timestamp):
mag = ph_log.get_time_attr(timestamp, 'MAG')
magx, magy = mag.data['MagX'], mag.data['MagY']
ofsx, ofsy = mag.data['OfsX'], mag.data['OfsY']
x = magx - ofsx
y = magy - ofsy
azimuth = 90 - math.atan2(y,x)*180/math.pi
azimuth = float(self.heading_format.format(azimuth))
return azimuth
def get_uavnorth(self, ph_log, timestamp):
gps = ph_log.get_time_attr(timestamp, 'GPS')
lat = gps.data['Lat']
lat = float(self.coord_format.format(lat))
return lat
def get_pitch(self, ph_log, timestamp):
att = ph_log.get_time_attr(timestamp, 'ATT')
pitch = att.data['Pitch']
pitch = float(self.pitch_format.format(pitch))
return pitch
def get_roll(self, ph_log, timestamp):
att = ph_log.get_time_attr(timestamp, 'ATT')
roll = att.data['Roll']
roll = float(self.roll_format.format(roll))
return roll
def get_horizfov(self, ph_log, timestamp): # Operture
return 106
def get_vertfov(self, ph_log, timestamp): # Vertical operture
return 106*3/4
def get_sensortype(self, ph_log, timestamp):
return 0
def get_dayornight(self, ph_log, timestamp):
return 1
def get_azimerr(self, ph_log, timestamp):
return 0
def get_eleverr(self, ph_log, timestamp):
return 0
def get_orleft(self, ph_log, timestamp):
return 0
def get_orright(self, ph_log, timestamp):
return 0
def get_ortop(self, ph_log, timestamp):
return 0
def get_orbottom(self, ph_log, timestamp):
return 0
def build_line(self, ph_log, timestamp):
attrs = {key:f(ph_log, timestamp) for key,f in self.attributes_dic.items()}
line = '\t'.join([str(att) for att in attrs.values()])
line += "\n"
return line
def get_timestamp_of_frame(self, start_ts, frame_ix, fps):
return start_ts + frame_ix/fps
def get_frame_timestamp_backw(self, last_ts, frame_ix, fps):
return last_ts - frame_ix/fps
def build_log_backward(self, ph_log, last_ts, frames_n, fps, crop_firstframe_ix):
outputfile = self.filename
first_line = self.line_format+'\n'
second_line = "[sec]\t[feet]\t[deg]\t[deg]\t[meter]\t[deg]\t[meter]\t[deg]\t[deg]\t[deg]\t[deg]\t[int]\t[int]\t[pixel]\t[pixel]\t[pixel]\t[pixel]\n"
open(outputfile, 'a').write(first_line+second_line)
start_ts = self.get_frame_timestamp_backw(last_ts, crop_firstframe_ix, fps)
for frame_ix in range(frames_n):
line = self.build_line(ph_log, start_ts)
open(outputfile, 'a').write(line)
start_ts += 1/fps
def build_log(self, ph_log, start_ts, frames_n, fps, crop_firstframe_ix):
outputfile = self.filename
first_line = self.line_format+'\n'
second_line = "[sec]\t[feet]\t[deg]\t[deg]\t[meter]\t[deg]\t[meter]\t[deg]\t[deg]\t[deg]\t[deg]\t[int]\t[int]\t[pixel]\t[pixel]\t[pixel]\t[pixel]\n"
open(outputfile, 'a').write(first_line+second_line)
ts = self.get_timestamp_of_frame(start_ts, crop_firstframe_ix, fps)
for frame_ix in range(frames_n):
line = self.build_line(ph_log, ts)
open(outputfile, 'a').write(line)
ts += 1/fps
@click.command()
@click.option('--pixhawk_log', help="Path to the pixhawk log", type=str)
@click.option('--output', help="Path to the output file", type=str)
@click.option('--frames_nb', help="Number of frames to log", type=int)
@click.option('--fps', help="Fps rate of the video", type=float)
@click.option('--crop_firstframe_ix', help="index of the first frame of the cropped video", type=float)
@click.option('--video_len', help="number of frames in the video", type=float)
def main(pixhawk_log, output, frames_nb, fps, crop_firstframe_ix, video_len):
"""
Translate a log from pixhawk flight controller to .TLM file (protrack
format)
Example of usage: python3 hawk.py --pixhawk_log pixhawk_log_example_2.bin --output .tlm --frames_nb 2 --fps 30 --crop_firstframe_ix 80 --start_ts 1561103385.8849998
"""
duration = frames_nb/fps
open(output,'w').close()
in_log = PixhawkLog(pixhawk_log)
out_log = ProtrackLog(output)
start_ts, end_ts = in_log.get_record_startandstop(fps, video_len)
crop_start_ts = start_ts + crop_firstframe_ix/fps
out_log.build_log(in_log, crop_start_ts, frames_nb, fps, crop_firstframe_ix)
if __name__ == "__main__":
# main('./pixhawk_log_example_2.bin', 'translated.tlm', 1561103385.79, 80,
# 15, 20)
# EXAMPLE COMMAND: python3 hawk.py data/25_07.bin --output 25_07.txt --frames_nb 500 --crop_firstframe_ix 50
main()