3 ###############################################################################
5 # The LibrePilot Project, http://www.librepilot.org Copyright (C) 2015.
6 # Script to update vehicle templates files to match current UAVO structure
9 ###############################################################################
17 class DecimalEncoder(json
.JSONEncoder
):
18 def default(self
, obj
):
19 if isinstance(obj
, Decimal
):
21 return json
.JSONEncoder
.default(self
, obj
)
23 json
.encoder
.FLOAT_REPR
= lambda o
: format(o
, '.17g')
26 for root
, dirnames
, filenames
in os
.walk('ground/gcs/src/share/vehicletemplates/'):
27 for filename
in fnmatch
.filter(filenames
, '*.optmpl'):
28 files
.append(os
.path
.join(root
, filename
))
31 data
= json
.load(open(f
, 'r'), object_pairs_hook
=collections
.OrderedDict
)
33 for item
in data
['objects']:
35 for i
in item
['fields']:
38 if re
.compile('ThrustPIDScaleCurve').match(name
):
41 j
['value'] = int(j
['value'] * 100)
42 elif re
.compile('AcroInsanityFactor').match(name
):
46 value
= int(j
['value'] * 100)
48 values
.append({'name': 'roll', 'value': value
})
49 values
.append({'name': 'pitch', 'value': value
})
50 values
.append({'name': 'yaw', 'value': value
})
51 elif re
.compile('FeedForward').match(name
):
52 fieldsToRemove
.append(i
)
53 elif re
.compile('MaxAccel').match(name
):
54 fieldsToRemove
.append(i
)
55 elif re
.compile('AccelTime').match(name
):
56 fieldsToRemove
.append(i
)
57 elif re
.compile('DecelTime').match(name
):
58 fieldsToRemove
.append(i
)
59 for field
in fieldsToRemove
:
60 item
['fields'].remove(field
)
61 with
open(f
, 'w') as outfile
:
62 json
.dump(data
, outfile
, indent
=4, separators
=(',', ': '), cls
=DecimalEncoder
)