flightdirector updates
[fg:toms-fgdata.git] / Aircraft / Citation-Bravo / Nasal / flightdirector.nas
1 #############################################################################
2 # Flight Director/Autopilot controller.
3 # Syd Adams
4 # HDG:
5 # Heading Bug hold - Low Bank can be selected
6 #
7 # NAV:
8 # Arm & Capture VOR , LOC or FMS
9 #
10 # APR : (ILS approach)
11 # Arm & Capture VOR APR , LOC or BC
12 # Also arm and capture GS
13 #
14 # BC :
15 # Arm & capture localizer backcourse
16 # Nav also illuminates
17 #
18 # VNAV:
19 # Arm and capture VOR/DME or FMS vertical profile
20 # profile entered in MFD VNAV menu
21 #
22 # ALT:
23 # Hold current Altitude or PFD preset altitude
24 #
25 # VS:
26 # Hold current vertical speed
27 # adjustable with pitch wheel
28 #
29 # SPD :
30 # Hold current speed 
31 # adjustable with pitch wheel
32 #
33 #############################################################################
34
35 # lnav 
36 #0=W-LVL
37 #1=HDG,
38 #2=Nav Arm
39 #3=Nav Cap
40 #4=ARP Arm
41 #5=APR Cap,
42 #6=BC Arm
43 #7=BC Cap
44
45 # vnav
46 #- 0=PIT
47 # 1=VNAV Arm,
48 #2=VNAV Cap,
49 #3=ALT Arm
50 #4=ALT Cap
51 #5=VS
52 #6=GS
53
54 var lnav_text=["wing-leveler","dg-heading-hold","dg-heading-hold","nav1-hold","dg-heading-hold","nav1-hold","dg-heading-hold","nav1-hold"];
55 var vnav_text=["pitch-hold","pitch-hold","pitch-hold","pitch-hold","altitude-hold","vertical-speed-hold","gs1-hold"];
56
57 var FMS = 0;
58 var in_range=0;
59 var Defl = props.globals.getNode("/instrumentation/nav/heading-needle-deflection");
60 var GSDefl = props.globals.getNode("/instrumentation/nav/gs-needle-deflection");
61 var AP_hdg = props.globals.getNode("/autopilot/locks/heading",1);
62 var AP_alt = props.globals.getNode("/autopilot/locks/altitude",1);
63 var AP_spd = props.globals.getNode("/autopilot/locks/speed",1);
64 var FD_lnav = props.globals.getNode("/instrumentation/flightdirector/lnav");
65 var FD_vnav = props.globals.getNode("/instrumentation/flightdirector/vnav");
66 var FD_pitch = props.globals.getNode("/instrumentation/flightdirector/Pitch",1);
67 var FD_roll = props.globals.getNode("/instrumentation/flightdirector/Roll",1);
68 var FD_speed = props.globals.getNode("/instrumentation/flightdirector/spd",1);
69 var DH = props.globals.getNode("/autopilot/route-manager/min-lock-altitude-agl-ft",1);
70 var lnav=0;
71 var vnav=0;
72
73
74 setlistener("/sim/signals/fdm-initialized", func {
75     AP_spd.setValue("");
76     AP_hdg.setValue("wing-leveler");
77     AP_alt.setValue("pitch-hold");
78     FD_lnav.setValue(0);
79     FD_vnav.setValue(0);
80     setprop("/autopilot/locks/passive-mode",1);
81     setprop("autopilot/settings/target-altitude-ft",0);
82     props.globals.getNode("instrumentation/primus1000/dc550/fms",1).setBoolValue(0);
83     FMS=0;
84     settimer(update, 5);
85     print("Flight Director ...Check");
86 });
87
88 ####    FD Controller inputs    ####
89
90 setlistener("/instrumentation/flightdirector/lnav", func(ln){
91 lnav = ln.getValue();
92 set_lateral_mode();
93 },0,0);
94
95 setlistener("/instrumentation/flightdirector/vnav", func(vn){
96 vnav=vn.getValue();
97 set_vertical_mode();
98 },0,0);
99
100 var set_lateral_mode=func(){
101 if(FMS ==1){
102     if(lnav ==2 or lnav ==3)AP_hdg.setValue("true-heading-hold");
103     }else{
104         AP_hdg.setValue(lnav_text[lnav]);
105         }
106 }
107
108 var set_vertical_mode=func(){
109 AP_alt.setValue(vnav_text[vnav]);
110 }
111
112 setlistener("/instrumentation/primus1000/dc550/fms", func(fms){
113     if(fms.getBoolValue()){
114         FMS=1;
115         }else{
116         FMS=0;
117         FD_lnav.setValue(0);
118         FD_vnav.setValue(0);
119         }
120 },0,0);
121
122
123 ####    update nav gps or nav setting    ####
124
125 var update = func {
126 var dst = getprop("instrumentation/nav/nav-distance");
127     if(dst == nil or dst >30000){
128     in_range=0;
129     }else{
130     in_range=1;
131     }
132     var APoff = getprop("/autopilot/locks/passive-mode");
133     if(APoff == 0){
134     var maxroll = getprop("/orientation/roll-deg");
135     var maxpitch = getprop("/orientation/pitch-deg");
136     if(maxroll > 45 or maxroll < -45)APoff = 1;
137     if(maxpitch > 45 or maxpitch < -45)APoff = 1;
138     if(getprop("/position/altitude-agl-ft") < DH.getValue())APoff = 1;
139     setprop("/autopilot/locks/passive-mode", APoff);
140 }
141
142
143 if(FMS==0){
144     var deflection = Defl.getValue();
145     var gs_deflection = GSDefl.getValue();
146     if(lnav ==2){
147         if(deflection > -7 or deflection < 7){
148             if(in_range==1){
149             lnav = 3;
150             }
151         }
152     FD_lnav.setValue(lnav);
153     }
154
155     if(lnav ==4){
156         if(getprop("/instrumentation/nav/nav-loc")==0){
157             lnav = 2;
158         }else{
159         if(deflection > -5 or deflection < 5){
160                 if(in_range==1)lnav = 5;
161                 }
162             }
163     FD_lnav.setValue(lnav);
164     }
165
166     if(lnav ==5){
167         if(getprop("/instrumentation/nav/has-gs")!=0){
168             if(gs_deflection  < 0.5 and gs_deflection > -1.0){
169                 if(in_range==1)vnav = 6;
170                 FD_vnav.setValue(vnav);
171                 }
172             }
173         }
174     }
175
176
177 if(vnav == 3){
178 var TGALT = getprop("autopilot/settings/target-altitude-ft");
179     if(TGALT > DH.getValue()){
180         var MyAlt = getprop("position/altitude-ft");
181             if(MyAlt > (TGALT -1000) or MyAlt < (TGALT +1000)){
182                 vnav=4;
183                 AP_alt.setValue(vnav_text[vnav]);
184             }
185         }
186     }
187
188
189 settimer(update, 0); 
190 }