Advertisement
KSA_MissionCtrl

PlaneNav.ks (10/12/15)

Oct 11th, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. // converts seconds into a string of years, days, minutes and seconds, showing only the values that exist (no "0y, 0d")
  2. function FormatTime {
  3. parameter seconds.
  4.  
  5. set days to 0.
  6. set hours to 0.
  7. set minutes to 0.
  8. set formattedTime to "".
  9.  
  10. if seconds >= 86400 {
  11. set days to floor(seconds / 86400).
  12. set seconds to seconds - (days * 86400).
  13. set formattedTime to days + "d, ".
  14. }.
  15.  
  16. if seconds >= 3600 {
  17. set hours to floor(seconds / 3600).
  18. set seconds to seconds - (hours * 3600).
  19. set formattedTime to formattedTime + hours + "h ".
  20. }.
  21.  
  22. if seconds >= 60 {
  23. set minutes to floor(seconds / 60).
  24. set seconds to seconds - (minutes * 60).
  25. set formattedTime to formattedTime + minutes + "m ".
  26. }.
  27.  
  28. set formattedTime to formattedTime + floor(seconds) + "s".
  29.  
  30. return formattedTime.
  31. }.
  32.  
  33. // from the KSLib - https://github.com/KSP-KOS/KSLib
  34. function circle_bearing {
  35. parameter
  36. p1, //...this point...
  37. p2. //...to this point.
  38.  
  39. return mod(360+arctan2(sin(p2:lng-p1:lng)*cos(p2:lat),cos(p1:lat)*sin(p2:lat)-sin(p1:lat)*cos(p2:lat)*cos(p2:lng-p1:lng)),360).
  40. }.
  41.  
  42. // from the KSLib - https://github.com/KSP-KOS/KSLib
  43. function circle_distance {
  44. parameter
  45. p1, //...this point...
  46. p2, //...to this point...
  47. radius. //...around a body of this radius. (note: if you are flying you may want to use ship:body:radius + altitude).
  48. local A is sin((p1:lat-p2:lat)/2)^2 + cos(p1:lat)*cos(p2:lat)*sin((p1:lng-p2:lng)/2)^2.
  49.  
  50. return radius*constant():PI*arctan2(sqrt(A),sqrt(1-A))/90.
  51. }.
  52.  
  53. // Action groups bounded to a script trigger via Action Groups Extended
  54. on AG11 {
  55. set waypointShift to 1.
  56. preserve.
  57. }.
  58.  
  59. on AG12 {
  60. set waypointShift to -1.
  61. preserve.
  62. }.
  63.  
  64. on AG13 {
  65. set running to false.
  66. }.
  67.  
  68. // initialize our data
  69. set waypointLatLng to list(latlng(82.6172, -158.5547), // ice caps
  70. latlng(-16.1719, 42.3633), // badlands
  71. latlng(2.1567, 26.6113), // lushlands
  72. latlng(-0.1025, -73.5753)). // KSC
  73. set waypointText to list("Ice Caps", "Badlands", "Lushlands", "KSC").
  74. set waypoint to 0.
  75. set running to true.
  76. set waypointShift to 0.
  77. set update to time:seconds.
  78. set liquidFuel to 7.
  79. set dstTraveled to 0.
  80.  
  81. // print out the initial information for us to update as we go along
  82. clearscreen.
  83. print "Current Waypoint: " + waypointText[waypoint].
  84. set bearing to circle_bearing(ship:geoposition, waypointLatLng[waypoint]).
  85. print "Bearing: " + bearing + "°".
  86. set distance to circle_distance(ship:geoposition, waypointLatlng[waypoint], ship:body:radius + altitude).
  87. print "Distance: " + distance + "m".
  88. print "ETA:".
  89.  
  90. // create the CSV header
  91. log "UT,WaypointName,WaypointLat,WaypointLng,WaypointDistance,PlaneLat,PlaneLng,Heading,Speed,ASL,Fuel,DstTraveled" to PlaneLog.
  92.  
  93. // loop until program exit is triggered by action group
  94. until not running {
  95.  
  96. // update our distance traveled every second based on our speed (which is in m/s)
  97. set dstTraveled to dstTraveled + ship:surfacespeed.
  98.  
  99. // update information text with new data
  100. print "Current Waypoint: " + waypointText[waypoint] + " " at (0,0).
  101. set bearing to circle_bearing(ship:geoposition, waypointLatLng[waypoint]).
  102. print "Bearing: " + bearing + "° " at (0,1).
  103. set distance to circle_distance(ship:geoposition, waypointLatlng[waypoint], ship:body:radius + altitude).
  104. print "Distance: " + distance + "m " at (0,2).
  105.  
  106. // don't calculate ETA if we're not moving
  107. if ship:airspeed > 1 {
  108. print "ETA: " + FormatTime(distance/ship:airspeed) + " " at (0,3).
  109. } else {
  110. print "ETA: N/A " at (0,3).
  111. }
  112.  
  113. // has the user selected a new waypoint?
  114. if waypointShift <> 0 {
  115. set newWaypoint to waypoint + waypointShift.
  116.  
  117. // if the new waypoint takes us out of bounds, reset to the current waypoint
  118. // otherwise update the screen with new waypoint data
  119. if newWaypoint = waypointText:length or newWaypoint < 0 {
  120. set newWaypoint to Waypoint.
  121. } else {
  122. clearscreen.
  123. set waypoint to newWaypoint.
  124. print "Current Waypoint: " + waypointText[waypoint].
  125. set bearing to circle_bearing(ship:geoposition, waypointLatLng[waypoint]).
  126. print "Bearing: " + bearing + "°".
  127. set distance to circle_distance(ship:geoposition, waypointLatlng[waypoint], ship:body:radius + altitude).
  128. print "Distance: " + distance + "m".
  129. }.
  130.  
  131. // we don't want to keep shifting waypoints...
  132. set waypointShift to 0.
  133. }.
  134.  
  135. // only log the plane data every minute
  136. if time:seconds - update > 60 {
  137.  
  138. // this code for determining the heading of the aircraft only works if no target is selected!!
  139. if ship:bearing < 0 {
  140. set hdg to abs(ship:bearing).
  141. } else {
  142. set hdg to (180 - ship:bearing) + 180.
  143. }
  144.  
  145. // log all the data
  146. log round(time:seconds) + "," + waypointText[waypoint] + "," + waypointLatLng[waypoint]:lat + "," +
  147. waypointLatLng[waypoint]:lng + "," + distance + "," + ship:latitude + "," + ship:longitude + "," + hdg + "," + ship:airspeed +
  148. "," + ship:altitude + "," + round(100*ship:resources[liquidFuel]:amount/ship:resources[liquidFuel]:capacity)
  149. + "," + dstTraveled to PlaneLog.
  150. copy PlaneLog to 0.
  151.  
  152. // wait for the next 60s
  153. set update to time:seconds.
  154. }.
  155.  
  156. // update once per second
  157. wait 1.
  158. }.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement