Advertisement
KSA_MissionCtrl

Orbit and Log

Jan 7th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.56 KB | None | 0 0
  1. // NOTE - this is a purpose-built script and is not indicative of the more modular way the KSA will approach launch scripts once the program begins anew
  2.  
  3. abort off.
  4. sas on.
  5. lights on.
  6. lock throttle to 0.
  7. // from the KSLib
  8. // https://github.com/KSP-KOS/KSLib/blob/master/library/lib_circle_nav.ks
  9. function circle_distance {
  10. parameter
  11. p1, //...this point...
  12. p2, //...to this point...
  13. radius. //...around a body of this radius. (note: if you are flying you may want to use ship:body:radius + altitude).
  14. local A is sin((p1:lat-p2:lat)/2)^2 + cos(p1:lat)*cos(p2:lat)*sin((p1:lng-p2:lng)/2)^2.
  15.  
  16. return radius*constant():PI*arctan2(sqrt(A),sqrt(1-A))/90.
  17. }.
  18.  
  19. // from the KSLib
  20. // https://github.com/KSP-KOS/KSLib/blob/master/library/lib_navball.ks
  21. function east_for {
  22. parameter ves.
  23.  
  24. return vcrs(ves:up:vector, ves:north:vector).
  25. }
  26.  
  27. function compass_for {
  28. parameter ves.
  29.  
  30. local pointing is ves:facing:forevector.
  31. local east is east_for(ves).
  32.  
  33. local trig_x is vdot(ves:north:vector, pointing).
  34. local trig_y is vdot(east, pointing).
  35.  
  36. local result is arctan2(trig_y, trig_x).
  37.  
  38. if result < 0 {
  39. return 360 + result.
  40. } else {
  41. return result.
  42. }
  43. }
  44.  
  45. function pitch_for {
  46. parameter ves.
  47.  
  48. return 90 - vang(ves:up:vector, ves:facing:forevector).
  49. }
  50.  
  51. function roll_for {
  52. parameter ves.
  53.  
  54. if vang(ship:facing:vector,ship:up:vector) < 0.2 { //this is the dead zone for roll when the ship is vertical
  55. return 0.
  56. } else {
  57. local raw is vang(vxcl(ship:facing:vector,ship:up:vector), ves:facing:starvector).
  58. if vang(ves:up:vector, ves:facing:topvector) > 90 {
  59. if raw > 90 {
  60. return 270 - raw.
  61. } else {
  62. return -90 - raw.
  63. }
  64. } else {
  65. return raw - 90.
  66. }
  67. }
  68. }.
  69.  
  70. on abort {
  71. set running to false.
  72. }.
  73.  
  74. //when stage:number < 2 then { set fuelOffset to 180. }
  75.  
  76. set TotalFuelIndex to 1.
  77. set StageFuelIndex to 0.
  78. set bTotalFuel to true.
  79. set bStageFuel to true.
  80. set dstTraveled to 0.
  81. set logInterval to 1.
  82. set TotalFuel to 1.
  83. set StageFuel to 1.
  84. //set fuelOffset to 0.
  85. set running to true.
  86. set currTime to ceiling(time:seconds).
  87. set launchTime to 59747400.
  88. set startStage to stage:number.
  89. set initialVelocity to ship:velocity:surface:mag.
  90. set launchPosition to ship:geoposition.
  91. set surfaceGravity to (ship:orbit:body:mass * constant:G)/(ship:orbit:body:radius^2).
  92. set SFCapacity to ship:resources[3]:capacity.
  93. set pitch to 89.
  94. set timeDelay to 99999999.
  95. lock srb to ship:partstagged("srb")[0]:getmodule("moduleengines"):getfield("status").
  96. lock lifter to ship:partstagged("lifter")[0]:getmodule("moduleengines"):getfield("status").
  97. set event to "Final Countdown".
  98.  
  99. when stage:number = 5 then {
  100. set event to "Main Engine Start".
  101. lock throttle to 0.15.
  102. }.
  103.  
  104. when stage:number = 4 then {
  105. set event to "Liftoff!".
  106. lock throttle to 0.38.
  107. }.
  108.  
  109. when ship:velocity:surface:mag > 180 then{
  110. set event to "Beginning Gravity Turn".
  111. sas off.
  112. lock steering to heading(90,pitch).
  113. set pitchTime to floor(time:seconds).
  114. }.
  115.  
  116. when srb = "flame-out!" then {
  117. set event to "SRB Drop, Main Engine Throttle-up".
  118. lock throttle to 0.75.
  119. stage.
  120. }.
  121.  
  122. when ship:altitude > 55000 then {
  123. set event to "Fairing Jettison".
  124. stage.
  125. }.
  126.  
  127. when ship:obt:apoapsis > 75000 then {
  128. set event to "MECO, Coast to Orbital Insertion Burn".
  129. lock throttle to 0.
  130. lock steering to prograde.
  131. }.
  132.  
  133. when ship:altitude > 74000 then {
  134. set event to "Orbital Insertion Burn".
  135. lock throttle to 1.
  136. }.
  137.  
  138. when lifter = "flame-out!" then {
  139. set event to "Main Engine Flameout".
  140. set timeDelay to time:seconds.
  141. stage.
  142. }.
  143.  
  144. when stage:number = 1 and time:seconds - timeDelay > 2 then {
  145. set event to "Orbital Engine Ignition".
  146. stage.
  147. }.
  148.  
  149. when ship:obt:periapsis > 70500 then {
  150. set event to "Orbital Insertion Completed".
  151. lock throttle to 0.
  152. }.
  153.  
  154. // print out the initial information for us to update as we go along
  155. clearscreen.
  156. print "Time to Launch: ".
  157. print "Compass: " + round(compass_for(ship), 1) + "°".
  158. print "Pitch: " + round(pitch_for(ship), 1) + "°".
  159. print "Roll: " + round(roll_for(ship), 1) + "°".
  160. print "StageFuel: 100%".
  161. print "TotalFuel: 100%".
  162. print "Distance Traveled: 0 km".
  163. print "Altitude: " + round(ship:altitude/1000, 3) + " km".
  164. print "".
  165.  
  166. list files in fileList.
  167. for fil in fileList {
  168. if fil:name = "TelemetryLog.ks" { delete TelemetryLog. }.
  169. }
  170.  
  171. // create the CSV headers
  172. log "ID,Heading,Pitch,Roll,DstTraveled,StageFuel,TotalFuel,Q,Mass,AoA,Altitude,Lat,Lon,Apoapsis,Periapsis,Inclination,Velocity,Thrust,Gravity,DstDownrange,Throttle,AoAWarn,Video,Camera,CommLost,Event,Image,Tweet" to TelemetryLog.
  173.  
  174. when launchTime - currTime = 8 then stage.
  175.  
  176. when launchTime - currTime = 0 then stage.
  177.  
  178. // loop until program exit is triggered by action group
  179. until not running {
  180. wait until time:seconds - currTime > logInterval.
  181. set currTime to floor(time:seconds).
  182.  
  183. if launchTime - currTime < 13 {
  184.  
  185. set thrust to 0.
  186. list engines in allEngines.
  187. for eng in allEngines {
  188. if eng:ignition { set thrust to thrust + eng:thrust. }.
  189. }.
  190.  
  191. if stage:number = 5 {
  192. set TotalFuel to (ship:resources[1]:amount + ship:resources[3]:amount) / ((ship:resources[1]:capacity-180) + SFCapacity).
  193. print "TotalFuel: " + 100*TotalFuel + "% " at (0,4).
  194. } else if stage:number = 4 {
  195. set TotalFuel to (ship:resources[1]:amount + ship:resources[3]:amount) / ((ship:resources[1]:capacity-180) + SFCapacity).
  196. set StageFuel to stage:resources[2]:amount / stage:resources[2]:capacity.
  197. print "TotalFuel: " + 100*TotalFuel + "% " at (0,4).
  198. print "StageFuel: " + 100*StageFuel + "% " at (0,5).
  199. } else if stage:number = 3 or stage:number = 2 {
  200. set TotalFuel to ship:resources[1]:amount/(ship:resources[1]:capacity-180+SFCapacity).
  201. set StageFuel to stage:resources[0]:amount/(stage:resources[0]:capacity).
  202. print "TotalFuel: " + 100*TotalFuel + "% " at (0,4).
  203. print "StageFuel: " + 100*StageFuel + "% " at (0,5).
  204. } else if stage:number < 2 {
  205. set TotalFuel to ship:resources[1]:amount/(ship:resources[1]:capacity-180+SFCapacity).
  206. set StageFuel to ship:resources[1]:amount/45.
  207. print "TotalFuel: " + 100*TotalFuel + "% " at (0,4).
  208. print "StageFuel: " + 100*StageFuel + "% " at (0,5).
  209. }.
  210.  
  211. if ship:obt:apoapsis < 75000 and ship:velocity:surface:mag > 180 { set pitch to pitch - 0.75. }.
  212.  
  213. // update information text with new data
  214. if currTime > launchTime {
  215. // update our distance traveled every second based on our speed (which is in m/s)
  216. // https://answers.yahoo.com/question/index?qid=20100423120148AADAkZ2
  217. // this should be set to ship:velocity:orbital:mag if launching into orbit
  218. set a to (initialVelocity - ship:velocity:surface:mag)/logInterval.
  219. set d to (initialVelocity * logInterval) + 0.5 * ((a * logInterval)^2).
  220. set dstTraveled to dstTraveled + d.
  221. set initialVelocity to ship:velocity:surface:mag.
  222.  
  223. print "Mission Elapsed Time: " + (currTime - launchTime) + "s " at (0,0).
  224. print "Compass: " + round(compass_for(ship), 1) + "°" + " " at (0,1).
  225. print "Pitch: " + round(pitch_for(ship), 1) + "°" + " " at (0,2).
  226. print "Roll: " + round(roll_for(ship), 1) + "°" + " " at (0,3).
  227. print "Distance Traveled: " + round(dstTraveled/1000, 3) + " km " at (0,6).
  228. print "Altitude: " + round(ship:altitude/1000, 3) + " km " at (0,7).
  229. }.
  230.  
  231. // log all the data
  232. log currTime + "," +
  233. compass_for(ship) + "," +
  234. pitch_for(ship) + "," +
  235. roll_for(ship) + "," +
  236. dstTraveled + "," +
  237. StageFuel + "," +
  238. TotalFuel + "," +
  239. ship:Q * constant:AtmToKPa + "," +
  240. ship:mass + "," +
  241. VANG(ship:facing:vector, ship:srfprograde:vector) + "," +
  242. ship:altitude + "," +
  243. ship:geoposition:lat + "," +
  244. ship:geoposition:lng + "," +
  245. ship:orbit:apoapsis + "," +
  246. ship:orbit:periapsis + "," +
  247. ship:orbit:inclination + "," +
  248. ship:velocity:surface:mag + "," +
  249. thrust + "," +
  250. surfaceGravity/((((ship:orbit:body:radius + ship:altitude)/1000)/(ship:orbit:body:radius/1000))^2) + "," +
  251. circle_distance(launchPosition, ship:geoposition, ship:orbit:body:radius) + "," +
  252. throttle + ",,,,," +
  253. event
  254. to TelemetryLog.
  255. }.
  256.  
  257. if launchTime - currTime >= 0 { print "Time to Launch: " + (launchTime - currTime) + "s " at (0,0). }.
  258. }.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement