CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/autotest/param_metadata/ednemit.py
Views: 1799
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
output_dict = dict()
38
# lowercase all keywords
39
for key in param.__dict__.keys():
40
output_dict[key.lower()] = param.__dict__[key]
41
42
# strip off any leading sillyness on the param name
43
split_name = param.__dict__["name"].split(":")
44
if len(split_name) == 2:
45
self.vehicle_name = split_name[0]
46
name = param.__dict__["name"].split(":")[-1]
47
output_dict["name"] = name
48
49
# remove any keys we don't really care to share
50
for key in self.remove_keys:
51
output_dict.pop(key, None)
52
for key in list(output_dict.keys()):
53
if not self.should_emit_field(param, key):
54
output_dict.pop(key, None)
55
56
# rearrange bitmasks to be a vector with nil's if the bit doesn't have meaning
57
if "bitmask" in output_dict:
58
highest_set_bit = 0
59
bits = []
60
for bit in output_dict["bitmask"].split(","):
61
bit_parts = bit.split(":")
62
bit_number = int(bit_parts[0])
63
bit_parts[0] = bit_number
64
bits.append(bit_parts)
65
if bit_number > highest_set_bit:
66
highest_set_bit = bit_number
67
output_bits = (highest_set_bit+1)*[None]
68
for bit in bits:
69
output_bits[bit[0]] = bit[1]
70
output_dict["bitmask"] = output_bits
71
72
# rearrange values into a float indexed map
73
if "values" in output_dict:
74
values = dict()
75
for value in output_dict["values"].split(","):
76
index, description = value.split(":")
77
values[float(index)] = description
78
output_dict["values"] = values
79
80
# remap range to be a map of floats
81
if "range" in output_dict:
82
low, high = output_dict["range"].split()
83
output_dict["range"] = {"low": float(low), "high": float(high)}
84
85
# remap the string to a float
86
if "increment" in output_dict:
87
output_dict["increment"] = float(output_dict["increment"])
88
89
# do any name changing desired
90
for remap in self.explict_remap:
91
output_dict[remap[1]] = output_dict.pop(remap[0])
92
93
self.output += "\"" + name + "\" " + edn_format.dumps(output_dict, keyword_keys=True)
94
95