Debian Codegraph @

Aircraft/Instruments-3d/computing-gun-sights/Nasal/lead-computer.nas

raw
1 #****************************************************************************
2 #
3 # Derived from
4 # A Digital Lead Computing Optical Sight Model
5 # Anthony L. Leatham, et al
6 # Air Force Academy
7 # Sept. 1974
8 # http://www.dtic.mil/dtic/tr/fulltext/u2/786464.pdf
9 #
10 # Appendex D Air Force Avionics Laboratory digital LCOS
11 # Contains FORTRAN code that is used here converted into Nasal code.
12 #
13 # Original source for equations and code was from:
14 #
15 # Air-to-Air Gun Fire Control Equations for Digital Lead Computing Optical Sights
16 # R.A. Manske
17 # AFAL-TM-74-8-NVE-1
18 # April 1974
19 # Air Force Avionics Laboratory
20 # Wring-Patterson AFB, Ohio
21 #
22 # Document location unknown.
23 #
24 # non-static inputs
25 #
26 # range in feet
27 # rangeRate range rate in ft/sec
28 # P angular pitch rate of aircraft body axis in radians/second
29 # Q angular yaw rate of aircraft body axis in radians/second
30 # R angular roll rate of aircraft body axis in radians/second
31 # normalAcceleration normal acceleration in ft/sec/sec (-32.17 straight and level)
32 # alpha angle of attack radians
33 # trueAirSpeed true airspeed - feet per second
34 # RHO air density in slugs per cubic foot
35 #
36 #********************************************************************************
37
38 # ================================ Initalize ======================================
39 # Make sure all needed properties are present and accounted
40 # for, and that they have sane default values.
41
42 # print("gunsight-computer.nas started");
43
44 var propertyTreeRoot = "/controls/armament/gunsight/";
45
46 var z_offset = 0;
47 var y_offset = 0;
48 var gunElevationRadians = 0;
49
50 var ballisticCoefficienct = 0;
51 var muzzleVelocity = 0;
52 var harmonizationRange = 0;
53 var range = 0;
54
55 # constants
56 var timeStep = getprop(propertyTreeRoot, "timeStep");
57
58 # sight damping factor
59 var SIG = getprop(propertyTreeRoot, "dampingFactor") * 0.4;
60
61 var KSIG = 1.0 / (1.0 + SIG);
62
63 var seaLevelAirDensity = 0.00238;
64 var radiansPerDrgree = 0.0174532925;
65
66 # Ballistic coefficient divided by sea level air density
67 var KBRHO = 0;
68
69 var cosGunElevation = 0;
70 var sinGunElevation = 0;
71
72 # Reciprocal of gun harmonization range
73 var RRH = 0;
74
75 var SQRV = 0;
76 var RHO = 0;
77
78 var RDC = 0;
79 var LA = 0;
80 var LE = 0;
81 var VLS = 0;
82 var rangeRate = 0;
83 var VOS = 0;
84 var VCM = 0;
85 var VMC = 0;
86 var rangeRateArray = [0, 0, 0, 0];
87 var rRAindex = 0;
88
89 var P = 0;
90 var Q = 0;
91 var R = 0;
92 var normalAcceleration = -32.17;
93
94 var maxElevation = getprop(propertyTreeRoot, "maxElevation");
95 var maxAzimuth = getprop(propertyTreeRoot, "maxAzimuth");
96
97 var initSightComputer = func()
98 {
99 # print("gunsite-computer.nas init()");
100
101 # Get aircraft/sight specific info
102
103 # Offset of gun in feet from sight line
104 z_offset = getprop(propertyTreeRoot, "z-offsetFeet");
105 y_offset = getprop(propertyTreeRoot, "y-offsetFeet");
106
107 # Elevation of gun(s) above alpha
108 gunElevationRadians = getprop(propertyTreeRoot, "gunElevationDegrees") * 0.0174532925;
109
110 # Projectile ballistic Coefficient and muzzle velocity in feet per second
111 ballisticCoefficienct = getprop(propertyTreeRoot, "ballisticCoefficienct");
112 muzzleVelocity = getprop(propertyTreeRoot, "muzzleVelocity");
113
114 # Range where the sight line is = the guns bore line
115 harmonizationRange = getprop(propertyTreeRoot, "gunHarminizationRangeFeet");
116
117 # range to target usually from an onboard radar system or a
118 # manual system controlled by the aircraft crew
119 range = getprop(propertyTreeRoot, "range");
120
121 var seaLevelAirDensity = 0.00238;
122
123 # Ballistic coefficient divided by sea level air density
124 KBRHO = ballisticCoefficienct / seaLevelAirDensity;
125
126 cosGunElevation = math.cos(gunElevationRadians);
127 sinGunElevation = math.sin(gunElevationRadians);
128
129 # Reciprocal of gun harmonization range
130 RRH = 1.0 / harmonizationRange;
131
132 SQRV = math.sqrt(muzzleVelocity);
133 RHO = getprop("/environment/density-slugft3");
134
135 VLS = KBRHO * RHO * SQRV * range;
136 rangeRate = 0;
137 VOS = muzzleVelocity + VLS + rangeRate;
138 VCM = math.sqrt(VOS * VOS - 4.0 * (rangeRate) * VLS);
139 VMC = math.sqrt(VOS * VOS - 4.0 * (rangeRate) * VLS);
140
141 # print("gunsite-computer.nas initialization done");
142 }
143
144 var getRangeRate = func()
145 {
146 # print("gunsite-computer.nas getRangeRate");
147 var oldRange = range;
148 range = getprop(propertyTreeRoot, "range");
149 rangeRateArray[rRAindex] = (oldRange - range) / timeStep;
150 rRAindex = rRAindex + 1;
151 if (rRAindex > 3)
152 {
153 rRAindex = 0;
154 }
155 var rangeRateSum = 0;
156 forindex(i; rangeRateArray)
157 {
158 rangeRateSum = rangeRateSum + rangeRateArray[i];
159 }
160 var rangeRateAve = rangeRateSum / 4;
161 setprop("/controls/armament/gunsight/rangeRate", rangeRateAve);
162 return -1.0 * rangeRateAve;
163 }
164
165 var getAccelerationData = func()
166 {
167 # print("gunsite-computer.nas getAccelerationData()");
168
169 P = getprop("/orientation/p-body");
170 Q = getprop("/orientation/q-body");
171 R = getprop("/orientation/r-body");
172 normalAcceleration = getprop("/accelerations/pilot/z-accel-fps_sec");
173 }
174
175 var getTrueAirSpeed = func()
176 {
177 # print("gunsite-computer.nas getTrueAirspeed()");
178
179 # from http://en.wikipedia.org/wiki/True_airspeed
180 # TASKnots = A0 * M * sqrt(T/T0)
181 # where A0 = speed of sound a standard sea level (661.47 Kts)
182 # M = MACH speed of aircraft
183 # T = static air temperature in Kelvin at aircraft position
184 # T0 = temperature a standard sea level (288.15 K)
185
186 var ambientTemperatureKelvin = getprop("/environment/temperature-degc") + 273.15;
187 var MACH = getprop("/velocities/mach");
188 var TASKnots = 661.47 * MACH * math.sqrt(ambientTemperatureKelvin / 288.15);
189
190 # print("true airspeed = ", TASKnots);
191 # return TAS in feet per second.
192 return TASKnots * 1.68781;
193 }
194
195 var getAzimuthAndElevation = func()
196 {
197 # print("gunsite-computer.nas getAzimuthAndElevation");
198
199 getAccelerationData();
200 # print("normalAcceleration = ", normalAcceleration);
201 rangeRate = getRangeRate();
202 range = getprop(propertyTreeRoot, "range");
203 var alpha = getprop("/orientation/alpha-deg") * radiansPerDrgree;
204
205 var trueAirSpeed = getTrueAirSpeed();
206
207 var totalInitialVelocity = trueAirSpeed + muzzleVelocity;
208 var SQRV = math.sqrt(trueAirSpeed + totalInitialVelocity);
209
210 var RHO = getprop("/environment/density-slugft3");
211
212 var SLA = LA * (1.0 - 0.16667 * LA * LA);
213 var SLE = LE * (1.0 - 0.16667 * LE * LE);
214 var CLE = 1.0 - (LE * LE)/2;
215 var CLA = 1.0 - (LA * LA)/2;
216 var RCOS = 1.0/(CLA * CLE);
217 var RDE = (-rangeRate - RDC * SLE) * RCOS;
218 var RE = range * RCOS;
219
220 # Approximation used for sqrt
221 # If y=approx. square root of x, then SQTR(x) = 0.5 * (y + x/y) is very close
222 SQRV = 0.5 * (SQRV + (trueAirSpeed + totalInitialVelocity)/SQRV);
223 VLS = KBRHO * RHO * RE * SQRV;
224 VOS = totalInitialVelocity - RDE - VLS;
225
226 # Approximation used for sqrt
227 # RDE = -rangeRate makes this calculation identical to other LCOSS
228 RDE = -rangeRate;
229 VCM = 0.5 * (VCM + (VOS * VOS - 4.0 * (trueAirSpeed + RDE) * VLS) / VCM);
230 var VE = 0.5 * (VOS + VCM);
231
232 var recipricalTimeOfFlight = VE / RE;
233 var averageRelativeVelocity = VE + RDE;
234
235 var rRange = 1.0 / range;
236 var KH = 1.0 - range * RRH;
237 var VN = averageRelativeVelocity - SIG * RDE * VLS * (averageRelativeVelocity + trueAirSpeed)/(VCM * averageRelativeVelocity);
238
239 var PG = P * cosGunElevation - R * sinGunElevation;
240 var RG = P * sinGunElevation + R * cosGunElevation;
241
242 RDC = trueAirSpeed * (alpha + gunElevationRadians) *
243 (totalInitialVelocity - averageRelativeVelocity) /
244 (trueAirSpeed + totalInitialVelocity) +
245 0.5 * normalAcceleration / recipricalTimeOfFlight;
246
247 # WJ and WK are computed sight line angular rates
248 var WK = (KH * y_offset * CLA * recipricalTimeOfFlight - VN * SLA) * rRange;
249 var WJ = ((RDC - KH * z_offset * recipricalTimeOfFlight) * CLE - VN * CLA * SLE) * rRange;
250 var LED = (WJ + SLA * PG - CLA * Q) * KSIG;
251 var LAD = ((WK - SLE * (CLA * PG + SLA * Q)) / CLE -RG) * KSIG;
252
253 LA = LA + LAD * timeStep;
254 LE = LE + LED * timeStep;
255 }
256
257 var gunSightMain = func()
258 {
259 # print("gunSightMain()");
260 if (getprop(propertyTreeRoot, "computer-on") == 1)
261 {
262 getAzimuthAndElevation();
263 # LA and LE are azimuth and elevation angles of the computed sight line
264 # LA and LE are in radians.
265
266 # Compute PLA and PLE as negative mills
267 # var PLA = -1000.0 * LA;
268 # var PLE = -1000.0 * LE;
269
270 # update reticle position
271 # Under extreme conditions such as a spin the algorithm
272 # can generate invalid results and return NaN for LA
273 # and LE which causes the whole thing to break down.
274 # If that happens set them to 0 (reinitialize LA and LE)
275 # and continue on.
276
277 # clamp LA and LE to max deflection values for the sight
278 var tempLA = LA;
279 var tempLE = LE;
280
281 if (LA > maxAzimuth/1000)
282 {
283 tempLA = maxAzimuth/1000;
284 }
285 else if (LA < -maxAzimuth/1000)
286 {
287 tempLA = -maxAzimuth/1000;
288 }
289 if (LE > maxElevation/1000)
290 {
291 tempLE = maxElevation/1000;
292 }
293 else if (LE < -maxElevation/1000)
294 {
295 tempLE = -maxElevation/1000;
296 }
297 call(func setprop(propertyTreeRoot, "azimuth", tempLA), nil, var LEerr = []);
298 if (size(LEerr))
299 {
300 tempLA = 0;
301 setprop(propertyTreeRoot, "azimuth", tempLA);
302 }
303
304 call (func setprop(propertyTreeRoot, "elevation", tempLE), nil, var LAerr = []);
305 if (size(LAerr))
306 {
307 tempLE = 0;
308 setprop(propertyTreeRoot, "elevation", tempLE)
309 }
310
311 # print("gunSightMain loop");
312 # print("LA (azimuth) = ", LA);
313 # print("LE (elevation)= ", LE);
314 # wait a while and do this again
315 settimer(gunSightMain, timeStep);
316 }
317 }
318
319 # listener for the the sight computer/gyro to be powered up or down.
320 # Will start gunSightMain process when the gun sight computer/gyro is powered on.
321 var listenGunSightGyroPower = func(i)
322 {
323 if (getprop(propertyTreeRoot, "computer-on") == 1)
324 {
325 initSightComputer();
326 setprop(propertyTreeRoot, "gunsightComputerInitialized", 1);
327 # print("gunsite-computer.nas power switched");
328 gunSightMain();
329 }
330 else
331 {
332 setprop(propertyTreeRoot, "gunsightComputerInitialized", 0);
333 }
334 }
335
336 setprop("/controls/armament/gunsight/gunsightComputerInitialized", 0);
337
338 var L1 = setlistener("/sim/signals/fdm-initialized", func(i)
339 {
340 var L = setlistener("/controls/armament/gunsight/computer-on", listenGunSightGyroPower, 1, 0);
341 }, 1, 0);
342
343