-
Notifications
You must be signed in to change notification settings - Fork 607
Expand file tree
/
Copy pathJSBSim.py
More file actions
258 lines (223 loc) · 9.65 KB
/
Copy pathJSBSim.py
File metadata and controls
258 lines (223 loc) · 9.65 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#! /usr/bin/python
#
# JSBSim.py
#
# Standalone version of JSBSim in Python language.
#
# Copyright (c) 2019-2026 Bertrand Coconnier
# Copyright (c) 2020 Ben Busby
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, see <http://www.gnu.org/licenses/>
#
import argparse
import os
import sys
import time
import xml.etree.ElementTree as et
import jsbsim
class LogConsole(jsbsim.DefaultLogger):
def format(self, log_format:jsbsim.LogFormat):
if log_format == jsbsim.LogFormat.RED:
print("\x1b[31m\0", end="")
elif log_format == jsbsim.LogFormat.BLUE:
print("\x1b[34m\0", end="")
elif log_format == jsbsim.LogFormat.CYAN:
print("\x1b[36m\0", end="")
elif log_format == jsbsim.LogFormat.GREEN:
print("\x1b[32m\0", end="")
elif log_format == jsbsim.LogFormat.DEFAULT:
print("\x1b[39m\0", end="")
elif log_format == jsbsim.LogFormat.BOLD:
print("\x1b[1m\0", end="")
elif log_format == jsbsim.LogFormat.NORMAL:
print("\x1b[22m\0", end="")
elif log_format == jsbsim.LogFormat.UNDERLINE_ON:
print("\x1b[4m\0", end="")
elif log_format == jsbsim.LogFormat.UNDERLINE_OFF:
print("\x1b[24m\0", end="")
else:
print("\x1b[0m\0", end="") # Reset
def main():
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("input", nargs='?', help="script file name")
parser.add_argument("--version", action="version",
version=f"{jsbsim.FGJSBBase().get_version()}")
parser.add_argument("--root", default='.', metavar="<path>",
help="specifies the JSBSim root directory (where aircraft/, engine/, etc. reside)")
parser.add_argument("--aircraft", metavar="<filename>",
help="specifies the name of the aircraft to be modeled")
parser.add_argument("--script", metavar="<filename>",
help="specifies a script to run")
parser.add_argument("--initfile", metavar="<filename>",
help="specifies an initialization file")
parser.add_argument("--planet", metavar="<filename>",
help="specifies a planet definition file")
parser.add_argument("--outputlogfile", action="append", metavar="<filename>",
help="sets (overrides) the name of a data output file")
parser.add_argument("--logdirectivefile", action="append", metavar="<filename>",
help="specifies the name of a data logging directives file")
parser.add_argument("--outputpath", metavar="<path>",
help="specifies the directory where the output files will be written.")
parser.add_argument("--realtime", default=False, action="store_true",
help="specifies to run in real world time")
parser.add_argument("--nice", default=False, action="store_true",
help="specifies to run at lower CPU usage")
parser.add_argument("--nohighlight", default=False, action="store_true",
help="specifies that console output should be pure text only (no color)")
parser.add_argument("--suspend", default=False, action="store_true",
help="specifies to suspend the simulation after initialization")
parser.add_argument("--catalog", default=False, action="store_true",
help="specifies that all properties for this aircraft model should be printed")
parser.add_argument("--property", action="append", metavar="<name=value>",
help="e.g. --property=simulation/integrator/rate/rotational=1")
parser.add_argument("--simulation-rate", type=float, metavar="<rate (float)>",
help="specifies the sim dT time or frequency"
"\nIf rate specified is less than 1, it is interpreted as a time step size,"
"\notherwise it is assumed to be a rate in Hertz.")
parser.add_argument("--end", type=float, default=1E99, metavar="<time (float)>",
help="specifies the sim end time")
args = parser.parse_args()
sleep_period = 0.01
def CheckXMLFile(f):
# Is f a file ?
if not os.path.isfile(f):
return None
# Is f an XML file ?
try:
tree = et.parse(f)
except et.ParseError:
return None
return tree
if args.input:
tree = CheckXMLFile(args.input)
if not tree:
print('The argument "{}" cannot be interpreted as a file name.'.format(args.input))
sys.exit(-1)
root = tree.getroot()
if root.tag == 'runscript':
if args.script:
print('Two script files are specified.')
sys.exit(-1)
else:
args.script = args.input
if root.tag == 'output':
if args.logdirectivefile:
args.logdirectivefile += [args.input]
else:
args.logdirectivefile = [args.input]
if root.tag == 'fdm_config':
if args.aircraft:
print('Two aircraft files are specified.')
sys.exit(-1)
else:
args.aircraft = args.input
fdm = jsbsim.FGFDMExec(args.root, None)
if not args.nohighlight:
jsbsim.set_logger(LogConsole())
if args.outputpath:
fdm.set_output_path(args.outputpath)
else:
fdm.set_output_path(".")
if args.simulation_rate:
if args.simulation_rate < 1.0:
fdm.set_dt(args.simulation_rate)
else:
fdm.set_dt(1.0/args.simulation_rate)
args.simulation_rate = fdm.get_delta_t()
if args.planet:
fdm.load_planet(args.planetfile, False)
if args.property:
pm = fdm.get_property_manager()
for p in args.property:
name, value = p.split("=")
if "simulation" in name and pm.hasNode(name):
fdm.set_property_value(name, float(value))
if args.script:
if args.aircraft:
print("You cannot specify an aircraft file with a script.")
sys.exit(-1)
if args.catalog:
print("Cannot specify catalog with script option")
sys.exit(-1)
if args.initfile is None:
args.initfile = ""
fdm.load_script(args.script, args.simulation_rate, args.initfile)
elif args.aircraft:
if args.catalog:
fdm.set_debug_level(0)
fdm.load_model(args.aircraft)
if args.catalog:
fdm.print_property_catalog()
sys.exit(0)
if args.initfile:
fdm.load_ic(args.initfile, True)
else:
print("You must specify an initialization file with the aircraft name.")
sys.exit(-1)
if args.initfile and not args.aircraft:
print("You must specify an initilization file with the aircraft name")
sys.exit(-1)
if args.logdirectivefile:
for f in args.logdirectivefile:
if not fdm.set_output_directive(f):
print("Output directives not properly set in file {}".format(f))
sys.exit(-1)
if args.outputlogfile:
for n, f in enumerate(args.outputlogfile):
old_filename = fdm.get_output_filename(n)
if not fdm.set_output_filename(n, f):
print("Output filename could not be set")
else:
print("Output filename change from {} from aircraft configuration file to {} specified on command line.".format(old_filename, f))
if args.property:
for p in args.property:
name, value = p.split("=")
if pm.hasNode(name):
fdm.set_property_value(name, float(value))
else:
print("No property by the name {}".format(name))
sys.exit(-1)
fdm.run_ic()
fdm.print_simulation_configuration()
frame_duration = fdm.get_delta_t()
sleep_nseconds = (frame_duration if args.realtime else sleep_period) * 1E9
current_seconds = initial_seconds = time.time()
result = fdm.run()
if args.suspend:
fdm.hold()
while result and fdm.get_sim_time() <= args.end:
fdm.check_incremental_hold()
if fdm.holding():
args.suspend = True
paused_seconds = time.time() - current_seconds
result = fdm.run()
else:
if args.realtime:
if args.suspend:
initial_seconds += paused_seconds
args.suspend = False
current_seconds = time.time()
actual_elapsed_time = current_seconds - initial_seconds
sim_lag_time = actual_elapsed_time - fdm.get_sim_time()
for _ in range(int(sim_lag_time / frame_duration)):
result = fdm.run()
current_seconds = time.time()
if fdm.holding():
break
else:
result = fdm.run()
if args.nice:
time.sleep(sleep_nseconds / 1000000.0)
if __name__ == "__main__":
main()