Guest User

Untitled

a guest
Jan 18th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. @lazyglobal off.
  2.  
  3. function gravMag {
  4. parameter alt,bdy.
  5. return bdy:mu/(bdy:radius+altitude)^2.
  6. }
  7.  
  8. function gravVec {
  9. parameter self is ship.
  10. return gravMag(self:altitude,self:obt:body)*-self:up:vector:normalized.
  11. }
  12.  
  13. function thrustVec {
  14. parameter self is ship.
  15. local t is V(0,0,0).
  16. for mod in self:modulesNamed("ModuleEngines") {
  17. print "avail: " + mod:part:availableThrust.
  18. print "facing: " + mod:part:facing:vector:normalized.
  19. set t to t + (mod:part:availableThrust*mod:part:facing:vector:normalized).
  20. print "thrust: " + t.
  21. }
  22. return t.
  23. }
  24.  
  25. function maxMag {
  26. parameter vec, mag.
  27. if vec:mag<mag return vec.
  28. return mag*vec:normalized.
  29. }
  30.  
  31. function constrictSteering {
  32. parameter vec, maxGamma is 20.
  33.  
  34. local vHat is ship:up:vector:normalized.
  35. local vAccV is vHat*vDot(vHat,vec).
  36.  
  37. local hHat is vCrs(vCrs(vHat,vec),vHat):normalized.
  38. local hDot is vDot(hHat,vec).
  39. local hSign is hDot/abs(hDot).
  40. local hAccV is hSign*hHat*min(abs(vDot(hHat,vec)),vAccV:mag*tan(maxGamma)).
  41.  
  42. return vAccV+hAccV.
  43. }
  44.  
  45. function fallTime {
  46. parameter r is ship:altitude, x is 0, b is ship:body.
  47. local xr is (x+b:radius)/(r+b:radius).
  48. local xr2 is sqrt(xr).
  49. return (r+b:radius)^1.5*(constant:degToRad*arccos(xr2)+xr2*sqrt(1-xr))/sqrt(2*b:mu).
  50. }
  51.  
  52. clearVecDraws().
  53.  
  54. lock throttle to 0.
  55. if ship:availableThrust=0 stage.
  56. sas off.
  57. rcs on.
  58.  
  59. lock steering to lookDirUp(ship:up:vector,heading(90,0):vector).
  60. lock throttle to 1.
  61. wait until apoapsis>80000.
  62. lock throttle to 0.
  63. wait until altitude>70000.
  64.  
  65. local rotRate is 360/ship:body:rotationPeriod.
  66.  
  67. local launchPad is LatLng(-0.097207933678136,-74.557757572037).
  68. local landingPad is LatLng(-0.0967218940930756,-74.6173570891441).
  69.  
  70. local posVec is vecDraw(V(0,0,0),V(0,0,0),red,"POS",1,true,0.2).
  71.  
  72. local t is eta:apoapsis+fallTime(apoapsis).
  73. local currentPos is positionAt(ship,time+t).
  74. local targetPos is LatLng(launchPad:lat,launchPad:lng+rotRate*t*1.005):position.
  75. local dV is (targetPos-currentPos)/fallTime(apoapsis).
  76.  
  77. local apoV is velocityAt(ship,time+eta:apoapsis):orbit.
  78.  
  79. local proV is apoV:normalized.
  80. local radV is (positionAt(ship,time+eta:apoapsis)-positionAt(ship:body,time+eta:apoapsis)):normalized.
  81. local nrmV is vCrs(proV,radV).
  82.  
  83. local nd is Node(time:seconds+eta:apoapsis,vDot(radV,dV),vDot(nrmV,dV),vDot(proV,dV)).
  84. add nd.
  85.  
  86. lock steering to lookDirUp(nd:deltaV,-ship:up:vector).
  87. wait until vAng(nd:deltaV,ship:facing:vector)<1.
  88.  
  89. lock bTime to nd:deltaV:mag*ship:mass/ship:availableThrust.
  90. wait until nd:eta/2 < bTime.
  91. lock throttle to min(1,bTime).
  92.  
  93. wait until vDot(dV,nd:deltaV) < 0 or nd:deltaV:mag < 0.1.
  94.  
  95. lock throttle to 0.
  96. remove nd.
  97. lock steering to lookDirUp(-LatLng(launchPad:lat,launchPad:lng):position,heading(270,-45):vector).
  98.  
  99. local hOffset is 0.
  100. local gModifier is 1.
  101. //local lock altRadar to altitude-body:geopositionof(ship:position):terrainheight.
  102. //local lock goal to airSpeed^2/(altRadar-hOffset-5)/2.
  103. //local lock grav to body:mu/((gModifier*altitude+body:radius)^2).
  104. //local lock thst to ship:availableThrust/mass.
  105. //local lock throttleGoal to(goal+grav)/thst.
  106. //wait until throttleGoal>0.99.
  107. wait until altitude<20000 and altitude/10<airSpeed.
  108. until status="LANDED"{
  109. local tgtPos is launchPad:altitudePosition(launchPad:terrainHeight).
  110. local tgtVel is 0.1*tgtPos. // sqrt(tgtPos:mag)*tgtPos:normalized.
  111. local tgtAcc is tgtVel-ship:velocity:surface.
  112. local tAccV is thrustVec()/ship:mass.
  113. local gAccV is gravVec().
  114. local accV is constrictSteering(tgtAcc-gAccV).
  115. lock steering to lookDirUp(accV,heading(270,-45):vector).
  116. local x is vDot(tAccV,accV).
  117. if x<>0 lock throttle to accV:vec:mag^2/x.
  118. set posVec:vec to tgtPos.
  119. //set accVec:vec to accV+gAccV.
  120. wait 0.
  121. }
  122.  
  123. lock throttle to 0.
  124. lock steering to lookDirUp(ship:up:vector,-ship:north:vector).
  125. wait 30.
  126. unlock steering.
  127. rcs off.
Add Comment
Please, Sign In to add comment