-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_sim.py
executable file
·84 lines (68 loc) · 2.21 KB
/
run_sim.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
#!/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
import json
import numpy as np
import time
from lib.joystick import Joystick
from lib.simulator import Simulator
from lib.state_mgr import StateManager
from visuals.fgfs import fgfs
from visuals.pvi.pvi import PVI
from visuals.xp.xp import XPlane
# command line arguments
parser = argparse.ArgumentParser(description="run the simulation")
parser.add_argument("model", help="flight model")
parser.add_argument('--realtime', action='store_true', help='run sim in realtime')
parser.add_argument('--no-trim', action='store_true', help="don't trim")
args = parser.parse_args()
run_time = 600
joystick = Joystick()
pvi = PVI()
xp = XPlane()
# load in the beast!
f = open(args.model, "r")
model = json.load(f)
print(model)
f.close()
state_mgr = StateManager()
rows = model["rows"]
cols = model["cols"]
dt = model["dt"]
for condition in model["conditions"]:
sim = Simulator(state_mgr)
condition["sim"] = sim
A = np.array(condition["A"]).reshape(rows, cols)
sim.setup(dt, A, condition["parameters"])
sim.reset()
if not args.no_trim: # fixme
sim.trim(20)
def update():
global model
joystick.update()
for i, condition in enumerate(model["conditions"]):
if "flaps" in condition["condition"] and abs(joystick.flaps - condition["condition"]["flaps"]) < 0.1:
print(i, condition["condition"])
sim = condition["sim"]
state_mgr.set_throttle(joystick.throttle)
state_mgr.set_flight_surfaces(joystick.aileron, joystick.elevator, joystick.rudder)
sim.update()
fgfs.send_to_fgfs(state_mgr)
pvi.update(state_mgr, 0, 0, 0, 0)
xp.update(state_mgr)
if args.realtime:
sched = BackgroundScheduler()
sched.add_job(update, 'interval', seconds=sim.dt)
sched.start()
while True:
time.sleep(run_time)
sched.shutdown()
else:
while sim.time <= run_time:
sim.update()
sim.plot()