Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/autotest/param_metadata/ednemit.py
9717 views
1
"""
2
Emits parameters as an EDN file, does some small remapping of names
3
"""
4
5
from emit import Emit
6
import edn_format
7
import datetime
8
import pytz
9
import subprocess
10
11
12
class EDNEmit(Emit):
13
def __init__(self, *args, **kwargs):
14
Emit.__init__(self, *args, **kwargs)
15
self.output = "{:date " + edn_format.dumps(datetime.datetime.now(pytz.utc)) + " "
16
git = subprocess.Popen(["git log --pretty=format:'%h' -n 1"], shell=True, stdout=subprocess.PIPE).communicate()[0]
17
self.output += ":git-hash \"" + git.decode("ascii") + "\" "
18
self.remove_keys = ["real_path"]
19
self.explict_remap = [["displayname", "display-name"]]
20
self.vehicle_name = None
21
22
def close(self):
23
if self.vehicle_name is not None:
24
self.output += ":vehicle \"" + self.vehicle_name + "\" "
25
else:
26
raise Exception('Vehicle name never found')
27
self.output += "}"
28
f = open("parameters.edn", mode='w')
29
f.write(self.output)
30
f.close()
31
32
def start_libraries(self):
33
pass
34
35
def emit(self, g):
36
for param in g.params:
37
if not self.should_emit_param(param):
38
continue
39
output_dict = dict()
40
# lowercase all keywords
41
for key in param.__dict__.keys():
42
output_dict[key.lower()] = param.__dict__[key]
43
44
# strip off any leading sillyness on the param name
45
split_name = param.__dict__["name"].split(":")
46
if len(split_name) == 2:
47
self.vehicle_name = split_name[0]
48
name = param.__dict__["name"].split(":")[-1]
49
output_dict["name"] = name
50
51
# remove any keys we don't really care to share
52
for key in self.remove_keys:
53
output_dict.pop(key, None)
54
for key in list(output_dict.keys()):
55
if not self.should_emit_field(param, key):
56
output_dict.pop(key, None)
57
58
# rearrange bitmasks to be a vector with nil's if the bit doesn't have meaning
59
if "bitmask" in output_dict:
60
highest_set_bit = 0
61
bits = []
62
for bit in output_dict["bitmask"].split(","):
63
bit_parts = bit.split(":")
64
bit_number = int(bit_parts[0])
65
bit_parts[0] = bit_number
66
bits.append(bit_parts)
67
if bit_number > highest_set_bit:
68
highest_set_bit = bit_number
69
output_bits = (highest_set_bit+1)*[None]
70
for bit in bits:
71
output_bits[bit[0]] = bit[1]
72
output_dict["bitmask"] = output_bits
73
74
# rearrange values into a float indexed map
75
if "values" in output_dict:
76
values = dict()
77
for value in output_dict["values"].split(","):
78
index, description = value.split(":")
79
values[float(index)] = description
80
output_dict["values"] = values
81
82
# remap range to be a map of floats
83
if "range" in output_dict:
84
low, high = output_dict["range"].split()
85
output_dict["range"] = {"low": float(low), "high": float(high)}
86
87
# remap the string to a float
88
if "increment" in output_dict:
89
output_dict["increment"] = float(output_dict["increment"])
90
91
# do any name changing desired
92
for remap in self.explict_remap:
93
output_dict[remap[1]] = output_dict.pop(remap[0])
94
95
self.output += "\"" + name + "\" " + edn_format.dumps(output_dict, keyword_keys=True)
96
97