HvrdlS

Rendezvous with ISS

Aug 21st, 2021 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. set T to vessel("Iss").
  2. lock steering to prograde.
  3. rcs on.
  4. b1().
  5.  
  6. function b1 {
  7. wait 0.
  8. set TargetAp to ship:apoapsis + body:radius.
  9. lock ap to ship:apoapsis + body:radius.
  10. set smja to (ap + TargetAp) / 2.
  11. set targetvel to sqrt(body:mu*((2/ap)-(1/smja))).
  12. print targetvel.
  13. set maneuvertime to time + eta:apoapsis.
  14. set apvel to velocityat(ship, maneuvertime):orbit:mag.
  15. print apvel.
  16. set needvel to targetvel - apvel.
  17. print needvel.
  18. set nd to node(maneuvertime, 0, 0, needvel).
  19. add nd.
  20. set max_acc to 8/ship:mass.
  21. set burn_duration to nd:deltav:mag/max_acc.
  22. print "Crude Estimated burn duration: " + round(burn_duration) + "s".
  23. lock steering to prograde.
  24. wait until nd:eta <= (burn_duration/2 + 60).
  25. set np to nd:deltav. //points to node, don't care about the roll direction.
  26. lock steering to np.
  27.  
  28. //now we need to wait until the burn vector and ship's facing are aligned
  29. wait until vang(np, ship:facing:vector) < 0.25.
  30.  
  31. //the ship is facing the right direction, let's wait for our burn time
  32. wait until nd:eta <= (burn_duration/2).
  33. //we only need to lock throttle once to a certain variable in the beginning of the loop, and adjust only the variable itself inside it
  34. set ship:control:fore to 0.
  35.  
  36. set done to False.
  37. //initial deltav
  38. set dv0 to nd:deltav.
  39. until done
  40. {
  41. set max_acc to 8/ship:mass.
  42. //throttle is 100% until there is less than 1 second of time left to burn
  43. //when there is less than 1 second - decrease the throttle linearly
  44. set ship:control:fore to min(nd:deltav:mag/max_acc, 1).
  45.  
  46. //here's the tricky part, we need to cut the throttle as soon as our nd:deltav and initial deltav start facing opposite directions
  47. //this check is done via checking the dot product of those 2 vectors
  48. if vdot(dv0, nd:deltav) < 0
  49. {
  50. print "End burn, remain dv " + round(nd:deltav:mag,1) + "m/s, vdot: " + round(vdot(dv0, nd:deltav),1).
  51. set ship:control:fore to 0.
  52. break.
  53. }
  54.  
  55. //we have very little left to burn, less then 0.1m/s
  56. if nd:deltav:mag < 0.1
  57. {
  58. print "Finalizing burn, remain dv " + round(nd:deltav:mag,1) + "m/s, vdot: " + round(vdot(dv0, nd:deltav),1).
  59. //we burn slowly until our node vector starts to drift significantly from initial vector
  60. //this usually means we are on point
  61. wait until vdot(dv0, nd:deltav) < 0.5.
  62.  
  63. set ship:control:fore to 0.
  64. print "End burn, remain dv " + round(nd:deltav:mag,1) + "m/s, vdot: " + round(vdot(dv0, nd:deltav),1).
  65. set done to True.
  66. }
  67. }
  68. unlock steering.
  69. unlock throttle.
  70. wait 1.
  71.  
  72. //we no longer need the maneuver node
  73. remove nd.
  74.  
  75. //set throttle to 0 just in case.
  76. SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
  77. }
  78.  
  79. function b2 {
  80. wait 0.
  81. set TargetAp to T:apoapsis + body:radius.
  82. set ap to ship:apoapsis + body:radius.
  83. set smja to (ap + TargetAp) / 2.
  84. set targetvel to sqrt(body:mu*((2/ap)-(1/smja))).
  85. print targetvel.
  86. set now to time:seconds + 30.
  87. set maneuvertime to now.
  88. set apvel to velocityat(ship, maneuvertime):orbit:mag.
  89. print apvel.
  90. set needvel to targetvel - apvel.
  91. print needvel.
  92. set nd to node(maneuvertime, 0, 0, needvel).
  93. add nd.
  94. set max_acc to 8/ship:mass.
  95. set burn_duration to nd:deltav:mag/max_acc.
  96. print "Crude Estimated burn duration: " + round(burn_duration) + "s".
  97. lock steering to prograde.
  98. wait until nd:eta <= (burn_duration/2 + 60).
  99. set np to nd:deltav. //points to node, don't care about the roll direction.
  100. lock steering to np.
  101.  
  102. //now we need to wait until the burn vector and ship's facing are aligned
  103. wait until vang(np, ship:facing:vector) < 0.25.
  104.  
  105. //the ship is facing the right direction, let's wait for our burn time
  106. wait until nd:eta <= (burn_duration/2).
  107. //we only need to lock throttle once to a certain variable in the beginning of the loop, and adjust only the variable itself inside it
  108. set ship:control:fore to 0.
  109.  
  110. set done to False.
  111. //initial deltav
  112. set dv0 to nd:deltav.
  113. until done
  114. {
  115. set max_acc to 8/ship:mass.
  116. //throttle is 100% until there is less than 1 second of time left to burn
  117. //when there is less than 1 second - decrease the throttle linearly
  118. set ship:control:fore to min(nd:deltav:mag/max_acc, 1).
  119.  
  120. //here's the tricky part, we need to cut the throttle as soon as our nd:deltav and initial deltav start facing opposite directions
  121. //this check is done via checking the dot product of those 2 vectors
  122. if vdot(dv0, nd:deltav) < 0
  123. {
  124. print "End burn, remain dv " + round(nd:deltav:mag,1) + "m/s, vdot: " + round(vdot(dv0, nd:deltav),1).
  125. set ship:control:fore to 0.
  126. break.
  127. }
  128.  
  129. //we have very little left to burn, less then 0.1m/s
  130. if nd:deltav:mag < 0.1
  131. {
  132. print "Finalizing burn, remain dv " + round(nd:deltav:mag,1) + "m/s, vdot: " + round(vdot(dv0, nd:deltav),1).
  133. //we burn slowly until our node vector starts to drift significantly from initial vector
  134. //this usually means we are on point
  135. wait until vdot(dv0, nd:deltav) < 0.5.
  136.  
  137. set ship:control:fore to 0.
  138. print "End burn, remain dv " + round(nd:deltav:mag,1) + "m/s, vdot: " + round(vdot(dv0, nd:deltav),1).
  139. set done to True.
  140. }
  141. }
  142. unlock steering.
  143. unlock throttle.
  144. wait 1.
  145.  
  146. //we no longer need the maneuver node
  147. remove nd.
  148.  
  149. //set throttle to 0 just in case.
  150. SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
  151. }
  152.  
  153. function calcangle {
  154. set sa to (2*body("Kerbin"):radius + T:altitude + ship:altitude)/2.
  155. set ta to body("Kerbin"):radius + T:altitude.
  156. print sa.
  157. print ta.
  158. return 180*(1-(sa/ta)^1.5).
  159. }
  160.  
  161. function checkang {
  162. set VecS to ship:position - body("Kerbin"):position.
  163. set VecT to T:position - body("Kerbin"):position.
  164. set VecHV to vxcl(ship:up:vector, ship:velocity:orbit).
  165. set VecST to T:position - ship:position.
  166. set t_angle to calcangle.
  167. set cur_angle to vang(VecT, VecS).
  168. if vang(Vect, VecS)>90 {
  169. set cur_angle to -cur_angle.
  170. }
  171.  
  172. print cur_angle - t_angle.
  173. return abs(cur_angle - t_angle) < 6.
  174. }
  175.  
  176. until checkang {
  177. lock steering to prograde.
  178. print checkang.
  179. wait 1.
  180. clearscreen.
  181. }
  182.  
  183. b2().
  184. LOCK TargetVector TO Target:ORBIT:POSITION-SHIP:ORBIT:POSITION.
  185. wait until TargetVector:mag < 5000.
  186. run approach.
  187. run dock.
Add Comment
Please, Sign In to add comment