Merged in f5soh/librepilot/LP-608_HoTT_telemetry_revonano (pull request #527)
[librepilot.git] / fix_vehicle_templates.py
blob8fa723bc5127444ed2cf0320f4a23b204bf28b71
1 #!/usr/bin/python
3 ###############################################################################
5 # The LibrePilot Project, http://www.librepilot.org Copyright (C) 2015.
6 # Script to update vehicle templates files to match current UAVO structure
7 # and data.
9 ###############################################################################
11 import json
12 import re
13 import collections
14 import fnmatch
15 import os
17 class DecimalEncoder(json.JSONEncoder):
18 def default(self, obj):
19 if isinstance(obj, Decimal):
20 return float(obj)
21 return json.JSONEncoder.default(self, obj)
23 json.encoder.FLOAT_REPR = lambda o: format(o, '.17g')
25 files = []
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))
30 for f in files:
31 data = json.load(open(f, 'r'), object_pairs_hook=collections.OrderedDict)
33 for item in data['objects']:
34 fieldsToRemove = []
35 for i in item['fields']:
36 name = i['name']
37 values = i['values']
38 if re.compile('ThrustPIDScaleCurve').match(name):
39 i['type'] = "int8"
40 for j in values:
41 j['value'] = int(j['value'] * 100)
42 elif re.compile('AcroInsanityFactor').match(name):
43 i['type'] = "int8"
44 value = 0
45 for j in values:
46 value = int(j['value'] * 100)
47 values.pop()
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)