Advertisement
Guest User

Booster Landing kOS

a guest
Nov 2nd, 2024
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.80 KB | None | 0 0
  1. print "Starting ascent script.".
  2.  
  3. function sign {
  4. parameter x.
  5. if x < 0 {
  6. return -1.
  7. } else {
  8. return 1.
  9. }
  10. }
  11.  
  12. function tradmod {
  13. parameter x, y.
  14. return x - y * floor(x / y).
  15. }
  16.  
  17. // Create GUI
  18. set descentGUI to gui(300).
  19.  
  20. // Add labels to GUI
  21. set headingHeader to descentGUI:addlabel("<b>Heading Info</b>").
  22. set headingHeader:style:align to "CENTER".
  23. set headingHeader:style:richtext to true.
  24.  
  25. set shipFacingYaw to descentGUI:addlabel("").
  26. set shipFacingPitch to descentGUI:addlabel("").
  27. set shipFacingRoll to descentGUI:addlabel("").
  28.  
  29. set vehicleHeader to descentGUI:addlabel("<b>Vehicle Info</b>").
  30. set vehicleHeader:style:align to "CENTER".
  31. set vehicleHeader:style:richtext to true.
  32.  
  33. set altitudeLabel to descentGUI:addlabel("").
  34. set radarLabel to descentGUI:addlabel("").
  35. set verticalSpeedLabel to descentGUI:addlabel("").
  36. set horizontalSpeedLabel to descentGUI:addlabel("").
  37. set throttleLabel to descentGUI:addlabel("").
  38. set massLabel to descentGUI:addlabel("").
  39. set thrustLabel to descentGUI:addlabel("").
  40. set statusLabel to descentGUI:addlabel("").
  41.  
  42. set descentHeader to descentGUI:addlabel("<b>Descent Trajectory Info</b>").
  43. set descentHeader:style:align to "CENTER".
  44. set descentHeader:style:richtext to true.
  45.  
  46. set decelerationLabel to descentGUI:addlabel("").
  47. set stopDistanceLabel to descentGUI:addlabel("").
  48. set distanceLeftLabel to descentGUI:addlabel("").
  49. set necessaryDecelerationLabel to descentGUI:addlabel("").
  50. set necessaryThrustLabel to descentGUI:addlabel("").
  51. set stationaryCounterLabel to descentGUI:addlabel("").
  52.  
  53. function UpdateGeneralGUI {
  54. set shipFacingYaw:text to "Ship Yaw: " + round(ship:facing:yaw) + "°".
  55. set shipFacingPitch:text to "Ship Pitch: " + round(ship:facing:pitch) + "°".
  56. set shipFacingRoll:text to "Ship Roll: " + round(ship:facing:roll) + "°".
  57. set altitudeLabel:text to "Altitude: " + round(ship:altitude) + " m".
  58. set radarLabel:text to "Radar: " + round(ship:bounds:bottomaltradar) + " m".
  59. set verticalSpeedLabel:text to "Vertical Speed: " + round(ship:verticalspeed) + " m/s".
  60. set horizontalSpeedLabel:text to "Horizontal Speed: " + round((ship:velocity:surface - (ship:up:vector * ship:verticalspeed)):mag) + " m/s".
  61. set throttleLabel:text to "Throttle: " + round(throttle * 100) + " %".
  62. set massLabel:text to "Mass: " + round(ship:mass, 2) + " t".
  63. set thrustLabel:text to "Thrust: " + round(ship:availablethrust) + " kN".
  64. set statusLabel:text to "Vessel Status: " + ship:status.
  65. }
  66.  
  67. function UpdateDescentGUI {
  68. set decelerationLabel:text to "Maximum Deceleration: " + round(DECELERATION, 2) + " m/s²".
  69. set stopDistanceLabel:text to "Distance Needed to Stop: " + round(STOPDISTANCE, 2) + " m".
  70. set distanceLeftLabel:text to "Distance Remaining: " + round(DISTANCELEFT, 2) + " m".
  71. }
  72.  
  73. // Define the update function
  74. function UpdateAllGUI {
  75. UpdateGeneralGUI().
  76. UpdateDescentGUI().
  77. }
  78.  
  79. set launchPosition to latlng(ship:geoposition:lat, ship:geoposition:lng):position. // landing target location, TODO rename this
  80. set chopstickPos to ship:partstagged("chopsticks")[0]:position.
  81. set neutralVec to chopstickPos - launchPosition - (vdot(chopstickPos - launchPosition, ship:up:vector) * ship:up:vector).
  82.  
  83. // Show the GUI
  84. descentGUI:show().
  85.  
  86. set targetAltitude to 75000. // Target apoapsis for orbit.
  87.  
  88. lock steering to heading(90, 90). // Point straight up initially.
  89. lock throttle to 1. // Full throttle for launch.
  90.  
  91. toggle ag3. // Start deluge
  92.  
  93. // Countdown and launch.
  94. print "5...".
  95. wait 1.
  96. print "4...".
  97. wait 1.
  98. print "3...".
  99. wait 1.
  100. print "2...".
  101. wait 1.
  102. print "Ignition!".
  103. stage.
  104. print "1...".
  105. wait 1.
  106. print "Liftoff!".
  107. toggle ag1. // Retract QD arm
  108. stage. // Activate the first stage.
  109.  
  110. UpdateGeneralGUI(). // TODO improve gui updates
  111.  
  112. // Store launch location.
  113. set originalLaunchLocation to latlng(ship:geoposition:lat, ship:geoposition:lng).
  114. set launchLocation to latlng(ship:geoposition:lat, ship:geoposition:lng + 0.00025). // landing target location, TODO rename this
  115.  
  116. // Main ascent loop.
  117. print "Starting gravity turn.".
  118. until apoapsis >= targetAltitude {
  119. UpdateGeneralGUI().
  120. // equation that gives pitch based on altitude: y=10.73485+(89.75962-10.73485)/(1+(x/11504.65))
  121. set pitch to 10.73485+(89.75962-10.73485)/(1+(ALTITUDE/11504.65)).
  122.  
  123. // make sure we can never pitch down
  124. if pitch < 0 {
  125. set pitch to 0.
  126. }
  127. lock steering to heading(90, pitch).
  128. wait 0.04.
  129. }
  130.  
  131. lock throttle to 0.3. // throttle down for separation
  132. toggle ag5. // switch to 13 engine mode
  133. wait 0.5.
  134. print "Throttling down, MECO.".
  135. toggle ag5. // switch to 3 engine mode
  136. wait 0.5.
  137.  
  138. // Stage separation and booster control.
  139. stage. // Stage separation.
  140. print "Hotstaging.".
  141.  
  142. // Activate RCS for the booster turn.
  143. wait 0.5.
  144. rcs on.
  145. print "Reaction control systems online.".
  146.  
  147. print "Turnaround for boostback burn.".
  148. lock steering to heading(90, -60). // start to turn retrograde for
  149. wait 2.
  150.  
  151. // switch back to 13 engine mode
  152. toggle ag5.
  153. toggle ag5.
  154.  
  155. // gradual change of heading to make the flip direction more predictable
  156. lock throttle to 0.2.
  157. wait 1.5.
  158. lock steering to heading(90, -89).
  159. wait 1.
  160. lock steering to heading(270, -45).
  161. wait 1.
  162. lock steering to heading(270, 0).
  163. wait 2.
  164.  
  165. print "Booster turnaround complete.".
  166. wait until vang(ship:facing:forevector, heading(270, 0):forevector) < 30. // Wait until properly aligned.
  167.  
  168.  
  169. // using 340 isp
  170. set deltaVBeforeBoostback to 9.81 * 340 * ln(ship:mass / ship:drymass).
  171. set targetDrainedDeltaV to 1600.
  172. set threeEngines to false. // flag for keeping track of engine mode
  173. set boostbackOffset to 0.015. // (working value: 0.015)
  174.  
  175. // Perform boostback burn with dynamic adjustment to minimize perpendicular velocity.
  176. print "Boostback burn initiated.".
  177. lock throttle to 1.
  178. if addons:tr:available {
  179. addons:tr:settarget(launchLocation). // Set the launch location as the target.
  180. until addons:tr:hasimpact and (addons:tr:impactpos:lng < launchLocation:lng + boostbackOffset) {
  181.  
  182. // drain excess fuel to reduce weight
  183. set valves to ship:partstagged("drainvalve").
  184. set currentDeltaV to 9.81 * 340 * ln(ship:mass / ship:drymass).
  185. if (currentDeltaV > deltaVBeforeBoostback - targetDrainedDeltaV) {
  186. for valve in valves {
  187. valve:getmodule("ModuleResourceDrain"):doaction("drain", true).
  188. }
  189. } else {
  190. for valve in valves {
  191. valve:getmodule("ModuleResourceDrain"):doaction("stop draining", true).
  192. }
  193. }
  194.  
  195. // boostback burn control loop
  196. UpdateGeneralGUI().
  197. if addons:tr:hasimpact {
  198. set targetBearing to launchLocation:heading.
  199. set horizontalVelocityVec to ship:velocity:surface - (ship:verticalspeed * ship:up:vector).
  200. set toTargetVector to (launchLocation:position - ship:position):normalized. // Vector pointing from the ship to the target
  201.  
  202. set sidewaysDirVector to vcrs(toTargetVector, ship:up:vector):normalized.
  203. set sidewaysVelocity to vdot(horizontalVelocityVec, sidewaysDirVector).
  204.  
  205. // Use sidewaysVelocity to adjust the heading
  206. set Kp to 0.2.
  207. set headingCorrection to sidewaysVelocity * Kp.
  208.  
  209. // Limit the correction angle
  210. set maxCorrectionAngle to 10.
  211. if abs(headingCorrection) > maxCorrectionAngle {
  212. set headingCorrection to maxCorrectionAngle * sign(headingCorrection).
  213. }
  214.  
  215. // Adjust throttle for finer control as we approach the target impact point
  216. set impactLongitudeError to abs(addons:tr:impactpos:lng - launchLocation:lng).
  217. if impactLongitudeError < 0.15 {
  218. set headingCorrection to (launchLocation:lat - addons:tr:impactpos:lat) * 100 .//+ sidewaysVelocity * Kp.
  219. lock throttle to 0.4. // throttle down for final adjustment
  220. } else if impactLongitudeError < 0.6 {
  221. if not threeEngines {
  222. toggle ag5. // switch to 3 engine mode
  223. set threeEngines to true.
  224. }
  225. set headingCorrection to (launchLocation:lat - addons:tr:impactpos:lat) * 100 .//+ sidewaysVelocity * Kp.
  226. }
  227.  
  228. // Adjust the desired heading, normalizing it to 0-360
  229. set desiredHeading to tradmod(targetBearing + headingCorrection, 360).
  230. lock steering to heading(desiredHeading, 0).
  231. }
  232. wait 0.05.
  233. }
  234. set ship:control:pilotmainthrottle to 0.
  235. print "Boostback burn complete.".
  236. } else {
  237. print "Trajectories mod not available. Cannot perform boostback.".
  238. }
  239.  
  240. lock throttle to 0.
  241. toggle ag2. // toggle grid fins
  242.  
  243. print "Starting entry reorientation.".
  244. // switch to 13 engine mode
  245. wait 0.2.
  246. toggle ag5.
  247. wait 0.2.
  248. toggle ag5.
  249.  
  250. set initialFacing to ship:facing. // initial facing after boostback burn
  251. set targetYaw to tradmod(ship:facing:yaw + 180, 360).
  252.  
  253. // slow precise reorientation for entry
  254. until (ship:facing:yaw > targetYaw - 15 and ship:facing:yaw < targetYaw + 10) {
  255. lock steering to R(initialFacing:pitch, ship:facing:yaw + 20, initialFacing:roll).
  256. UpdateGeneralGUI().
  257. }
  258. lock steering to retrograde.
  259. print "Reorientation for entry complete.".
  260.  
  261. set initiatedArmClose to false. // flag for keeping track of arm closure
  262.  
  263. set targetAltitude to 300. // (working value: 300)
  264. set targetSpeed to 50.
  265. set landingBurnMultiplier to 0.9. // planned throttle level (working value: 0.9)
  266.  
  267. lock distanceLeft to max(1, ship:bounds:bottomaltradar - targetAltitude). // distance left to target altitude
  268. lock slowDownLeft to ship:verticalspeed + targetSpeed. // vertical speed left to slow down to target speed
  269. lock deceleration to landingBurnMultiplier * ship:availablethrust / ship:mass. // planned deceleration (m/s^2)
  270. lock stopDistance to (slowDownLeft * slowDownLeft) / (2 * deceleration). // distance needed to stop with planned deceleration
  271.  
  272. set gAcc to constant:g * body:mass / body:radius^2.
  273.  
  274. // Descent loops with GUI updates
  275. if addons:tr:available {
  276.  
  277. print "Waiting for landing burn conditions to be met.".
  278. // First loop: wait until landing burn conditions are met
  279. until (stopDistance > distanceLeft) {
  280. UpdateAllGUI().
  281. wait 0.02.
  282. }
  283.  
  284. lock steering to addons:tr:plannedvec.
  285. lock toTargetVector to addons:tr:gettarget:position - addons:tr:impactpos:position.
  286.  
  287. // Second loop: stage of the large positional corrections and most of the deceleration
  288. print "Landing burn startup.".
  289. set steeringStrength to 0.
  290. until (-ship:verticalspeed < targetSpeed + 3) {
  291.  
  292. if steeringStrength < 0.0025 {
  293. set steeringStrength to steeringStrength + 0.00005. // increases to full strength over 1 second
  294. }
  295.  
  296. if (-ship:verticalspeed > targetSpeed + 50) {
  297. lock steering to addons:tr:plannedvec + steeringStrength * toTargetVector.
  298. } else {
  299. lock steering to R(ship:up:pitch, ship:up:yaw, -90).
  300. }
  301.  
  302. // adjust throttle for planned deceleration
  303. set necessaryDeceleration to gAcc - (targetSpeed*targetSpeed - ship:verticalspeed*ship:verticalspeed)/(2 * distanceLeft).
  304. set necessaryThrust to necessaryDeceleration * SHIP:MASS.
  305. lock throttle to necessaryThrust / ship:availablethrust.
  306.  
  307. // Update GUI
  308. UpdateAllGUI().
  309. set necessaryDecelerationLabel:text to "Necessary Deceleration for Exact Stop: " + round(necessaryDeceleration, 2) + " m/s²".
  310. set necessaryThrustLabel:text to "Necessary Thrust for Exact Deceleration: " + round(necessaryThrust) + " kN".
  311.  
  312. wait 0.02.
  313. }
  314.  
  315. // Third loop: final descent phase
  316. print "Chopstick manouvering commenced.".
  317. set stationaryCounter to 0.
  318. set finalDescent to false.
  319. set steeringStrength to 0.005.
  320. set targetTWR to 1.0.
  321. until (stationaryCounter > 20) {
  322. if addons:tr:hasimpact {
  323.  
  324. set horizontalVelocityVec to ship:velocity:surface - (ship:verticalspeed * ship:up:vector).
  325. set steeringVector to ship:up:vector + steeringStrength * toTargetVector.
  326. set steeringVectorDir to steeringVector:direction.
  327.  
  328. set thrustAngleMultiplier to 1 / (steeringVector:normalized * ship:up:vector).
  329.  
  330. if (ship:bounds:bottomaltradar > 43 and ship:bounds:bottomaltradar < 120) {
  331. set activeLoc to latlng(ship:geoposition:lat, ship:geoposition:lng):position.
  332. set directedVec to chopstickPos - activeLoc - (vdot(chopstickPos - activeLoc, ship:up:vector) * ship:up:vector).
  333. set directionSign to sign(vdot(vcrs(neutralVec, directedVec), ship:up:vector)).
  334. set movingAngle to vang(neutralVec, directedVec).
  335.  
  336. vessel("Mechazilla"):connection:sendmessage(directionSign * movingAngle).
  337. }
  338.  
  339. // original steering strength: 0.003
  340. if (-ship:verticalspeed > 15) { // if we're still going too fast at the start of the final descent
  341. set targetTWR to 2.0.
  342. set steeringStrength to 0.003.
  343. } else if (ship:bounds:bottomaltradar > 100) { // move towards chopsticks with a constant velocity
  344. set targetTWR to 1.0.
  345. set steeringStrength to 0.005.
  346.  
  347. if (not finalDescent) {
  348. toggle ag5. // switch to 3 engine mode
  349. set finalDescent to true.
  350. print "Cut back to 3 engines.".
  351. }
  352. } else if (-ship:verticalspeed > 4) { // slow down towards the second phase, arriving at the chopsticks (slower, but not final descent)
  353. set targetTWR to 2.0.
  354. set steeringStrength to 0.005.
  355. //set launchLocation to originalLaunchLocation.
  356. } else if (ship:bounds:bottomaltradar > 46) { // second phase, slow descent into the chopsticks almost until the end
  357. set targetTWR to 1.0.
  358. set steeringStrength to 0.005.
  359.  
  360. set steeringVector to ship:up:vector - (0.03 * horizontalVelocityVec).
  361. set steeringVectorDir to steeringVector:direction.
  362.  
  363. if not initiatedArmClose {
  364. vessel("Mechazilla"):connection:sendmessage("Close arms").
  365. set initiatedArmClose to true.
  366. print "Final landing insertion.".
  367. print "Chopsticks closing.".
  368. }
  369. } else if (-ship:verticalspeed > 1.1) {
  370. set targetTWR to 1.5.
  371. set steeringStrength to 0.005.
  372. } else if (-ship:verticalspeed < 0.8) {
  373. set targetTWR to 0.75.
  374. set steeringStrength to 0.005.
  375. } else { // third and final phase, slow descent into the chopsticks
  376. set targetTWR to 1.0.
  377. set steeringStrength to 0.003.
  378.  
  379. set steeringVector to ship:up:vector - (0.03 * horizontalVelocityVec).
  380. set steeringVectorDir to steeringVector:direction.
  381. }
  382.  
  383. lock steering to R(steeringVectorDir:pitch, steeringVectorDir:yaw, -90).
  384. lock throttle to targetTWR * thrustAngleMultiplier * ship:mass / (ship:availablethrust / 9.80665)..
  385.  
  386. // Update GUI
  387. UpdateAllGUI().
  388. }
  389.  
  390. if (ship:status = "LANDED" and ship:bounds:bottomaltradar < 42.5) {
  391. set stationaryCounter to stationaryCounter + 1.
  392. } else {
  393. set stationaryCounter to 0.
  394. }
  395. wait 0.02.
  396. }
  397.  
  398. set ship:control:pilotmainthrottle to 0.
  399. print "Chopstick landing successful.".
  400. }
  401.  
  402. // Hide the GUI after landing
  403. descentGUI:hide().
  404. descentGUI:dispose().
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement