common.nas 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. var common_ten_ps_loop = func {
  2. setprop("velocities/vertical-speed-fpm", 60*getprop("/velocities/vertical-speed-fps") ) ;
  3. # --------------------------------------------------------------------------- Level Change
  4. if(getprop("/afcs/level_change") == 1){
  5. var targetAlt = getprop("/autopilot/settings/target-altitude-ft");
  6. var currentAlt = getprop("/position/altitude-ft");
  7. var targetFpm = getprop("/autopilot/settings/vertical-speed-fpm");
  8. var altDiff = targetAlt - currentAlt ;
  9. # aim for 6 sec level-off within 200ft (2000fpm = 200ft per 6 seconds )
  10. if(altDiff >= -200 and 200 >= altDiff){ # we're done
  11. setprop("/autopilot/locks/altitude", 'altitude-hold');
  12. setprop("/afcs/level_change", 0);
  13. }else{
  14. setprop("/autopilot/locks/altitude", 'vertical-speed-hold');
  15. }
  16. }
  17. # for needles on webpanel instrument
  18. var h0 = getprop("/orientation/heading-deg");
  19. var h1 = getprop("/instrumentation/nav/heading-deg");
  20. var h2 = getprop("/instrumentation/nav[1]/heading-deg");
  21. setprop("/afcs/VOR1", -h0 + h1);
  22. setprop("/afcs/VOR2", -h0 + h2);
  23. # ---------------------------------------------------------------------------
  24. settimer(common_ten_ps_loop, 0.10);
  25. }
  26. var common_stop_watch = func { # ----------------------- every second
  27. var newSecond = getprop("/afcs/timerSeconds") + 1 ;
  28. if (newSecond == 60) {
  29. setprop("/afcs/timerMinutes", getprop("/afcs/timerMinutes") +1 );
  30. newSecond = 0;
  31. }
  32. setprop("/afcs/timerSeconds", newSecond );
  33. if (newSecond > 9){
  34. setprop("/afcs/timerReadout", getprop("/afcs/timerMinutes") ~ ':' ~ newSecond);
  35. }
  36. else{
  37. setprop("/afcs/timerReadout", getprop("/afcs/timerMinutes") ~ ':0' ~ newSecond);
  38. }
  39. var active = getprop("autopilot/route-manager/active") ;
  40. var maxID = getprop("autopilot/route-manager/route/num") - 2 ;
  41. var wp_current = getprop("autopilot/route-manager/current-wp") ;
  42. if( active == 1 and maxID >= wp_current){
  43. var wp_next = wp_current + 1 ;
  44. var wp_path = 'autopilot/route-manager/route/wp[' ~ wp_next ~ ']/leg-bearing-true-deg' ;
  45. var brg_next = math.round( getprop(wp_path) );
  46. var wp_path = 'autopilot/route-manager/route/wp[' ~ wp_next ~ ']/id' ;
  47. var id_next = getprop(wp_path);
  48. var txt = 'next brg = ' ~ brg_next ~ ', to: ' ~ id_next ;
  49. setprop("navigation/next_up", txt) ;
  50. }
  51. # this only works if the relevant tile has been LOADED!!
  52. setprop("/afcs/WP_Alt", 0);
  53. setprop("/afcs/WP_Alt_diff", 0);
  54. if(getprop("/instrumentation/gps/wp/wp[1]/ID") != '')
  55. {
  56. #var WP_ID = getprop("/instrumentation/gps/wp/wp[1]/ID") ;
  57. #print (WP_ID) ;
  58. setprop("/afcs/WP_Alt", 1);
  59. setprop("/afcs/WP_Alt_diff", 1);
  60. WP_Lat = getprop("/instrumentation/gps/wp/wp[1]/latitude-deg") ;
  61. WP_Lon = getprop("/instrumentation/gps/wp/wp[1]/longitude-deg") ;
  62. #print (WP_Lat) ;
  63. #print (WP_Lon) ;
  64. var info = geodinfo(WP_Lat, WP_Lon) ;
  65. if ( info[0] == nil ){
  66. setprop("/afcs/WP_Alt", 2);
  67. setprop("/afcs/WP_Alt_diff", 2);
  68. return ;
  69. }
  70. #debug.dump(info);
  71. WP_Alt = int(info[0] * 3.280839895) ;
  72. #gui.popupTip(WP_Alt, 3);
  73. #print (WP_Alt) ;
  74. setprop("/afcs/WP_Alt", WP_Alt);
  75. WP_Alt_diff = WP_Alt - int(getprop("/position/altitude-ft")) ;
  76. setprop("/afcs/WP_Alt_diff", WP_Alt_diff);
  77. }
  78. settimer(common_stop_watch, 1.00);
  79. }
  80. var common_every_ten_seconds = func { # ----------------------- every ten seconds
  81. var myLat = getprop("/position/latitude-deg") ;
  82. myLat = sprintf("%.6f", myLat) ;
  83. var myLon = getprop("/position/longitude-deg") ;
  84. myLon = sprintf("%.6f", myLon) ;
  85. var myAlt = getprop("/position/altitude-ft") ;
  86. myAlt = math.floor(myAlt) ;
  87. var myHdg = getprop("/orientation/heading-magnetic-deg");
  88. myHdg = math.floor(myHdg) ;
  89. var myMach = getprop("/velocities/mach") ;
  90. myMach = sprintf("%.2f", myMach) ;
  91. var logFileText = "--lat=" ~ myLat ~ " --lon=" ~ myLon ~ " --altitude=" ~ myAlt ~ " --heading=" ~ myHdg ~ " --mach=" ~ myMach;
  92. var path = getprop("/sim/fg-home") ~ '/Export/positionLogger.txt';
  93. var file = io.open(path, "wb"); # open in write mode
  94. io.write(file, logFileText); # write the data
  95. settimer(common_every_ten_seconds, 10.00);
  96. }
  97. setlistener("/sim/signals/fdm-initialized", func { # ====================== initialization
  98. setprop("/afcs/fms", 0);
  99. setprop("/afcs/WP_Alt", 0);
  100. setprop("/afcs/WP_Alt_diff", 0);
  101. setprop("/afcs/level_change", 0);
  102. setprop("/autopilot/settings/target-altitude-ft", int(1000+getprop("/position/altitude-ft")));
  103. setprop("/autopilot/settings/vertical-speed-fpm", 1000);
  104. setprop("/afcs/timerSeconds", 0);
  105. setprop("/afcs/timerMinutes", 0);
  106. setprop("/afcs/timerReadout", 0 ~ ':' ~ 0);
  107. setprop("/afcs/smoke/on", 0);
  108. setprop("/afcs/VOR1", 0);
  109. setprop("/afcs/VOR2", 0);
  110. setprop("/instrumentation/nav/heading-deg", 0);
  111. setprop("/instrumentation/nav[1]/heading-deg", 0);
  112. common_ten_ps_loop(); # things than need doing ten-times per second.
  113. common_stop_watch() ; # things than need doing ONCE every second.
  114. common_every_ten_seconds(); # things than need doing every ten seconds.
  115. })