From 18720ea36b10ee89e9c5ea37f3da02aa7a12943b Mon Sep 17 00:00:00 2001 From: Joshua Davidson Date: Thu, 14 Mar 2019 00:08:48 -0400 Subject: [PATCH 1/7] System: New autopilot controller based on IT-AUTOFLIGHT V4 -- needs more testing and possibly fixing --- Models/FlightDeck/a320.flightdeck.xml | 2 +- Nasal/FMGC-b.nas | 2259 +++++++++++-------------- Nasal/FMGC-c.nas | 62 +- Nasal/FMGC.nas | 5 +- Nasal/buttons.nas | 16 +- Nasal/libraries.nas | 6 +- 6 files changed, 977 insertions(+), 1373 deletions(-) diff --git a/Models/FlightDeck/a320.flightdeck.xml b/Models/FlightDeck/a320.flightdeck.xml index 803d5886..f0a557ce 100644 --- a/Models/FlightDeck/a320.flightdeck.xml +++ b/Models/FlightDeck/a320.flightdeck.xml @@ -3226,7 +3226,7 @@ 1 - it-autoflight/input/lat-arm + it-autoflight/output/lnav-armed 1 diff --git a/Nasal/FMGC-b.nas b/Nasal/FMGC-b.nas index d3df4a40..e3168011 100644 --- a/Nasal/FMGC-b.nas +++ b/Nasal/FMGC-b.nas @@ -1,1331 +1,1008 @@ -# A3XX FMGC/Autoflight -# Joshua Davidson (it0uchpods) and Jonathan Redpath (legoboyvdlp) - +# A3XX FMGC Autopilot +# Based off IT-AUTOFLIGHT System Controller V4.0.X # Copyright (c) 2019 Joshua Davidson (it0uchpods) -################################# -# IT-AUTOFLIGHT Based Autopilot # -################################# +# Initialize all used variables and property nodes +# Sim +var Velocity = { + airspeedKt: props.globals.getNode("/velocities/airspeed-kt", 1), # Only used for gain scheduling + groundspeedKt: props.globals.getNode("/velocities/groundspeed-kt", 1), + groundspeedMps: 0, + indicatedAirspeedKt: props.globals.getNode("/instrumentation/airspeed-indicator/indicated-speed-kt", 1), + indicatedMach: props.globals.getNode("/instrumentation/airspeed-indicator/indicated-mach", 1), + trueAirspeedKt: props.globals.getNode("/instrumentation/airspeed-indicator/true-speed-kt", 1), + trueAirspeedKtTemp: 0, +}; -var trueSpeedKts = 0; -var locdefl = 0; -var signal = 0; -var gear1 = 0; -var gear2 = 0; -var gnds_mps = 0; -var current_course = 0; -var wp_fly_from = 0; -var wp_fly_to = 0; -var next_course = 0; -var max_bank_limit = 0; -var delta_angle = 0; -var max_bank = 0; -var radius = 0; -var time = 0; -var delta_angle_rad = 0; -var R = 0; -var dist_coeff = 0; -var turn_dist = 0; -var vsnow = 0; -var fpanow = 0; -var altinput = 0; -setprop("/it-autoflight/internal/heading-deg", getprop("/orientation/heading-magnetic-deg")); -setprop("/it-autoflight/internal/track-deg", getprop("/orientation/track-magnetic-deg")); -setprop("/it-autoflight/internal/vert-speed-fpm", 0); -setprop("/it-autoflight/internal/heading-error-deg", 0); -setprop("/it-autoflight/internal/heading-predicted", 0); -setprop("/it-autoflight/internal/altitude-predicted", 0); -setprop("/it-autoflight/internal/lnav-advance-nm", 1); +var Position = { + gearAglFtTemp: 0, + gearAglFt: props.globals.getNode("/position/gear-agl-ft", 1), + indicatedAltitudeFt: props.globals.getNode("/instrumentation/altimeter/indicated-altitude-ft", 1), + indicatedAltitudeFtTemp: 0, +}; -var APinit = func { - setprop("/instrumentation/efis[0]/trk-selected", 0); - setprop("/instrumentation/efis[1]/trk-selected", 0); - setprop("/it-autoflight/custom/trk-fpa", 0); - setprop("/it-autoflight/input/kts-mach", 0); - setprop("/it-autoflight/input/ap1", 0); - setprop("/it-autoflight/input/ap2", 0); - setprop("/it-autoflight/input/athr", 0); - setprop("/it-autoflight/input/fd1", 0); - setprop("/it-autoflight/input/fd2", 0); - setprop("/it-autoflight/input/hdg", 360); - setprop("/it-autoflight/input/alt", 10000); - setprop("/it-autoflight/input/vs", 0); - setprop("/it-autoflight/input/fpa", 0); - setprop("/it-autoflight/input/lat", 9); - setprop("/it-autoflight/input/lat-arm", 0); - setprop("/it-autoflight/input/vert", 9); - setprop("/it-autoflight/input/trk", 0); - setprop("/it-autoflight/input/toga", 0); - setprop("/it-autoflight/input/spd-managed", 0); - setprop("/it-autoflight/output/ap1", 0); - setprop("/it-autoflight/output/ap2", 0); - setprop("/it-autoflight/output/athr", 0); - setprop("/it-autoflight/output/fd1", 0); - setprop("/it-autoflight/output/fd2", 0); - setprop("/it-autoflight/output/loc-armed", 0); - setprop("/it-autoflight/output/appr-armed", 0); - setprop("/it-autoflight/output/thr-mode", 2); - setprop("/it-autoflight/output/lat", 9); - setprop("/it-autoflight/output/vert", 9); - setprop("/it-autoflight/output/vert-mng", 4); - setprop("/it-autoflight/output/fma-pwr", 0); - setprop("/it-autoflight/settings/use-backcourse", 0); - setprop("/it-autoflight/internal/min-vs", -500); - setprop("/it-autoflight/internal/max-vs", 500); - setprop("/it-autoflight/internal/alt", 10000); - setprop("/it-autoflight/internal/alt", 10000); - setprop("/it-autoflight/internal/fpa", 0); - setprop("/it-autoflight/internal/top-of-des-nm", 0); - setprop("/it-autoflight/internal/alt-const", 10000); - setprop("/it-autoflight/internal/mng-alt", 10000); - setprop("/it-autoflight/internal/prof-mode", "XX"); - setprop("/it-autoflight/internal/prof-fpm", 0); - setprop("/it-autoflight/internal/top-of-des-nm", 0); - setprop("/it-autoflight/mode/thr", "THRUST"); - setprop("/it-autoflight/mode/arm", " "); - setprop("/it-autoflight/mode/lat", " "); - setprop("/it-autoflight/mode/vert", " "); - setprop("/it-autoflight/input/spd-kts", 100); - setprop("/it-autoflight/input/spd-mach", 0.50); - setprop("/it-autoflight/custom/show-hdg", 1); - trkfpa_off(); - ap_varioust.start(); - thrustmode(); -} +var Gear = { + wow0: props.globals.getNode("/gear/gear[0]/wow", 1), + wow1: props.globals.getNode("/gear/gear[1]/wow", 1), + wow1Temp: 1, + wow2: props.globals.getNode("/gear/gear[2]/wow", 1), + wow2Temp: 1, +}; + +var Control = { + aileron: props.globals.getNode("/controls/flight/aileron", 1), + elevator: props.globals.getNode("/controls/flight/elevator", 1), + rudder: props.globals.getNode("/controls/flight/rudder", 1), +}; + +var Radio = { + gsDefl: [props.globals.getNode("/instrumentation/nav[0]/gs-needle-deflection-norm", 1), props.globals.getNode("/instrumentation/nav[1]/gs-needle-deflection-norm", 1)], + gsDeflTemp: 0, + inRange: [props.globals.getNode("/instrumentation/nav[0]/in-range", 1), props.globals.getNode("/instrumentation/nav[1]/in-range", 1)], + inRangeTemp: 0, + locDefl: [props.globals.getNode("/instrumentation/nav[0]/heading-needle-deflection-norm", 1), props.globals.getNode("/instrumentation/nav[1]/heading-needle-deflection-norm", 1)], + locDeflTemp: 0, + radioSel: 0, + signalQuality: [props.globals.getNode("/instrumentation/nav[0]/signal-quality-norm", 1), props.globals.getNode("/instrumentation/nav[1]/signal-quality-norm", 1)], + signalQualityTemp: 0, +}; + +var FPLN = { + active: props.globals.getNode("/autopilot/route-manager/active", 1), + activeTemp: 0, + currentCourse: 0, + currentWP: props.globals.getNode("/autopilot/route-manager/current-wp", 1), + currentWPTemp: 0, + deltaAngle: 0, + deltaAngleRad: 0, + distCoeff: 0, + maxBank: 0, + maxBankLimit: 0, + nextCourse: 0, + num: props.globals.getNode("/autopilot/route-manager/route/num", 1), + numTemp: 0, + R: 0, + radius: 0, + turnDist: 0, + wp0Dist: props.globals.getNode("/autopilot/route-manager/wp/dist", 1), + wpFlyFrom: 0, + wpFlyTo: 0, +}; + +var Misc = { + acEss: props.globals.getNode("/systems/electrical/bus/ac-ess", 1), + elapsedSec: props.globals.getNode("/sim/time/elapsed-sec", 1), + fbwLaw: props.globals.getNode("/it-fbw/law", 1), + flapNorm: props.globals.getNode("/surface-positions/flap-pos-norm", 1), + pfdHeadingScale: props.globals.getNode("/instrumentation/pfd/heading-scale", 1), +}; + +# IT-AUTOFLIGHT +var Input = { + alt: props.globals.initNode("/it-autoflight/input/alt", 10000, "INT"), + ap1: props.globals.initNode("/it-autoflight/input/ap1", 0, "BOOL"), + ap2: props.globals.initNode("/it-autoflight/input/ap2", 0, "BOOL"), + athr: props.globals.initNode("/it-autoflight/input/athr", 0, "BOOL"), + altDiff: 0, + bankLimitSW: props.globals.initNode("/it-autoflight/input/bank-limit-sw", 0, "INT"), + bankLimitSWTemp: 0, + fd1: props.globals.initNode("/it-autoflight/input/fd1", 0, "BOOL"), + fd2: props.globals.initNode("/it-autoflight/input/fd2", 0, "BOOL"), + fpa: props.globals.initNode("/it-autoflight/input/fpa", 0, "DOUBLE"), + hdg: props.globals.initNode("/it-autoflight/input/hdg", 0, "INT"), + ias: props.globals.initNode("/it-autoflight/input/spd-kts", 250, "INT"), + ktsMach: props.globals.initNode("/it-autoflight/input/kts-mach", 0, "BOOL"), + lat: props.globals.initNode("/it-autoflight/input/lat", 5, "INT"), + latTemp: 5, + mach: props.globals.initNode("/it-autoflight/input/spd-mach", 0.5, "DOUBLE"), + toga: props.globals.initNode("/it-autoflight/input/toga", 0, "BOOL"), + trk: props.globals.initNode("/it-autoflight/input/trk", 0, "BOOL"), + trueCourse: props.globals.initNode("/it-autoflight/input/true-course", 0, "BOOL"), + vs: props.globals.initNode("/it-autoflight/input/vs", 0, "INT"), + vert: props.globals.initNode("/it-autoflight/input/vert", 7, "INT"), + vertTemp: 7, +}; + +var Internal = { + alt: props.globals.initNode("/it-autoflight/internal/alt", 10000, "INT"), + altCaptureActive: 0, + altDiff: 0, + altTemp: 0, + altPredicted: props.globals.initNode("/it-autoflight/internal/altitude-predicted", 0, "DOUBLE"), + bankLimit: props.globals.initNode("/it-autoflight/internal/bank-limit", 30, "INT"), + bankLimitAuto: 30, + captVS: 0, + flchActive: 0, + fpa: props.globals.initNode("/it-autoflight/internal/fpa", 0, "DOUBLE"), + hdg: props.globals.initNode("/it-autoflight/internal/heading-deg", 0, "DOUBLE"), + hdgErrorDeg: props.globals.initNode("/it-autoflight/internal/heading-error-deg", 0, "DOUBLE"), + hdgPredicted: props.globals.initNode("/it-autoflight/internal/heading-predicted", 0, "DOUBLE"), + lnavAdvanceNm: props.globals.initNode("/it-autoflight/internal/lnav-advance-nm", 0, "DOUBLE"), + minVS: props.globals.initNode("/it-autoflight/internal/min-vs", -500, "INT"), + maxVS: props.globals.initNode("/it-autoflight/internal/max-vs", 500, "INT"), + trk: props.globals.initNode("/it-autoflight/internal/track-deg", 0, "DOUBLE"), + vs: props.globals.initNode("/it-autoflight/internal/vert-speed-fpm", 0, "DOUBLE"), + vsTemp: 0, +}; + +var Output = { + ap1: props.globals.initNode("/it-autoflight/output/ap1", 0, "BOOL"), + ap1Temp: 0, + ap2: props.globals.initNode("/it-autoflight/output/ap2", 0, "BOOL"), + ap2Temp: 0, + apprArm: props.globals.initNode("/it-autoflight/output/appr-armed", 0, "BOOL"), + athr: props.globals.initNode("/it-autoflight/output/athr", 0, "BOOL"), + athrTemp: 0, + fd1: props.globals.initNode("/it-autoflight/output/fd1", 0, "BOOL"), + fd1Temp: 0, + fd2: props.globals.initNode("/it-autoflight/output/fd2", 0, "BOOL"), + fd2Temp: 0, + lat: props.globals.initNode("/it-autoflight/output/lat", 5, "INT"), + latTemp: 5, + lnavArm: props.globals.initNode("/it-autoflight/output/lnav-armed", 0, "BOOL"), + locArm: props.globals.initNode("/it-autoflight/output/loc-armed", 0, "BOOL"), + thrMode: props.globals.initNode("/it-autoflight/output/thr-mode", 2, "INT"), + vert: props.globals.initNode("/it-autoflight/output/vert", 7, "INT"), + vertTemp: 7, +}; + +var Text = { + arm: props.globals.initNode("/it-autoflight/mode/arm", " ", "STRING"), + lat: props.globals.initNode("/it-autoflight/mode/lat", "T/O", "STRING"), + thr: props.globals.initNode("/it-autoflight/mode/thr", "PITCH", "STRING"), + vert: props.globals.initNode("/it-autoflight/mode/vert", "T/O CLB", "STRING"), + vertTemp: 0, +}; + +var Setting = { + reducAglFt: props.globals.initNode("/it-autoflight/settings/reduc-agl-ft", 3000, "INT"), # Changable from MCDU +}; + +var Sound = { + apOff: props.globals.initNode("/it-autoflight/sound/apoffsound", 0, "BOOL"), + enableApOff: 0, +}; + +# A3XX Custom +var Custom = { + apFdOn: 0, + hdgTime: props.globals.getNode("/modes/fcu/hdg-time", 1), + ndTrkSel: [props.globals.getNode("/instrumentation/efis[0]/trk-selected", 1), props.globals.getNode("/instrumentation/efis[1]/trk-selected", 1)], + showHdg: props.globals.initNode("/it-autoflight/custom/show-hdg", 1, "BOOL"), + trkFpa: props.globals.initNode("/it-autoflight/custom/trk-fpa", 0, "BOOL"), + Input: { + spdManaged: props.globals.getNode("/it-autoflight/input/spd-managed", 1), + }, + Output: { + fmaPower: props.globals.initNode("/it-autoflight/output/fma-pwr", 0, "BOOL"), + }, + Sound: { + athrOff: props.globals.initNode("/it-autoflight/sound/athrsound", 0, "BOOL"), + enableAthrOff: 0, + }, +}; + +var ITAF = { + init: func() { + Custom.ndTrkSel[0].setBoolValue(0); + Custom.ndTrkSel[1].setBoolValue(0); + Custom.trkFpa.setBoolValue(0); + Input.ktsMach.setBoolValue(0); + Input.ap1.setBoolValue(0); + Input.ap2.setBoolValue(0); + Input.athr.setBoolValue(0); + Input.fd1.setBoolValue(0); + Input.fd2.setBoolValue(0); + Input.hdg.setValue(360); + Input.alt.setValue(10000); + Input.vs.setValue(0); + Input.fpa.setValue(0); + Input.lat.setValue(9); + Input.vert.setValue(9); + Input.trk.setBoolValue(0); + Input.trueCourse.setBoolValue(0); + Input.toga.setBoolValue(0); + Custom.Input.spdManaged.setBoolValue(0); + Output.ap1.setBoolValue(0); + Output.ap2.setBoolValue(0); + Output.athr.setBoolValue(0); + Output.fd1.setBoolValue(0); + Output.fd2.setBoolValue(0); + Output.lnavArm.setBoolValue(0); + Output.locArm.setBoolValue(0); + Output.apprArm.setBoolValue(0); + Output.thrMode.setValue(0); + Output.lat.setValue(9); + Output.vert.setValue(9); + Internal.minVS.setValue(-500); + Internal.maxVS.setValue(500); + Internal.bankLimit.setValue(30); + Internal.bankLimitAuto = 30; + Internal.alt.setValue(10000); + Internal.altCaptureActive = 0; + Input.ias.setValue(100); + Input.mach.setValue(0.5); + Text.thr.setValue("THRUST"); + Text.arm.setValue(" "); + Text.lat.setValue(" "); + Text.vert.setValue(" "); + Custom.showHdg.setBoolValue(1); + Custom.Output.fmaPower.setBoolValue(1); + ManagedSPD.stop(); + loopTimer.start(); + slowLoopTimer.start(); + }, + loop: func() { + Output.latTemp = Output.lat.getValue(); + Output.vertTemp = Output.vert.getValue(); + + # VOR/ILS Revision + if (Output.latTemp == 2 or Output.vertTemp == 2 or Output.vertTemp == 6) { + me.checkRadioRevision(Output.latTemp, Output.vertTemp); + } + + Gear.wow1Temp = Gear.wow1.getBoolValue(); + Gear.wow2Temp = Gear.wow2.getBoolValue(); + Output.ap1Temp = Output.ap1.getBoolValue(); + Output.ap2Temp = Output.ap2.getBoolValue(); + Output.latTemp = Output.lat.getValue(); + Output.vertTemp = Output.vert.getValue(); + Text.vertTemp = Text.vert.getValue(); + Position.gearAglFtTemp = Position.gearAglFt.getValue(); + Internal.vsTemp = Internal.vs.getValue(); + Position.indicatedAltitudeFtTemp = Position.indicatedAltitudeFt.getValue(); + + # LNAV Engagement + if (Output.lnavArm.getBoolValue()) { + me.checkLNAV(1); + } + + # VOR/LOC or ILS/LOC Capture + if (Output.locArm.getBoolValue()) { + me.checkLOC(1, 0); + } + + # G/S Capture + if (Output.apprArm.getBoolValue()) { + me.checkAPPR(1); + } + + # Autoland Logic + if (Output.latTemp == 2) { + if (Position.gearAglFtTemp <= 150) { + me.setLatMode(4); + } + } + if (Output.vertTemp == 2) { + if (Position.gearAglFtTemp <= 300 and Position.gearAglFtTemp >= 5) { + Text.vert.setValue("LAND"); + } + if (Position.gearAglFtTemp <= 100 and Position.gearAglFtTemp >= 5) { + me.setVertMode(6); + } + } else if (Output.vertTemp == 6) { + if (Position.gearAglFtTemp <= 50 and Position.gearAglFtTemp >= 5) { + Text.vert.setValue("FLARE"); + } + if (Gear.wow1Temp and Gear.wow2Temp) { + Text.lat.setValue("RLOU"); + Text.vert.setValue("ROLLOUT"); + } + } + + # FLCH Engagement + if (Text.vertTemp == "T/O CLB") { + me.checkFLCH(Setting.reducAglFt.getValue()); + } + + # Altitude Capture/Sync Logic + if (Output.vertTemp != 0) { + Internal.alt.setValue(Input.alt.getValue()); + } + Internal.altTemp = Internal.alt.getValue(); + Internal.altDiff = Internal.altTemp - Position.indicatedAltitudeFtTemp; + + if (Output.vertTemp != 0 and Output.vertTemp != 2 and Output.vertTemp != 6 and Output.vertTemp != 9 and Text.vertTemp != "G/A CLB") { + Internal.captVS = math.clamp(math.round(abs(Internal.vs.getValue()) / 5, 100), 50, 2500); # Capture limits + Custom.apFdOn = Output.ap1Temp or Output.ap2Temp or Output.fd1.getBoolValue() or Output.fd2.getBoolValue(); + if (abs(Internal.altDiff) <= Internal.captVS and !Gear.wow1Temp and !Gear.wow2Temp and Custom.apFdOn) { + if (Internal.altTemp >= Position.indicatedAltitudeFtTemp and Internal.vsTemp >= -25) { # Don't capture if we are going the wrong way + me.setVertMode(3); + } else if (Internal.altTemp < Position.indicatedAltitudeFtTemp and Internal.vsTemp <= 25) { # Don't capture if we are going the wrong way + me.setVertMode(3); + } + } + } + + # Altitude Hold Min/Max Reset + if (Internal.altCaptureActive) { + if (abs(Internal.altDiff) <= 25) { + me.resetClimbRateLim(); + Text.vert.setValue("ALT HLD"); + } + } + + # Thrust Mode Selector + if (Output.athr.getBoolValue() and Output.vertTemp != 7 and (Output.ap1Temp or Output.ap2Temp) and Position.gearAglFt.getValue() <= 18 and (Output.vertTemp == 2 or Output.vertTemp == 6)) { + Output.thrMode.setValue(1); + Text.thr.setValue("RETARD"); + } else if (Output.vertTemp == 4) { + if (Internal.altTemp >= Position.indicatedAltitudeFtTemp) { + Output.thrMode.setValue(2); + Text.thr.setValue("PITCH"); + if (Internal.flchActive) { + Text.vert.setValue("SPD CLB"); + } + } else { + Output.thrMode.setValue(1); + Text.thr.setValue("PITCH"); + if (Internal.flchActive) { + Text.vert.setValue("SPD DES"); + } + } + } else if (Output.vertTemp == 7) { + Output.thrMode.setValue(2); + Text.thr.setValue("PITCH"); + } else { + Output.thrMode.setValue(0); + Text.thr.setValue("THRUST"); + } + + # Custom Stuff Below + # Heading Sync + if (!Custom.showHdg.getBoolValue()) { + Input.hdg.setValue(Misc.pfdHeadingScale.getValue()); + } + + # Preselect Heading + if (Output.latTemp != 0 and Output.latTemp != 9) { # Modes that always show HDG + if (Custom.hdgTime.getValue() + 45 >= Misc.elapsedSec.getValue()) { + setprop("/it-autoflight/custom/show-hdg", 1); + } else { + setprop("/it-autoflight/custom/show-hdg", 0); + } + } + + # Misc + if (Output.ap1Temp == 1 or Output.ap2Temp == 1) { # Trip AP off + if (abs(Control.aileron.getValue()) >= 0.2 or abs(Control.elevator.getValue()) >= 0.2 or abs(Control.rudder.getValue()) >= 0.2) { + me.ap1Master(0); + me.ap2Master(0); + libraries.apOff("hard", 0); + } + } + }, + slowLoop: func() { + Input.bankLimitSWTemp = Input.bankLimitSW.getValue(); + Velocity.trueAirspeedKtTemp = Velocity.trueAirspeedKt.getValue(); + FPLN.activeTemp = FPLN.active.getValue(); + FPLN.currentWPTemp = FPLN.currentWP.getValue(); + FPLN.numTemp = FPLN.num.getValue(); + + # Bank Limit + if (Velocity.trueAirspeedKtTemp >= 420) { + Internal.bankLimitAuto = 15; + } else if (Velocity.trueAirspeedKtTemp >= 340) { + Internal.bankLimitAuto = 20; + } else { + Internal.bankLimitAuto = 25; + } + + Internal.bankLimit.setValue(Internal.bankLimitAuto); + + # If in LNAV mode and route is not longer active, switch to HDG HLD + if (Output.lat.getValue() == 1) { # Only evaulate the rest of the condition if we are in LNAV mode + if (FPLN.num.getValue() == 0 or !FPLN.active.getBoolValue()) { + me.setLatMode(3); + } + } + + # Waypoint Advance Logic + if (FPLN.numTemp > 0 and FPLN.activeTemp == 1) { + if ((FPLN.currentWPTemp + 1) < FPLN.numTemp) { + Velocity.groundspeedMps = Velocity.groundspeedKt.getValue() * 0.5144444444444; + FPLN.wpFlyFrom = FPLN.currentWPTemp; + if (FPLN.wpFlyFrom < 0) { + FPLN.wpFlyFrom = 0; + } + FPLN.currentCourse = getprop("/autopilot/route-manager/route/wp[" ~ FPLN.wpFlyFrom ~ "]/leg-bearing-true-deg"); # Best left at getprop + FPLN.wpFlyTo = FPLN.currentWPTemp + 1; + if (FPLN.wpFlyTo < 0) { + FPLN.wpFlyTo = 0; + } + FPLN.nextCourse = getprop("/autopilot/route-manager/route/wp[" ~ FPLN.wpFlyTo ~ "]/leg-bearing-true-deg"); # Best left at getprop + FPLN.maxBankLimit = Internal.bankLimit.getValue(); + + FPLN.deltaAngle = math.abs(geo.normdeg180(FPLN.currentCourse - FPLN.nextCourse)); + FPLN.maxBank = FPLN.deltaAngle * 1.5; + if (FPLN.maxBank > FPLN.maxBankLimit) { + FPLN.maxBank = FPLN.maxBankLimit; + } + FPLN.radius = (Velocity.groundspeedMps * Velocity.groundspeedMps) / (9.81 * math.tan(FPLN.maxBank / 57.2957795131)); + FPLN.deltaAngleRad = (180 - FPLN.deltaAngle) / 114.5915590262; + FPLN.R = FPLN.radius / math.sin(FPLN.deltaAngleRad); + FPLN.distCoeff = FPLN.deltaAngle * -0.011111 + 2; + if (FPLN.distCoeff < 1) { + FPLN.distCoeff = 1; + } + FPLN.turnDist = math.cos(FPLN.deltaAngleRad) * FPLN.R * FPLN.distCoeff / 1852; + if (Gear.wow0.getBoolValue() and FPLN.turnDist < 1) { + FPLN.turnDist = 1; + } + Internal.lnavAdvanceNm.setValue(FPLN.turnDist); + + if (FPLN.wp0Dist.getValue() <= FPLN.turnDist) { + FPLN.currentWP.setValue(FPLN.currentWPTemp + 1); + } + } + } + }, + ap1Master: func(s) { + if (s == 1) { + if (Output.vert.getValue() != 6 and !Gear.wow1.getBoolValue() and !Gear.wow2.getBoolValue() and Misc.acEss.getValue() >= 110 and Misc.fbwLaw.getValue() == 0 and Position.gearAglFt.getValue() >= 100) { + me.revertBasicMode(); + Output.ap1.setBoolValue(1); + Output.latTemp = Output.lat.getValue(); + if (Output.ap2.getBoolValue() and !Output.apprArm.getBoolValue() and Output.latTemp != 2 and Output.latTemp != 4) { + me.ap2Master(0); + } + Sound.enableApOff = 1; + Sound.apOff.setBoolValue(0); + } + } else { + Output.ap1.setBoolValue(0); + me.apOffFunction(); + } + Output.ap1Temp = Output.ap1.getBoolValue(); + if (Input.ap1.getBoolValue() != Output.ap1Temp) { + Input.ap1.setBoolValue(Output.ap1Temp); + } + }, + ap2Master: func(s) { + if (s == 1) { + if (Output.vert.getValue() != 6 and !Gear.wow1.getBoolValue() and !Gear.wow2.getBoolValue() and Misc.acEss.getValue() >= 110 and Misc.fbwLaw.getValue() == 0 and Position.gearAglFt.getValue() >= 100) { + me.revertBasicMode(); + Output.ap2.setBoolValue(1); + Output.latTemp = Output.lat.getValue(); + if (Output.ap1.getBoolValue() and !Output.apprArm.getBoolValue() and Output.latTemp != 2 and Output.latTemp != 4) { + me.ap1Master(0); + } + Sound.enableApOff = 1; + Sound.apOff.setBoolValue(0); + } + } else { + Output.ap2.setBoolValue(0); + me.apOffFunction(); + } + Output.ap2Temp = Output.ap2.getBoolValue(); + if (Input.ap2.getBoolValue() != Output.ap2Temp) { + Input.ap2.setBoolValue(Output.ap2Temp); + } + }, + apOffFunction: func() { + if (!Output.ap1.getBoolValue() and !Output.ap2.getBoolValue()) { # Only do if both APs are off + me.updateFma(); + if (Sound.enableApOff) { + Sound.apOff.setBoolValue(1); + Sound.enableApOff = 0; + } + } + }, + athrMaster: func(s) { + if (s == 1) { + if (Misc.acEss.getValue() >= 110) { + Output.athr.setBoolValue(1); + Custom.Sound.enableAthrOff = 1; + Custom.Sound.athrOff.setBoolValue(0); + } + } else { + Output.athr.setBoolValue(0); + if (Custom.Sound.enableAthrOff) { + Custom.Sound.athrOff.setBoolValue(1); + Custom.Sound.enableAthrOff = 0; + } + } + Output.athrTemp = Output.athr.getBoolValue(); + if (Input.athr.getBoolValue() != Output.athrTemp) { + Input.athr.setBoolValue(Output.athrTemp); + } + }, + fd1Master: func(s) { + if (s == 1) { + Output.fd1.setBoolValue(1); + me.updateFma(); + } else { + Output.fd1.setBoolValue(0); + if (!Output.fd2.getBoolValue()) { + me.updateFma(); + } + } + Output.fd1Temp = Output.fd1.getBoolValue(); + if (Input.fd1.getBoolValue() != Output.fd1Temp) { + Input.fd1.setBoolValue(Output.fd1Temp); + } + }, + fd2Master: func(s) { + if (s == 1) { + Output.fd2.setBoolValue(1); + me.updateFma(); + } else { + Output.fd2.setBoolValue(0); + if (!Output.fd1.getBoolValue()) { + me.updateFma(); + } + } + Output.fd2Temp = Output.fd2.getBoolValue(); + if (Input.fd2.getBoolValue() != Output.fd2Temp) { + Input.fd2.setBoolValue(Output.fd2Temp); + } + }, + setLatMode: func(n) { + Output.vertTemp = Output.vert.getValue(); + if (n == 0) { # HDG SEL + Output.lnavArm.setBoolValue(0); + Output.locArm.setBoolValue(0); + Output.apprArm.setBoolValue(0); + Output.lat.setValue(0); + Custom.showHdg.setBoolValue(1); + Text.lat.setValue("HDG"); + if (Output.vertTemp == 2 or Output.vertTemp == 6) { # Also cancel G/S or FLARE if active + me.setVertMode(1); + } else { + me.armTextCheck(); + } + } else if (n == 1) { # LNAV + me.checkLNAV(0); + } else if (n == 2) { # VOR/LOC + Output.lnavArm.setBoolValue(0); + me.armTextCheck(); + me.checkLOC(0, 0); + } else if (n == 3) { # HDG HLD + Output.lnavArm.setBoolValue(0); + Output.locArm.setBoolValue(0); + Output.apprArm.setBoolValue(0); + me.syncHDG(); + Output.lat.setValue(0); + Custom.showHdg.setBoolValue(1); + Text.lat.setValue("HDG"); + if (Output.vertTemp == 2 or Output.vertTemp == 6) { # Also cancel G/S or FLARE if active + me.setVertMode(1); + } else { + me.armTextCheck(); + } + } else if (n == 4) { # ALIGN + Output.lnavArm.setBoolValue(0); + Output.locArm.setBoolValue(0); + Output.apprArm.setBoolValue(0); + Output.lat.setValue(4); + Custom.showHdg.setBoolValue(0); + Text.lat.setValue("ALGN"); + me.armTextCheck(); + } else if (n == 5) { # RWY + Output.lnavArm.setBoolValue(0); + Output.locArm.setBoolValue(0); + Output.apprArm.setBoolValue(0); + Output.lat.setValue(5); + Custom.showHdg.setBoolValue(0); + Text.lat.setValue("T/O"); + me.armTextCheck(); + } else if (n == 9) { # NONE + Output.locArm.setBoolValue(0); + Output.lat.setValue(9); + Custom.showHdg.setBoolValue(1); + Text.lat.setValue(" "); + me.armTextCheck(); + } + }, + setLatArm: func(n) { + if (n == 0) { + Output.lnavArm.setBoolValue(0); + Custom.showHdg.setBoolValue(1); + me.armTextCheck(); + } else if (n == 1) { + if (FPLN.num.getValue() > 0 and FPLN.active.getBoolValue()) { + Output.lnavArm.setBoolValue(1); + Custom.showHdg.setBoolValue(0); + me.armTextCheck(); + } + } else if (n == 3) { + me.syncHDG(); + Output.lnavArm.setBoolValue(0); + Custom.showHdg.setBoolValue(1); + me.armTextCheck(); + } + }, + setVertMode: func(n) { + Input.altDiff = Input.alt.getValue() - Position.indicatedAltitudeFt.getValue(); + if (n == 0) { # ALT HLD + Internal.flchActive = 0; + Internal.altCaptureActive = 0; + Output.apprArm.setBoolValue(0); + Output.vert.setValue(0); + me.resetClimbRateLim(); + Text.vert.setValue("ALT HLD"); + me.syncALT(); + me.armTextCheck(); + } else if (n == 1) { # V/S + if (abs(Input.altDiff) >= 25) { + Internal.flchActive = 0; + Internal.altCaptureActive = 0; + Output.apprArm.setBoolValue(0); + Output.vert.setValue(1); + Text.vert.setValue("V/S"); + me.syncVS(); + me.armTextCheck(); + } else { + Output.apprArm.setBoolValue(0); + me.armTextCheck(); + } + } else if (n == 2) { # G/S + me.checkLOC(0, 1); + me.checkAPPR(0); + } else if (n == 3) { # ALT CAP + Internal.flchActive = 0; + Output.vert.setValue(0); + me.setClimbRateLim(); + Internal.altCaptureActive = 1; + Text.vert.setValue("ALT CAP"); + } else if (n == 4) { # FLCH + Output.apprArm.setBoolValue(0); + Output.vert.setValue(1); + Internal.alt.setValue(Input.alt.getValue()); + Internal.altDiff = Internal.alt.getValue() - Position.indicatedAltitudeFt.getValue(); + if (abs(Internal.altDiff) >= 250) { # SPD CLB or SPD DES + Internal.altCaptureActive = 0; + Output.vert.setValue(4); + Internal.flchActive = 1; + } else { # ALT CAP + Internal.flchActive = 0; + Internal.alt.setValue(Input.alt.getValue()); + Internal.altCaptureActive = 1; + Output.vert.setValue(0); + Text.vert.setValue("ALT CAP"); + } + me.armTextCheck(); + } else if (n == 5) { # FPA + if (abs(Input.altDiff) >= 25) { + Internal.flchActive = 0; + Internal.altCaptureActive = 0; + Output.apprArm.setBoolValue(0); + Output.vert.setValue(5); + Text.vert.setValue("FPA"); + me.syncFPA(); + me.armTextCheck(); + } else { + Output.apprArm.setBoolValue(0); + me.armTextCheck(); + } + } else if (n == 6) { # FLARE/ROLLOUT + Internal.flchActive = 0; + Internal.altCaptureActive = 0; + Output.apprArm.setBoolValue(0); + Output.vert.setValue(6); + me.armTextCheck(); + } else if (n == 7) { # T/O CLB or G/A CLB, text is set by TOGA selector + Internal.flchActive = 0; + Internal.altCaptureActive = 0; + Output.apprArm.setBoolValue(0); + Output.vert.setValue(7); + me.armTextCheck(); + } else if (n == 9) { # NONE + Internal.flchActive = 0; + Internal.altCaptureActive = 0; + Output.apprArm.setBoolValue(0); + Output.vert.setValue(9); + Text.vert.setValue(" "); + me.armTextCheck(); + } + }, + activateLNAV: func() { + if (Output.lat.getValue() != 1) { + Output.lnavArm.setBoolValue(0); + Output.locArm.setBoolValue(0); + Output.apprArm.setBoolValue(0); + Output.lat.setValue(1); + Custom.showHdg.setBoolValue(0); + Text.lat.setValue("LNAV"); + if (Output.vertTemp == 2 or Output.vertTemp == 6) { # Also cancel G/S or FLARE if active + me.setVertMode(1); + } else { + me.armTextCheck(); + } + } + }, + activateLOC: func() { + if (Output.lat.getValue() != 2) { + Output.lnavArm.setBoolValue(0); + Output.locArm.setBoolValue(0); + Output.lat.setValue(2); + Custom.showHdg.setBoolValue(0); + Text.lat.setValue("LOC"); + me.armTextCheck(); + } + }, + activateGS: func() { + if (Output.vert.getValue() != 2) { + Internal.flchActive = 0; + Internal.altCaptureActive = 0; + Output.apprArm.setBoolValue(0); + Output.vert.setValue(2); + Text.vert.setValue("G/S"); + me.armTextCheck(); + } + }, + checkLNAV: func(t) { + if (FPLN.num.getValue() > 0 and FPLN.active.getBoolValue() and Position.gearAglFt.getValue() >= 30) { + me.activateLNAV(); + } else if (Output.lat.getValue() != 1 and t != 1) { + Output.lnavArm.setBoolValue(1); + me.armTextCheck(); + } + }, + checkFLCH: func(a) { + if (Position.indicatedAltitudeFt.getValue() >= a and a != 0 and !Gear.wow1.getBoolValue() and !Gear.wow2.getBoolValue()) { + me.setVertMode(4); + } + }, + checkLOC: func(t, a) { + if (Radio.inRange[Radio.radioSel].getBoolValue()) { # # Only evaulate the rest of the condition unless we are in range + Radio.locDeflTemp = Radio.locDefl[Radio.radioSel].getValue(); + Radio.signalQualityTemp = Radio.signalQuality[Radio.radioSel].getValue(); + if (abs(Radio.locDeflTemp) <= 0.95 and Radio.locDeflTemp != 0 and Radio.signalQualityTemp >= 0.99) { + me.activateLOC(); + } else if (t != 1) { # Do not do this if loop calls it + if (Output.lat.getValue() != 2) { + Output.lnavArm.setBoolValue(0); + Output.locArm.setBoolValue(1); + if (a != 1) { # Don't call this if arming with G/S + me.armTextCheck(); + } + } + } + } else { # Prevent bad behavior due to FG not updating it when not in range + Radio.signalQuality[Radio.radioSel].setValue(0); + } + }, + checkAPPR: func(t) { + if (Radio.inRange[Radio.radioSel].getBoolValue()) { # # Only evaulate the rest of the condition unless we are in range + Radio.gsDeflTemp = Radio.gsDefl[Radio.radioSel].getValue(); + if (abs(Radio.gsDeflTemp) <= 0.2 and Radio.gsDeflTemp != 0 and Output.lat.getValue() == 2) { # Only capture if LOC is active + me.activateGS(); + } else if (t != 1) { # Do not do this if loop calls it + if (Output.vert.getValue() != 2) { + Output.apprArm.setBoolValue(1); + } + me.armTextCheck(); + } + } else { # Prevent bad behavior due to FG not updating it when not in range + Radio.signalQuality[Radio.radioSel].setValue(0); + } + }, + checkRadioRevision: func(l, v) { # Revert mode if signal lost + Radio.inRangeTemp = Radio.inRange[Radio.radioSel].getBoolValue(); + if (!Radio.inRangeTemp) { + if (l == 4 or v == 6) { + me.ap1Master(0); + me.ap2Master(0); + me.setLatMode(3); + me.setVertMode(1); + } else { + me.setLatMode(3); # Also cancels G/S if active + } + } + }, + setClimbRateLim: func() { + Internal.vsTemp = Internal.vs.getValue(); + if (Internal.alt.getValue() >= Position.indicatedAltitudeFt.getValue()) { + Internal.maxVS.setValue(math.round(Internal.vsTemp)); + Internal.minVS.setValue(-500); + } else { + Internal.maxVS.setValue(500); + Internal.minVS.setValue(math.round(Internal.vsTemp)); + } + }, + resetClimbRateLim: func() { + Internal.minVS.setValue(-500); + Internal.maxVS.setValue(500); + }, + takeoffGoAround: func() { + Output.vertTemp = Output.vert.getValue(); + if ((Output.vertTemp == 2 or Output.vertTemp == 6) and Velocity.indicatedAirspeedKt.getValue() >= 80) { + if (Gear.wow1.getBoolValue() or Gear.wow2.getBoolValue()) { + me.ap1Master(0); + me.ap2Master(0); + } + me.setLatMode(3); + me.setVertMode(7); + Text.vert.setValue("G/A CLB"); + Input.ktsMach.setBoolValue(0); + me.syncIAS(); + } else if (Gear.wow1Temp or Gear.wow2Temp) { + me.athrMaster(1); + me.setLatMode(5); + me.setVertMode(7); + Text.vert.setValue("T/O CLB"); + } + }, + armTextCheck: func() { + if (Output.apprArm.getBoolValue()) { + Text.arm.setValue("ILS"); + } else if (Output.locArm.getBoolValue()) { + Text.arm.setValue("LOC"); + } else if (Output.lnavArm.getBoolValue()) { + Text.arm.setValue("LNV"); + } else { + Text.arm.setValue(" "); + } + }, + syncIAS: func() { + Input.ias.setValue(math.clamp(math.round(Velocity.indicatedAirspeedKt.getValue()), 100, 350)); + }, + syncMach: func() { + Input.mach.setValue(math.clamp(math.round(Velocity.indicatedMach.getValue(), 0.001), 0.5, 0.82)); + }, + syncHDG: func() { + Input.hdg.setValue(math.round(Internal.hdgPredicted.getValue())); # Switches to track automatically + }, + syncALT: func() { + Input.alt.setValue(math.clamp(math.round(Internal.altPredicted.getValue(), 100), 0, 50000)); + Internal.alt.setValue(math.clamp(math.round(Internal.altPredicted.getValue(), 100), 0, 50000)); + }, + syncVS: func() { + Input.vs.setValue(math.clamp(math.round(Internal.vs.getValue(), 100), -6000, 6000)); + }, + syncFPA: func() { + Input.fpa.setValue(math.clamp(math.round(Internal.fpa.getValue(), 0.1), -9.9, 9.9)); + }, + # Custom Stuff Below + updateFma: func() { + if (!Output.ap1.getBoolValue() and !Output.ap2.getBoolValue() and !Output.fd1.getBoolValue() and !Output.fd2.getBoolValue()) { + me.setLatMode(9); + me.setVertMode(9); + Custom.Output.fmaPower.setBoolValue(0); + } else { + Custom.Output.fmaPower.setBoolValue(1); + me.revertBasicMode(); + } + }, + revertBasicMode: func() { + if (!Gear.wow1.getBoolValue() and !Gear.wow2.getBoolValue()) { # Don't do this on ground + if (Output.lat.getValue() == 9) { + me.setLatMode(3); + } + if (Output.vert.getValue() == 9) { + if (Custom.trkFpa.getBoolValue()) { + me.setVertMode(5); + } else { + me.setVertMode(1); + } + } + } + }, + disarmLOC: func() { + Output.locArm.setBoolValue(0); + ITAF.armTextCheck(); + }, + disarmGS: func() { + Output.apprArm.setBoolValue(0); + ITAF.armTextCheck(); + }, + toggleTrkFpa: func() { + if (Custom.trkFpa.getBoolValue()) { + me.trkFpaOff(); + } else { + me.trkFpaOn(); + } + }, + trkFpaOn: func() { + Custom.trkFpa.setBoolValue(1); + if (Output.vert.getValue() == 1) { + Input.vert.setValue(5); # This way we only do this if all conditions are true + } + Input.trk.setBoolValue(1); + Custom.ndTrkSel[0].setBoolValue(1); + Custom.ndTrkSel[1].setBoolValue(1); + if (abs(Internal.hdgErrorDeg.getValue()) <= 10 and Output.lat.getValue() == 0) { + me.setLatMode(3); + } + }, + trkFpaOff: func() { + Custom.trkFpa.setBoolValue(0); + if (Output.vert.getValue() == 5) { + Input.vert.setValue(1); # This way we only do this if all conditions are true + } + Input.trk.setBoolValue(0); + Custom.ndTrkSel[0].setBoolValue(0); + Custom.ndTrkSel[1].setBoolValue(0); + if (abs(Internal.hdgErrorDeg.getValue()) <= 10 and Output.lat.getValue() == 0) { + me.setLatMode(3); + } + }, +}; -# AP 1 Master System setlistener("/it-autoflight/input/ap1", func { - var apmas = getprop("/it-autoflight/input/ap1"); - var apout = getprop("/it-autoflight/output/ap1"); - if (apmas != apout) { - AP1Master(); + Input.ap1Temp = Input.ap1.getBoolValue(); + if (Input.ap1Temp != Output.ap1.getBoolValue()) { + ITAF.ap1Master(Input.ap1Temp); } }); -var AP1Master = func { - var apmas = getprop("/it-autoflight/input/ap1"); - var ac_ess = getprop("/systems/electrical/bus/ac-ess"); - var law = getprop("/it-fbw/law"); - if (apmas == 0) { - setprop("/it-autoflight/output/ap1", 0); - if (getprop("/it-autoflight/sound/enableapoffsound") == 1 and getprop("/it-autoflight/output/ap2") == 0) { - setprop("/it-autoflight/sound/apoffsound", 1); - setprop("/it-autoflight/sound/enableapoffsound", 0); - } - fmabox(); - updateTimers(); - } else if (apmas == 1 and ac_ess >= 110 and law == 0) { - if (getprop("/gear/gear[1]/wow") == 0 and getprop("/gear/gear[2]/wow") == 0 and getprop("/position/gear-agl-ft") >= 100) { - if (getprop("/it-autoflight/output/lat") == 9) { - setprop("/it-autoflight/input/lat", 3); - } - if (getprop("/it-autoflight/output/vert") == 9) { - if (getprop("/it-autoflight/custom/trk-fpa") == 0) { - setprop("/it-autoflight/input/vert", 1); - } else if (getprop("/it-autoflight/custom/trk-fpa") == 1) { - setprop("/it-autoflight/input/vert", 5); - } - } - setprop("/it-autoflight/output/ap1", 1); - if (getprop("/it-autoflight/output/ap2") == 1 and (getprop("/it-autoflight/output/appr-armed") != 1 and getprop("/it-autoflight/output/lat") != 2 and getprop("/it-autoflight/output/lat") != 6)) { - setprop("/it-autoflight/input/ap2", 0); - } - setprop("/it-autoflight/sound/enableapoffsound", 1); - setprop("/it-autoflight/sound/apoffsound", 0); - fmabox(); - } - } - - var apout = getprop("/it-autoflight/output/ap1"); - if (apmas != apout) { - setprop("/it-autoflight/input/ap1", apout); - } -} - -# AP 2 Master System setlistener("/it-autoflight/input/ap2", func { - var apmas = getprop("/it-autoflight/input/ap2"); - var apout = getprop("/it-autoflight/output/ap2"); - - if (apmas != apout) { - AP2Master(); + Input.ap2Temp = Input.ap2.getBoolValue(); + if (Input.ap2Temp != Output.ap2.getBoolValue()) { + ITAF.ap2Master(Input.ap2Temp); } }); -var AP2Master = func { - var apmas = getprop("/it-autoflight/input/ap2"); - var ac_ess = getprop("/systems/electrical/bus/ac-ess"); - var law = getprop("/it-fbw/law"); - if (apmas == 0) { - setprop("/it-autoflight/output/ap2", 0); - if (getprop("/it-autoflight/sound/enableapoffsound2") == 1 and getprop("/it-autoflight/output/ap1") == 0) { - setprop("/it-autoflight/sound/apoffsound2", 1); - setprop("/it-autoflight/sound/enableapoffsound2", 0); - } - fmabox(); - updateTimers(); - } else if (apmas == 1 and ac_ess >= 110 and law == 0) { - if (getprop("/gear/gear[1]/wow") == 0 and getprop("/gear/gear[2]/wow") == 0 and getprop("/position/gear-agl-ft") >= 100) { - if (getprop("/it-autoflight/output/lat") == 9) { - setprop("/it-autoflight/input/lat", 3); - } - if (getprop("/it-autoflight/output/vert") == 9) { - if (getprop("/it-autoflight/custom/trk-fpa") == 0) { - setprop("/it-autoflight/input/vert", 1); - } else if (getprop("/it-autoflight/custom/trk-fpa") == 1) { - setprop("/it-autoflight/input/vert", 5); - } - } - setprop("/it-autoflight/output/ap2", 1); - if (getprop("/it-autoflight/output/ap1") == 1 and (getprop("/it-autoflight/output/appr-armed") != 1 and getprop("/it-autoflight/output/lat") != 2 and getprop("/it-autoflight/output/lat") != 6)) { - setprop("/it-autoflight/input/ap1", 0); - } - setprop("/it-autoflight/sound/enableapoffsound2", 1); - setprop("/it-autoflight/sound/apoffsound2", 0); - fmabox(); - } - } - - var apout = getprop("/it-autoflight/output/ap2"); - if (apmas != apout) { - setprop("/it-autoflight/input/ap2", apout); - } -} - -# ATHR Master System setlistener("/it-autoflight/input/athr", func { - var athrmas = getprop("/it-autoflight/input/athr"); - var athrout = getprop("/it-autoflight/output/athr"); - - if (athrmas != athrout) { - ATHRMaster(); + Input.athrTemp = Input.athr.getBoolValue(); + if (Input.athrTemp != Output.athr.getBoolValue()) { + ITAF.athrMaster(Input.athrTemp); } }); -var ATHRMaster = func { - var athrmas = getprop("/it-autoflight/input/athr"); - if (athrmas == 0) { - setprop("/it-autoflight/output/athr", 0); - if (getprop("/it-autoflight/sound/enableathrsound") == 1 and getprop("/it-autoflight/output/athr") == 0 and getprop("/position/gear-agl-ft") > 50) { - setprop("/it-autoflight/sound/athrsound", 1); - setprop("/it-autoflight/sound/enableathrsound", 0); - } - } else if (athrmas == 1) { - thrustmode(); - setprop("/it-autoflight/output/athr", 1); - setprop("/it-autoflight/sound/athrsound", 0); - setprop("/it-autoflight/sound/enableathrsound", 1); - } - - var athrout = getprop("/it-autoflight/output/athr"); - if (athrmas != athrout) { - setprop("/it-autoflight/input/athr", athrout); - } -} - -# Flight Director 1 Master System setlistener("/it-autoflight/input/fd1", func { - var fdmas = getprop("/it-autoflight/input/fd1"); - var fdout = getprop("/it-autoflight/output/fd1"); - - if (fdmas != fdout) { - FD1Master(); + Input.fd1Temp = Input.fd1.getBoolValue(); + if (Input.fd1Temp != Output.fd1.getBoolValue()) { + ITAF.fd1Master(Input.fd1Temp); } }); -var FD1Master = func { - var fdmas = getprop("/it-autoflight/input/fd1"); - if (fdmas == 0) { - setprop("/it-autoflight/output/fd1", 0); - if (getprop("/it-autoflight/output/fd1") == 0 and getprop("/it-autoflight/output/fd2") == 0) { - fmabox(); - } - updateTimers(); - } else if (fdmas == 1) { - setprop("/it-autoflight/output/fd1", 1); - fmabox(); - } - - var fdout = getprop("/it-autoflight/output/fd1"); - if (fdmas != fdout) { - setprop("/it-autoflight/input/fd1", fdout); - } -} - -# Flight Director 2 Master System setlistener("/it-autoflight/input/fd2", func { - var fdmas = getprop("/it-autoflight/input/fd2"); - var fdout = getprop("/it-autoflight/output/fd2"); - - if (fdmas != fdout) { - FD2Master(); + Input.fd2Temp = Input.fd2.getBoolValue(); + if (Input.fd2Temp != Output.fd2.getBoolValue()) { + ITAF.fd2Master(Input.fd2Temp); } }); -var FD2Master = func { - var fdmas = getprop("/it-autoflight/input/fd2"); - if (fdmas == 0) { - setprop("/it-autoflight/output/fd2", 0); - if (getprop("/it-autoflight/output/fd1") == 0 and getprop("/it-autoflight/output/fd2") == 0) { - fmabox(); - } - updateTimers(); - } else if (fdmas == 1) { - setprop("/it-autoflight/output/fd2", 1); - fmabox(); - } - - var fdout = getprop("/it-autoflight/output/fd2"); - if (fdmas != fdout) { - setprop("/it-autoflight/input/fd2", fdout); - } -} - -# FMA Boxes and Mode -var fmabox = func { - var ap1 = getprop("/it-autoflight/output/ap1"); - var ap2 = getprop("/it-autoflight/output/ap2"); - var fd1 = getprop("/it-autoflight/output/fd1"); - var fd2 = getprop("/it-autoflight/output/fd2"); - if (!ap1 and !ap2 and !fd1 and !fd2) { - setprop("/it-autoflight/input/lat", 9); - setprop("/it-autoflight/input/vert", 9); - setprop("/it-autoflight/output/fma-pwr", 0); - } else { - setprop("/it-autoflight/output/fma-pwr", 1); - if (getprop("/it-autoflight/output/lat") == 9) { - setprop("/it-autoflight/input/lat", 3); - } - if (getprop("/it-autoflight/output/vert") == 9) { - if (getprop("/it-autoflight/custom/trk-fpa") == 0) { - setprop("/it-autoflight/input/vert", 1); - } else if (getprop("/it-autoflight/custom/trk-fpa") == 1) { - setprop("/it-autoflight/input/vert", 5); - } - setprop("/it-autoflight/input/vs", math.round(getprop("/it-autoflight/internal/vert-speed-fpm"), 100)); - setprop("/it-autoflight/input/fpa", math.round(getprop("/it-autoflight/internal/fpa"), 0.1)); - } - } -} - -# Master Lateral -setlistener("/it-autoflight/input/lat", func { - var ap1 = getprop("/it-autoflight/output/ap1"); - var ap2 = getprop("/it-autoflight/output/ap2"); - var fd1 = getprop("/it-autoflight/output/fd1"); - var fd2 = getprop("/it-autoflight/output/fd2"); - if (getprop("/gear/gear[1]/wow") == 0 and getprop("/gear/gear[2]/wow") == 0 and (ap1 or ap2 or fd1 or fd2)) { - setprop("/it-autoflight/input/lat-arm", 0); - lateral(); - } else if (getprop("/gear/gear[1]/wow") == 0 and getprop("/gear/gear[2]/wow") == 0 and !ap1 and !ap2 and !fd1 and !fd2) { - setprop("/it-autoflight/input/lat", 9); - setprop("/it-autoflight/input/lat-arm", 0); - lateral(); - } else { - lat_arm(); - } -}); - -var lateral = func { - var latset = getprop("/it-autoflight/input/lat"); - if (latset == 0) { - alandt.stop(); - alandt1.stop(); - setprop("/it-autoflight/input/lat-arm", 0); - setprop("/it-autoflight/output/loc-armed", 0); - setprop("/it-autoflight/output/appr-armed", 0); - setprop("/it-autoflight/output/lat", 0); - setprop("/it-autoflight/mode/lat", "HDG"); - setprop("/it-autoflight/mode/arm", " "); - } else if (latset == 1) { - if (getprop("/autopilot/route-manager/route/num") > 0 and getprop("/autopilot/route-manager/active") == 1 and getprop("/position/gear-agl-ft") >= 30) { - make_lnav_active(); - } else { - if (getprop("/it-autoflight/output/lat") != 1) { - setprop("/it-autoflight/input/lat-arm", 1); - setprop("/it-autoflight/mode/arm", "LNV"); - } - } - } else if (latset == 2) { - if (getprop("/instrumentation/nav[0]/in-range") == 1) { - locdefl = abs(getprop("/instrumentation/nav[0]/heading-needle-deflection-norm")); - if (locdefl < 0.95 and locdefl != 0 and getprop("/instrumentation/nav[0]/signal-quality-norm") > 0.99) { - make_loc_active(); - } else { - if (getprop("/it-autoflight/output/lat") != 2) { - setprop("/it-autoflight/input/lat-arm", 0); - setprop("/it-autoflight/output/loc-armed", 1); - setprop("/it-autoflight/mode/arm", "LOC"); - } - } - } else { - setprop("/instrumentation/nav[0]/signal-quality-norm", 0); - } - } else if (latset == 3) { - alandt.stop(); - alandt1.stop(); - setprop("/it-autoflight/input/lat-arm", 0); - setprop("/it-autoflight/output/loc-armed", 0); - setprop("/it-autoflight/output/appr-armed", 0); - var hdgpredic = math.round(getprop("/it-autoflight/internal/heading-predicted")); - setprop("/it-autoflight/input/hdg", hdgpredic); - setprop("/it-autoflight/output/lat", 0); - setprop("/it-autoflight/mode/lat", "HDG"); - setprop("/it-autoflight/mode/arm", " "); - } else if (latset == 4) { - setprop("/it-autoflight/input/lat-arm", 0); - setprop("/it-autoflight/output/lat", 4); - setprop("/it-autoflight/mode/lat", "ALGN"); - setprop("/it-autoflight/custom/show-hdg", 0); - } else if (latset == 5) { - setprop("/it-autoflight/output/lat", 5); - setprop("/it-autoflight/custom/show-hdg", 0); - } else if (latset == 9) { - setprop("/it-autoflight/output/loc-armed", 0); - setprop("/it-autoflight/output/lat", 9); - setprop("/it-autoflight/mode/lat", " "); - setprop("/it-autoflight/mode/arm", " "); - setprop("/it-autoflight/custom/show-hdg", 1); - } -} - -var lat_arm = func { - var latset = getprop("/it-autoflight/input/lat"); - if (latset == 0) { - setprop("/it-autoflight/input/lat-arm", 0); - setprop("/it-autoflight/mode/arm", " "); - setprop("/it-autoflight/custom/show-hdg", 1); - } else if (latset == 1) { - if (getprop("/autopilot/route-manager/route/num") > 0 and getprop("/autopilot/route-manager/active") == 1) { - setprop("/it-autoflight/input/lat-arm", 1); - setprop("/it-autoflight/mode/arm", "LNV"); - } - } else if (latset == 3) { - if (getprop("/it-autoflight/custom/trk-fpa") == 1) { - var hdgnow = math.round(getprop("/it-autoflight/internal/track-deg")); - } else { - var hdgnow = math.round(getprop("/it-autoflight/internal/heading-deg")); - } - setprop("/it-autoflight/input/hdg", hdgnow); - setprop("/it-autoflight/input/lat-arm", 0); - setprop("/it-autoflight/mode/arm", " "); - setprop("/it-autoflight/custom/show-hdg", 1); - } -} - -# Master Vertical -setlistener("/it-autoflight/input/vert", func { - var ap1 = getprop("/it-autoflight/output/ap1"); - var ap2 = getprop("/it-autoflight/output/ap2"); - var fd1 = getprop("/it-autoflight/output/fd1"); - var fd2 = getprop("/it-autoflight/output/fd2"); - if (getprop("/gear/gear[1]/wow") == 0 and getprop("/gear/gear[2]/wow") == 0 and (ap1 or ap2 or fd1 or fd2)) { - vertical(); - } else if (getprop("/gear/gear[1]/wow") == 0 and getprop("/gear/gear[2]/wow") == 0 and !ap1 and !ap2 and !fd1 and !fd2) { - setprop("/it-autoflight/input/vert", 9); - vertical(); - } -}); - -var vertical = func { - var vertset = getprop("/it-autoflight/input/vert"); - if (vertset == 0) { - alandt.stop(); - alandt1.stop(); - mng_sys_stop(); - setprop("/it-autoflight/output/appr-armed", 0); - setprop("/it-autoflight/output/vert", 0); - setprop("/it-autoflight/mode/vert", "ALT HLD"); - if (getprop("/it-autoflight/output/loc-armed")) { - setprop("/it-autoflight/mode/arm", "LOC"); - } else if (getprop("/it-autoflight/input/lat-arm") == 1) { - setprop("/it-autoflight/mode/arm", "LNV"); - } else { - setprop("/it-autoflight/mode/arm", " "); - } - var altpredic = math.round(getprop("/it-autoflight/internal/altitude-predicted"), 100); - setprop("/it-autoflight/input/alt", altpredic); - setprop("/it-autoflight/internal/alt", altpredic); - thrustmode(); - } else if (vertset == 1) { - alandt.stop(); - alandt1.stop(); - mng_sys_stop(); - setprop("/it-autoflight/output/appr-armed", 0); - altinput = getprop("/it-autoflight/input/alt"); - setprop("/it-autoflight/internal/alt", altinput); - vsnow = math.clamp(math.round(getprop("/it-autoflight/internal/vert-speed-fpm"), 100), -6000, 6000); - setprop("/it-autoflight/input/vs", vsnow); - setprop("/it-autoflight/output/vert", 1); - setprop("/it-autoflight/mode/vert", "V/S"); - if (getprop("/it-autoflight/output/loc-armed")) { - setprop("/it-autoflight/mode/arm", "LOC"); - } else if (getprop("/it-autoflight/input/lat-arm") == 1) { - setprop("/it-autoflight/mode/arm", "LNV"); - } else { - setprop("/it-autoflight/mode/arm", " "); - } - thrustmode(); - } else if (vertset == 2) { - if (getprop("/instrumentation/nav[0]/in-range") == 1) { - locdefl = abs(getprop("/instrumentation/nav[0]/heading-needle-deflection-norm")); - if (locdefl < 0.95 and locdefl != 0 and getprop("/instrumentation/nav[0]/signal-quality-norm") > 0.99) { - make_loc_active(); - } else { - if (getprop("/it-autoflight/output/lat") != 2) { - setprop("/it-autoflight/input/lat-arm", 0); - setprop("/it-autoflight/output/loc-armed", 1); - } - } - signal = getprop("/instrumentation/nav[0]/gs-needle-deflection-norm"); - if (((signal < 0 and signal >= -0.20) or (signal > 0 and signal <= 0.20)) and getprop("/it-autoflight/output/lat") == 2) { - make_appr_active(); - } else { - if (getprop("/it-autoflight/output/vert") != 2 and getprop("/it-autoflight/output/vert") != 6) { - setprop("/it-autoflight/output/appr-armed", 1); - setprop("/it-autoflight/mode/arm", "ILS"); - } - } - } else { - setprop("/instrumentation/nav[0]/signal-quality-norm", 0); - setprop("/instrumentation/nav[0]/gs-rate-of-climb", 0); - } - } else if (vertset == 3) { - alandt.stop(); - alandt1.stop(); - mng_sys_stop(); - var calt = getprop("/instrumentation/altimeter/indicated-altitude-ft"); - var alt = getprop("/it-autoflight/internal/alt"); - var dif = calt - alt; - vsnow = getprop("/it-autoflight/internal/vert-speed-fpm"); - if (calt < alt) { - setprop("/it-autoflight/internal/max-vs", vsnow); - } else if (calt > alt) { - setprop("/it-autoflight/internal/min-vs", vsnow); - } - minmaxtimer.start(); - thrustmode(); - setprop("/it-autoflight/output/vert", 0); - setprop("/it-autoflight/mode/vert", "ALT CAP"); - } else if (vertset == 4) { - alandt.stop(); - alandt1.stop(); - mng_sys_stop(); - setprop("/it-autoflight/output/appr-armed", 0); - altinput = getprop("/it-autoflight/input/alt"); - setprop("/it-autoflight/internal/alt", altinput); - var calt = getprop("/instrumentation/altimeter/indicated-altitude-ft"); - var alt = getprop("/it-autoflight/internal/alt"); - var dif = calt - alt; - if (dif < 250 and dif > -250) { - alt_on(); - } else { - flch_on(); - } - if (getprop("/it-autoflight/output/loc-armed")) { - setprop("/it-autoflight/mode/arm", "LOC"); - } else if (getprop("/it-autoflight/input/lat-arm") == 1) { - setprop("/it-autoflight/mode/arm", "LNV"); - } else { - setprop("/it-autoflight/mode/arm", " "); - } - } else if (vertset == 5) { - alandt.stop(); - alandt1.stop(); - mng_sys_stop(); - setprop("/it-autoflight/output/appr-armed", 0); - altinput = getprop("/it-autoflight/input/alt"); - setprop("/it-autoflight/internal/alt", altinput); - fpanow = math.clamp(math.round(getprop("/it-autoflight/internal/fpa"), 0.1), -9.9, 9.9); - setprop("/it-autoflight/input/fpa", fpanow); - setprop("/it-autoflight/output/vert", 5); - setprop("/it-autoflight/mode/vert", "FPA"); - if (getprop("/it-autoflight/output/loc-armed")) { - setprop("/it-autoflight/mode/arm", "LOC"); - } else if (getprop("/it-autoflight/input/lat-arm") == 1) { - setprop("/it-autoflight/mode/arm", "LNV"); - } else { - setprop("/it-autoflight/mode/arm", " "); - } - thrustmode(); - } else if (vertset == 6) { - setprop("/it-autoflight/output/vert", 6); - setprop("/it-autoflight/mode/arm", " "); - thrustmode(); - mng_sys_stop(); - alandt.stop(); - alandt1.start(); - } else if (vertset == 7) { - alandt.stop(); - alandt1.stop(); - mng_sys_stop(); - setprop("/it-autoflight/output/vert", 7); - if (getprop("/it-autoflight/output/loc-armed") == 1) { - setprop("/it-autoflight/mode/arm", "LOC"); - } else if (getprop("/it-autoflight/input/lat-arm") == 1) { - # Do nothing - } else { - setprop("/it-autoflight/mode/arm", " "); - } - altinput = getprop("/it-autoflight/input/alt"); - setprop("/it-autoflight/internal/alt", altinput); - thrustmodet.start(); - } else if (vertset == 8) { - if (getprop("/autopilot/route-manager/route/num") > 0 and getprop("/autopilot/route-manager/active") == 1 and getprop("/it-autoflight/internal/alt-const") >= 100) { - alandt.stop(); - alandt1.stop(); - setprop("/it-autoflight/output/appr-armed", 0); - setprop("/it-autoflight/output/vert", 8); - mng_run(); - setprop("/it-autoflight/mode/vert", "mng"); - setprop("/it-autoflight/mode/arm", " "); - altinput = getprop("/it-autoflight/input/alt"); - setprop("/it-autoflight/internal/alt", altinput); - if (getprop("/it-autoflight/output/loc-armed")) { - setprop("/it-autoflight/mode/arm", "LOC"); - } else if (getprop("/it-autoflight/input/lat-arm") == 1) { - setprop("/it-autoflight/mode/arm", "LNV"); - } else { - setprop("/it-autoflight/mode/arm", " "); - } - mng_maint.start(); - thrustmodet.stop(); - } else { - setprop("/it-autoflight/input/vert", 4); - vertical(); - } - } else if (vertset == 9) { - alandt.stop(); - alandt1.stop(); - mng_sys_stop(); - setprop("/it-autoflight/output/appr-armed", 0); - altinput = getprop("/it-autoflight/input/alt"); - setprop("/it-autoflight/internal/alt", altinput); - setprop("/it-autoflight/output/vert", 9); - setprop("/it-autoflight/mode/vert", " "); - if (getprop("/it-autoflight/output/loc-armed")) { - setprop("/it-autoflight/mode/arm", "LOC"); - } else if (getprop("/it-autoflight/input/lat-arm") == 1) { - setprop("/it-autoflight/mode/arm", "LNV"); - } else { - setprop("/it-autoflight/mode/arm", " "); - } - thrustmode(); - } -} - -var disarmLOC = func { - setprop("/it-autoflight/output/loc-armed", 0); -} - -var disarmGS = func { - alandt.stop(); - alandt1.stop(); - setprop("/it-autoflight/output/appr-armed", 0); -} - -# Helpers -var toggle_trkfpa = func { - var trkfpa = getprop("/it-autoflight/custom/trk-fpa"); - if (trkfpa == 0) { - trkfpa_on(); - } else if (trkfpa == 1) { - trkfpa_off(); - } -} - -var trkfpa_off = func { - setprop("/it-autoflight/custom/trk-fpa", 0); - if (getprop("/it-autoflight/output/vert") == 5) { - setprop("/it-autoflight/input/vert", 1); - } - setprop("/it-autoflight/input/trk", 0); - setprop("/instrumentation/efis[0]/trk-selected", 0); - setprop("/instrumentation/efis[1]/trk-selected", 0); - var hed = getprop("/it-autoflight/internal/heading-error-deg"); - if (hed >= -10 and hed <= 10 and getprop("/it-autoflight/output/lat") == 0) { - setprop("/it-autoflight/input/lat", 3); - } -} - -var trkfpa_on = func { - setprop("/it-autoflight/custom/trk-fpa", 1); - if (getprop("/it-autoflight/output/vert") == 1) { - setprop("/it-autoflight/input/vert", 5); - } - setprop("/it-autoflight/input/trk", 1); - setprop("/instrumentation/efis[0]/trk-selected", 1); - setprop("/instrumentation/efis[1]/trk-selected", 1); - var hed = getprop("/it-autoflight/internal/heading-error-deg"); - if (hed >= -10 and hed <= 10 and getprop("/it-autoflight/output/lat") == 0) { - setprop("/it-autoflight/input/lat", 3); - } -} - -setlistener("/autopilot/route-manager/current-wp", func { - setprop("/autopilot/internal/wp-change-time", getprop("/sim/time/elapsed-sec")); -}); - -var ap_various = func { - trueSpeedKts = getprop("/instrumentation/airspeed-indicator/true-speed-kt"); - if (trueSpeedKts > 420) { - setprop("/it-autoflight/internal/bank-limit", 15); - } else if (trueSpeedKts > 340) { - setprop("/it-autoflight/internal/bank-limit", 20); - } else { - setprop("/it-autoflight/internal/bank-limit", 25); - } - - if (getprop("/autopilot/route-manager/route/num") > 0 and getprop("/autopilot/route-manager/active") == 1) { - if ((getprop("/autopilot/route-manager/current-wp") + 1) < getprop("/autopilot/route-manager/route/num")) { - gnds_mps = getprop("/velocities/groundspeed-kt") * 0.5144444444444; - wp_fly_from = getprop("/autopilot/route-manager/current-wp"); - if (wp_fly_from < 0) { - wp_fly_from = 0; - } - current_course = getprop("/autopilot/route-manager/route/wp[" ~ wp_fly_from ~ "]/leg-bearing-true-deg"); - wp_fly_to = getprop("/autopilot/route-manager/current-wp") + 1; - if (wp_fly_to < 0) { - wp_fly_to = 0; - } - next_course = getprop("/autopilot/route-manager/route/wp[" ~ wp_fly_to ~ "]/leg-bearing-true-deg"); - max_bank_limit = getprop("/it-autoflight/internal/bank-limit"); - - delta_angle = math.abs(geo.normdeg180(current_course - next_course)); - max_bank = delta_angle * 1.5; - if (max_bank > max_bank_limit) { - max_bank = max_bank_limit; - } - radius = (gnds_mps * gnds_mps) / (9.81 * math.tan(max_bank / 57.2957795131)); - time = 0.64 * gnds_mps * delta_angle * 0.7 / (360 * math.tan(max_bank / 57.2957795131)); - delta_angle_rad = (180 - delta_angle) / 114.5915590262; - R = radius/math.sin(delta_angle_rad); - dist_coeff = delta_angle * -0.011111 + 2; - if (dist_coeff < 1) { - dist_coeff = 1; - } - turn_dist = math.cos(delta_angle_rad) * R * dist_coeff / 1852; - if (getprop("/gear/gear[0]/wow") == 1 and turn_dist < 1) { - turn_dist = 1; - } - setprop("/it-autoflight/internal/lnav-advance-nm", turn_dist); - if (getprop("/sim/time/elapsed-sec")-getprop("/autopilot/internal/wp-change-time") > 60) { - setprop("/autopilot/internal/wp-change-check-period", time); - } - - if (getprop("/autopilot/route-manager/wp/dist") <= turn_dist) { - setprop("/autopilot/route-manager/current-wp", getprop("/autopilot/route-manager/current-wp") + 1); - } - } - } - - if (getprop("/it-autoflight/output/ap1") == 1 or getprop("/it-autoflight/output/ap2") == 1) { - if (getprop("/controls/flight/aileron") > 0.2 or getprop("/controls/flight/aileron") < -0.2 or getprop("/controls/flight/elevator") > 0.2 or getprop("/controls/flight/elevator") < -0.2 or getprop("/controls/flight/rudder") > 0.2 or getprop("/controls/flight/rudder") < -0.2) { - setprop("/it-autoflight/input/ap1", 0); - setprop("/it-autoflight/input/ap2", 0); - libraries.apOff("hard", 0); - } - } -} - -var flch_on = func { - setprop("/it-autoflight/output/appr-armed", 0); - setprop("/it-autoflight/output/vert", 4); - thrustmodet.start(); -} -var alt_on = func { - setprop("/it-autoflight/output/appr-armed", 0); - setprop("/it-autoflight/output/vert", 0); - setprop("/it-autoflight/mode/vert", "ALT CAP"); - setprop("/it-autoflight/internal/max-vs", 500); - setprop("/it-autoflight/internal/min-vs", -500); - minmaxtimer.start(); -} - setlistener("/it-autoflight/input/kts-mach", func { - var ias = getprop("/it-autoflight/input/spd-kts") or 0; - var mach = getprop("/it-autoflight/input/spd-mach") or 0; - if (getprop("/it-autoflight/input/kts-mach") == 0) { - if (ias >= 100 and ias <= 350) { - setprop("/it-autoflight/input/spd-kts", math.round(ias, 1)); - } else if (ias < 100) { - setprop("/it-autoflight/input/spd-kts", 100); - } else if (ias > 350) { - setprop("/it-autoflight/input/spd-kts", 350); - } - } else if (getprop("/it-autoflight/input/kts-mach") == 1) { - if (mach >= 0.50 and mach <= 0.82) { - setprop("/it-autoflight/input/spd-mach", math.round(mach, 0.001)); - } else if (mach < 0.50) { - setprop("/it-autoflight/input/spd-mach", 0.50); - } else if (mach > 0.82) { - setprop("/it-autoflight/input/spd-mach", 0.82); - } + if (Input.ktsMach.getBoolValue()) { + ITAF.syncMach(); + } else { + ITAF.syncIAS(); } -}); +}, 0, 0); -# Takeoff Modes -# TOGA setlistener("/it-autoflight/input/toga", func { - if (getprop("/it-autoflight/input/toga") == 1) { - setprop("/it-autoflight/input/vert", 7); - vertical(); - setprop("/it-autoflight/output/loc-armed", 0); - setprop("/it-autoflight/output/appr-armed", 0); - setprop("/it-autoflight/input/toga", 0); - togasel(); + if (Input.toga.getBoolValue()) { + ITAF.takeoffGoAround(); + Input.toga.setBoolValue(0); } }); -var togasel = func { - if ((getprop("/gear/gear[1]/wow") == 0) and (getprop("/gear/gear[2]/wow") == 0)) { - var iasnow = math.round(getprop("/instrumentation/airspeed-indicator/indicated-speed-kt")); - setprop("/it-autoflight/input/spd-kts", iasnow); - setprop("/it-autoflight/input/kts-mach", 0); - setprop("/it-autoflight/mode/vert", "G/A CLB"); - setprop("/it-autoflight/input/lat", 3); - } else { - setprop("/it-autoflight/input/lat", 5); - lateral(); - setprop("/it-autoflight/mode/lat", "T/O"); - setprop("/it-autoflight/mode/vert", "T/O CLB"); - } -} - -setlistener("/it-autoflight/mode/vert", func { - var vertm = getprop("/it-autoflight/mode/vert"); - if (vertm == "T/O CLB") { - reduct.start(); - } else { - reduct.stop(); - } -}); - -var toga_reduc = func { - if (getprop("/instrumentation/altimeter/indicated-altitude-ft") >= getprop("/it-autoflight/settings/reduc-agl-ft") and getprop("/gear/gear[1]/wow") == 0 and getprop("/gear/gear[2]/wow") == 0) { - setprop("/it-autoflight/input/vert", 4); - } -} - -# Altitude Capture and FPA Timer Logic -setlistener("/it-autoflight/output/vert", func { - updateTimers(); -}); - -var updateTimers = func { - if (getprop("/it-autoflight/output/fd1") == 1 or getprop("/it-autoflight/output/fd2") == 1 or getprop("/it-autoflight/output/ap1") == 1 or getprop("/it-autoflight/output/ap2") == 1) { - var vertm = getprop("/it-autoflight/output/vert"); - if (vertm == 1) { - altcaptt.start(); - } else if (vertm == 4) { - altcaptt.start(); - } else if (vertm == 5) { - altcaptt.start(); - } else if (vertm == 7) { - altcaptt.start(); - } else if (vertm == 8) { - altcaptt.stop(); - } else if (vertm == 9) { - altcaptt.start(); +setlistener("/it-autoflight/input/lat", func { + Input.latTemp = Input.lat.getValue(); + Output.ap1Temp = Output.ap1.getBoolValue(); + Output.ap2Temp = Output.ap2.getBoolValue(); + Output.fd1Temp = Output.fd1.getBoolValue(); + Output.fd2Temp = Output.fd2.getBoolValue(); + if (!Gear.wow1.getBoolValue() and !Gear.wow2.getBoolValue()) { + if (Output.ap1Temp or Output.ap2Temp or Output.fd1Temp or Output.fd2Temp) { + ITAF.setLatMode(Input.latTemp); } else { - altcaptt.stop(); + ITAF.setLatMode(9); } } else { - altcaptt.start(); - } -} - -# Altitude Capture -var altcapt = func { - vsnow = getprop("/it-autoflight/internal/vert-speed-fpm"); - setprop("/it-autoflight/internal/captvs", math.round(abs(vsnow) / 5, 100)); - setprop("/it-autoflight/internal/captvsneg", -1 * math.round(abs(vsnow) / 5, 100)); - if ((getprop("/it-autoflight/output/fd1") == 1 or getprop("/it-autoflight/output/fd2") == 1 or getprop("/it-autoflight/output/ap1") == 1 or getprop("/it-autoflight/output/ap2") == 1) and getprop("/it-autoflight/output/vert") != 9) { - var calt = getprop("/instrumentation/altimeter/indicated-altitude-ft"); - var alt = getprop("/it-autoflight/internal/alt"); - var dif = calt - alt; - if (dif < getprop("/it-autoflight/internal/captvs") and dif > getprop("/it-autoflight/internal/captvsneg")) { - if (vsnow > 0 and dif < 0) { - setprop("/it-autoflight/input/vert", 3); - setprop("/it-autoflight/output/thr-mode", 0); - setprop("/it-autoflight/mode/thr", "THRUST"); - } else if (vsnow < 0 and dif > 0) { - setprop("/it-autoflight/input/vert", 3); - setprop("/it-autoflight/output/thr-mode", 0); - setprop("/it-autoflight/mode/thr", "THRUST"); - } - } - } - altinput = getprop("/it-autoflight/input/alt"); - setprop("/it-autoflight/internal/alt", altinput); -} - -# Min and Max Pitch Reset -var minmax = func { - var calt = getprop("/instrumentation/altimeter/indicated-altitude-ft"); - var alt = getprop("/it-autoflight/internal/alt"); - var dif = calt - alt; - if (dif < 50 and dif > -50) { - setprop("/it-autoflight/internal/max-vs", 500); - setprop("/it-autoflight/internal/min-vs", -500); - var vertmode = getprop("/it-autoflight/output/vert"); - if (vertmode == 1 or vertmode == 2 or vertmode == 4 or vertmode == 5 or vertmode == 6 or vertmode == 7) { - # Do not change the vertical mode because we are not trying to capture altitude. + if (Output.ap1Temp or Output.ap2Temp or Output.fd1Temp or Output.fd2Temp) { + ITAF.setLatArm(Input.latTemp); } else { - setprop("/it-autoflight/mode/vert", "ALT HLD"); + ITAF.setLatArm(0); } - minmaxtimer.stop(); - } -} - -# Thrust Mode Selector -var thrustmode = func { - var calt = getprop("/instrumentation/altimeter/indicated-altitude-ft"); - var alt = getprop("/it-autoflight/internal/alt"); - if (getprop("/it-autoflight/output/vert") == 4) { - if (calt < alt) { - setprop("/it-autoflight/output/thr-mode", 2); - setprop("/it-autoflight/mode/thr", " PITCH"); - setprop("/it-autoflight/mode/vert", "SPD CLB"); - } else if (calt > alt) { - setprop("/it-autoflight/output/thr-mode", 1); - setprop("/it-autoflight/mode/thr", " PITCH"); - setprop("/it-autoflight/mode/vert", "SPD DES"); - } else { - setprop("/it-autoflight/output/thr-mode", 0); - setprop("/it-autoflight/mode/thr", "THRUST"); - setprop("/it-autoflight/input/vert", 3); - } - } else if (getprop("/it-autoflight/output/vert") == 7) { - setprop("/it-autoflight/output/thr-mode", 2); - setprop("/it-autoflight/mode/thr", " PITCH"); - } else if (getprop("/it-autoflight/output/vert") == 8) { - thrustmodet.stop(); - } else { - setprop("/it-autoflight/output/thr-mode", 0); - setprop("/it-autoflight/mode/thr", "THRUST"); - thrustmodet.stop(); - } -} - -# ILS and Autoland -# LOC and G/S arming -setlistener("/it-autoflight/input/lat-arm", func { - check_arms(); -}); - -setlistener("/it-autoflight/output/loc-armed", func { - check_arms(); -}); - -setlistener("/it-autoflight/output/appr-armed", func { - check_arms(); -}); - -var check_arms = func { - if (getprop("/it-autoflight/input/lat-arm") or getprop("/it-autoflight/output/loc-armed") or getprop("/it-autoflight/output/appr-armed")) { - update_armst.start(); - } else { - update_armst.stop(); - } -} - -var update_arms = func { - if (getprop("/it-autoflight/input/lat-arm") == 1 and getprop("/autopilot/route-manager/route/num") > 0 and getprop("/autopilot/route-manager/active") == 1 and getprop("/position/gear-agl-ft") >= 30) { - make_lnav_active(); - } - if (getprop("/instrumentation/nav[0]/in-range") == 1) { - if (getprop("/it-autoflight/output/loc-armed")) { - locdefl = abs(getprop("/instrumentation/nav[0]/heading-needle-deflection-norm")); - if (locdefl < 0.95 and locdefl != 0 and getprop("/instrumentation/nav[0]/signal-quality-norm") > 0.99) { - make_loc_active(); - } else { - return 0; - } - } - if (getprop("/it-autoflight/output/appr-armed")) { - signal = getprop("/instrumentation/nav[0]/gs-needle-deflection-norm"); - if (((signal < 0 and signal >= -0.20) or (signal > 0 and signal <= 0.20)) and getprop("/it-autoflight/output/lat") == 2) { - make_appr_active(); - } else { - return 0; - } - } - } -} - -var make_lnav_active = func { - if (getprop("/it-autoflight/output/lat") != 1) { - alandt.stop(); - alandt1.stop(); - setprop("/it-autoflight/input/lat-arm", 0); - setprop("/it-autoflight/output/loc-armed", 0); - setprop("/it-autoflight/output/appr-armed", 0); - setprop("/it-autoflight/output/lat", 1); - setprop("/it-autoflight/mode/lat", "LNAV"); - setprop("/it-autoflight/mode/arm", " "); - setprop("/it-autoflight/custom/show-hdg", 0); - } -} - -var make_loc_active = func { - if (getprop("/it-autoflight/output/lat") != 2) { - setprop("/it-autoflight/input/lat-arm", 0); - setprop("/it-autoflight/output/loc-armed", 0); - setprop("/it-autoflight/output/lat", 2); - setprop("/it-autoflight/mode/lat", "LOC"); - setprop("/it-autoflight/custom/show-hdg", 0); - if (getprop("/it-autoflight/output/appr-armed") == 1) { - # Do nothing because G/S is armed - } else { - setprop("/it-autoflight/mode/arm", " "); - } - } -} - -var make_appr_active = func { - if (getprop("/it-autoflight/output/vert") != 2) { - mng_sys_stop(); - setprop("/it-autoflight/output/appr-armed", 0); - setprop("/it-autoflight/output/vert", 2); - setprop("/it-autoflight/mode/vert", "G/S"); - setprop("/it-autoflight/mode/arm", " "); - alandt.start(); - thrustmode(); - } -} - -# Autoland Stage 1 Logic (Land) -var aland = func { - if (getprop("/position/gear-agl-ft") <= 300) { - setprop("/it-autoflight/mode/vert", "LAND"); - } - if (getprop("/position/gear-agl-ft") <= 100) { - setprop("/it-autoflight/input/vert", 6); - } -} - -var aland1 = func { - var aglal = getprop("/position/gear-agl-ft"); - if (aglal <= 80 and aglal > 5) { - setprop("/it-autoflight/input/lat", 4); - } - if (aglal <= 50 and aglal > 5) { - setprop("/it-autoflight/mode/vert", "FLARE"); - } - var ap1 = getprop("/it-autoflight/output/ap1"); - var ap2 = getprop("/it-autoflight/output/ap2"); - if (aglal <= 18 and aglal > 5 and (ap1 or ap2)) { - thrustmodet.stop(); - setprop("/it-autoflight/output/thr-mode", 1); - setprop("/it-autoflight/mode/thr", "RETARD"); - } - gear1 = getprop("/gear/gear[1]/wow"); - gear2 = getprop("/gear/gear[2]/wow"); - if (gear1 == 1 or gear2 == 1) { - alandt1.stop(); - setprop("/it-autoflight/mode/lat", "RLOU"); - setprop("/it-autoflight/mode/vert", "ROLLOUT"); - } -} - -# Managed Climb/Descent -var mng_main = func { - if (getprop("/autopilot/route-manager/route/num") > 0 and getprop("/autopilot/route-manager/active") == 1) { - altinput = getprop("/it-autoflight/input/alt"); - setprop("/it-autoflight/internal/alt", altinput); - var wp_curr = getprop("/autopilot/route-manager/current-wp"); - var mng_alt_wp = getprop("/autopilot/route-manager/route/wp",wp_curr,"altitude-ft"); - if (getprop("/it-autoflight/internal/alt-const") == mng_alt_wp) { - # Do nothing - } else { - setprop("/it-autoflight/internal/alt-const", mng_alt_wp); - } - mng_alt_selector(); - if (getprop("/it-autoflight/internal/alt-const") < 100) { - setprop("/it-autoflight/input/vert", 4); - } - } else { - setprop("/it-autoflight/input/vert", 4); - } -} - -var mng_sys_stop = func { - mng_maint.stop(); - mng_altcaptt.stop(); - mng_minmaxt.stop(); - mng_des_fpmt.stop(); - mng_des_todt.stop(); - setprop("/it-autoflight/mode/mng", "NONE"); -} - -setlistener("/it-autoflight/input/alt", func { - if (getprop("/it-autoflight/output/vert") == 8) { - mng_alt_selector(); - mng_run(); } }); -setlistener("/it-autoflight/internal/alt-const", func { - if (getprop("/it-autoflight/output/vert") == 8) { - mng_alt_selector(); - mng_run(); +setlistener("/it-autoflight/input/vert", func { + if (!Gear.wow1.getBoolValue() and !Gear.wow2.getBoolValue() and (Output.ap1.getBoolValue() or Output.ap2.getBoolValue() or Output.fd1.getBoolValue() or Output.fd2.getBoolValue())) { + ITAF.setVertMode(Input.vert.getValue()); + } else { + ITAF.setVertMode(9); } }); -setlistener("/autopilot/route-manager/current-wp", func { - if (getprop("/it-autoflight/output/vert") == 8) { - mng_alt_selector(); - mng_run(); - } +setlistener("/sim/signals/fdm-initialized", func { + ITAF.init(); }); -var mng_run = func { - if (getprop("/autopilot/route-manager/route/num") > 0 and getprop("/autopilot/route-manager/active") == 1) { - var wp_curr = getprop("/autopilot/route-manager/current-wp"); - var wptnum = getprop("/autopilot/route-manager/current-wp"); - var mng_alt_wp = getprop("/autopilot/route-manager/route/wp",wp_curr,"altitude-ft"); - if ((wptnum - 1) < getprop("/autopilot/route-manager/route/num")) { - var mng_alt_wp_prev = getprop("/autopilot/route-manager/route/wp",wp_curr - 1,"altitude-ft"); - var altcurr = getprop("/instrumentation/altimeter/indicated-altitude-ft"); - if (mng_alt_wp_prev >= 100) { - if (mng_alt_wp_prev > mng_alt_wp) { - mng_des_todt.start(); - setprop("/it-autoflight/internal/mng-mode", "DES"); - } else if (mng_alt_wp_prev == mng_alt_wp) { - mng_des_todt.stop(); - setprop("/it-autoflight/internal/top-of-des-nm", 0); - setprop("/it-autoflight/internal/mng-mode", "XX"); - } else if (mng_alt_wp_prev <= mng_alt_wp) { - mng_des_todt.stop(); - setprop("/it-autoflight/internal/top-of-des-nm", 0); - setprop("/it-autoflight/internal/mng-mode", "CLB"); - } - } else if (mng_alt_wp_prev < 100) { - if (altcurr > mng_alt_wp) { - mng_des_todt.start(); - setprop("/it-autoflight/internal/mng-mode", "DES"); - } else if (altcurr == mng_alt_wp) { - mng_des_todt.stop(); - setprop("/it-autoflight/internal/top-of-des-nm", 0); - setprop("/it-autoflight/internal/mng-mode", "XX"); - } else if (altcurr <= mng_alt_wp) { - mng_des_todt.stop(); - setprop("/it-autoflight/internal/top-of-des-nm", 0); - setprop("/it-autoflight/internal/mng-mode", "CLB"); - } - } - } else { - mng_des_todt.stop(); - setprop("/it-autoflight/internal/top-of-des-nm", 0); - } - if (mng_alt_wp >= 100) { - if (getprop("/it-autoflight/internal/mng-mode") == "CLB") { - var calt = getprop("/instrumentation/altimeter/indicated-altitude-ft"); - var valt = getprop("/it-autoflight/internal/mng-alt"); - var vdif = calt - valt; - if (vdif > 250 or vdif < -250) { - mng_clb(); - } else { - mng_alt_sel(); - } - } else if (getprop("/it-autoflight/internal/mng-mode") == "DES") { - var calt = getprop("/instrumentation/altimeter/indicated-altitude-ft"); - var valt = getprop("/it-autoflight/internal/mng-alt"); - var vdif = calt - valt; - if (vdif > 250 or vdif < -250) { - mng_des_spd(); - } else { - mng_alt_sel(); - } - } else if (getprop("/it-autoflight/internal/mng-mode") == "XX") { - # Do nothing for now - } - } else { - setprop("/it-autoflight/input/vert", 4); - } - } else { - setprop("/it-autoflight/input/vert", 4); - } -} - -# Managed Top of Descent -var mng_des_tod = func { - if (getprop("/autopilot/route-manager/route/num") > 0 and getprop("/autopilot/route-manager/active") == 1) { - var wp_curr = getprop("/autopilot/route-manager/current-wp"); - var mng_alt_wp = getprop("/autopilot/route-manager/route/wp",wp_curr,"altitude-ft"); - var alt_curr = getprop("/instrumentation/altimeter/indicated-altitude-ft"); - var dist = getprop("/autopilot/route-manager/wp/dist"); - var vdist = dist + 1; - var alttl = abs(alt_curr - mng_alt_wp); - setprop("/it-autoflight/internal/top-of-des-nm", (alttl / 1000) * 3); - if (vdist < getprop("/it-autoflight/internal/top-of-des-nm")) { - mng_des_todt.stop(); - var salt = getprop("/it-autoflight/internal/alt"); - var valt = getprop("/it-autoflight/internal/alt-const"); - var calt = getprop("/instrumentation/altimeter/indicated-altitude-ft"); - var sdif = abs(calt - salt); - var vdif = abs(calt - valt); - if (sdif <= vdif) { - setprop("/it-autoflight/internal/mng-alt", getprop("/it-autoflight/internal/alt")); - } else if (sdif > vdif) { - setprop("/it-autoflight/internal/mng-alt", getprop("/it-autoflight/internal/alt-const")); - } - var calt = getprop("/instrumentation/altimeter/indicated-altitude-ft"); - var valt = getprop("/it-autoflight/internal/mng-alt"); - var vdif = calt - valt; - if (vdif > 550 or vdif < -550) { - mng_des_spd(); - } else { - mng_alt_sel(); - } - } - } -} - -# Managed Altitude Selector -var mng_alt_selector = func { - var salt = getprop("/it-autoflight/internal/alt"); - var valt = getprop("/it-autoflight/internal/alt-const"); - var calt = getprop("/instrumentation/altimeter/indicated-altitude-ft"); - var sdif = abs(calt - salt); - var vdif = abs(calt - valt); - if (getprop("/it-autoflight/internal/mng-mode") == "CLB") { - if (sdif <= vdif) { - setprop("/it-autoflight/internal/mng-alt", getprop("/it-autoflight/internal/alt")); - } else if (sdif > vdif) { - setprop("/it-autoflight/internal/mng-alt", getprop("/it-autoflight/internal/alt-const")); - } - } else if (getprop("/it-autoflight/internal/mng-mode") == "DES") { - var dist = getprop("/autopilot/route-manager/wp/dist"); - var vdist = dist - 1; - if (vdist < getprop("/it-autoflight/internal/top-of-des-nm")) { - if (sdif <= vdif) { - setprop("/it-autoflight/internal/mng-alt", getprop("/it-autoflight/internal/alt")); - } else if (sdif > vdif) { - setprop("/it-autoflight/internal/mng-alt", getprop("/it-autoflight/internal/alt-const")); - } - } - } -} - -# Managed Altitude -var mng_alt_sel = func { - setprop("/it-autoflight/internal/max-vs", 500); - setprop("/it-autoflight/internal/min-vs", -500); - setprop("/it-autoflight/output/thr-mode", 0); - setprop("/it-autoflight/output/vert-mng", 0); - setprop("/it-autoflight/mode/thr", "THRUST"); - setprop("/it-autoflight/mode/mng", "MNG CAP"); - mng_minmaxt.start(); -} - -# Managed Climb -var mng_clb = func { - mng_des_fpmt.stop(); - setprop("/it-autoflight/output/thr-mode", 2); - setprop("/it-autoflight/mode/thr", " PITCH"); - setprop("/it-autoflight/output/vert-mng", 4); - setprop("/it-autoflight/mode/vert", "MNG CLB"); - mng_altcaptt.start(); -} - -# Managed Descent -var mng_des_spd = func { - mng_des_fpmt.stop(); - setprop("/it-autoflight/output/thr-mode", 1); - setprop("/it-autoflight/mode/thr", " PITCH"); - setprop("/it-autoflight/output/mng-vert", 4); - setprop("/it-autoflight/mode/vert", "MNG DES"); - mng_altcaptt.start(); -} -var mng_des_pth = func { - mng_des_fpmt.start(); - setprop("/it-autoflight/output/thr-mode", 0); - setprop("/it-autoflight/mode/thr", "THRUST"); - setprop("/it-autoflight/output/mng-vert", 1); - setprop("/it-autoflight/mode/vert", "MNG DES"); - mng_altcaptt.start(); -} -var mng_des_fpm = func { - if (getprop("/autopilot/route-manager/route/num") > 0 and getprop("/autopilot/route-manager/active") == 1) { - var gndspd = getprop("/velocities/groundspeed-kt"); - var desfpm = ((gndspd * 0.5) * 10); - setprop("/it-autoflight/internal/mng-fpm", desfpm); - } -} - -# Managed Capture -var mng_altcapt = func { - vsnow = getprop("/it-autoflight/internal/vert-speed-fpm"); - setprop("/it-autoflight/internal/captvs", math.round(abs(vsnow) / 5, 100)); - setprop("/it-autoflight/internal/captvsneg", -1 * math.round(abs(vsnow) / 5, 100)); - var MNGalt = getprop("/it-autoflight/internal/mng-alt"); - var MCPalt = getprop("/it-autoflight/internal/alt"); - var MNGdif = abs(MNGalt - MCPalt); - if (MNGdif <= 20) { - var calt = getprop("/instrumentation/altimeter/indicated-altitude-ft"); - var alt = getprop("/it-autoflight/internal/alt"); - var dif = calt - alt; - if (dif < getprop("/it-autoflight/internal/captvs") and dif > getprop("/it-autoflight/internal/captvsneg")) { - if (vsnow > 0 and dif < 0) { - setprop("/it-autoflight/input/vert", 3); - setprop("/it-autoflight/output/thr-mode", 0); - } else if (vsnow < 0 and dif > 0) { - setprop("/it-autoflight/input/vert", 3); - setprop("/it-autoflight/output/thr-mode", 0); - } - } - } else { - var calt = getprop("/instrumentation/altimeter/indicated-altitude-ft"); - var valt = getprop("/it-autoflight/internal/mng-alt"); - var vdif = calt - valt; - if (vdif < getprop("/it-autoflight/internal/captvs") and vdif > getprop("/it-autoflight/internal/captvsneg")) { - if (vsnow > 0 and vdif < 0) { - mng_capture_alt(); - } else if (vsnow < 0 and vdif > 0) { - mng_capture_alt(); - } - } - - } -} - -var mng_capture_alt = func { - vsnow = getprop("/it-autoflight/internal/vert-speed-fpm"); - mng_altcaptt.stop(); - mng_des_fpmt.stop(); - var calt = getprop("/instrumentation/altimeter/indicated-altitude-ft"); - var alt = getprop("/it-autoflight/internal/alt"); - var valt = getprop("/it-autoflight/internal/mng-alt"); - if (calt < valt) { - setprop("/it-autoflight/internal/max-vs", vsnow); - } else if (calt > valt) { - setprop("/it-autoflight/internal/min-vs", vsnow); - } - mng_minmaxt.start(); - setprop("/it-autoflight/output/thr-mode", 0); - setprop("/it-autoflight/output/vert-mng", 0); - setprop("/it-autoflight/mode/thr", "THRUST"); - setprop("/it-autoflight/mode/vert", "MNG CAP"); -} - -# Managed Min and Max Pitch Reset -var mng_minmax = func { - var calt = getprop("/instrumentation/altimeter/indicated-altitude-ft"); - var valt = getprop("/it-autoflight/internal/mng-alt"); - var vdif = calt - valt; - if (vdif < 50 and vdif > -50) { - setprop("/it-autoflight/internal/max-vs", 500); - setprop("/it-autoflight/internal/min-vs", -500); - var vertmode = getprop("/it-autoflight/output/vert-mng"); - if (vertmode == 0) { - setprop("/it-autoflight/mode/vert", "MNG HLD"); - } - mng_minmaxt.stop(); - } -} - # For Canvas Nav Display. setlistener("/it-autoflight/input/hdg", func { setprop("/autopilot/settings/heading-bug-deg", getprop("/it-autoflight/input/hdg")); @@ -1335,17 +1012,5 @@ setlistener("/it-autoflight/internal/alt", func { setprop("/autopilot/settings/target-altitude-ft", getprop("/it-autoflight/internal/alt")); }); -# Timers -var update_armst = maketimer(0.5, update_arms); -var altcaptt = maketimer(0.5, altcapt); -var thrustmodet = maketimer(0.5, thrustmode); -var minmaxtimer = maketimer(0.5, minmax); -var alandt = maketimer(0.5, aland); -var alandt1 = maketimer(0.5, aland1); -var reduct = maketimer(0.5, toga_reduc); -var ap_varioust = maketimer(1, ap_various); -var mng_maint = maketimer(0.5, mng_main); -var mng_altcaptt = maketimer(0.5, mng_altcapt); -var mng_minmaxt = maketimer(0.5, mng_minmax); -var mng_des_fpmt = maketimer(0.5, mng_des_fpm); -var mng_des_todt = maketimer(0.5, mng_des_tod); +var loopTimer = maketimer(0.1, ITAF, ITAF.loop); +var slowLoopTimer = maketimer(1, ITAF, ITAF.slowLoop); diff --git a/Nasal/FMGC-c.nas b/Nasal/FMGC-c.nas index 67e33d8c..56d34534 100644 --- a/Nasal/FMGC-c.nas +++ b/Nasal/FMGC-c.nas @@ -33,7 +33,7 @@ setprop("/modes/pfd/fma/roll-mode-armed-time", 0); setprop("/modes/pfd/fma/ap-mode-time", 0); setprop("/modes/pfd/fma/fd-mode-time", 0); setprop("/modes/pfd/fma/athr-mode-time", 0); -setprop("/modes/fcu/hdg-time", 0); +setprop("/modes/fcu/hdg-time", -45); setlistener("sim/signals/fdm-initialized", func { loopFMA.start(); @@ -126,20 +126,17 @@ var loopFMA = maketimer(0.05, func { if (((state1 == "TOGA" or state2 == "TOGA") or (flx == 1 and (state1 == "MCT" or state2 == "MCT")) or (flx == 1 and ((state1 == "MAN THR" and thr1 >= 0.83) or (state2 == "MAN THR" and thr2 >= 0.83)))) and (engstate1 == 3 or engstate2 == 3)) { # RWY Engagement would go here, but automatic ILS selection is not simulated yet. if (wow and getprop("/FMGC/internal/v2-set") == 1 and getprop("/it-autoflight/output/vert") != 7) { - setprop("/it-autoflight/input/vert", 7); + ITAF.setVertMode(7); setprop("/it-autoflight/mode/vert", "T/O CLB"); - fmgc.vertical(); } } else { var gear1 = getprop("/gear/gear[1]/wow"); var gear2 = getprop("/gear/gear[2]/wow"); if (getprop("/it-autoflight/input/lat") == 5 and (gear1 or gear2)) { - setprop("/it-autoflight/input/lat", 9); - fmgc.lateral(); + ITAF.setLatMode(9); } if (getprop("/it-autoflight/output/vert") == 7 and (gear1 or gear2)) { - setprop("/it-autoflight/input/vert", 9); - fmgc.vertical(); + ITAF.setVertMode(9); } } @@ -201,13 +198,6 @@ var loopFMA = maketimer(0.05, func { } else { setprop("/modes/pfd/fma/pitch-mode2-armed-box", 0); } - - # Preselect HDG - if (getprop("/modes/fcu/hdg-time") + 45 >= elapsedtime) { - setprop("/it-autoflight/custom/show-hdg", 1); - } else if (getprop("/it-autoflight/output/lat") != 0 and getprop("/it-autoflight/output/lat") != 5 and getprop("/it-autoflight/output/lat") != 9) { - setprop("/it-autoflight/custom/show-hdg", 0); - } }); var loopFMA_b = func { @@ -344,34 +334,6 @@ setlistener("/it-autoflight/mode/vert", func { if (newvertarm != "ALT") { setprop("/modes/pfd/fma/pitch-mode2-armed", "ALT"); } - } else if (vert == "MNG HLD") { - if (newvert != "ALT") { - setprop("/modes/pfd/fma/pitch-mode", "ALT"); - } - if (newvertarm != " ") { - setprop("/modes/pfd/fma/pitch-mode2-armed", " "); - } - } else if (vert == "MNG CAP") { - if (newvert != "ALT*") { - setprop("/modes/pfd/fma/pitch-mode", "ALT*"); - } - if (newvertarm != " ") { - setprop("/modes/pfd/fma/pitch-mode2-armed", " "); - } - } else if (vert == "MNG CLB") { - if (newvert != "CLB") { - setprop("/modes/pfd/fma/pitch-mode", "CLB"); - } - if (newvertarm != "ALT") { - setprop("/modes/pfd/fma/pitch-mode2-armed", "ALT"); - } - } else if (vert == "MNG DES") { - if (newvert != "DES") { - setprop("/modes/pfd/fma/pitch-mode", "DES"); - } - if (newvertarm != "ALT") { - setprop("/modes/pfd/fma/pitch-mode2-armed", "ALT"); - } } else if (vert == " ") { if (newvert != " ") { setprop("/modes/pfd/fma/pitch-mode", " "); @@ -532,21 +494,7 @@ var at = func { } var boxchk = func { - var ap1 = getprop("/it-autoflight/output/ap1"); - var ap2 = getprop("/it-autoflight/output/ap2"); - var fd1 = getprop("/it-autoflight/output/fd1"); - var fd2 = getprop("/it-autoflight/output/fd2"); - var fma_pwr = getprop("/it-autoflight/output/fma-pwr"); - if (ap1 and !ap2 and !fd1 and !fd2 and !fma_pwr) { - setprop("/it-autoflight/input/lat", 3); - boxchk_b(); - } else if (!ap1 and ap2 and !fd1 and !fd2 and !fma_pwr) { - setprop("/it-autoflight/input/lat", 3); - boxchk_b(); - } else if (!ap1 and !ap2 and fd1 and !fd2 and !fma_pwr) { - setprop("/it-autoflight/input/lat", 3); - boxchk_b(); - } else if (!ap1 and !ap2 and !fd1 and fd2 and !fma_pwr) { + if ((getprop("/it-autoflight/output/ap1") or getprop("/it-autoflight/output/ap2") or getprop("/it-autoflight/output/fd1") or getprop("/it-autoflight/output/fd2")) and getprop("/it-autoflight/output/fma-pwr") == 0) { setprop("/it-autoflight/input/lat", 3); boxchk_b(); } diff --git a/Nasal/FMGC.nas b/Nasal/FMGC.nas index 6091cf5b..fc244a85 100644 --- a/Nasal/FMGC.nas +++ b/Nasal/FMGC.nas @@ -300,7 +300,7 @@ var reset_FMGC = func { spd = getprop("/it-autoflight/input/spd-kts"); hdg = getprop("/it-autoflight/input/hdg"); alt = getprop("/it-autoflight/input/alt"); - APinit(); + ITAF.init(); FMGCinit(); mcdu.MCDU_reset(0); mcdu.MCDU_reset(1); @@ -353,9 +353,6 @@ var various2 = maketimer(0.5, func { nav3(); adf0(); adf1(); - if (getprop("/it-autoflight/output/lat") == 0) { - setprop("/it-autoflight/custom/show-hdg", 1); - } }); var nav0 = func { diff --git a/Nasal/buttons.nas b/Nasal/buttons.nas index ec7f3412..89119ef0 100644 --- a/Nasal/buttons.nas +++ b/Nasal/buttons.nas @@ -52,7 +52,7 @@ var variousReset = func { setprop("/controls/lighting/DU/du6", 1); setprop("/controls/lighting/DU/mcdu1", 1); setprop("/controls/lighting/DU/mcdu2", 1); - setprop("/modes/fcu/hdg-time", 0); + setprop("/modes/fcu/hdg-time", -45); setprop("/controls/switching/ATTHDG", 0); setprop("/controls/switching/AIRDATA", 0); setprop("/controls/switches/no-smoking-sign", 1); @@ -358,10 +358,8 @@ var APPanel = { if (fd1.getBoolValue() or fd2.getBoolValue() or ap1.getBoolValue() or ap2.getBoolValue()) { if (latMode.getValue() == 0 or !showHDG.getBoolValue()) { setprop("/it-autoflight/input/lat", 3); - showHDG.setBoolValue(1); } else { setprop("/it-autoflight/input/lat", 0); - showHDG.setBoolValue(1); } } } @@ -399,26 +397,26 @@ var APPanel = { if (latMode.getValue() == 2) { setprop("/it-autoflight/input/lat", 0); } else { - fmgc.disarmLOC(); + fmgc.ITAF.disarmLOC(); } if (vertTemp == 2 or vertTemp == 6) { me.VSPull(); } else { - fmgc.disarmGS(); + fmgc.ITAF.disarmGS(); } } else { setprop("/it-autoflight/input/lat", 2); if (vertTemp == 2 or vertTemp == 6) { me.VSPull(); } else { - fmgc.disarmGS(); + fmgc.ITAF.disarmGS(); } } } }, TRKFPA: func() { if (dcEss.getValue() >= 25) { - fmgc.toggle_trkfpa(); + fmgc.ITAF.toggleTrkFpa(); } }, ALTPush: func() { @@ -535,12 +533,12 @@ var APPanel = { if (latMode.getValue() == 2) { setprop("/it-autoflight/input/lat", 0); } else { - fmgc.disarmLOC(); + fmgc.ITAF.disarmLOC(); } if (vertTemp == 2 or vertTemp == 6) { me.VSPull(); } else { - fmgc.disarmGS(); + fmgc.ITAF.disarmGS(); } } else { setprop("/it-autoflight/input/vert", 2); diff --git a/Nasal/libraries.nas b/Nasal/libraries.nas index 00aac91c..c70d1426 100644 --- a/Nasal/libraries.nas +++ b/Nasal/libraries.nas @@ -192,7 +192,7 @@ var systemsInit = func { systems.fire_init(); systems.autobrake_init(); fadec.FADEC.init(); - fmgc.APinit(); + fmgc.ITAF.init(); fmgc.FMGCinit(); mcdu.MCDU_init(0); mcdu.MCDU_init(1); @@ -235,10 +235,6 @@ var systemsLoop = maketimer(0.1, func { setprop("/systems/shake/effect", 0); } - if (getprop("/it-autoflight/custom/show-hdg") == 0 and getprop("/it-autoflight/output/lat") != 4) { - setprop("/it-autoflight/input/hdg", math.round(getprop("/instrumentation/pfd/heading-scale"))); - } - if (getprop("/instrumentation/mk-viii/inputs/discretes/momentary-flap-all-override") == 1 or (getprop("/instrumentation/mk-viii/inputs/discretes/momentary-flap-3-override") == 1 and getprop("/controls/flight/flap-pos") >= 4)) { setprop("/instrumentation/mk-viii/inputs/discretes/momentary-flap-override", 1); } else { From 9627d9c0224c2291eabed14e095c5e614f61c405 Mon Sep 17 00:00:00 2001 From: Joshua Davidson Date: Thu, 14 Mar 2019 13:58:02 -0400 Subject: [PATCH 2/7] System: Fix some A/P stuff --- Systems/fmgc-drivers.xml | 10 ------ Systems/fmgc-pitch.xml | 70 +++++----------------------------------- 2 files changed, 8 insertions(+), 72 deletions(-) diff --git a/Systems/fmgc-drivers.xml b/Systems/fmgc-drivers.xml index c0e32d8b..f65e1c70 100644 --- a/Systems/fmgc-drivers.xml +++ b/Systems/fmgc-drivers.xml @@ -570,16 +570,6 @@ /it-autoflight/output/vert 7 - - - /it-autoflight/output/vert - 8 - - - /it-autoflight/output/vert-mng - 4 - - diff --git a/Systems/fmgc-pitch.xml b/Systems/fmgc-pitch.xml index e7675c61..0d5d3ea4 100644 --- a/Systems/fmgc-pitch.xml +++ b/Systems/fmgc-pitch.xml @@ -143,16 +143,6 @@ /it-autoflight/output/vert 7 - - - /it-autoflight/output/vert - 8 - - - /it-autoflight/output/vert-mng - 4 - - @@ -352,22 +342,10 @@ 1.0 - - - /it-autoflight/output/vert - 0 - - - - /it-autoflight/output/vert - 8 - - - /it-autoflight/output/vert-mng - 0 - - - + + /it-autoflight/output/vert + 0 + /it-autoflight/internal/target-fpm @@ -391,16 +369,6 @@ /it-autoflight/output/vert 7 - - - /it-autoflight/output/vert - 8 - - - /it-autoflight/output/vert-mng - 4 - - /it-autoflight/internal/target-fpm-b @@ -501,22 +469,10 @@ /it-autoflight/internal/vert-speed-fpm - - - /it-autoflight/output/vert - 0 - - - - /it-autoflight/output/vert - 8 - - - /it-autoflight/output/vert-mng - 0 - - - + + /it-autoflight/output/vert + 0 + /it-autoflight/internal/target-fpm @@ -540,16 +496,6 @@ /it-autoflight/output/vert 7 - - - /it-autoflight/output/vert - 8 - - - /it-autoflight/output/vert-mng - 4 - - /it-autoflight/internal/target-fpm-b From 75f14241880bd3f932e78b084f4e42c2d453c67a Mon Sep 17 00:00:00 2001 From: Joshua Davidson Date: Thu, 14 Mar 2019 17:07:44 -0400 Subject: [PATCH 3/7] Control: Fix autoland THR IDLE --- Systems/fadec-cfm.xml | 28 ++++++++++++++++++++-------- Systems/fadec-iae.xml | 28 ++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/Systems/fadec-cfm.xml b/Systems/fadec-cfm.xml index b782ba9c..91367a77 100644 --- a/Systems/fadec-cfm.xml +++ b/Systems/fadec-cfm.xml @@ -382,10 +382,16 @@ MAN - - /it-autoflight/output/thr-mode - 0 - + + + /it-autoflight/output/thr-mode + 0 + + + /it-autoflight/mode/thr + RETARD + + 0.3 @@ -594,10 +600,16 @@ MAN - - /it-autoflight/output/thr-mode - 0 - + + + /it-autoflight/output/thr-mode + 0 + + + /it-autoflight/mode/thr + RETARD + + 0.3 diff --git a/Systems/fadec-iae.xml b/Systems/fadec-iae.xml index 3cbf412d..c5a85bc6 100644 --- a/Systems/fadec-iae.xml +++ b/Systems/fadec-iae.xml @@ -670,10 +670,16 @@ MAN - - /it-autoflight/output/thr-mode - 0 - + + + /it-autoflight/output/thr-mode + 0 + + + /it-autoflight/mode/thr + RETARD + + 0.3 @@ -882,10 +888,16 @@ MAN - - /it-autoflight/output/thr-mode - 0 - + + + /it-autoflight/output/thr-mode + 0 + + + /it-autoflight/mode/thr + RETARD + + 0.3 From 9578f7d5c162c03a50b92c37aa48b89089aadf69 Mon Sep 17 00:00:00 2001 From: Joshua Davidson Date: Thu, 14 Mar 2019 17:08:04 -0400 Subject: [PATCH 4/7] Sim: Revision --- revision.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/revision.txt b/revision.txt index b26d92eb..81bbe608 100644 --- a/revision.txt +++ b/revision.txt @@ -1 +1 @@ -4779 \ No newline at end of file +4780 \ No newline at end of file From 8d2e4b97fe076d65ca62864fec9995ce8c20efc2 Mon Sep 17 00:00:00 2001 From: Joshua Davidson Date: Thu, 14 Mar 2019 17:11:08 -0400 Subject: [PATCH 5/7] Revert "Fix engine state property" This reverts commit 919b697e25b83fd2d22f58438cb0a8d4384ff678. --- Nasal/engines-cfm.nas | 4 ++-- Nasal/engines-iae.nas | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Nasal/engines-cfm.nas b/Nasal/engines-cfm.nas index 386fa3b5..b025beea 100644 --- a/Nasal/engines-cfm.nas +++ b/Nasal/engines-cfm.nas @@ -256,7 +256,7 @@ var eng_one_n2_check = maketimer(0.5, func { setprop("/controls/engines/engine[0]/igniter-b", 0); } } - if (getprop("/engines/engine[0]/n2-actual") >= 61.1) { + if (getprop("/engines/engine[0]/n2-actual") >= 57.0) { eng_one_n2_check.stop(); setprop("/systems/pneumatic/eng1-starter", 0); setprop("/engines/engine[0]/state", 3); @@ -319,7 +319,7 @@ var eng_two_n2_check = maketimer(0.5, func { setprop("/controls/engines/engine[1]/igniter-b", 0); } } - if (getprop("/engines/engine[1]/n2-actual") >= 61.1) { + if (getprop("/engines/engine[1]/n2-actual") >= 57.0) { eng_two_n2_check.stop(); setprop("/systems/pneumatic/eng2-starter", 0); setprop("/engines/engine[1]/state", 3); diff --git a/Nasal/engines-iae.nas b/Nasal/engines-iae.nas index 466a9caf..eeecd296 100644 --- a/Nasal/engines-iae.nas +++ b/Nasal/engines-iae.nas @@ -256,7 +256,7 @@ var eng_one_n2_check = maketimer(0.5, func { setprop("/controls/engines/engine[0]/igniter-b", 0); } } - if (getprop("/engines/engine[0]/n2-actual") >= 60.8) { + if (getprop("/engines/engine[0]/n2-actual") >= 57.0) { eng_one_n2_check.stop(); setprop("/systems/pneumatic/eng1-starter", 0); setprop("/engines/engine[0]/state", 3); @@ -319,7 +319,7 @@ var eng_two_n2_check = maketimer(0.5, func { setprop("/controls/engines/engine[1]/igniter-b", 0); } } - if (getprop("/engines/engine[1]/n2-actual") >= 60.8) { + if (getprop("/engines/engine[1]/n2-actual") >= 57.0) { eng_two_n2_check.stop(); setprop("/systems/pneumatic/eng2-starter", 0); setprop("/engines/engine[1]/state", 3); From 4239da97550450144e607165dd6473e907c1a99f Mon Sep 17 00:00:00 2001 From: Joshua Davidson Date: Thu, 14 Mar 2019 17:30:37 -0400 Subject: [PATCH 6/7] System: Fix something --- Systems/fmgc-drivers.xml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Systems/fmgc-drivers.xml b/Systems/fmgc-drivers.xml index f65e1c70..95abc031 100644 --- a/Systems/fmgc-drivers.xml +++ b/Systems/fmgc-drivers.xml @@ -506,6 +506,16 @@ + + + /it-autoflight/output/ap1 + 1 + + + /it-autoflight/output/ap2 + 1 + + /it-autoflight/output/vert 1 From 69374a53b37264459b5ec59ae0e0fe7cb77da73c Mon Sep 17 00:00:00 2001 From: Joshua Davidson Date: Thu, 14 Mar 2019 17:32:15 -0400 Subject: [PATCH 7/7] System: Fix something else --- Systems/fmgc-roll-yaw.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Systems/fmgc-roll-yaw.xml b/Systems/fmgc-roll-yaw.xml index ad641450..93df681b 100644 --- a/Systems/fmgc-roll-yaw.xml +++ b/Systems/fmgc-roll-yaw.xml @@ -284,7 +284,7 @@ 0 /fdm/jsbsim/fbw/fmgc/yaw-cmd - 0.6 + 1.2