Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/Replay/examples/rewrite-RFND-values-to-floats.py
6351 views
1
#!/usr/bin/env python3
2
3
'''In ArduPilot 4.7 the distance values were moved to float to
4
facilitate supporting rangefinders with more than 327m of range.
5
6
The dataflash Replay log messages changes to support this.
7
8
This script will take an older log and convert those messages such
9
that older logs can be replayed with newer code.
10
11
AP_FLAKE8_CLEAN
12
'''
13
14
from argparse import ArgumentParser
15
16
import struct
17
18
from pymavlink import DFReader
19
from pymavlink import mavutil
20
21
new_RRNH_format = "ffB"
22
new_RRNI_format = "ffffBBB"
23
24
25
class Rewrite():
26
def __init__(self, rewrite_fmt, rewrite_fmtu, rewrite_instance):
27
self.rewrite_fmt = rewrite_fmt
28
self.rewrite_fmtu = rewrite_fmtu
29
self.rewrite_instance = rewrite_instance
30
31
@staticmethod
32
def format_to_struct(fmt):
33
ret = bytes("<", 'ascii')
34
for c in fmt:
35
(s, _mul, _type) = DFReader.FORMAT_TO_STRUCT[c]
36
ret += bytes(s, 'ascii')
37
return bytes(ret)
38
39
40
rewrites = {
41
# convert RRNI.distance_cm > RRNI.distance
42
"RRNH": Rewrite(
43
rewrite_fmt=lambda buf, m : buf[:3] + struct.pack(
44
Rewrite.format_to_struct("BBnNZ"),
45
m.Type,
46
struct.calcsize(Rewrite.format_to_struct(new_RRNH_format)) + 3, # m.Length
47
bytes(m.Name, 'ascii'),
48
bytes(new_RRNH_format, 'ascii'),
49
bytes(m.Columns, 'ascii')
50
),
51
rewrite_fmtu=lambda buf, m : buf[:3] + struct.pack(
52
Rewrite.format_to_struct("QBNN"),
53
m.TimeUS,
54
m.FmtType,
55
bytes("mm-", 'ascii'), # new units
56
bytes("00-", 'ascii') # new mults
57
),
58
rewrite_instance=lambda buf, m : buf[:3] + struct.pack(
59
Rewrite.format_to_struct(new_RRNH_format),
60
m.GCl * 0.01,
61
m.MaxD * 0.01,
62
m.NumSensors
63
),
64
),
65
"RRNI": Rewrite(
66
rewrite_fmt=lambda buf, m : buf[:3] + struct.pack(
67
Rewrite.format_to_struct("BBnNZ"),
68
m.Type,
69
struct.calcsize(Rewrite.format_to_struct(new_RRNI_format)) + 3, # m.Length
70
bytes(m.Name, 'ascii'),
71
bytes(new_RRNI_format, 'ascii'),
72
bytes(m.Columns, 'ascii')
73
),
74
rewrite_fmtu=lambda buf, m : buf[:3] + struct.pack(
75
Rewrite.format_to_struct("QBNN"),
76
m.TimeUS,
77
m.FmtType,
78
bytes("???m--#", 'ascii'), # new units
79
bytes("???0---", 'ascii') # new mults
80
),
81
rewrite_instance=lambda buf, m : buf[:3] + struct.pack(
82
Rewrite.format_to_struct(new_RRNI_format),
83
m.PX,
84
m.PY,
85
m.PZ,
86
m.Dist * 0.01,
87
m.Orient,
88
m.Status,
89
m.I
90
),
91
),
92
}
93
94
95
parser = ArgumentParser(description=__doc__)
96
97
parser.add_argument("login")
98
parser.add_argument("logout")
99
100
args = parser.parse_args()
101
102
login = mavutil.mavlink_connection(args.login)
103
output = open(args.logout, mode='wb')
104
105
type_name_map = {}
106
107
108
def rewrite_message(m):
109
buf = bytearray(m.get_msgbuf())
110
111
mtype = m.get_type()
112
if mtype == 'FMT':
113
type_name_map[m.Type] = m.Name
114
if m.Name in rewrites:
115
return rewrites[m.Name].rewrite_fmt(buf, m)
116
117
if mtype == 'FMTU':
118
if m.FmtType not in type_name_map:
119
raise ValueError(f"Have not seen format for ID {m.FmtType}")
120
name = type_name_map[m.FmtType]
121
if name in rewrites:
122
return rewrites[name].rewrite_fmtu(buf, m)
123
124
if mtype in rewrites:
125
return rewrites[mtype].rewrite_instance(buf, m)
126
127
return buf
128
129
130
while True:
131
m = login.recv_msg()
132
if m is None:
133
break
134
output.write(rewrite_message(m))
135
136