-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_jsbsim.py
executable file
·127 lines (104 loc) · 3.97 KB
/
run_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
#!/usr/bin/env python3
"""run_simulator
Front end to the simulation module
Author: Curtis L. Olson, University of Minnesota, Dept of Aerospace
Engineering and Mechanics, UAV Lab.
"""
from apscheduler.schedulers.background import BackgroundScheduler # pip install APScheduler (dnf install python3-APScheduler)
import argparse
from pathlib import Path
import time
from nstSimulator.sim.init_position import PositionInit
from nstSimulator.sim.jsbsim import JSBSimWrap
from nstSimulator.sim.joystick import Joystick
from nstSimulator.sim.visuals.fgfs import fgfs
from nstSimulator.sim.visuals.display import Display
from nstSimulator.sim.visuals.xp.xp import XPlane
from lib_sim.comms.HIL_nsLink import HIL
from lib_sim.FCS.fcs_mgr import FCSMgr
# command line arguments
parser = argparse.ArgumentParser(description="run the simulation")
parser.add_argument("--model", default="Rascal110", help="flight dynamics model")
parser.add_argument("--modelpath", help="flight dynamics model")
parser.add_argument("--takeoff", help="takeoff from APT:RWY")
parser.add_argument("--final", help="final approach to APT:RWY:dist_nm")
parser.add_argument("--pattern", help="45 degree downwind entry to pattern APT:RWY")
parser.add_argument("--vc", help="initial airspeed for in-air starts")
parser.add_argument("--hz", default=60, type=int, help="outer loop hz")
parser.add_argument("--fdm-steps-per-frame", default=4, help="number of jsbsim steps per outer loop frame")
args = parser.parse_args()
# setup initial position and velocity for trimmming
pos_init = PositionInit()
apt_id = "KPAN"
rwy_id = "06"
dist_nm = 0
vc_kts = 0
pos_lla = None
hdg_deg = 0
if args.takeoff:
if ":" in args.takeoff:
apt_id, rwy_id = args.takeoff.split(":", 1)
pos_lla, hdg_deg = pos_init.takeoff(apt_id, rwy_id)
else:
print("Please use the form APT_ID:RWY_ID")
quit()
if args.final:
if ":" in args.final and args.vc:
apt_id, rwy_id, dist_str = args.final.split(":", 2)
dist_nm = float(dist_str)
vc_kts = float(args.vc)
pos_lla, hdg_deg = pos_init.final_approach(apt_id, rwy_id, dist_nm)
else:
print("Please use the form --final APT_ID:RWY_ID:dist_nm --vc airspeed_kts")
quit()
if args.pattern:
if ":" in args.pattern and args.vc:
apt_id, rwy_id = args.pattern.split(":", 1)
vc_kts = float(args.vc)
pos_lla, hdg_deg = pos_init.pattern_entry(apt_id, rwy_id)
else:
print("Please use the form --final APT_ID:RWY_ID:dist_nm --vc airspeed_kts")
quit()
if not pos_lla:
print("Need to provide an initial position.")
parser.print_usage()
quit()
# if main loop hz is 60 and fdm steps per frame is 4, then the JSBSim hz will be
# 60*4 = 240 hz, while the main program loop steps forward at 60 hz (i.e.
# matches the graphical update rate, or logging rate preferences.) The
# advantage to running JSBSim at a higher rate is slightly better integration
# accuracy.
jsbsim_hz = args.fdm_steps_per_frame * args.hz
joystick = Joystick()
display = Display()
xp = XPlane()
hil = HIL()
# initialize JSBSim and load the aircraft model
if args.modelpath is None:
pathJSB = Path(__file__).parent / "./nstSimulator/data/models_jsbsim"
else:
pathJSB = Path(args.modelpath)
print("JSBSim path:", pathJSB)
sim = JSBSimWrap(args.model, pathJSB.as_posix(), dt=1/jsbsim_hz)
sim.setup_initial_conditions(pos_lla, hdg_deg, vc_kts)
sim.SetTurb(turbSeverity=0, vWind20_mps=2, vWindHeading_deg=45) # Trim with wind, no turbulence
fcs = FCSMgr()
start_time = time.time()
def update():
joystick.update()
hil.read()
if sim.trimmed:
fcs.update(dt=1/args.hz)
# sim.UpdateTerrainElevation()
if time.time() - start_time >= 1:
print("calling sim.update()")
sim.update(args.fdm_steps_per_frame, updateWind=True)
hil.write()
fgfs.send_to_fgfs()
display.update()
xp.update()
sched = BackgroundScheduler()
sched.add_job(update, 'interval', seconds=1/args.hz)
sched.start()
while True:
time.sleep(1)