Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DFReader: added support for adding new messages to bin logs #804

Merged
merged 1 commit into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions DFReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,38 @@ def _parse_next(self):

return m

def find_unused_format(self):
'''find an unused format code'''
for i in range(254, 1, -1):
if not i in self.formats:
return i
return None

def add_format(self, fmt):
'''add a new format'''
new_type = self.find_unused_format()
if new_type is None:
return None
fmt.type = new_type
self.formats[new_type] = fmt
return fmt

def make_msgbuf(self, fmt, values):
'''make a message buffer from a list of values'''
ret = struct.pack("BBB", 0xA3, 0x95, fmt.type)
ret += struct.pack(fmt.msg_struct, *values)
return ret

def make_format_msgbuf(self, fmt):
'''make a message buffer for a FMT message'''
fmt_fmt = self.formats[0x80]
ret = struct.pack("BBB", 0xA3, 0x95, 0x80)
ret += struct.pack(fmt_fmt.msg_struct, *[fmt.type,struct.calcsize(fmt.msg_struct)+3,
fmt.name.encode('ascii'),
fmt.format.encode('ascii'),
','.join(fmt.columns).encode('ascii')])
return ret


def DFReader_is_text_log(filename):
'''return True if a file appears to be a valid text log'''
Expand Down
36 changes: 36 additions & 0 deletions examples/add_msg_to_bin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
'''
this shows how to add a new message to a bin log
'''

from pymavlink import DFReader

import argparse
parser = argparse.ArgumentParser("add msg example")
parser.add_argument("login")
parser.add_argument("logout")

args = parser.parse_args()

inf = DFReader.DFReader_binary(args.login)
outf = open(args.logout, 'wb')

# make a GPSX message which will mirror GPS message
GPSX = inf.add_format(DFReader.DFFormat(0, "GPSX", 0, "QLLf", "TimeUS,Lat,Lng,Alt"))
outf.write(inf.make_format_msgbuf(GPSX))

count = 0

while True:
m = inf.recv_msg()
if m is None:
break
outf.write(m.get_msgbuf())
if m.get_type() == 'GPS':
# mirror GPS message as GPSX
values = [m.TimeUS, int(m.Lat*1.0e7), int(m.Lng*1.0e7), m.Alt]
outf.write(inf.make_msgbuf(GPSX, values))
count += 1

print("Added %u GPSX messages to %s from %s" % (count, args.logout, args.login))