Skip to content

Commit

Permalink
mavparm: allow param load of files containing 0x hex values
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbarker authored and tridge committed Jan 10, 2024
1 parent 0a6dbf3 commit 87173db
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions mavparm.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ def mavset(self, mav, name, value, retries=3, parm_type=None):
else:
print("can't send %s of type %u" % (name, parm_type))
return False
vfloat, = struct.unpack(">f", vstr)
numeric_value, = struct.unpack(">f", vstr)
else:
vfloat = float(value)
if value.lower().startswith('0x'):
numeric_value = int(value[2:], 16)
else:
numeric_value = float(value)

while retries > 0 and not got_ack:
retries -= 1
mav.param_set_send(name.upper(), vfloat, parm_type=parm_type)
mav.param_set_send(name.upper(), numeric_value, parm_type=parm_type)
tstart = time.time()
while time.time() - tstart < 1:
ack = mav.recv_match(type='PARAM_VALUE', blocking=False)
Expand All @@ -63,10 +66,10 @@ def mavset(self, mav, name, value, retries=3, parm_type=None):
continue
if str(name).upper() == str(ack.param_id).upper():
got_ack = True
self.__setitem__(name, float(value))
self.__setitem__(name, numeric_value)
break
if not got_ack:
print("timeout setting %s to %f" % (name, vfloat))
print("timeout setting %s to %f" % (name, numeric_value))
return False
return True

Expand Down Expand Up @@ -113,23 +116,29 @@ def load(self, filename, wildcard='*', mav=None, check=True, use_excludes=True):
continue
if not fnmatch.fnmatch(a[0].upper(), wildcard.upper()):
continue
value = a[1].strip()
if value.lower().startswith('0x'):
numeric_value = int(value[2:], 16)
else:
numeric_value = float(value)

if mav is not None:
if check:
if a[0] not in list(self.keys()):
print("Unknown parameter %s" % a[0])
continue
old_value = self.__getitem__(a[0])
if math.fabs(old_value - float(a[1])) <= self.mindelta:
if math.fabs(old_value - numeric_value) <= self.mindelta:
count += 1
continue
if self.mavset(mav, a[0], a[1]):
print("changed %s from %f to %f" % (a[0], old_value, float(a[1])))
if self.mavset(mav, a[0], value):
print("changed %s from %f to %f" % (a[0], old_value, numeric_value))
else:
print("set %s to %f" % (a[0], float(a[1])))
self.mavset(mav, a[0], a[1])
print("set %s to %f" % (a[0], numeric_value))
self.mavset(mav, a[0], value)
changed += 1
else:
self.__setitem__(a[0], float(a[1]))
self.__setitem__(a[0], numeric_value)
count += 1
f.close()
if mav is not None:
Expand Down

0 comments on commit 87173db

Please sign in to comment.