Debian Codegraph @

Nasal/Autopush/autopush.nas

raw
1 # AUTOPUSH
2 # Basic pushback logic class.
3 #
4 # Copyright (c) 2018 Autopush authors:
5 # Joshua Davidson http://github.com/Octal450
6 # Merspieler http://gitlab.com/merspieler
7 # Distribute under the terms of GPLv2.
8
9
10 var _K_p = nil;
11 var _F_p = nil;
12 var _K_i = nil;
13 var _F_i = nil;
14 var _K_d = nil;
15 var _F_d = nil;
16 var _F = nil;
17 var _int = nil;
18 var _V = nil;
19 var _T_f = nil;
20 var _K_yaw = nil;
21 var _yasim = 0;
22 var _time = nil;
23 # (ft / s^2) / ((km / h) / s)
24 var _unitconv = M2FT / 3.6;
25 var _debug = nil;
26
27 var _loop = func() {
28 if (!getprop("/sim/model/autopush/available")) {
29 _stop();
30 return;
31 }
32 var force = 0.0;
33 var x = 0.0;
34 var y = 0.0;
35 var z = 0.0;
36 # Rollspeed is only adequate if the wheel is touching the ground.
37 if (getprop("/gear/gear[0]/wow")) {
38 var V = getprop("/gear/gear[0]/rollspeed-ms") * 3.6;
39 var deltaV = getprop("/sim/model/autopush/target-speed-km_h") - V;
40 var minus_dV = _V - V;
41 var time = getprop("/sim/time/elapsed-sec");
42 var prop = math.min(math.max(_K_p * deltaV, -_F_p), _F_p);
43 var dt = time - _time;
44 var deriv = 0.0;
45 # XXX Sanitising dt. Smaller chance of freakout on lag spike.
46 if(dt > 0.0) {
47 if(dt < 0.05) {
48 _int = math.min(math.max(_int + _K_i * deltaV * dt, -_F_i), _F_i);
49 }
50 if(dt > 0.002) {
51 deriv = math.min(math.max(_K_d * minus_dV / dt, -_F_d), _F_d);
52 }
53 }
54 var accel = prop + _int + deriv;
55 if (_debug > 2) {
56 print("pushback prop " ~ prop ~ ", _int " ~ _int ~ ", deriv " ~ deriv);
57 }
58 _V = V;
59 _time = time;
60 if (!_yasim) {
61 force = accel * getprop("/fdm/jsbsim/inertia/weight-lbs") * _unitconv;
62 } else {
63 force = accel * getprop("/fdm/yasim/gross-weight-lbs") * _unitconv;
64 }
65 var pitch = getprop("/sim/model/autopush/pitch-deg") * D2R;
66 z = math.sin(pitch);
67 var pz = math.cos(pitch);
68 var yaw = getprop("/sim/model/autopush/yaw") * _K_yaw;
69 x = math.cos(yaw) * pz;
70 y = math.sin(yaw) * pz;
71 setprop("/sim/model/autopush/force-x", x);
72 setprop("/sim/model/autopush/force-y", y);
73 # JSBSim force's z is down.
74 setprop("/sim/model/autopush/force-z", -z);
75 }
76 setprop("/sim/model/autopush/force-lbf", force);
77 if (_yasim) {
78 # The force is divided by YASim thrust="100000.0" setting.
79 setprop("/sim/model/autopush/force-x-yasim", x * force * 0.00001);
80 # YASim force's y is to the left.
81 setprop("/sim/model/autopush/force-y-yasim", -y * force * 0.00001);
82 setprop("/sim/model/autopush/force-z-yasim", z * force * 0.00001);
83 }
84 }
85
86 var _timer = maketimer(0.0167, func{_loop()});
87 _timer.simulatedTime = 1;
88
89 var _start = func() {
90 # Else overwritten by dialog.
91 settimer(func() {
92 setprop("/sim/model/autopush/target-speed-km_h", 0.0)
93 }, 0.1, 1);
94 _K_p = getprop("/sim/model/autopush/K_p");
95 _F_p = getprop("/sim/model/autopush/F_p");
96 _K_i = getprop("/sim/model/autopush/K_i");
97 _F_i = getprop("/sim/model/autopush/F_i");
98 _K_d = getprop("/sim/model/autopush/K_d");
99 _F_d = getprop("/sim/model/autopush/F_d");
100 _F = getprop("/sim/model/autopush/F");
101 _T_f = getprop("/sim/model/autopush/T_f");
102 _K_yaw = getprop("/sim/model/autopush/yaw-mult") * D2R;
103 _yasim = (getprop("/sim/flight-model") == "yasim");
104 _debug = getprop("/sim/model/autopush/debug") or 0;
105 _int = 0.0;
106 _V = 0.0;
107 _time = getprop("/sim/time/elapsed-sec");
108 setprop("/sim/model/autopush/connected", 1);
109 if (!_timer.isRunning) {
110 if (getprop("/sim/model/autopush/chocks")) {
111 setprop("/sim/model/autopush/chocks", 0);
112 screen.log.write("(pushback): Pushback connected, chocks removed. Please release brakes.");
113 } else {
114 screen.log.write("(pushback): Pushback connected, please release brakes.");
115 }
116 }
117 _timer.start();
118 }
119
120 var _stop = func() {
121 if (_timer.isRunning) {
122 screen.log.write("(pushback): Pushback and bypass pin removed.");
123 }
124 _timer.stop();
125 setprop("/sim/model/autopush/force-lbf", 0.0);
126 if (_yasim) {
127 setprop("/sim/model/autopush/force-x-yasim", 0.0);
128 setprop("/sim/model/autopush/force-y-yasim", 0.0);
129 }
130 setprop("/sim/model/autopush/connected", 0);
131 setprop("/sim/model/autopush/enabled", 0);
132 }
133
134 setlistener("/sim/model/autopush/enabled", func(p) {
135 var enabled = p.getValue();
136 if ((enabled) and getprop("/sim/model/autopush/available")) {
137 _start();
138 } else {
139 _stop();
140 }
141 }, 1, 0);