| 1 |
# Manages the engine |
| 2 |
# |
| 3 |
# Fuel system: based on the Spitfire. Manages primer and negGCutoff |
| 4 |
# Hobbs meter |
| 5 |
|
| 6 |
# =============================== DEFINITIONS =========================================== |
| 7 |
|
| 8 |
# set the update period |
| 9 |
|
| 10 |
var UPDATE_PERIOD = 0.3; |
| 11 |
|
| 12 |
# =============================== Hobbs meter ======================================= |
| 13 |
|
| 14 |
# this property is saved by aircraft.timer |
| 15 |
var hobbsmeter_engine_160hp = aircraft.timer.new("/sim/time/hobbs/engine[0]", 60, 1); |
| 16 |
var hobbsmeter_engine_180hp = aircraft.timer.new("/sim/time/hobbs/engine[1]", 60, 1); |
| 17 |
|
| 18 |
var init_hobbs_meter = func(index, meter) { |
| 19 |
setlistener("/engines/engine[" ~ index ~ "]/running", func { |
| 20 |
if (getprop("/engines/engine[" ~ index ~ "]/running")) { |
| 21 |
meter.start(); |
| 22 |
print("Hobbs system started"); |
| 23 |
} else { |
| 24 |
meter.stop(); |
| 25 |
print("Hobbs system stopped"); |
| 26 |
} |
| 27 |
}, 1, 0); |
| 28 |
}; |
| 29 |
|
| 30 |
init_hobbs_meter(0, hobbsmeter_engine_160hp); |
| 31 |
init_hobbs_meter(1, hobbsmeter_engine_180hp); |
| 32 |
|
| 33 |
var update_hobbs_meter = func { |
| 34 |
# in seconds |
| 35 |
var hobbs_160hp = getprop("/sim/time/hobbs/engine[0]") or 0.0; |
| 36 |
var hobbs_180hp = getprop("/sim/time/hobbs/engine[1]") or 0.0; |
| 37 |
# This uses minutes, for testing |
| 38 |
#hobbs = hobbs / 60.0; |
| 39 |
# in hours |
| 40 |
#hobbs = (hobbs_160hp + hobbs_180hp) / 3600.0; |
| 41 |
# tenths of hour |
| 42 |
|
| 43 |
var hobbs = 0; |
| 44 |
var current_engine = getprop("controls/engines/active-engine"); |
| 45 |
if (current_engine == 0) { |
| 46 |
hobbs = hobbs_160hp / 3600.0; |
| 47 |
} else |
| 48 |
if (current_engine == 1) { |
| 49 |
hobbs = hobbs_180hp / 3600.0; |
| 50 |
} |
| 51 |
|
| 52 |
setprop("/instrumentation/hobbs-meter/digits0", math.mod(int(hobbs * 10), 10)); |
| 53 |
# rest of digits |
| 54 |
setprop("/instrumentation/hobbs-meter/digits1", math.mod(int(hobbs), 10)); |
| 55 |
setprop("/instrumentation/hobbs-meter/digits2", math.mod(int(hobbs / 10), 10)); |
| 56 |
setprop("/instrumentation/hobbs-meter/digits3", math.mod(int(hobbs / 100), 10)); |
| 57 |
setprop("/instrumentation/hobbs-meter/digits4", math.mod(int(hobbs / 1000), 10)); |
| 58 |
}; |
| 59 |
|
| 60 |
setlistener("/sim/time/hobbs/engine[0]", update_hobbs_meter, 1, 0); |
| 61 |
setlistener("/sim/time/hobbs/engine[1]", update_hobbs_meter, 1, 0); |
| 62 |
setlistener("/controls/engines/active-engine", update_hobbs_meter, 1, 0); |
| 63 |
|
| 64 |
# ========== primer stuff ====================== |
| 65 |
|
| 66 |
# Toggles the state of the primer |
| 67 |
var pumpPrimer = func { |
| 68 |
var push = getprop("/controls/engines/engine/primer-lever") or 0; |
| 69 |
|
| 70 |
if (push) { |
| 71 |
var pump = getprop("/controls/engines/engine/primer") or 0; |
| 72 |
setprop("/controls/engines/engine/primer", pump + 1); |
| 73 |
setprop("/controls/engines/engine/primer-lever", 0); |
| 74 |
} |
| 75 |
else { |
| 76 |
setprop("/controls/engines/engine/primer-lever", 1); |
| 77 |
} |
| 78 |
}; |
| 79 |
|
| 80 |
# Primes the engine automatically. This function takes several seconds |
| 81 |
var autoPrime = func { |
| 82 |
var p = getprop("/controls/engines/engine/primer") or 0; |
| 83 |
if (p < 3) { |
| 84 |
pumpPrimer(); |
| 85 |
settimer(autoPrime, 1); |
| 86 |
} |
| 87 |
}; |
| 88 |
|
| 89 |
# Mixture will be calculated using the primer during 5 seconds AFTER the pilot used the starter |
| 90 |
# This prevents the engine to start just after releasing the starter: the propeller will be running |
| 91 |
# thanks to the electric starter, but carburator has not yet enough mixture |
| 92 |
var primerTimer = maketimer(5, func { |
| 93 |
setprop("/controls/engines/engine/use-primer", 0); |
| 94 |
# Reset the number of times the pilot used the primer only AFTER using the starter |
| 95 |
setprop("/controls/engines/engine/primer", 0); |
| 96 |
print("Primer reset to 0"); |
| 97 |
primerTimer.stop(); |
| 98 |
}); |
| 99 |
|
| 100 |
# ========== oil consumption ====================== |
| 101 |
# Thanks to HHS81 (Benedikt Hallinger) for more advanced simulation |
| 102 |
var service_hours = getprop("/engines/active-engine/oil-service-hours"); |
| 103 |
var consumption_qps = 0.0; |
| 104 |
var rpm_factor = 0.0; |
| 105 |
var rpm = 0; |
| 106 |
var oil_level = 0; |
| 107 |
var oil_full = 0; |
| 108 |
var oil_lacking = 0; |
| 109 |
var oil_level_limited = 0; |
| 110 |
var service_hours_increase_qph = 0; |
| 111 |
var service_hours_new = 0; |
| 112 |
var low_oil_pressure_factor = 0.0; |
| 113 |
var low_oil_temperature_factor = 0.0; |
| 114 |
|
| 115 |
# ======= OIL SYSTEM INIT ======= |
| 116 |
if (!getprop("/engines/active-engine/oil-service-hours")) { |
| 117 |
setprop("/engines/active-engine/oil-service-hours", 0); |
| 118 |
} |
| 119 |
|
| 120 |
var oil_consumption = maketimer(1.0, func { |
| 121 |
|
| 122 |
oil_level = getprop("/engines/active-engine/oil-level"); |
| 123 |
if (getprop("/controls/engines/active-engine") == 0) |
| 124 |
oil_full = 7; |
| 125 |
if (getprop("/controls/engines/active-engine") == 1) |
| 126 |
oil_full = 8; |
| 127 |
oil_lacking = oil_full - oil_level; |
| 128 |
setprop("/engines/active-engine/oil-lacking", oil_lacking); |
| 129 |
|
| 130 |
if (getprop("/engines/active-engine/oil_consumption_allowed")) { |
| 131 |
|
| 132 |
rpm = getprop("/engines/active-engine/rpm"); |
| 133 |
|
| 134 |
# Quadratic formula which outputs 1.0 for input 2300 RPM (cruise value), |
| 135 |
# 0.6 for 700 RPM (idle) and 1.2 for 2700 RPM (max) |
| 136 |
rpm_factor = 0.00000012 * math.pow(rpm, 2) - 0.0001 * rpm + 0.62; |
| 137 |
|
| 138 |
# Consumption rate defined as 0.33 quarts per 1 hour (3600 seconds) (Lycoming Manual 3-6 p27) |
| 139 |
# at 2350 RPM (normal cruise) |
| 140 |
consumption_qps = 0.33 / 3600; |
| 141 |
|
| 142 |
# Raise consumption when oil level is > 8 quarts (blowout) |
| 143 |
if (oil_level > oil_full) { |
| 144 |
consumption_qps = consumption_qps * 1.3; |
| 145 |
} |
| 146 |
|
| 147 |
# Consumption also raises with oil in service time (lower viscosity => more friction) |
| 148 |
# (Oil should be changed at 50 hrs!) |
| 149 |
# See: http://www.t-craft.org/Reference/Aircraft.Oil.Usage.pdf |
| 150 |
# Hours: 0 | 10 | 25 | 50 | 75 |
| 151 |
# Add Qts/hr: 0 | 0.02 | 0.125 | 0.5 | 1.125 |
| 152 |
service_hours = getprop("/engines/active-engine/oil-service-hours"); |
| 153 |
service_hours_increase_qph = 0.00020 * math.pow(service_hours, 2); |
| 154 |
service_hours_increase_qph = std.min(1.5, service_hours_increase_qph); # limit increase to 1.5 (at which point you really should think of changing it) |
| 155 |
service_hours_increase_qps = service_hours_increase_qph / 3600; |
| 156 |
consumption_qps = consumption_qps + service_hours_increase_qps; |
| 157 |
|
| 158 |
if (getprop("/engines/active-engine/running")) { |
| 159 |
oil_level = oil_level - consumption_qps * rpm_factor; |
| 160 |
setprop("/engines/active-engine/oil-level", oil_level); |
| 161 |
setprop("/engines/active-engine/oil-consume-qps", consumption_qps); |
| 162 |
setprop("/engines/active-engine/oil-consume-qph", consumption_qps * 3600); |
| 163 |
|
| 164 |
service_hours_new = (service_hours * 3600 + 1) / 3600; # add one second service time |
| 165 |
setprop("/engines/active-engine/oil-service-hours", service_hours_new); |
| 166 |
} else { |
| 167 |
setprop("/engines/active-engine/oil-consume-qps", 0); |
| 168 |
} |
| 169 |
|
| 170 |
low_oil_pressure_factor = 1.0; |
| 171 |
low_oil_temperature_factor = 1.0; |
| 172 |
|
| 173 |
# If oil gets low (< 3.0), pressure should drop and temperature should rise |
| 174 |
oil_level_limited = std.min(oil_level, 3.0); |
| 175 |
|
| 176 |
# Should give 1.0 for oil_level = 3 and 0.1 for oil_level 1.97, |
| 177 |
# which is the min before the engine stops |
| 178 |
low_oil_pressure_factor = 0.873786408 * oil_level_limited - 1.621359224; |
| 179 |
|
| 180 |
# Should give 1.0 for oil_level = 3 and 1.5 for oil_level 1.97 |
| 181 |
low_oil_temperature_factor = -0.485436893 * oil_level_limited + 2.456310679; |
| 182 |
|
| 183 |
setprop("/engines/active-engine/low-oil-pressure-factor", low_oil_pressure_factor); |
| 184 |
setprop("/engines/active-engine/low-oil-temperature-factor", low_oil_temperature_factor); |
| 185 |
} |
| 186 |
|
| 187 |
else { |
| 188 |
# if oil consumption is not allowed, the oil level is set to full and pressure and temp factors are set to 1.0 |
| 189 |
if (getprop("/controls/engines/active-engine") == 0) |
| 190 |
setprop("/engines/active-engine/oil-level", 7); |
| 191 |
if (getprop("/controls/engines/active-engine") == 1) |
| 192 |
setprop("/engines/active-engine/oil-level", 8); |
| 193 |
setprop("/engines/active-engine/low-oil-pressure-factor", 1.0); |
| 194 |
setprop("/engines/active-engine/low-oil-temperature-factor", 1.0); |
| 195 |
} |
| 196 |
}); |
| 197 |
|
| 198 |
# Oil Refilling |
| 199 |
var oil_refill = func(){ |
| 200 |
var previous_oil_level = getprop("/engines/active-engine/oil-level"); |
| 201 |
var service_hours = getprop("/engines/active-engine/oil-service-hours"); |
| 202 |
var oil_level = getprop("/engines/active-engine/oil-level"); |
| 203 |
var refilled = oil_level - previous_oil_level; |
| 204 |
#print("OIL Refill init: svcHrs=", service_hours, "; oil_level=",oil_level, "; previous_oil_level=",previous_oil_level, "; refilled=",refilled); |
| 205 |
|
| 206 |
if (refilled >= 0) { |
| 207 |
# when refill occured, the new oil "makes the old oil younger" |
| 208 |
var pct = 0; |
| 209 |
if (oil_level > 0) { |
| 210 |
pct = previous_oil_level / oil_level; |
| 211 |
} |
| 212 |
var newService_hours = service_hours * pct; |
| 213 |
setprop("/engines/active-engine/oil-service-hours", newService_hours); |
| 214 |
#print("OIL Refill: pct=", pct, "; service_hours=",service_hours, "; newService_hours=", newService_hours, "; previous_oil_level=", previous_oil_level, "; oil_level=",oil_level); |
| 215 |
} |
| 216 |
|
| 217 |
previous_oil_level = oil_level; |
| 218 |
} |
| 219 |
|
| 220 |
# ======= Oil temperature jsbsim compensator ======= |
| 221 |
# Currently, jsbsim oil temperature always initializes at 60°F. |
| 222 |
# We want an temperature that initialize to environment temperature until first start |
| 223 |
# and then gradually switch over to the real jsbsim value after some time. |
| 224 |
var calculate_real_oiltemp = maketimer(0.5, func { |
| 225 |
if (!getprop("/engines/active-engine/already-started-in-session")) { |
| 226 |
# engine is still cold |
| 227 |
var temp_env = getprop("/environment/temperature-degf") or 60; |
| 228 |
var temp_jsbsim_oil = getprop("/engines/active-engine/oil-temperature-degf") or 60; |
| 229 |
current_temp_diff = temp_jsbsim_oil - temp_env; |
| 230 |
setprop("/engines/active-engine/oil-temperature-env-diff", current_temp_diff); |
| 231 |
} else { |
| 232 |
# engine has been started at least one time: |
| 233 |
# gradually remove the difference as jsbsim adapts to real environment temperature |
| 234 |
calculate_real_oiltemp.stop(); |
| 235 |
interpolate("/engines/active-engine/oil-temperature-env-diff", 0, 180); # hand over to jsbsim caluclation gradually over 2 minutes |
| 236 |
} |
| 237 |
}); |
| 238 |
|
| 239 |
# ========== carburetor icing ====================== |
| 240 |
|
| 241 |
var carb_icing_function = maketimer(1.0, func { |
| 242 |
if (getprop("/engines/active-engine/carb_icing_allowed")) { |
| 243 |
var rpm = getprop("/engines/active-engine/rpm"); |
| 244 |
var dewpointC = getprop("/environment/dewpoint-degc"); |
| 245 |
var dewpointF = dewpointC * 9.0 / 5.0 + 32; |
| 246 |
var airtempF = getprop("/environment/temperature-degf"); |
| 247 |
var oil_temp = getprop("/engines/active-engine/oil-temperature-degf"); |
| 248 |
var egt_degf = getprop("/engines/active-engine/egt-degf"); |
| 249 |
var engine_running = getprop("/engines/active-engine/running"); |
| 250 |
var carb_ice = getprop("/engines/active-engine/carb_ice"); |
| 251 |
|
| 252 |
# the formula below attempts to model the graph found in the POH which relates air temperature, dew point and RPM to icing |
| 253 |
# conditions. The outputs of carb_icing_formula ranges from 0.65 to -0.35 (positive means ice is accumulating, negative |
| 254 |
# means that ice is melting) |
| 255 |
var factorX = 13.2 - 3.2 * math.atan2 ( ((rpm - 2000.0) * 0.008), 1); |
| 256 |
var factorY = 7.0 - 2.0 * math.atan2 ( ((rpm - 2000.0) * 0.008), 1); |
| 257 |
var carb_icing_formula = (math.exp( math.pow((0.6 * airtempF + 0.3 * dewpointF - 42.0),2) / (-2 * math.pow(factorX,2))) * math.exp( math.pow((0.3 * airtempF - 0.6 * dewpointF + 14.0),2) / (-2 * math.pow(factorY,2))) - 0.35) * engine_running; |
| 258 |
|
| 259 |
# the efficacy of carb heat depends on the EGT. With a typical EGT of ~1500, the carb_heat_rate will be around -1.5. |
| 260 |
# This value is an educated guess of the RL effect, and should melt ice regardless of the icing rate |
| 261 |
if (getprop("/controls/engines/current-engine/carb-heat")) |
| 262 |
var carb_heat_rate = -0.001 * egt_degf; |
| 263 |
else |
| 264 |
var carb_heat_rate = 0.0; |
| 265 |
|
| 266 |
# a warm engine will accumulate less ice than a cold one, which is what oil temp factor is used for. oil_temp_factor |
| 267 |
# ranges from 0 to aprox -0.2 (at 250 oF). These values are educated guesses of the RL effect |
| 268 |
var oil_temp_factor = oil_temp / -1250; |
| 269 |
|
| 270 |
# the final rate of icing or melting is then calculated by all these effects together |
| 271 |
var carb_icing_rate = carb_icing_formula + carb_heat_rate + oil_temp_factor; |
| 272 |
|
| 273 |
# since the carb_icing_rate gives an arbitrary final value, the rate is then scaled down by 0.00001 to ensure ice |
| 274 |
# accumulates as slowly as expected |
| 275 |
carb_ice = carb_ice + carb_icing_rate * 0.00001; |
| 276 |
carb_ice = std.max(0.0, std.min(carb_ice, 1.0)); |
| 277 |
|
| 278 |
# this property is used to lower the RPM of the engine as ice accumulates (more ice in the carburator == less power) |
| 279 |
var vol_eff_factor = std.max(0.0, 0.85 - 1.72 * carb_ice); |
| 280 |
|
| 281 |
setprop("/engines/active-engine/carb_ice", carb_ice); |
| 282 |
setprop("/engines/active-engine/carb_icing_rate", carb_icing_rate); |
| 283 |
setprop("/engines/active-engine/volumetric-efficiency-factor", vol_eff_factor); |
| 284 |
setprop("/engines/active-engine/oil_temp_factor", oil_temp_factor); |
| 285 |
|
| 286 |
} |
| 287 |
else { |
| 288 |
setprop("/engines/active-engine/carb_ice", 0.0); |
| 289 |
setprop("/engines/active-engine/carb_icing_rate", 0.0); |
| 290 |
setprop("/engines/active-engine/volumetric-efficiency-factor", 0.85); |
| 291 |
setprop("/engines/active-engine/oil_temp_factor", 0.0); |
| 292 |
}; |
| 293 |
}); |
| 294 |
|
| 295 |
# ========== engine coughing ====================== |
| 296 |
|
| 297 |
var engine_coughing = func(){ |
| 298 |
|
| 299 |
var coughing = getprop("/engines/active-engine/coughing"); |
| 300 |
var running = getprop("/engines/active-engine/running"); |
| 301 |
|
| 302 |
if (coughing and running) { |
| 303 |
# the code below kills the engine and then brings it back to life after 0.25 seconds, simulating a cough |
| 304 |
setprop("/engines/active-engine/kill-engine", 1); |
| 305 |
settimer(func { |
| 306 |
setprop("/engines/active-engine/kill-engine", 0); |
| 307 |
}, 0.25); |
| 308 |
}; |
| 309 |
|
| 310 |
# basic value for the delay (interval between consecutive coughs), in case no fuel contamination nor carb ice are present |
| 311 |
var delay = 2; |
| 312 |
|
| 313 |
# if coughing due to fuel contamination, then cough interval depends on quantity of water |
| 314 |
var water_contamination0 = getprop("/consumables/fuel/tank[0]/water-contamination"); |
| 315 |
var water_contamination1 = getprop("/consumables/fuel/tank[1]/water-contamination"); |
| 316 |
var total_water_contamination = std.min((water_contamination0 + water_contamination1), 0.4); |
| 317 |
if (total_water_contamination > 0) { |
| 318 |
# if contamination is near 0, then interval is between 17 and 20 seconds, but if contamination is near the |
| 319 |
# engine stopping value of 0.4, then interval falls to around 0.5 and 3.5 seconds |
| 320 |
delay = 3.0 * rand() + 17 - 41.25 * total_water_contamination; |
| 321 |
}; |
| 322 |
|
| 323 |
# if coughing due to carb ice melting, then cough depends on quantity of ice in the carburettor |
| 324 |
var carb_ice = getprop("/engines/active-engine/carb_ice"); |
| 325 |
if (carb_ice > 0) { |
| 326 |
# if carb_ice is near 0, then interval is between 17 and 20 seconds, but if carb_ice is near the |
| 327 |
# engine stopping value of 0.3, then interval falls to around 0.5 and 3.5 seconds |
| 328 |
delay = 3.0 * rand() + 17 - 41.25 * carb_ice; |
| 329 |
}; |
| 330 |
|
| 331 |
coughing_timer.restart(delay); |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
var coughing_timer = maketimer(1, engine_coughing); |
| 336 |
|
| 337 |
# ====== Engine starting actions ====== |
| 338 |
var engine_starting = props.globals.initNode("/engines/engine/starting", 0, "BOOL"); |
| 339 |
setlistener("/engines/engine/running", func(ngn){ |
| 340 |
if (ngn.getValue() and !getprop("/engines/engine[0]/coughing")) { |
| 341 |
engine_starting.setValue(1); |
| 342 |
var timer = maketimer(1, func(){ |
| 343 |
engine_starting.setValue(0); |
| 344 |
}); |
| 345 |
timer.singleShot = 1; # timer will only be run once |
| 346 |
timer.start(); |
| 347 |
} else { |
| 348 |
engine_starting.setValue(0); |
| 349 |
} |
| 350 |
},0,0); |
| 351 |
|
| 352 |
setlistener("/engines/engine/starting", func(ngn){ |
| 353 |
# Eye-candy: when engine starts, let the view shake a bit |
| 354 |
if (ngn.getValue() and getprop("/sim/current-view/internal")) { |
| 355 |
var curX = getprop("/sim/current-view/x-offset-m"); |
| 356 |
var xtimer = maketimer(0.05, func(){ |
| 357 |
interpolate("/sim/current-view/x-offset-m", curX-0.0015+rand()*0.003, 0.05); |
| 358 |
}); |
| 359 |
xtimer.start(); |
| 360 |
var curY = getprop("/sim/current-view/y-offset-m"); |
| 361 |
var ytimer = maketimer(0.05, func(){ |
| 362 |
interpolate("/sim/current-view/y-offset-m", curY-0.0015+rand()*0.003, 0.05); |
| 363 |
}); |
| 364 |
ytimer.start(); |
| 365 |
var stoptimer = maketimer(0.8, func(){ |
| 366 |
xtimer.stop(); |
| 367 |
ytimer.stop(); |
| 368 |
interpolate("/sim/current-view/x-offset-m", curX, 0.1); |
| 369 |
interpolate("/sim/current-view/y-offset-m", curY, 0.1); |
| 370 |
}); |
| 371 |
stoptimer.singleShot = 1; |
| 372 |
stoptimer.start(); |
| 373 |
} |
| 374 |
}, 0, 0); |
| 375 |
|
| 376 |
# ========== Main loop ====================== |
| 377 |
|
| 378 |
var update = func { |
| 379 |
#this block should be moved out of nasal and into jsbsim or autopilot logic |
| 380 |
var leftTankUsable = 0; |
| 381 |
var rightTankUsable = 0; |
| 382 |
if (getprop("/consumables/fuel/tank[0]/selected") and getprop("/consumables/fuel/tank[0]/level-gal_us") > 0) leftTankUsable = 1; |
| 383 |
if (getprop("/consumables/fuel/tank[1]/selected") and getprop("/consumables/fuel/tank[1]/level-gal_us") > 0) rightTankUsable = 1; |
| 384 |
if (getprop("/consumables/fuel/tank[2]/selected") and getprop("/consumables/fuel/tank[2]/level-gal_us") > 0) leftTankUsable = 1; |
| 385 |
if (getprop("/consumables/fuel/tank[3]/selected") and getprop("/consumables/fuel/tank[3]/level-gal_us") > 0) rightTankUsable = 1; |
| 386 |
var outOfFuel = !(leftTankUsable or rightTankUsable); |
| 387 |
|
| 388 |
# We use the mixture to control the engines, so set the mixture |
| 389 |
var usePrimer = getprop("/controls/engines/engine/use-primer") or 0; |
| 390 |
if(getprop("/controls/panel/glass")) usePrimer = 0; |
| 391 |
|
| 392 |
var engine_running = getprop("/engines/active-engine/running"); |
| 393 |
|
| 394 |
if (outOfFuel and (engine_running or usePrimer)) { |
| 395 |
print("Out of fuel!"); |
| 396 |
gui.popupTip("Out of fuel!"); |
| 397 |
} |
| 398 |
elsif (usePrimer and !engine_running and getprop("/engines/active-engine/oil-temperature-degf") <= 75) { |
| 399 |
# Mixture is controlled by start conditions |
| 400 |
var primer = getprop("/controls/engines/engine/primer"); |
| 401 |
if (!getprop("/fdm/jsbsim/fcs/mixture-primer-cmd") and getprop("/controls/switches/starter") and getprop("/controls/switches/master-bat")) { |
| 402 |
if (primer < 3) { |
| 403 |
print("Use the primer!"); |
| 404 |
gui.popupTip("Use the primer!"); |
| 405 |
} |
| 406 |
elsif (primer > 6) { |
| 407 |
print("Flooded engine!"); |
| 408 |
gui.popupTip("Flooded engine!"); |
| 409 |
} |
| 410 |
else { |
| 411 |
print("Check the throttle!"); |
| 412 |
gui.popupTip("Check the throttle!"); |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
var active_engine = getprop("/controls/engines/active-engine"); |
| 418 |
var rpm = getprop("/engines/engine", active_engine, "rpm"); |
| 419 |
|
| 420 |
# sorry - had to hack this to prevent coughing on startup due to the oil pressure simulation. Maybe this can be used elsewhere |
| 421 |
if (rpm < 900 and getprop("/controls/switches/starter") == 1) { # make sure it is not triggered if you accidentally hit s in the air |
| 422 |
setprop("/engines/active-engine/ready-oil-press-checker", 1); # 0 = off, 1 = checker is armed, 2 = engine is running and ready |
| 423 |
} |
| 424 |
|
| 425 |
if (getprop("/engines/active-engine/ready-oil-press-checker") == 1 and getprop("/engines/active-engine/rpm") > 900) { |
| 426 |
setprop("/engines/active-engine/ready-oil-press-checker", 2); # engine is ready for use |
| 427 |
} |
| 428 |
}; |
| 429 |
|
| 430 |
setlistener("/controls/switches/starter", func { |
| 431 |
var v = getprop("/controls/switches/starter") or 0; |
| 432 |
if (v == 0) { |
| 433 |
print("Starter off"); |
| 434 |
# notice the starter will be reset after 5 seconds |
| 435 |
primerTimer.restart(5); |
| 436 |
} |
| 437 |
else { |
| 438 |
print("Starter on"); |
| 439 |
if(getprop("/controls/panel/glass")) |
| 440 |
setprop("/controls/engines/engine/use-primer", 0); |
| 441 |
else |
| 442 |
setprop("/controls/engines/engine/use-primer", 1); |
| 443 |
if (primerTimer.isRunning) { |
| 444 |
primerTimer.stop(); |
| 445 |
} |
| 446 |
} |
| 447 |
}, 1, 0); |
| 448 |
|
| 449 |
# ================================ Initalize ====================================== |
| 450 |
# Make sure all needed properties are present and accounted |
| 451 |
# for, and that they have sane default values. |
| 452 |
|
| 453 |
setprop("/engines/active-engine/rpm", 0); |
| 454 |
setprop("/engines/active-engine/ready-oil-press-checker", 0); |
| 455 |
|
| 456 |
# =============== Variables ================ |
| 457 |
|
| 458 |
# 2018.2 introduces new "all" properties for throttle, mixture and prop pitch. |
| 459 |
# this is the correct way to interface with the axis based controls - use a listener |
| 460 |
# on the *-all property |
| 461 |
setlistener("/controls/engines/throttle-all", func{ |
| 462 |
var value = (1 - getprop("/controls/engines/throttle-all")) / 2; |
| 463 |
var new_value = std.max(0.0, std.min(value, 1.0)); |
| 464 |
setprop("/controls/engines/current-engine/throttle", new_value); |
| 465 |
}, 0, 0); |
| 466 |
|
| 467 |
setlistener("/controls/engines/mixture-all", func{ |
| 468 |
var value = (1 - getprop("/controls/engines/mixture-all")) / 2; |
| 469 |
var new_value = std.max(0.0, std.min(value, 1.0)); |
| 470 |
setprop("/controls/engines/current-engine/mixture", new_value); |
| 471 |
}, 0, 0); |
| 472 |
|
| 473 |
controls.adjMixture = func { |
| 474 |
var delta = arg[0] * controls.THROTTLE_RATE * getprop("/sim/time/delta-realtime-sec"); |
| 475 |
var old_value = getprop("/controls/engines/current-engine/mixture"); |
| 476 |
var new_value = std.max(0.0, std.min(old_value + delta, 1.0)); |
| 477 |
setprop("/controls/engines/current-engine/mixture", new_value); |
| 478 |
}; |
| 479 |
|
| 480 |
# backwards compatibility only - the controls.throttleAxis should not be overridden like this. The joystick binding Throttle (all) has |
| 481 |
# been replaced and controls.throttleAxis will not be called from the controls binding - so this is to |
| 482 |
# maintain compatibility with existing joystick xml files. |
| 483 |
controls.mixtureAxis = func { |
| 484 |
var value = (1 - cmdarg().getNode("setting").getValue()) / 2; |
| 485 |
var new_value = std.max(0.0, std.min(value, 1.0)); |
| 486 |
setprop("/controls/engines/current-engine/mixture", new_value); |
| 487 |
}; |
| 488 |
|
| 489 |
var _axisMode = { |
| 490 |
0: controls.perIndexAxisHandler("/controls/engines/current-engine[", |
| 491 |
"]/throttle"), |
| 492 |
1: controls.perIndexAxisHandler("/controls/engines/current-engine[", |
| 493 |
"]/mixture"), |
| 494 |
2: controls.perIndexAxisHandler("/controls/engines/current-engine[", |
| 495 |
"]/propeller-pitch") |
| 496 |
}; |
| 497 |
controls.perEngineSelectedAxisHandler = func(mode) { |
| 498 |
return _axisMode[mode]; |
| 499 |
}; |
| 500 |
|
| 501 |
controls.stepMagnetos = func { |
| 502 |
var old_value = getprop("/controls/switches/magnetos"); |
| 503 |
var new_value = std.max(0, std.min(old_value + arg[0], 3)); |
| 504 |
setprop("/controls/switches/magnetos", new_value); |
| 505 |
}; |
| 506 |
|
| 507 |
# key 's' calls to this function when it is pressed DOWN even if I overwrite the binding in the -set.xml file! |
| 508 |
# fun fact: the key UP event can be overwriten! |
| 509 |
controls.startEngine = func(v = 1) { |
| 510 |
# Only operate in non-walker mode ('s' is also bound to walk-backward) |
| 511 |
var view_name = getprop("/sim/current-view/name"); |
| 512 |
if (view_name == getprop("/sim/view[110]/name") or view_name == getprop("/sim/view[111]/name")) { |
| 513 |
return; |
| 514 |
} |
| 515 |
if (getprop("/engines/active-engine/running")) |
| 516 |
{ |
| 517 |
setprop("/controls/switches/starter", 0); |
| 518 |
return; |
| 519 |
} |
| 520 |
else { |
| 521 |
setprop("/controls/switches/magnetos", 3); |
| 522 |
setprop("/controls/switches/starter", v); |
| 523 |
} |
| 524 |
}; |
| 525 |
|
| 526 |
var engine_timer = maketimer(UPDATE_PERIOD, func { update(); }); |
| 527 |
coughing_timer.singleShot = 1; |
| 528 |
oil_consumption.simulatedTime = 1; |
| 529 |
|
| 530 |
setlistener("/sim/signals/fdm-initialized", func { |
| 531 |
engine_timer.start(); |
| 532 |
carb_icing_function.start(); |
| 533 |
coughing_timer.start(); |
| 534 |
if (oil_consumption.isRunning) { |
| 535 |
oil_consumption.stop(); |
| 536 |
} |
| 537 |
oil_consumption.start(); |
| 538 |
calculate_real_oiltemp.start(); |
| 539 |
}); |