nav_scripts.nas 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. # Fairchild A-10 radio and navigation system
  2. # Alexis BORY < xiii at g2ms dot com > -- Public Domain
  3. # ILS: nav[0]
  4. # -----------
  5. var ils_freq = props.globals.getNode("instrumentation/nav[0]/frequencies");
  6. var ils_freq_sel = ils_freq.getNode("selected-mhz", 1);
  7. var ils_freq_sel_fmt = ils_freq.getNode("selected-mhz-fmt", 1);
  8. var ils_btn = props.globals.getNode("sim/model/A-10/A-10-nav/selector-ils");
  9. # update selected-mhz with translated decimals
  10. var nav0_freq_update = func {
  11. var test = getprop("instrumentation/nav[0]/frequencies/selected-mhz");
  12. if (! test) {
  13. setprop("sim/model/A-10/instrumentation/nav[0]/frequencies/freq-whole", 0);
  14. } else {
  15. setprop("sim/model/A-10/instrumentation/nav[0]/frequencies/freq-whole", test * 100);
  16. }
  17. }
  18. # TACAN: nav[1]
  19. # -------------
  20. var nav1_back = 0;
  21. setlistener( "instrumentation/tacan/switch-position", func {nav1_freq_update();} );
  22. var tc = props.globals.getNode("instrumentation/tacan/");
  23. var tc_sw_pos = tc.getNode("switch-position");
  24. var tc_freq = tc.getNode("frequencies");
  25. var tc_true_hdg = props.globals.getNode("instrumentation/tacan/indicated-bearing-true-deg");
  26. var tc_mag_hdg = props.globals.getNode("sim/model/A-10/instrumentation/tacan/indicated-bearing-mag-deg");
  27. var tcn_btn = props.globals.getNode("instrumentation/tacan/switch-position");
  28. var heading_offset = props.globals.getNode("instrumentation/heading-indicator-fg/offset-deg");
  29. var tcn_ident = props.globals.getNode("instrumentation/tacan/ident");
  30. var vtc_ident = props.globals.getNode("instrumentation/nav[1]/nav-id");
  31. var from_flag = props.globals.getNode("sim/model/A-10/instrumentation/cdi/from-flag");
  32. var to_flag = props.globals.getNode("sim/model/A-10/instrumentation/cdi/to-flag");
  33. var cdi_deflection = props.globals.getNode("sim/model/A-10/instrumentation/cdi/needle-deflection");
  34. var vtc_from_flag = props.globals.getNode("instrumentation/nav[1]/from-flag");
  35. var vtc_to_flag = props.globals.getNode("instrumentation/nav[1]/to-flag");
  36. var vtc_deflection = props.globals.getNode("instrumentation/nav[1]/heading-needle-deflection");
  37. var course_radial = props.globals.getNode("instrumentation/nav[1]/radials/selected-deg");
  38. var tacan_offset_apply = func {
  39. tcn = tcn_btn.getValue();
  40. var hdg_offset = heading_offset.getValue();
  41. var true_hdg = tc_true_hdg.getValue();
  42. if ( true_hdg and ( tcn == 1) ) {
  43. var new_mag_hdg = hdg_offset + true_hdg;
  44. tc_mag_hdg.setDoubleValue( geo.normdeg( new_mag_hdg ) );
  45. } else {
  46. tc_mag_hdg.setDoubleValue( 0 );
  47. }
  48. }
  49. var nav1_freq_update = func {
  50. if ( tc_sw_pos.getValue() == 1 ) {
  51. #print("nav1_freq_updat etc_sw_pos = 1");
  52. var tacan_freq = getprop( "instrumentation/tacan/frequencies/selected-mhz" );
  53. var nav1_freq = getprop( "instrumentation/nav[1]/frequencies/selected-mhz" );
  54. var nav1_back = nav1_freq;
  55. setprop("instrumentation/nav[1]/frequencies/selected-mhz", tacan_freq);
  56. } else {
  57. setprop("instrumentation/nav[1]/frequencies/selected-mhz", nav1_back);
  58. }
  59. }
  60. var tacan_XYtoggle = func {
  61. var xy_sign = tc_freq.getNode("selected-channel[4]");
  62. var s = xy_sign.getValue();
  63. if ( s == "X" ) {
  64. xy_sign.setValue( "Y" );
  65. } else {
  66. xy_sign.setValue( "X" );
  67. }
  68. }
  69. var tacan_tenth_adjust = func {
  70. var tenths = getprop( "instrumentation/tacan/frequencies/selected-channel[2]" );
  71. var hundreds = getprop( "instrumentation/tacan/frequencies/selected-channel[1]" );
  72. var value = (10 * tenths) + (100 * hundreds);
  73. var adjust = arg[0];
  74. var new_value = value + adjust;
  75. var new_hundreds = int(new_value/100);
  76. var new_tenths = (new_value - (new_hundreds*100))/10;
  77. setprop( "instrumentation/tacan/frequencies/selected-channel[1]", new_hundreds );
  78. setprop( "instrumentation/tacan/frequencies/selected-channel[2]", new_tenths );
  79. }
  80. # TACAN on HSI's Course Deviation Indicator
  81. # -----------------------------------------------------
  82. # CDI works with ils OR tacan OR vortac (which freq is tuned from the tacan panel)
  83. var compas_card_dev_indicator = func {
  84. var tcn = tcn_btn.getValue();
  85. if ( tcn ) {
  86. var tcnid = tcn_ident.getValue();
  87. var vtcid = vtc_ident.getValue();
  88. if ( tcnid == vtcid ) {
  89. # we have a VORTAC
  90. from_flag.setBoolValue(vtc_from_flag.getBoolValue());
  91. to_flag.setBoolValue(vtc_to_flag.getBoolValue());
  92. cdi_deflection.setValue(vtc_deflection.getValue());
  93. } else {
  94. # we have a legacy TACAN
  95. var tcn_toflag = 1;
  96. var tcn_fromflag = 0;
  97. var tcn_bearing = tc_mag_hdg.getValue();
  98. var radial = course_radial.getValue();
  99. var delt = tcn_bearing - radial;
  100. if ( delt > 180 ) {
  101. delt -= 360;
  102. } elsif ( delt < -180 ) {
  103. delt += 360;
  104. }
  105. if ( delt > 90 ) {
  106. delt -= 180;
  107. tcn_toflag = 0;
  108. tcn_fromflag = 1;
  109. } elsif ( delt < - 90 ) {
  110. delt += 180;
  111. tcn_toflag = 0;
  112. tcn_fromflag = 1;
  113. }
  114. if ( delt > 10 ) { delt = 10 };
  115. if ( delt < -10 ) { delt = -10 };
  116. from_flag.setBoolValue(tcn_fromflag);
  117. to_flag.setBoolValue(tcn_toflag);
  118. cdi_deflection.setValue(delt);
  119. }
  120. }
  121. }
  122. # AN/ARC-186: VHF voice on comm[0] and homing on nav[2]
  123. # -----------------------------------------------------
  124. var nav2_selected_mhz = props.globals.getNode("instrumentation/nav[2]/frequencies/selected-mhz", 1);
  125. var comm0_selected_mhz = props.globals.getNode("instrumentation/comm[0]/frequencies/selected-mhz", 1);
  126. var vhf = props.globals.getNode("sim/model/A-10/instrumentation/vhf");
  127. var vhf_mode = vhf.getNode("mode");
  128. var vhf_selector = vhf.getNode("selector");
  129. var vhf_load_state = vhf.getNode("load-state");
  130. var vhf_preset = vhf.getNode("selected-preset");
  131. var vhf_presets = vhf.getNode("presets");
  132. var vhf_fqs = vhf.getNode("frequencies");
  133. var vhf_fqs10 = vhf_fqs.getNode("alt-selected-mhz-x10");
  134. var vhf_fqs1 = vhf_fqs.getNode("alt-selected-mhz-x1");
  135. var vhf_fqs01 = vhf_fqs.getNode("alt-selected-mhz-x01");
  136. var vhf_fqs0001 = vhf_fqs.getNode("alt-selected-mhz-x0001");
  137. aircraft.data.add(vhf_preset);
  138. # Displays nav[2] selected-mhz on the VHF radio set.
  139. var alt_freq_update = func {
  140. var freq = nav2_selected_mhz.getValue();
  141. if (freq == nil) { freq = 0; }
  142. var freq10 = int(freq / 10);
  143. var freq1 = int(freq) - (freq10 * 10);
  144. var resr = rounding25((freq - int(freq)) * 1000);
  145. var freq01 = int(resr / 100);
  146. var freq0001 = ((resr / 100) - freq01) * 100;
  147. vhf_fqs10.setValue( freq10 );
  148. vhf_fqs1.setValue( freq1 );
  149. vhf_fqs01.setValue( freq01 );
  150. vhf_fqs0001.setValue( freq0001 );
  151. }
  152. var rounding25 = func(n) {
  153. var a = int( n / 25 );
  154. var l = ( a + 0.5 ) * 25;
  155. n = (n >= l) ? ((a + 1) * 25) : (a * 25);
  156. return( n );
  157. }
  158. # Updates comm[0] and nav[2] selected-mhz property from the VHF dialed freq
  159. var alt_freq_to_freq = func {
  160. var freq10 = vhf_fqs10.getValue();
  161. var freq1 = vhf_fqs1.getValue();
  162. var freq01 = vhf_fqs01.getValue();
  163. var freq0001 = vhf_fqs0001.getValue();
  164. var freq = ( freq10 * 10 ) + freq1 + ( freq01 / 10 ) + ( freq0001 / 1000 );
  165. nav2_selected_mhz.setValue( freq );
  166. comm0_selected_mhz.setValue( freq );
  167. }
  168. # Changes the selected freq on comm[0] and nav[2]
  169. var change_preset = func {
  170. var presets = vhf.getNode("presets");
  171. var p = vhf_preset.getValue();
  172. if ( p == nil ) { p = 1; }
  173. var new_p = p + arg[0];
  174. if (arg[0] == 1 and new_p == 21 ) {
  175. new_p = 1;
  176. } elsif (arg[0] == -1 and new_p == 0 ) {
  177. new_p = 20;
  178. }
  179. vhf_preset.setValue( new_p );
  180. var f_data = "preset[" ~ new_p ~ "]";
  181. p_freq = vhf_presets.getNode(f_data);
  182. var f = p_freq.getValue();
  183. if ( f == nil ) { f = 0; }
  184. nav2_selected_mhz.setValue( f );
  185. comm0_selected_mhz.setValue( f );
  186. alt_freq_update();
  187. }
  188. # Saves displayed freq in the pressets memory
  189. # load_state used for the load button animation
  190. var load_freq = func {
  191. var mode = vhf_mode.getValue();
  192. var selector = vhf_selector.getValue();
  193. if ( mode == 1 and selector == 3 ) {
  194. # mode to TR, selector to MAN
  195. var to_load_freq = nav2_selected_mhz.getValue();
  196. var p = vhf_preset.getValue();
  197. var f = "preset[" ~ p ~ "]";
  198. var p_freq = vhf_presets.getNode(f);
  199. p_freq.setValue(to_load_freq);
  200. vhf_load_state.setValue(1);
  201. settimer(func { vhf_load_state.setValue(0) }, 0.5);
  202. }
  203. }
  204. # Init ####################
  205. var freq_startup = func {
  206. ## nav[0] - ILS
  207. nav0_freq_update();
  208. aircraft.data.add(ils_freq_sel);
  209. aircraft.data.add(ils_freq_sel_fmt);
  210. ## nav[1] - TACAN
  211. foreach (var f_tc; tc_freq.getChildren()) {
  212. aircraft.data.add(f_tc);
  213. }
  214. ## comm[0] and nav[2] - VHF
  215. change_preset(0);
  216. # add all the restored pressets to a new aircraft data file
  217. foreach (var p_freq; vhf_presets.getChildren()) {
  218. aircraft.data.add(p_freq);
  219. }
  220. ## HSI
  221. aircraft.data.add("instrumentation/heading-indicator-fg/offset-deg",
  222. "instrumentation/nav[1]/radials/selected-deg");
  223. }
  224. # Homing deviations computing loop
  225. var ac_hdg = props.globals.getNode("/orientation/heading-deg", 1);
  226. var st_hdg = props.globals.getNode("instrumentation/nav[2]/heading-deg", 1);
  227. var vhf_hdev = vhf.getNode("homing-deviation",1);
  228. var nav2_homing_devs = func {
  229. var ahdg = ac_hdg.getValue();
  230. var shdg = st_hdg.getValue();
  231. if ( shdg != nil ) {
  232. var d = shdg - ahdg;
  233. while ( d > 180) d -= 360;
  234. while ( d < -180) d += 360;
  235. vhf_hdev.setDoubleValue(d);
  236. }
  237. }
  238. # Other navigation panels
  239. # -----------------------
  240. # A-10-nav-mode-selector panel:
  241. # buttons indices: HARS=0, VLOCS=1, TISL=2, NAV-CRS=3, MAN=4, ILS=5, TCN=6
  242. var nav_mode_selector = func(n) {
  243. if ( n == 5 ) {
  244. var n_state = ils_btn.getBoolValue();
  245. ils_btn.setBoolValue(!n_state);
  246. tcn_btn.setBoolValue(0);
  247. } elsif ( n == 6) {
  248. var n_state = tcn_btn.getBoolValue();
  249. tcn_btn.setBoolValue(!n_state);
  250. ils_btn.setBoolValue(0);
  251. }
  252. }