-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSBsim.py
144 lines (122 loc) · 4.88 KB
/
JSBsim.py
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
#! /usr/bin/python
#
# JSBSim.py
#
# Standalone version of JSBSim in Python language.
#
# Copyright (c) 2019 Bertrand Coconnier
#
# 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 xml.etree.ElementTree as et
import argparse, sys, os
import jsbsim
import time
parser = argparse.ArgumentParser()
parser.add_argument("input", nargs='?', help="script file name")
parser.add_argument("--version", action="version",
version="%(prog)s {}".format(jsbsim.FGJSBBase().get_version()))
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("--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("--end", type=float, default=1E99, metavar="<time (double)>",
help="specifies the sim end time")
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")
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 args.script:
fdm.load_script(args.script)
elif args.aircraft:
fdm.load_model(args.aircraft, False)
if args.initfile:
fdm.load_ic(args.initfile, True)
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))
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()
while result and fdm.get_sim_time() <= args.end:
if args.realtime:
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()
else:
result = fdm.run()
if args.nice:
time.sleep(sleep_nseconds / 1000000.0)