HvrdlS

Rendezvous

Jun 16th, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.60 KB | None | 0 0
  1. @clobberbuiltins on.
  2. clearscreen.
  3. set T to vessel("ISS").
  4. set accel to 1.1.
  5. set steeringManager:maxstoppingtime to 0.05.
  6. lock steering to prograde.
  7. set missionstatus to "Burn 1".
  8. print "Press 9 to come into next step".
  9. until ag9 {
  10. printing().
  11. }
  12.  
  13. on ag10 {
  14. abort().
  15. }
  16.  
  17. /// here should be b1() but it is skipped for testing purpose
  18. ag9 off.
  19. lock steering to prograde.
  20. checkan().
  21. until tn < -1000000000 { /// that is for testing purpose so cycle would never stop
  22. clearscreen.
  23. checkan().
  24. print("Angle to AN: " + ANang + " Degrees").
  25. print("Angle to DN: " + DNang + " Degrees").
  26. print("True anomaly of the ship: " + ta + " Degrees").
  27. print("Eccentric anomaly of the ship: " + E + " Degrees").
  28. print("Mean anomaly of the ship: " + M + " Degrees").
  29. print("True anomaly of the AN: " + ANTA + " Degrees").
  30. print("Eccentric anomaly of the AN: " + ANE + " Degrees").
  31. print("Mean anomaly of the AN: " + ANM + " Degrees").
  32. print("Time till ascending node: " + tn + " Seconds").
  33. wait 1.
  34. }
  35.  
  36. b2().
  37. until checkang {
  38. clearscreen.
  39. print "Mission status: " + missionstatus.
  40. print "Angle: " + (cur_angle - t_angle) + " Degrees".
  41. print "Angle: " + targetang + " Degrees".
  42. print "Press 10 to abort current program".
  43. }
  44.  
  45. b3().
  46. run DragonApproach.
  47.  
  48.  
  49. function checkan {
  50. set targetorbitnormal to vcrs(T:orbit:velocity:orbit:normalized,(T:body:position-T:position):normalized):normalized.
  51. set shiporbitnormal to vcrs(ship:orbit:velocity:orbit:normalized,(ship:body:position-ship:position):normalized):normalized.
  52. set vectoan to vcrs(shiporbitnormal,targetorbitnormal):normalized.
  53. set vectoship to (ship:position-ship:body:position):normalized.
  54. set ANang to vang(vectoship,vectoan).
  55. set DNang to -ANang.
  56. set shipvelxcl to vxcl((ship:body:position-ship:position), ship:orbit:velocity:orbit):normalized.
  57. set signang to vdot(vectoan,shipvelxcl).
  58. if signang < 0 {
  59. set ANang to 360 - ANang.
  60. }
  61.  
  62. set e to orbit:eccentricity.
  63. set ta to mod(orbit:trueanomaly, 360).
  64. set n to (2 * constant:pi / orbit:period) * constant:radtodeg().
  65. set ANTA to mod(ANang + ta,360).
  66. set DNTA to 180 + ANTA.
  67. if DNTA > 360 {
  68. set DNTA to DNTA - 360.
  69. }
  70.  
  71. set ANE to arctan2(sqrt(1 - e^2) * sin(ANTA), e + cos(ANTA)).
  72. set DNE to arctan2(sqrt(1 - e^2) * sin(DNTA), e + cos(DNTA)).
  73. set ANMi to mod((ANE - (e*sin(ANE) * constant:radtodeg())) + 360, 360).
  74. set ANM to mod(ANMi + 360, 360).
  75. set DNMi to mod((DNE - (e*sin(DNE) * constant:radtodeg())) + 360, 360).
  76. set DNM to mod(DNMi + 360, 360).
  77. set E to arctan2(sqrt(1 - e^2) * sin(ta), e + cos(ta)).
  78. set Mi to (E - (e*sin(E) * constant:radtodeg())).
  79. set M to mod(Mi + 360,360).
  80. set tShip to M / n.
  81. set tAN to ANM / n.
  82. set tn to tAN - tShip.
  83. }
  84.  
  85.  
  86. function abort {
  87. clearscreen.
  88. print "Program aborted".
  89. wait 2.
  90. print 3/0.
  91. }
  92.  
  93.  
  94. function printing {
  95. clearscreen.
  96. print "Mission status: " + missionstatus.
  97. print "Press 10 to abort current program".
  98. wait 1.
  99. }
  100.  
  101.  
  102. function b1 {
  103. set T to vessel("ISS").
  104. set TargetAp to ship:apoapsis + body:radius.
  105. lock ap to ship:apoapsis + body:radius.
  106. set smja to (ap + TargetAp) / 2.
  107. set targetvel to sqrt(body:mu*((2/ap)-(1/smja))).
  108. print targetvel.
  109. set maneuvertime to time + eta:apoapsis.
  110. set apvel to velocityat(ship, maneuvertime):orbit:mag.
  111. print apvel.
  112. set needvel to targetvel - apvel.
  113. print needvel.
  114. set nd to node(maneuvertime, 0, 0, needvel).
  115. add nd.
  116. burn_exec().
  117. set missionstatus to "Burn 2".
  118. }
  119.  
  120. function b2 {
  121. set T to vessel("ISS").
  122. set maneuvertime to tn+time:seconds.
  123. set velocityatnode to velocityat(ship, maneuvertime):orbit.
  124. set targetorbitnormal to vcrs(T:velocity:orbit:normalized,(T:body:position-T:position):normalized):normalized.
  125. set shiporbitnormal to vcrs(ship:velocity:orbit:normalized,(ship:body:position-ship:position):normalized):normalized.
  126. set vectortobodynorm to (body:position-positionat(ship,maneuvertime)):normalized.
  127. set inclination to vang(shiporbitnormal,targetorbitnormal).
  128. if vdot(targetorbitnormal,velocityatnode) > 0 {
  129. set inc to inclination.
  130. } else {
  131. set inc to -inclination.
  132. }
  133.  
  134. set targetvelvector to angleaxis(inc,vectortobodynorm) * velocityatnode.
  135. set burnvec to targetvelvector-velocityatnode.
  136. set vectornodenormal to vcrs(velocityatnode,positionat(ship,maneuvertime)-ship:body:position).
  137. set vectornoderadial to vcrs(vectornodenormal,velocityatnode).
  138. set nodeprog to vdot(burnvec,velocityatnode:normalized).
  139. set nodenormal to vdot(burnvec,vectornodenormal:normalized).
  140. set noderadial to vdot(burnvec,vectornoderadial:normalized).
  141. set nd to node(maneuvertime, noderadial, nodenormal, nodeprog).
  142. add nd.
  143. burn_exec().
  144. set missionstatus to "Burn 3".
  145. }
  146.  
  147. function b3 {
  148. set T to vessel("ISS").
  149. set TargetAp to T:apoapsis + body:radius+1000.
  150. set ap to ship:apoapsis + body:radius.
  151. set smja to (ap + TargetAp) / 2.
  152. set targetvel to sqrt(body:mu*((2/ap)-(1/smja))).
  153. print targetvel.
  154. set now to time:seconds + 50.
  155. set maneuvertime to now.
  156. set apvel to velocityat(ship, maneuvertime):orbit:mag.
  157. print apvel.
  158. set needvel to targetvel - apvel.
  159. print needvel.
  160. set nd to node(maneuvertime, 0, 0, needvel).
  161. add nd.
  162. burn_exec().
  163. }
  164.  
  165.  
  166. function burn_exec {
  167. set max_acc to accel.
  168. set burn_duration to nd:deltav:mag/max_acc.
  169. print "Burn duration: " + burn_duration + "s".
  170. lock steering to prograde.
  171. wait 5.
  172. until nd:eta <= (burn_duration / 2 + 60) {
  173. printing().
  174. }
  175.  
  176. set np to nd:deltav.
  177. lock steering to np.
  178. until vang(np, ship:facing:vector) < 0.5 {
  179. printing().
  180. }
  181.  
  182. print("Vehicle is aligned with node").
  183. wait 4.
  184. until nd:eta <= (burn_duration / 2) {
  185. printing().
  186. }
  187.  
  188. set ship:control:fore to 0.
  189. set done to False.
  190. set dv0 to nd:deltav.
  191. until done
  192. {
  193. set max_acc to accel.
  194. set ship:control:fore to min(nd:deltav:mag+0.1 / max_acc, 1).
  195. if vdot(dv0, nd:deltav) < 0
  196. {
  197. print "Burn has been ended, dv " + nd:deltav:mag + "m/s".
  198. set ship:control:fore to 0.
  199. break.
  200. }
  201.  
  202. if nd:deltav:mag < 0.1
  203. {
  204. print "Finishing burn, dv " + nd:deltav:mag + "m/s".
  205. wait until vdot(dv0, nd:deltav) < 0.5.
  206. set ship:control:fore to 0.
  207. print "Burn has been ended, dv " + nd:deltav:mag + "m/s".
  208. set done to True.
  209. }
  210. }
  211.  
  212. unlock steering.
  213. unlock throttle.
  214. remove nd.
  215. }
  216.  
  217. function calcangle {
  218. set Sa to (2*body("Kerbin"):radius + T:altitude + ship:altitude)/2.
  219. set Ta to body("Kerbin"):radius + T:altitude.
  220. return 180*(1-(Sa/Ta)^1.5).
  221. }
  222.  
  223. function checkang {
  224. set Tap to T:apoapsis+body:radius.
  225. set Sap to (T:apoapsis+ship:altitude+2*body:radius)/2.
  226. set VecS to ship:position - body("Kerbin"):position.
  227. set VecT to T:position - body("Kerbin"):position.
  228. set VecHV to vxcl(ship:up:vector, ship:velocity:orbit).
  229. set VecST to T:position - ship:position.
  230. set t_angle to calcangle.
  231. set cur_angle to vang(VecT, VecS).
  232. set targetang to 1 - sqrt(Sap^3 / Tap^3).
  233. if vang(Vect, VecS)>90 {
  234. set cur_angle to -cur_angle.
  235. }
  236.  
  237. return (cur_angle - t_angle) < abs(targetang) + 0.1 and (cur_angle - t_angle) > abs(targetang) - 0.1.
  238. }
Advertisement
Add Comment
Please, Sign In to add comment