Advertisement
hubin162577

twokmclosing

Oct 21st, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Closing on Space Station from 2 km
  2. clearscreen.
  3. set pi to 3.1415926535897932384626433832795028841971693993751058209749445923.
  4. set mu to 3.986*(10^14).
  5. ag1 off.
  6. set target to "LIESS".
  7. set lateraloffset to 0.
  8. set verticaloffset to 0.
  9. rcs on.
  10. sas off.
  11. gear off.
  12. lock steering to prograde + r(0,0,180).
  13.  
  14. set maneuverSpeed to 1.
  15.  
  16. //functions
  17. function lateraldist {
  18. set uct to vcrs(ship:up:vector,target:position). //up cross target
  19. set ev to vcrs(ship:body:position,ship:north:vector).
  20.  
  21. if vang(uct,ship:north:vector) > 90 {
  22. set thdg to vang(uct,ev).
  23. } else {
  24. set thdg to 360-vang(uct,ev). //heading of the target vector
  25. }.
  26. if thdg > 360 {
  27. set thdg to thdg-360.
  28. }.
  29. set ucv to vcrs(ship:up:vector,ship:velocity:orbit).
  30. if vang(ucv,ship:north:vector) > 90 {
  31. set vhdg to vang(ucv,ev).
  32. } else {
  33. set vhdg to 360-vang(ucv,ev). //heading of the velocity vector
  34. }
  35.  
  36. if vhdg-thdg > 180{
  37. set hdgdiff to vhdg-thdg-360.
  38. }. else {
  39. set hdgdiff to vhdg-thdg.
  40. }.
  41. set latdist to sin(hdgdiff)*target:position:mag.
  42. return latdist.
  43. }.
  44. function verticaldist {
  45. set velangle to vang(ship:velocity:orbit,ship:up:vector).
  46. set targangle to vang(target:position,ship:up:vector).
  47. set pitchdiff to velangle-(targangle-verticaloffset).
  48. set vdist to sin(pitchdiff)*target:position:mag.
  49. return vdist.
  50. }.
  51. function depthdist {
  52. set targpos to (target:position+ship:body:position).
  53. set ang to vang(targpos,ship:body:position)*pi/180.
  54. set ddist to ang*ship:body:position:mag.
  55. return ddist.
  56. }
  57. function lateralinputFunction {
  58. if abs(lateraldistance2) > 10 {
  59. set vlat to (lateralDistance2-lateralDistance1)/(dt).
  60. if lateralDistance2 > 0 {
  61. set ms to -maneuverSpeed.
  62. }. else {
  63. set ms to maneuverSpeed.
  64. }
  65. set vlatError to ms-vlat.
  66. set latinput to kpv*vlatError.
  67. }. else {
  68. set slatError to lateralDistance2.
  69. set vlat to (lateralDistance2-lateralDistance1)/(dt).
  70. set latinput to -((kps*lateralDistance2)+(kds*vlat)).
  71. }.
  72. return latinput.
  73. }
  74. function verticalinputFunction {
  75. if abs(verticalDistance2+extraH) > 10 {
  76. set vver to (verticalDistance2-verticalDistance1)/(dt).
  77. if verticaldistance2+extraH > 0 {
  78. set ms to -maneuverSpeed.
  79. }. else {
  80. set ms to maneuverSpeed.
  81. }
  82. set vverError to ms-vver.
  83. set vertinput to kpv*vverError.
  84. ///////////////////////////////////////////
  85. }. else {
  86. set sverError to (verticaldistance2+extraH).
  87. set vver to (verticalDistance2-verticalDistance1)/(dt).
  88. set vertinput to -((kps*sverError)+(kds*vver)).
  89. }.
  90. return vertinput.
  91. }.
  92. function depthinputFunction {
  93. if abs(depthDistance2-depthoffset) > 10 {
  94. set vdep to (depthDistance2-depthDistance1)/(dt).
  95. if depthdistance2-depthoffset > 0 {
  96. set ms to -maneuverSpeed.
  97. }. else {
  98. set ms to maneuverSpeed.
  99. }
  100. set vdepError to ms-vdep.
  101. set depinput to -kpv*vdepError.
  102. ///////////////////////////////////////////
  103. }. else {
  104. set sdepError to depthdistance2-depthoffset.
  105. set vdep to ((depthDistance2)-(depthDistance1))/(dt).
  106. set depinput to ((kps*sdepError)+(kds*vdep)).
  107. }.
  108. return depinput.
  109. }
  110. ////////////////////////////////////////////////////////////////////////////////
  111. //defining values and gains
  112. set kpv to 1.
  113. set kps to .1.
  114. set kds to 2.
  115.  
  116. set latinput to 0.
  117. set verticalinput to 0.
  118. set depthinput to 0.
  119. set extraH to 0.
  120. set vver to 0.
  121. set depthoffset to depthdist().
  122.  
  123. //matching planes
  124. set lateraldistance2 to 4123.
  125.  
  126. until lateralDistance2<200 {
  127. if 2 > 1 {
  128. set lateralDistance1 to lateraldist().
  129. set verticalDistance1 to verticaldist().
  130. set depthDistance1 to depthdist().
  131. set t1 to time:seconds.
  132. print "Lateral Difference "+round(lateralDistance1)+" m".
  133. print "Vertical Difference "+round(verticalDistance1)+" m".
  134. print "Depth Difference "+round(depthDistance1)+" m".
  135. print " ".
  136. print "Lateral Input "+round(100*latinput).
  137. print "Vertical Input "+round(100*verticalinput).
  138. print "Depth Input "+round(100*depthinput).
  139. print " ".
  140. print "Depth Offset "+round(depthoffset)+" m".
  141. wait 0.2.
  142. clearscreen.
  143. set lateralDistance2 to lateraldist().
  144. set verticalDistance2 to verticaldist().
  145. set depthDistance2 to depthdist().
  146. set t2 to time:seconds.
  147. set dt to t2-t1.
  148. }. //this is just so i can hide it easily
  149. ////////////////////////////////////////////
  150. set lateralinput to 0.
  151. set verticalinput to verticalinputfunction().
  152. set depthinput to depthinputfunction().
  153. ///////////////////////////////////////////
  154. set ship:control:starboard to lateralinput.
  155. set ship:control:top to verticalinput.
  156. set ship:control:fore to depthinput.
  157.  
  158. if sas =true {
  159. unlock steering.
  160. }. else {
  161. lock steering to prograde+r(0,0,180).
  162. }.
  163.  
  164. }.
  165.  
  166. lock steering to prograde+r(0,0,180).
  167.  
  168. //Two KM station keeping
  169. until ag1 {
  170. if 2 > 1 {
  171. set lateralDistance1 to lateraldist().
  172. set verticalDistance1 to verticaldist().
  173. set depthDistance1 to depthdist().
  174. set t1 to time:seconds.
  175. print "Lateral Difference "+round(lateralDistance1)+" m".
  176. print "Vertical Difference "+round(verticalDistance1)+" m".
  177. print "Depth Difference "+round(depthDistance1)+" m".
  178. print " ".
  179. print "Lateral Input "+round(100*latinput).
  180. print "Vertical Input "+round(100*verticalinput).
  181. print "Depth Input "+round(100*depthinput).
  182. print " ".
  183. print "Depth Offset "+round(depthoffset)+" m".
  184. wait 0.2.
  185. clearscreen.
  186. set lateralDistance2 to lateraldist().
  187. set verticalDistance2 to verticaldist().
  188. set depthDistance2 to depthdist().
  189. set t2 to time:seconds.
  190. set dt to t2-t1.
  191. }. //this is just so i can hide it easily
  192. ////////////////////////////////////////////
  193. set lateralinput to lateralinputFunction().
  194. set verticalinput to verticalinputfunction().
  195. set depthinput to depthinputfunction().
  196. ///////////////////////////////////////////
  197. set ship:control:starboard to lateralinput.
  198. set ship:control:top to verticalinput.
  199. set ship:control:fore to depthinput.
  200. }.
  201.  
  202.  
  203.  
  204. set extrah to -1000. //initiates downward maneuver
  205. ag1 off.
  206.  
  207.  
  208. //downward maneuver begins
  209. until ag1 {
  210. if 2 > 1 {
  211. set lateralDistance1 to lateraldist().
  212. set verticalDistance1 to verticaldist().
  213. set depthDistance1 to depthdist().
  214. set t1 to time:seconds.
  215. print "Lateral Difference "+round(lateralDistance1)+" m".
  216. print "Vertical Difference "+round(verticalDistance1)+" m".
  217. print "Depth Difference "+round(depthDistance1)+" m".
  218. print " ".
  219. print "Lateral Input "+round(100*latinput).
  220. print "Vertical Input "+round(100*verticalinput).
  221. print "Depth Input "+round(100*depthinput).
  222. print " ".
  223. print "Depth Offset "+round(depthoffset)+" m".
  224. wait 0.2.
  225. clearscreen.
  226. set lateralDistance2 to lateraldist().
  227. set verticalDistance2 to verticaldist().
  228. set depthDistance2 to depthdist().
  229. set t2 to time:seconds.
  230. set dt to t2-t1.
  231. }. //this is just so i can hide it easily
  232. ////////////////////////////////////////////
  233. set lateralinput to lateralinputFunction().
  234. set verticalinput to verticalinputfunction().
  235. set depthinput to depthinputfunction().
  236. ///////////////////////////////////////////
  237. set ship:control:starboard to lateralinput.
  238. set ship:control:top to verticalinput.
  239. set ship:control:fore to depthinput.
  240. }.
  241. //I found a short boost foreward works well. this is about 1 m/s dv prograde
  242. //foreward acceleration averages .4 m/s2
  243. set ship:control:neutralize to true.
  244. print "Executing foreward boost maneuver".
  245. set t0 to time:seconds.
  246. set ship:control:fore to 1.
  247. wait until time:seconds>t0+2.5.
  248. set ship:control:neutralize to true.
  249.  
  250. //coasting
  251. set extrah to 0.
  252. clearscreen.
  253. set ship:control:neutralize to true.
  254.  
  255. lock steering to prograde + r(0,0,180).
  256. ag1 off.
  257. unlock steering.
  258. sas on.
  259. wait until ship:altitude<(target:altitude-21).
  260. until ship:altitude>(target:altitude-20) {
  261. print "Coasting One".
  262. print "Altitude Difference: "+round(abs(target:altitude-ship:altitude))+" m".
  263. wait 0.1.
  264. clearscreen.
  265. }.
  266. set warp to 0.
  267. sas off.
  268. lock steering to prograde + r(0,0,180).
  269. print "Preparing for Stationkeeping".
  270. wait until ship:altitude>target:altitude.
  271. set depthoffset to depthdist().
  272.  
  273.  
  274. //stationkeeping at approximately 1km
  275. until ag1 {
  276. if 2 > 1 {
  277. set lateralDistance1 to lateraldist().
  278. set verticalDistance1 to verticaldist().
  279. set depthDistance1 to depthdist().
  280. set t1 to time:seconds.
  281. print "Lateral Difference "+round(lateralDistance1)+" m".
  282. print "Vertical Difference "+round(verticalDistance1)+" m".
  283. print "Depth Difference "+round(depthDistance1)+" m".
  284. print " ".
  285. print "Lateral Input "+round(100*latinput).
  286. print "Vertical Input "+round(100*verticalinput).
  287. print "Depth Input "+round(100*depthinput).
  288. print " ".
  289. print "Depth Offset "+round(depthoffset)+" m".
  290. wait 0.2.
  291. clearscreen.
  292. set lateralDistance2 to lateraldist().
  293. set verticalDistance2 to verticaldist().
  294. set depthDistance2 to depthdist().
  295. set t2 to time:seconds.
  296. set dt to t2-t1.
  297. }. //this is just so i can hide it easily
  298. ////////////////////////////////////////////
  299. set lateralinput to lateralinputFunction().
  300. set verticalinput to verticalinputfunction().
  301. set depthinput to depthinputfunction().
  302. ///////////////////////////////////////////
  303. set ship:control:starboard to lateralinput.
  304. set ship:control:top to verticalinput.
  305. set ship:control:fore to depthinput.
  306. }.
  307. ag1 off.
  308.  
  309. //initiates second closing maneuver
  310. set maneuverspeed to 0.5.
  311. set extrah to -1000.
  312. ag1 off.
  313.  
  314.  
  315. until ag1 {
  316. if 2 > 1 {
  317. set lateralDistance1 to lateraldist().
  318. set verticalDistance1 to verticaldist().
  319. set depthDistance1 to depthdist().
  320. set t1 to time:seconds.
  321. print "Lateral Difference "+round(lateralDistance1)+" m".
  322. print "Vertical Difference "+round(verticalDistance1)+" m".
  323. print "Depth Difference "+round(depthDistance1)+" m".
  324. print " ".
  325. print "Lateral Input "+round(100*latinput).
  326. print "Vertical Input "+round(100*verticalinput).
  327. print "Depth Input "+round(100*depthinput).
  328. print " ".
  329. print "Depth Offset "+round(depthoffset)+" m".
  330. wait 0.2.
  331. clearscreen.
  332. set lateralDistance2 to lateraldist().
  333. set verticalDistance2 to verticaldist().
  334. set depthDistance2 to depthdist().
  335. set t2 to time:seconds.
  336. set dt to t2-t1.
  337. }. //this is just so i can hide it easily
  338. ////////////////////////////////////////////
  339. set lateralinput to lateralinputFunction().
  340. set verticalinput to verticalinputfunction().
  341. set depthinput to depthinputfunction().
  342. ///////////////////////////////////////////
  343. set ship:control:starboard to lateralinput.
  344. set ship:control:top to verticalinput.
  345. set ship:control:fore to depthinput.
  346. }.
  347.  
  348. set ship:control:neutralize to true.
  349. print "Executing foreward boost maneuver".
  350. set t0 to time:seconds.
  351. set ship:control:fore to 1.
  352. wait until time:seconds>t0+1.25. //average acceleration 0.4. this is 0.5 m/s dv
  353. set ship:control:neutralize to true.
  354.  
  355. set extrah to 0.
  356. clearscreen.
  357. set ship:control:neutralize to true.
  358. lock steering to prograde + r(0,0,180).
  359. ag1 off.
  360. unlock steering.
  361. sas on.
  362. wait until ship:altitude<(target:altitude-21).
  363. until ship:altitude>(target:altitude-20) {
  364. print "Coasting".
  365. print "Altitude Difference: "+round(abs(target:altitude-ship:altitude))+" m".
  366. wait 0.1.
  367. clearscreen.
  368. }.
  369. set warp to 0.
  370. sas off.
  371. lock steering to prograde + r(0,0,180).
  372. print "Preparing for Stationkeeping".
  373. wait until ship:altitude>target:altitude.
  374. set depthoffset to depthdist().
  375.  
  376.  
  377. //stationkeeping at approximately 500m
  378. until ag1 {
  379. if 2 > 1 {
  380. set lateralDistance1 to lateraldist().
  381. set verticalDistance1 to verticaldist().
  382. set depthDistance1 to depthdist().
  383. set t1 to time:seconds.
  384. print "Lateral Difference "+round(lateralDistance1)+" m".
  385. print "Vertical Difference "+round(verticalDistance1)+" m".
  386. print "Depth Difference "+round(depthDistance1)+" m".
  387. print " ".
  388. print "Lateral Input "+round(100*latinput).
  389. print "Vertical Input "+round(100*verticalinput).
  390. print "Depth Input "+round(100*depthinput).
  391. print " ".
  392. print "Depth Offset "+round(depthoffset)+" m".
  393. wait 0.2.
  394. clearscreen.
  395. set lateralDistance2 to lateraldist().
  396. set verticalDistance2 to verticaldist().
  397. set depthDistance2 to depthdist().
  398. set t2 to time:seconds.
  399. set dt to t2-t1.
  400. }. //this is just so i can hide it easily
  401. ////////////////////////////////////////////
  402. set lateralinput to lateralinputFunction().
  403. set verticalinput to verticalinputfunction().
  404. set depthinput to depthinputfunction().
  405. ///////////////////////////////////////////
  406. set ship:control:starboard to lateralinput.
  407. set ship:control:top to verticalinput.
  408. set ship:control:fore to depthinput.
  409.  
  410. }.
  411. ag1 off.
  412.  
  413. //initiates third closing maneuver
  414. set maneuverspeed to 0.25.
  415. set extrah to -1000.
  416. ag1 off.
  417.  
  418.  
  419. until ag1 {
  420. if 2 > 1 {
  421. set lateralDistance1 to lateraldist().
  422. set verticalDistance1 to verticaldist().
  423. set depthDistance1 to depthdist().
  424. set t1 to time:seconds.
  425. print "Lateral Difference "+round(lateralDistance1)+" m".
  426. print "Vertical Difference "+round(verticalDistance1)+" m".
  427. print "Depth Difference "+round(depthDistance1)+" m".
  428. print " ".
  429. print "Lateral Input "+round(100*latinput).
  430. print "Vertical Input "+round(100*verticalinput).
  431. print "Depth Input "+round(100*depthinput).
  432. print " ".
  433. wait 0.2.
  434. clearscreen.
  435. set lateralDistance2 to lateraldist().
  436. set verticalDistance2 to verticaldist().
  437. set depthDistance2 to depthdist().
  438. set t2 to time:seconds.
  439. set dt to t2-t1.
  440. }. //this is just so i can hide it easily
  441. ////////////////////////////////////////////
  442. set lateralinput to lateralinputFunction().
  443. set verticalinput to verticalinputfunction().
  444. set depthinput to depthinputfunction().
  445. ///////////////////////////////////////////
  446. set ship:control:starboard to lateralinput.
  447. set ship:control:top to verticalinput.
  448. set ship:control:fore to depthinput.
  449. }.
  450.  
  451. set ship:control:neutralize to true.
  452. print "Executing foreward boost maneuver".
  453. set t0 to time:seconds.
  454. set ship:control:fore to 1.
  455. wait until time:seconds>t0+.5. //average acceleration 0.4. this is 0.25 m/s dv
  456. set ship:control:neutralize to true.
  457.  
  458. set extrah to 0.
  459. clearscreen.
  460. set ship:control:neutralize to true.
  461. lock steering to prograde + r(0,0,180).
  462. ag1 off.
  463. print "Coasting".
  464. unlock steering.
  465. sas on.
  466. wait until ship:altitude<(target:altitude-11).
  467. until ship:altitude>(target:altitude-10) {
  468. print "Coasting".
  469. print "Altitude Difference: "+round(abs(target:altitude-ship:altitude))+" m".
  470. wait 0.1.
  471. clearscreen.
  472. }.
  473. set warp to 0.
  474. sas off.
  475. lock steering to prograde + r(0,0,180).
  476. print "Preparing for Stationkeeping".
  477. wait until ship:altitude>target:altitude.
  478. set depthoffset to 175.
  479.  
  480. //stationkeeping at approximately 175m
  481. set maneuverSpeed to 1.
  482. until ag1 {
  483. if 2 > 1 {
  484. set lateralDistance1 to lateraldist().
  485. set verticalDistance1 to verticaldist().
  486. set depthDistance1 to target:position:mag.
  487. set t1 to time:seconds.
  488. print "Lateral Difference "+round(lateralDistance1)+" m".
  489. print "Vertical Difference "+round(verticalDistance1)+" m".
  490. print "Depth Difference "+round(depthDistance1)+" m".
  491. print " ".
  492. print "Lateral Input "+round(100*latinput).
  493. print "Vertical Input "+round(100*verticalinput).
  494. print "Depth Input "+round(100*depthinput).
  495. print " ".
  496. print "Depth Offset "+round(depthoffset)+" m".
  497. wait 0.2.
  498. clearscreen.
  499. set lateralDistance2 to lateraldist().
  500. set verticalDistance2 to verticaldist().
  501. set depthDistance2 to target:position:mag.
  502. set t2 to time:seconds.
  503. set dt to t2-t1.
  504. }. //this is just so i can hide it easily
  505. ////////////////////////////////////////////
  506. set lateralinput to lateralinputFunction().
  507. set verticalinput to verticalinputfunction().
  508. set depthinput to depthinputfunction().
  509. ///////////////////////////////////////////
  510. set ship:control:starboard to lateralinput.
  511. set ship:control:top to verticalinput.
  512. set ship:control:fore to depthinput.
  513.  
  514. }.
  515. set ship:control:neutralize to true.
  516. sas on.
  517. ag1 off.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement