Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/scripts/convert_plane_PID.py
Views: 1798
#!/usr/bin/env python1'''2convert fixed wing PIDs in a parameter file from old system to ACPID system3'''45import os6import re78found_line = False9changed_param = False1011def process_file(fname):12global found_line13global changed_param1415found_line = False16changed_param = False1718print("Processing %s" % fname)19lines = open(fname).readlines()20for i in range(len(lines)):21lines[i] = lines[i].rstrip()22while lines[len(lines)-1].strip() == "":23lines = lines[:-1]2425def find_line(pname):26global found_line27for i in range(len(lines)):28if lines[i].startswith(pname) and not lines[i][len(pname)].isalpha():29found_line = True30return i31return None3233def find_param(pname, dvalue):34i = find_line(pname)35if i is None:36return dvalue37s = lines[i].replace(","," ")38a = s.split()39return float(a[1])4041def set_param(pname, value, old_name):42global changed_param43i = find_line(pname)44if i is None:45i = find_line(old_name)46if i is None:47changed_param = True48lines.append("%s %f" % (pname, value))49return50# maintain separator if possible51sep = " "52m = re.match("[A-Z_0-9]+([\s,]+)[0-9.-]+", lines[i])53if m is not None:54sep = m.group(1)55changed_param = True56lines[i] = "%s%s%f" % (pname, sep, value)5758print("Converting pitch")59FF = find_param("PTCH2SRV_FF", 0)60P = find_param("PTCH2SRV_P", 1.0)61I = find_param("PTCH2SRV_I", 0.3)62D = find_param("PTCH2SRV_D", 0.04)63IMAX = find_param("PTCH2SRV_IMAX", 3000)64TCONST = find_param("PTCH2SRV_TCONST", 0.5)6566if FF <= 0:67I = max(I,0.3)6869kp_ff = max((P - I * TCONST) * TCONST - D, 0)70if found_line:71set_param("PTCH_RATE_FF", FF + kp_ff, "PTCH2SRV_FF")72set_param("PTCH_RATE_P", D, "PTCH2SRV_P")73set_param("PTCH_RATE_I", I * TCONST, "PTCH2SRV_I")74set_param("PTCH_RATE_D", 0, "PTCH2SRV_D")75set_param("PTCH_RATE_IMAX", IMAX/4500.0, "PTCH2SRV_IMAX")7677found_line = False7879print("Converting roll")80FF = find_param("RLL2SRV_FF", 0)81P = find_param("RLL2SRV_P", 1.0)82I = find_param("RLL2SRV_I", 0.3)83D = find_param("RLL2SRV_D", 0.08)84IMAX = find_param("RLL2SRV_IMAX", 3000)85TCONST = find_param("RLL2SRV_TCONST", 0.5)8687kp_ff = max((P - I * TCONST) * TCONST - D, 0)88if found_line:89set_param("RLL_RATE_FF", FF + kp_ff, "RLL2SRV_FF")90set_param("RLL_RATE_P", D, "RLL2SRV_P")91set_param("RLL_RATE_I", I * TCONST, "RLL2SRV_I")92set_param("RLL_RATE_D", 0, "RLL2SRV_D")93set_param("RLL_RATE_IMAX", IMAX/4500.0, "RLL2SRV_IMAX")9495if not changed_param:96print("No fixed wing PID params")97return9899print("Writing")100tfile = fname + ".tmp"101f = open(tfile,"w")102for i in range(len(lines)):103f.write("%s\n" % lines[i])104f.close()105os.rename(tfile, fname)106107import argparse108parser = argparse.ArgumentParser(description='convert plane PIDs from old to new system')109110parser.add_argument('param_file', nargs='+')111args = parser.parse_args()112113for f in args.param_file:114process_file(f)115116117