Skip to content

Commit 55fcf4d

Browse files
shancock884peterbarker
authored andcommitted
mavlogdump: CSV option: Don't cut out messages with same timestamp
This also fixes the CSV output option for DF Text logs Fail nicely if the specified type doesn't exist
1 parent e39879a commit 55fcf4d

File tree

1 file changed

+16
-35
lines changed

1 file changed

+16
-35
lines changed

tools/mavlogdump.py

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -187,22 +187,21 @@ def match_type(mtype, patterns):
187187
currentOffset += len(fields)
188188
except IndexError:
189189
quit()
190+
except AttributeError:
191+
print("Message type '%s' not found" % (type))
192+
quit()
190193
except TypeError:
191194
print("You must specify a list of message types if outputting CSV format via the --types argument.")
192195
exit()
193196

194197
# The first line output are names for all columns
195-
csv_out = ["" for x in fields]
196198
print(args.csv_sep.join(fields))
197199

198-
if isbin and args.format == 'csv': # need to accumulate columns from message
200+
if (isbin or islog) and args.format == 'csv': # need to accumulate columns from message
199201
if types is None or len(types) != 1:
200202
print("Need exactly one type when dumping CSV from bin file")
201203
quit()
202204

203-
# Track the last timestamp value. Used for compressing data for the CSV output format.
204-
last_timestamp = None
205-
206205
# Track types found
207206
available_types = set()
208207

@@ -217,7 +216,11 @@ def match_type(mtype, patterns):
217216
match_types = []
218217
match_types.append(k)
219218

220-
if isbin and args.format == 'csv':
219+
if (isbin or islog) and args.format == 'csv':
220+
# Make sure the specified type was found
221+
if match_types is None:
222+
print("Specified type '%s' not found in log file" % (types[0]))
223+
quit()
221224
# we need FMT messages for column headings
222225
match_types.append("FMT")
223226

@@ -226,17 +229,12 @@ def match_type(mtype, patterns):
226229
while True:
227230
m = mlog.recv_match(blocking=args.follow, type=match_types)
228231
if m is None:
229-
# write the final csv line before exiting
230-
if args.format == 'csv' and csv_out:
231-
csv_out[0] = "{:.8f}".format(last_timestamp)
232-
print(args.csv_sep.join(csv_out))
233232
break
234233
m_type = m.get_type()
235234
available_types.add(m_type)
236-
if isbin and m_type == "FMT" and args.format == 'csv':
235+
if (isbin or islog) and m_type == "FMT" and args.format == 'csv':
237236
if m.Name == types[0]:
238237
fields += m.Columns.split(',')
239-
csv_out = ["" for x in fields]
240238
print(args.csv_sep.join(fields))
241239

242240
if args.reduce and reduce_msg(m_type, args.reduce):
@@ -329,27 +327,13 @@ def match_type(mtype, patterns):
329327
# CSV format outputs columnar data with a user-specified delimiter
330328
elif args.format == 'csv':
331329
data = m.to_dict()
332-
333-
# If this message has a duplicate timestamp, copy its data into the existing data list. Also
334-
# do this if it's the first message encountered.
335-
if timestamp == last_timestamp or last_timestamp is None:
336-
if isbin:
337-
newData = [str(data[y]) if y != "timestamp" else "" for y in fields]
338-
else:
339-
newData = [str(data[y.split('.')[-1]]) if y.split('.')[0] == m_type and y.split('.')[-1] in data else "" for y in fields]
340-
341-
for i, val in enumerate(newData):
342-
if val:
343-
csv_out[i] = val
344-
345-
# Otherwise if this is a new timestamp, print out the old output data, and store the current message for later output.
330+
if isbin or islog:
331+
csv_out = [str(data[y]) if y != "timestamp" else "" for y in fields]
346332
else:
347-
csv_out[0] = "{:.8f}".format(last_timestamp)
348-
print(args.csv_sep.join(csv_out))
349-
if isbin:
350-
csv_out = [str(data[y]) if y != "timestamp" else "" for y in fields]
351-
else:
352-
csv_out = [str(data[y.split('.')[-1]]) if y.split('.')[0] == m_type and y.split('.')[-1] in data else "" for y in fields]
333+
csv_out = [str(data[y.split('.')[-1]]) if y.split('.')[0] == m_type and y.split('.')[-1] in data else "" for y in fields]
334+
csv_out[0] = "{:.8f}".format(timestamp)
335+
print(args.csv_sep.join(csv_out))
336+
353337
# MAT format outputs data to a .mat file specified through the
354338
# --mat_file option
355339
elif args.format == 'mat':
@@ -393,9 +377,6 @@ def match_type(mtype, patterns):
393377
s += " seq=%u" % m.get_seq()
394378
print(s)
395379

396-
# Update our last timestamp value.
397-
last_timestamp = timestamp
398-
399380
# Export the .mat file
400381
if args.format == 'mat':
401382
scipy.io.savemat(args.mat_file, MAT, do_compression=args.compress)

0 commit comments

Comments
 (0)