kOS (Kerbal Operating System) programs https://ksp-kos.github.io/KOS/
game
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

orbit.ks 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. clearscreen.
  2. SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.0.
  3. SET terminal:width TO 41.
  4. SET terminal:height TO 23.
  5. SET GETVOICE(0):wave to "triangle".
  6. SET GETVOICE(0):attack to 0.
  7. WAIT 0.5.
  8. print "=========================================".
  9. print " Launch to orbit program".
  10. print " First arg is a target TWR".
  11. print " Second arg is target altitude in m".
  12. print "=========================================".
  13. print "".
  14. DECLARE PARAMETER wantedTWR is 2.3, wantedALT is 95000, wantedHEAD is 90, maneuver_prep is 50.
  15. GETVOICE(0):PLAY( NOTE(300, .1) ).wait(0.1).GETVOICE(0):PLAY( NOTE(400, .2) ).wait(0.1).
  16. declare function LocalG {
  17. // Return local gravity
  18. return ship:body:mu / ship:body:position:mag ^ 2.
  19. }
  20. declare function getTwrThrust {
  21. // Estimate thrust for a given TWR (1.5 by default)
  22. DECLARE PARAMETER TWR is 1.5.
  23. set m to ship:mass.
  24. set w to m * LocalG().
  25. set p to ship:sensors:pres / 100.
  26. set t to ship:maxthrustat(p).
  27. set throt to (TWR * w) / t.
  28. if throt <= 1.0 {
  29. return throt.
  30. } else {
  31. return 1.0.
  32. }
  33. }
  34. declare function dt {
  35. SET dtim TO TIME - start.
  36. return dtim:minute+"m"+dtim:second+"s".
  37. }
  38. declare function spd {
  39. if ship:distance < 40000 {
  40. return ship:velocity:surface:mag.
  41. } else {
  42. return ship:velocity:orbit:mag.
  43. }
  44. }
  45. SET ctl TO ship:control.
  46. SET ctl:MAINTHROTTLE TO 0.0.
  47. LOCK throttle TO 0.0.
  48. print "Ready to orbit at "+wantedALT/1000+"km with TWR " + wantedTWR.
  49. HUDTEXT("Orbit "+wantedALT/1000+"km sequence ready(TWR "+wantedTWR+")", 10, 2, 20, red, false).
  50. HUDTEXT("Waiting 1st stage to run", 10, 2, 20, red, false).
  51. WAIT UNTIL not stage:ready.
  52. WAIT UNTIL ship:maxthrust() <> 0.
  53. HUDTEXT("Orbit sequence started. Lift off in 13s", 2, 2, 20, green, false).
  54. GETVOICE(0):PLAY( LIST(
  55. SLIDENOTE(300, 320, 0.45, .5), SLIDENOTE(320, 300, 0.42, 0.5),
  56. SLIDENOTE(300, 320, 0.45, .5), SLIDENOTE(320, 300, 0.42, 0.5),
  57. SLIDENOTE(300, 320, 0.45, .5), SLIDENOTE(320, 300, 0.42, 0.5))).
  58. HUDTEXT("Throttle will be " + round(getTwrThrust(wantedTWR)*100) + "% for TWR " + wantedTWR, 5, 2, 20, green, false).
  59. print "Throttle at takeoff :" + round(getTwrThrust(wantedTWR)*100) + "% for TWR " + wantedTWR.
  60. WAIT 2.0.
  61. HUDTEXT("Moving controls in all directions...", 3, 2, 15, blue, false).
  62. SET ctl:yaw TO -1.0.
  63. SET ctl:pitch TO -1.0.
  64. SET ctl:roll TO -1.0.
  65. WAIT 1.0.
  66. HUDTEXT("So cool !", 3, 2, 12, blue, false).
  67. SET ctl:yaw TO 1.0.
  68. SET ctl:pitch TO 1.0.
  69. SET ctl:roll TO 1.0.
  70. WAIT 1.0.
  71. SET ctl:yaw TO 0.0.
  72. SET ctl:pitch TO 0.0.
  73. SET ctl:roll TO 0.0.
  74. WAIT 1.0.
  75. DECLARE i TO 10.
  76. UNTIL i < 1 {
  77. HUDTEXT(i, 1, 2, 20, green, false).
  78. print i+" " AT (0,7).
  79. GETVOICE(0):PLAY( NOTE(300, .2) ).
  80. wait(1.0).
  81. SET i TO i - 1.
  82. }
  83. lock THROTTLE to getTwrThrust(wantedTWR).
  84. HUDTEXT("Lift off !", 2, 2, 20, green, false).
  85. GETVOICE(0):PLAY( NOTE(400, .1) ).wait(0.1).GETVOICE(0):PLAY( NOTE(300, .2) ).
  86. DECLARE start TO TIME.
  87. print "-----------------------------------------".
  88. print dt() + " Lift off !".
  89. UNTIL spd() > 1 {
  90. WAIT 0.1.
  91. }
  92. HUDTEXT(dt()+" Rotation 0", 2, 2, 20, green, false).
  93. GETVOICE(0):PLAY( NOTE(400, .1) ).wait(0.1).GETVOICE(0):PLAY( NOTE(400, .1) ).
  94. lock STEERING to HEADING(wantedHEAD,90).
  95. print dt() + " Heading "+ wantedHEAD.
  96. UNTIL spd() > 50 {
  97. WAIT 0.1.
  98. }
  99. HUDTEXT(dt()+" Rotation 1", 2, 2, 20, green, false).
  100. GETVOICE(0):PLAY( NOTE(400, .1) ).wait(0.1).GETVOICE(0):PLAY( NOTE(300, .2) ).
  101. SET tgt_spd TO 600.
  102. print dt() + " Next rotation at "+tgt_spd+" m/s".
  103. lock STEERING to HEADING(wantedHEAD,70).
  104. UNTIL spd() > tgt_spd {
  105. WAIT 0.1.
  106. }
  107. HUDTEXT(dt()+" Rotation 2", 2, 2, 20, green, false).
  108. GETVOICE(0):PLAY( NOTE(400, .1) ).wait(0.1).GETVOICE(0):PLAY( NOTE(300, .2) ).
  109. print dt() + " Last rotation 1000 m/s".
  110. //declare function endrot {
  111. // return 60 - ((spd() - tgt_spd) * (60 - 10) / (1000 - tgt_spd)).
  112. //}
  113. //lock STEERING to HEADING(wantedHEAD,endrot()).
  114. //WAIT UNTIL spd() > 1000 or SHIP:ORBIT:APOAPSIS > wantedALT.
  115. //if endrot() >= 10 {
  116. // LOCK STEERING to HEADING(wantedHEAD, 10).
  117. //} else {
  118. // LOCK STEERING to ship:prograde.
  119. //}
  120. UNTIL spd() > 1000 {
  121. LOCK STEERING to ship:prograde.
  122. WAIT 0.1.
  123. }
  124. LOCK STEERING to ship:prograde.
  125. HUDTEXT("Waiting for AP to reach 80km", 5, 2, 20, green, false).
  126. GETVOICE(0):PLAY( NOTE(400, .1) ).wait(0.1).GETVOICE(0):PLAY( NOTE(300, .2) ).
  127. print dt() + " Waiting for AP to reach "+wantedALT/1000+"km".
  128. WAIT UNTIL SHIP:ORBIT:APOAPSIS > wantedALT.
  129. HUDTEXT("Apoapsis 100km reached", 5, 2, 20, green, false).
  130. GETVOICE(0):PLAY( LIST(NOTE(300, .1),NOTE(400, .2),NOTE(400, 0.2), NOTE(220,0.2)) ).
  131. UNLOCK throttle.
  132. SET ctl:MAINTHROTTLE TO 0.0.
  133. LOCK THROTTLE to 0.0.
  134. LOCK steering TO ship:prograde.
  135. print dt() + " Engine cut-off".
  136. print dt() + " Waiting for AP. ETA : " + ROUND(ETA:APOAPSIS) + "s".
  137. // TODO : calculate timeout given TWR
  138. SET ins_alrm TO addAlarm("Maneuver",time:seconds+(ETA:APOAPSIS - maneuver_prep + 0.6), "[kOS] "+(wantedALT/1000)+"km orbit insertion", "Targeting "+(wantedALT/1000)+"km").
  139. WAIT UNTIL MOD(round(eta:apoapsis), 5) = 0.
  140. UNTIL ETA:APOAPSIS < maneuver_prep {
  141. if wantedALT - SHIP:ORBIT:APOAPSIS > 100 {
  142. LOCK throttle TO 1.0.
  143. WAIT UNTIL SHIP:ORBIT:APOAPSIS > wantedALT.
  144. LOCK throttle TO 0.0.
  145. }
  146. if MOD(round(ETA:APOAPSIS), 5) = 0 {
  147. HUDTEXT("Time to apoapsis " + round(eta:apoapsis) + "s", 5, 2, 20, green, false).
  148. HUDTEXT("Time to insertion maneuver " + round(eta:apoapsis) - maneuver_prep + "s", 5, 2, 20, green, false).
  149. }
  150. WAIT .3.
  151. }
  152. DELETEALARM(ins_alrm:id).
  153. LOCK steering TO ship:prograde.
  154. DECLARE prev_ETA to ETA:APOAPSIS.
  155. LOCK throttle TO 1.0.
  156. SET throt TO SHIP:CONTROL:MAINTHROTTLE.
  157. SET throt_min TO 0.7.
  158. SET throt_brk TO 0.2.
  159. print dt() + " Starting insertion".
  160. GETVOICE(0):PLAY( NOTE(300, .1) ).wait(0.1).GETVOICE(0):PLAY( NOTE(400, .2) ).
  161. HUDTEXT("Starting insertion", 5, 2, 20, green, false).
  162. // TODO : Problem occurs when there is stages left to drop
  163. // causing wrong estimation overtaking AP
  164. UNTIL SHIP:ORBIT:PERIAPSIS > wantedALT {
  165. SET dETA TO ETA:APOAPSIS - prev_ETA.
  166. if ETA:APOAPSIS > 120 {
  167. print dt() + " AP behind us ! Full thrust to safe orbit !".
  168. HUDTEXT("AP behind us ! Full thrust for orbit !", 0.1 ,2,20, red, false).
  169. SET throt TO 1.0.
  170. WAIT UNTIL SHIP:ORBIT:PERIAPSIS > 68000.
  171. HUDTEXT("Wrong orbitreached "+dt(), 25, 2, 20, red, false).
  172. break.
  173. } else {
  174. if dETA < 0 {
  175. SET throt to throt + 0.0005.
  176. if throt > 1.0 {
  177. print dt() + "Node approach, notenough thrust !!".
  178. SET throt TO 1.0.
  179. WAIT 1.0.
  180. }
  181. if throt < throt_min - throt_brk {
  182. GETVOICE(0):PLAY( SLIDENOTE(220,250, .3, .3) ).
  183. set throt to throt_min.
  184. } else {
  185. //GETVOICE(0):PLAY( NOTE(300 + ((throt - .5) * 500), .1) ).
  186. }
  187. }
  188. if dETA > 0 {
  189. SET throt TO throt - 0.0005.
  190. if throt < 0.0 {
  191. print "Failing ! Node escape, WTF !!".
  192. SET throt TO 0.
  193. } else {
  194. if throt < throt_min - throt_brk AND ETA:APOAPSIS > 10
  195. {
  196. // Breaking !
  197. GETVOICE(0):PLAY( SLIDENOTE(250, 220, .3, .3) ).
  198. SET throt TO 0.
  199. LOCK throttle TO 0.
  200. print "AP ETA " + round(ETA:APOAPSIS,1) + "s throttle BREAK" AT (0, terminal:height - 1).
  201. if ETA:APOAPSIS > 20 {
  202. WAIT((ETA:APOAPSIS / 3.5)-0.1).
  203. } else {
  204. if ETA:APOAPSIS > 10 {
  205. WAIT 1.0.
  206. }
  207. }
  208. } else {
  209. //GETVOICE(0):PLAY( NOTE(300 + ((throt - .5) * 500), .1) ).
  210. }
  211. }
  212. }
  213. }
  214. //SET SHIP:CONTROL:MAINTHROTTLE TO throt.
  215. //SET SHIP:CONTROL:PILOTMAINTHROTTLE TO throt.
  216. LOCK throttle TO throt.
  217. //LOCK throttle TO throt.
  218. SET prev_ETA to ETA:APOAPSIS.
  219. print "AP ETA " + round(ETA:APOAPSIS,1) + "s throttle "+ round(throt * 100) + "% " AT (0, terminal:height - 1).
  220. //HUDTEXT("AP ETA " + ETA:APOAPSIS + "s throttle "+ throt * 100 + "%", 0.1,2,20, green, false).
  221. WAIT 0.
  222. }
  223. print dt() + " Orbiting !".
  224. HUDTEXT("Orbiting after "+dt(), 15, 2, 20, green, false).
  225. HUDTEXT("programs exiting...", 15, 2, 20, red, false).
  226. GETVOICE(0):PLAY( LIST(
  227. SLIDENOTE(300, 320, 0.3, .3), SLIDENOTE(320, 300, 0.3, 0.3),
  228. SLIDENOTE(300, 320, 0.3, .3), SLIDENOTE(320, 300, 0.3, 0.3),
  229. SLIDENOTE(300, 320, 0.3, .3), SLIDENOTE(320, 300, 0.3, 0.3))).
  230. unlock STEERING.
  231. unlock throttle.
  232. SET SHIP:CONTROL:MAINTHROTTLE TO 0.0.
  233. SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.0.