FG_run.py
3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python3
import json
import sys
import os
import xml.etree.ElementTree as ET
import xml.dom.minidom as minidom
import shutil
import subprocess
if len(sys.argv)!=3:
print('FG_run.py -- bad argument count')
exit(-1)
filename=sys.argv[1]
px4id=int(sys.argv[2])
if not os.path.exists('./'+filename):
print('FG_run.py -- file not found: '+filename)
exit(-1)
#get FG binary to run
fgbin=os.getenv("FG_BINARY")
if fgbin is None:
fgbin='fgfs'
#pick fgroot (fgdata) from flightgear
fgroot=""
fghelp=subprocess.check_output([fgbin, '--version']).decode("utf-8").split('\n');
for s in fghelp:
if s.find("FG_ROOT")>=0:
fgroot=s.split('=')[1]
if not fgroot:
print('fgroot not found.. abort')
exit(-1)
#get FGFS MODEL dir
fgmodelsdir=os.getenv("FG_MODELS_DIR")
if fgmodelsdir is None:
fgmodelsdir='./models'
#get FGFS EXTRA PARAMS dir
fgargsex=os.getenv("FG_ARGS_EX")
print(fgargsex)
if fgargsex is None:
fgargsex=''
protocols=fgroot+'/Protocol'
if not os.access(protocols, os.W_OK):
print('Cannot Write into direcotry: '+ protocols)
exit(-1)
############################ Parse Config ######################################
with open(filename) as json_file:
data = json.load(json_file)
model=data['FgModel']
url=data['Url']
controls=data['Controls']
print(model)
print(url)
for c in controls:
print(c[0]+' '+c[1]+' '+c[2])
############################ Create FG Input Protocol ###########################
propertyList=ET.Element('PropertyList')
generic= ET.SubElement(propertyList, 'generic')
input=ET.SubElement(generic,'input')
binary_mode=ET.SubElement(input,'binary_mode')
binary_mode.text='true'
for c in controls:
chunk=ET.SubElement(input,'chunk')
name=ET.SubElement(chunk,'name')
name.text=c[0]
type=ET.SubElement(chunk,'type')
type.text='double'
node=ET.SubElement(chunk,'node')
node.text=c[1]
rough_string = ET.tostring(propertyList, 'utf-8')
reparsed = minidom.parseString(rough_string)
xmlstring=reparsed.toprettyxml(indent=" ")
with open(protocols+'/PX4toFG.xml','w') as xml_file:
xml_file.write(xmlstring)
############################ Copy FG Output Protocol ############################
shutil.copy('px4bridge.xml',protocols+'/FGtoPX4.xml' )
############################ Run FG #############################################
parameters = [
"--aircraft=" + model,
"--fg-aircraft=" + fgmodelsdir,
"--enable-terrasync",
"--timeofday=noon",
"--disable-sound",
"--telnet="+str(15400+px4id),
"--generic=socket,out,100,127.0.0.1,"+str(15200+px4id)+",udp,FGtoPX4",
"--generic=socket,in,100,,"+str(15300+px4id)+",udp,PX4toFG",
"--model-hz=120",
"--disable-random-objects",
"--prop:/sim/rendering/texture-compression=off",
"--prop:/sim/rendering/quality-level=0",
"--prop:/sim/rendering/shaders/quality-level=0",
"--disable-ai-traffic",
"--prop:/sim/ai/enabled=0",
"--prop:/sim/rendering/random-vegetation=0",
"--prop:/sim/rendering/random-buildings=0",
"--disable-specular-highlight",
"--disable-ai-models",
"--disable-clouds",
"--disable-clouds3d",
"--fog-fastest",
"--visibility=2000",
"--disable-distance-attenuation",
"--disable-real-weather-fetch",
"--prop:/sim/rendering/particles=0",
"--prop:/sim/rendering/multi-sample-buffers=1",
"--prop:/sim/rendering/multi-samples=2",
"--prop:/sim/rendering/draw-mask/clouds=false",
"--prop:/sim/rendering/draw-mask/aircraft=true",
"--prop:/sim/rendering/draw-mask/models=true",
"--prop:/sim/rendering/draw-mask/terrain=true",
"--disable-random-vegetation",
"--disable-random-buildings",
"--disable-rembrandt",
"--disable-horizon-effect"
]
#with FG output
commnad=fgbin+" "+fgargsex+" "+" ".join(parameters)+" & echo $! > /tmp/px4fgfspid_"+str(px4id)
print(commnad)
os.system(commnad)