Skip to content

Commit

Permalink
mavlogdump: CSV option: Don't cut out messages with same timestamp
Browse files Browse the repository at this point in the history
This also fixes the CSV output option for DF Text logs
Fail nicely if the specified type doesn't exist
  • Loading branch information
shancock884 authored and peterbarker committed Feb 1, 2024
1 parent e39879a commit 55fcf4d
Showing 1 changed file with 16 additions and 35 deletions.
51 changes: 16 additions & 35 deletions tools/mavlogdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,21 @@ def match_type(mtype, patterns):
currentOffset += len(fields)
except IndexError:
quit()
except AttributeError:
print("Message type '%s' not found" % (type))
quit()
except TypeError:
print("You must specify a list of message types if outputting CSV format via the --types argument.")
exit()

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

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

# Track the last timestamp value. Used for compressing data for the CSV output format.
last_timestamp = None

# Track types found
available_types = set()

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

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

Expand All @@ -226,17 +229,12 @@ def match_type(mtype, patterns):
while True:
m = mlog.recv_match(blocking=args.follow, type=match_types)
if m is None:
# write the final csv line before exiting
if args.format == 'csv' and csv_out:
csv_out[0] = "{:.8f}".format(last_timestamp)
print(args.csv_sep.join(csv_out))
break
m_type = m.get_type()
available_types.add(m_type)
if isbin and m_type == "FMT" and args.format == 'csv':
if (isbin or islog) and m_type == "FMT" and args.format == 'csv':
if m.Name == types[0]:
fields += m.Columns.split(',')
csv_out = ["" for x in fields]
print(args.csv_sep.join(fields))

if args.reduce and reduce_msg(m_type, args.reduce):
Expand Down Expand Up @@ -329,27 +327,13 @@ def match_type(mtype, patterns):
# CSV format outputs columnar data with a user-specified delimiter
elif args.format == 'csv':
data = m.to_dict()

# If this message has a duplicate timestamp, copy its data into the existing data list. Also
# do this if it's the first message encountered.
if timestamp == last_timestamp or last_timestamp is None:
if isbin:
newData = [str(data[y]) if y != "timestamp" else "" for y in fields]
else:
newData = [str(data[y.split('.')[-1]]) if y.split('.')[0] == m_type and y.split('.')[-1] in data else "" for y in fields]

for i, val in enumerate(newData):
if val:
csv_out[i] = val

# Otherwise if this is a new timestamp, print out the old output data, and store the current message for later output.
if isbin or islog:
csv_out = [str(data[y]) if y != "timestamp" else "" for y in fields]
else:
csv_out[0] = "{:.8f}".format(last_timestamp)
print(args.csv_sep.join(csv_out))
if isbin:
csv_out = [str(data[y]) if y != "timestamp" else "" for y in fields]
else:
csv_out = [str(data[y.split('.')[-1]]) if y.split('.')[0] == m_type and y.split('.')[-1] in data else "" for y in fields]
csv_out = [str(data[y.split('.')[-1]]) if y.split('.')[0] == m_type and y.split('.')[-1] in data else "" for y in fields]
csv_out[0] = "{:.8f}".format(timestamp)
print(args.csv_sep.join(csv_out))

# MAT format outputs data to a .mat file specified through the
# --mat_file option
elif args.format == 'mat':
Expand Down Expand Up @@ -393,9 +377,6 @@ def match_type(mtype, patterns):
s += " seq=%u" % m.get_seq()
print(s)

# Update our last timestamp value.
last_timestamp = timestamp

# Export the .mat file
if args.format == 'mat':
scipy.io.savemat(args.mat_file, MAT, do_compression=args.compress)
Expand Down

0 comments on commit 55fcf4d

Please sign in to comment.