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/scripts/convert_param_scale.py
Views: 1798
1
#!/usr/bin/env python3
2
'''
3
tool to convert parameter names and scales, useful for conversion for cm -> m and cdeg -> deg
4
5
this looks for files called *.param or *.parm
6
7
example:
8
Tools/scripts/convert_param_scale.py --scale 0.1 TRIM_ARSPD_CM AIRSPEED_CRUISE
9
'''
10
11
import os
12
import sys
13
14
from argparse import ArgumentParser
15
16
parser = ArgumentParser(description="parameter conversion tool")
17
18
parser.add_argument("--scale", default=1.0, type=float, help="scale factor")
19
parser.add_argument("--directory", default=".", help="directory to search")
20
parser.add_argument("old_name", default=None, help="old parameter name")
21
parser.add_argument("new_name", default=None, help="new parameter name")
22
23
args = parser.parse_args()
24
25
def process_file(fname):
26
needs_write = False
27
with open(fname, "r") as f:
28
lines = f.readlines()
29
for i in range(len(lines)):
30
line = lines[i]
31
if line.startswith(args.old_name):
32
a = line.split()
33
if len(a) == 2:
34
sep = ' '
35
else:
36
a = line.split(',')
37
if len(a) == 2:
38
sep = ','
39
else:
40
continue
41
v = float(a[1])
42
v *= args.scale
43
lines[i] = "%s%s%.2f\n" % (args.new_name, sep, v)
44
needs_write = True
45
if not needs_write:
46
return
47
print("Updating %s" % fname)
48
with open(fname, "w") as f:
49
for line in lines:
50
f.write(line)
51
52
for root, dirs, files in os.walk(args.directory):
53
for file in files:
54
if file.endswith(".parm") or file.endswith(".param"):
55
process_file(os.path.join(root, file))
56
57