Advertisement
Guest User

rdvlaunch.ks

a guest
May 12th, 2018
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. run once util.ks.
  2. run once "/boot/test.ks".
  3.  
  4. set twrsetting to 2.5.
  5. set lngsetting to 15. //apo longitude dist from ksc (inc planetspin). ~8-18 are solid grav turns.
  6.  
  7. local cpitch is 0.
  8. local cazimuth is 0.
  9. local claunchtime is 0.
  10. set target to Vessel("Noodle").
  11.  
  12. //get launch parameters
  13. CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Open Terminal"). //ty spaceishard
  14. calculatelaunch(target,twrsetting,lngsetting).
  15.  
  16. //wait for launch time
  17. set kuniverse:timewarp:mode to "RAILS".
  18. kuniverse:timewarp:warpto(claunchtime-2).
  19. wait until time:seconds >= claunchtime - 1.5.
  20.  
  21. //launch
  22. set launchdata to launchtest(twrsetting,cpitch,cazimuth).
  23.  
  24. //circularize and fine tune approach.
  25. wait until altitude >= 65000. lock steering to prograde.
  26. wait until altitude >= 68000. boostapo(target,time:seconds + eta:apoapsis).
  27. wait until altitude >= 70000. circRdvBurn(target).
  28.  
  29. wait 15. kuniverse:reverttolaunch().
  30.  
  31. //ensure elevation at apo is boosted at least to elevation of target.
  32. function boostapo{
  33. parameter targetship, apotime.
  34.  
  35. set interceptalt to (positionat(targetship,apotime)-Kerbin:position):mag - 600000.
  36. if apoapsis < interceptalt{
  37. print "boosting apoapsis".
  38. lock throttle to .1.
  39. wait until apoapsis >= interceptalt or altitude > 77000.
  40. lock throttle to 0.
  41. }
  42. }
  43.  
  44. function circRdvBurn{
  45. parameter targetship.
  46. wait until altitude >= 70000.
  47. set apotime to time:seconds + eta:apoapsis.
  48. set shipapopos to positionat(ship,apotime).
  49.  
  50. set targvel to velocityat(targetship,apotime):orbit.
  51. set shipvel to velocityat(ship,apotime):orbit.
  52. set burn to targvel:mag - shipvel:mag.
  53. set burnaccel to maxthrust/mass.
  54. set burntime to burn/burnaccel.
  55. set burnstarttime to apotime - burntime/2.
  56.  
  57. print "Burn in " + round(burnstarttime - time:seconds, 1) + "s".
  58. wait until time:seconds >= burnstarttime.
  59. lock throttle to burnaccel * mass / availablethrust.
  60. wait until time:seconds >= burnstarttime + burntime.
  61. unlock throttle.
  62.  
  63. print "Final distance: " + round((ship:position - targetship:position):mag) + "m".
  64. print "Relative speed: " + round((ship:velocity:orbit - targetship:velocity:orbit):mag,1) + "m/s".
  65. }
  66.  
  67.  
  68.  
  69. function rdzbisection{
  70. parameter targetlng, margintime, accuracy,mintime is 0, maxtime is 0.
  71.  
  72. set fun to {parameter t. return groundtrack(t-time:seconds,target):lng.}.
  73. set targetahead to {
  74. parameter lng,tlng.
  75. if lng<0 return tlng>lng and tlng<lng+180.
  76. else return not (tlng<lng and tlng>lng-180).
  77. }.
  78.  
  79. //if 0, they havent been set so calculate min/max. else use provided vals
  80. if mintime = 0{
  81. if targetahead(groundtrack(margintime+2,target):lng,targetlng)
  82. set mintime to time:seconds+margintime.
  83. else
  84. set mintime to time:seconds+target:orbit:period/2-10.
  85. }
  86. if maxtime = 0
  87. set maxtime to mintime+target:orbit:period.
  88.  
  89.  
  90.  
  91. print " ". print "Target lng: "+ round(targetlng,2) + " Search space: " + round(mintime-time:seconds) + "-" + round(maxtime-time:seconds) + "s".
  92.  
  93. return bisectionsearch(fun,targetahead,mintime,maxtime,targetlng,accuracy).
  94. }
  95.  
  96. function calculatelaunch{
  97. parameter targetship, twr, dlng.
  98. set cpitch to calculatepitch(twr,dlng).
  99. set L2ApTime to calculatetime(twr,dlng).
  100.  
  101. //calculate apoapsis intersection with target.
  102. set interceptlng to ship:geoposition:lng + dlng.
  103. set intercepttime to rdzbisection(interceptlng, L2ApTime,.001).
  104. set claunchtime to intercepttime - L2ApTime.
  105.  
  106. //calculate launch azimuth for SLIGHT inclination.
  107. set difvec to positionat(targetship,intercepttime) - positionat(ship,intercepttime).
  108. set cazimuth to 90 - vang(difvec,v(difvec:x,0,difvec:z)). //works for prograde launches, I think it will fail retrograde ones.
  109.  
  110. wait 0.
  111. print " ".
  112. print "Launch parameters calculated:".
  113. print "Target " + targetship:name.
  114. print "Pitch " + round(cpitch,2).
  115. print "LaunchETA " + round(claunchtime - time:seconds) + "s".
  116. print "Launch to intercept: " + round(L2ApTime,1) + "s".
  117. print "Intercept longitude: " + round(ship:geoposition:lng + dlng,1).
  118. print "Intercept azimuth: " + round(cazimuth,2).
  119. }
  120.  
  121.  
  122. //magic
  123. function calculatepitch{
  124. parameter twr, dlng.
  125.  
  126. set x to ln(twr).
  127. set y to ln(dlng).
  128. set p00 to -0.1462.
  129. set p10 to 4.5.
  130. set p01 to -0.8013.
  131. set p20 to -10.66.
  132. set p11 to -0.1052.
  133. set p02 to 0.2868.
  134. set p30 to 14.35.
  135. set p21 to 6.558.
  136. set p12 to -2.396.
  137. set p03 to 0.2297.
  138. set p40 to -7.551.
  139. set p31 to -7.39.
  140. set p22 to 2.967.
  141. set p13 to 0.1478.
  142. set p04 to -0.07455.
  143. set p50 to 1.247.
  144. set p41 to 2.363.
  145. set p32 to -0.9006.
  146. set p23 to -0.125.
  147. set p14 to -0.007399.
  148. set p05 to 0.007187.
  149.  
  150. set p to p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 + p30*x^3 + p21*x^2*y
  151. + p12*x*y^2 + p03*y^3 + p40*x^4 + p31*x^3*y + p22*x^2*y^2
  152. + p13*x*y^3 + p04*y^4 + p50*x^5 + p41*x^4*y + p32*x^3*y^2
  153. + p23*x^2*y^3 + p14*x*y^4 + p05*y^5.
  154.  
  155. return p^2.
  156. }
  157.  
  158. function calculatetime{
  159. parameter twr,dlng.
  160.  
  161. set x to ln(twr).
  162. set y to ln(dlng).
  163. set p00 to 17.07.
  164. set p10 to -10.22.
  165. set p01 to 3.107.
  166. set p20 to 15.22.
  167. set p11 to -11.09.
  168. set p02 to -0.5024.
  169. set p30 to -16.62.
  170. set p21 to 13.71.
  171. set p12 to 2.337.
  172. set p03 to -0.1342.
  173. set p40 to 9.853.
  174. set p31 to -7.422.
  175. set p22 to -1.864.
  176. set p13 to -0.2921.
  177. set p04 to 0.08525.
  178. set p50 to -2.298.
  179. set p41 to 1.469.
  180. set p32 to 0.5305.
  181. set p23 to 0.08212.
  182. set p14 to 0.02153.
  183. set p05 to -0.006024.
  184.  
  185. set t to p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 + p30*x^3 + p21*x^2*y
  186. + p12*x*y^2 + p03*y^3 + p40*x^4 + p31*x^3*y + p22*x^2*y^2
  187. + p13*x*y^3 + p04*y^4 + p50*x^5 + p41*x^4*y + p32*x^3*y^2
  188. + p23*x^2*y^3 + p14*x*y^4 + p05*y^5.
  189.  
  190.  
  191. return t^2.
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement