wirawafiy1

Untitled

Oct 16th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. // A simple kerboscript code to launch a rocketship to orbit
  2. // Assumes a standard rocket design with a single stage and a fairing
  3. // Adjust the target altitude, inclination, and heading as needed
  4.  
  5. // Set the target altitude in meters
  6. set targetAlt to 100000.
  7.  
  8. // Set the target inclination in degrees
  9. set targetInc to 0.
  10.  
  11. // Set the launch heading in degrees
  12. set launchHeading to 90 - targetInc * cos(LATITUDE).
  13.  
  14. // Set the initial pitch angle in degrees
  15. set pitchAngle to 90.
  16.  
  17. // Set the pitch program parameters
  18. set pitchStartAlt to 1000. // The altitude to start pitching down
  19. set pitchEndAlt to 45000. // The altitude to end pitching down
  20. set pitchEndAngle to 10. // The final pitch angle
  21.  
  22. // Set the steering parameters
  23. set maxSteer to 0.05. // The maximum steering error allowed
  24. set steerFactor to 0.5. // The steering correction factor
  25.  
  26. // Set the throttle parameters
  27. set maxThrottle to 1. // The maximum throttle level
  28. set minThrottle to 0.1. // The minimum throttle level
  29. set throttleFactor to 0.1. // The throttle correction factor
  30.  
  31. // Set the circularization parameters
  32. set circBurnAlt to 70000. // The altitude to start circularization burn
  33. set circBurnTolerance to 10. // The tolerance for circularization burn
  34.  
  35. // Lock the throttle and steering
  36. lock throttle to maxThrottle.
  37. lock steering to heading(launchHeading, pitchAngle).
  38.  
  39. // Wait for launch clamps to release
  40. wait until STAGE:READY.
  41.  
  42. // Start the main engine and stage
  43. stage.
  44.  
  45. // Print some information
  46. print "Launching rocketship to orbit.".
  47. print "Target altitude: " + round(targetAlt) + " m.".
  48. print "Target inclination: " + round(targetInc) + " deg.".
  49. print "Launch heading: " + round(launchHeading) + " deg.".
  50.  
  51. // Main ascent loop
  52. until apoapsis > targetAlt {
  53.  
  54. // Update the pitch angle according to the pitch program
  55. if altitude > pitchStartAlt and altitude < pitchEndAlt {
  56. set pitchAngle to map(altitude, pitchStartAlt, pitchEndAlt, 90, pitchEndAngle).
  57. } else if altitude >= pitchEndAlt {
  58. set pitchAngle to pitchEndAngle.
  59. }
  60.  
  61. // Update the steering according to the prograde vector and the pitch angle
  62. lock steering to lookdirup(vxcl(up:vector, ship:velocity:surface), up:vector) + R(0, -maxSteer, 0) * steerFactor * (pitchAngle - vang(ship:facing:forevector, ship:velocity:surface)).
  63.  
  64. // Update the throttle according to the apoapsis error and the throttle factor
  65. lock throttle to max(minThrottle, min(maxThrottle, maxThrottle - throttleFactor * (apoapsis - targetAlt))).
  66.  
  67. // Print some information
  68. print "Altitude: " + round(altitude) + " m." at (0,0).
  69. print "Apoapsis: " + round(apoapsis) + " m." at (0,1).
  70. print "Pitch angle: " + round(pitchAngle) + " deg." at (0,2).
  71. print "Throttle: " + round(throttle * 100) + " %" at (0,3).
  72.  
  73. }
  74.  
  75. // Cut off the engine and coast to the circularization burn altitude
  76. lock throttle to 0.
  77. print "Coasting to circularization burn altitude.".
  78.  
  79. // Wait until the circularization burn altitude is reached
  80. wait until altitude > circBurnAlt.
  81.  
  82. // Calculate the circularization burn delta-v and time-to-node
  83. set circNode to node(time:seconds + eta:apoapsis, 0, 0, sqrt(body:mu / apoapsis) - ship:orbit:velocityat(time:seconds + eta:apoapsis):orbit:mag).
  84. set circDV to circNode:deltav:mag.
  85. set circBurnTime to circDV / ship:maxthrust * ship:mass.
  86. set circTimeToNode to circNode:eta - circBurnTime / 2.
  87.  
  88. // Orient the ship towards the node and wait for the node time
  89. lock steering to circNode.
  90. wait until circTimeToNode < 0.
  91.  
  92. // Start the circularization burn and loop until the delta-v is close enough
  93. lock throttle to maxThrottle.
  94. print "Starting circularization burn.".
  95. until circNode:deltav:mag < circBurnTolerance {
  96.  
  97. // Print some information
  98. print "Delta-v: " + round(circNode:deltav:mag) + " m/s." at (0,4).
  99. print "Time to node: " + round(circTimeToNode) + " s." at (0,5).
  100.  
  101. }
  102.  
  103. // Cut off the engine and remove the node
  104. lock throttle to 0.
  105. remove circNode.
  106. print "Circularization burn complete.".
  107.  
  108. // Deploy the fairing and unlock the steering
  109. if ship:fairing.
  110. stage.
  111. print "Fairing deployed.".
  112. unlock steering.
  113.  
  114. // Print the final orbit information
  115. print "Orbit achieved.".
  116. print "Apoapsis: " + round(apoapsis) + " m.".
  117. print "Periapsis: " + round(periapsis) + " m.".
  118. print "Inclination: " + round(inclination) + " deg.".
  119.  
  120. // End of script
  121.  
Add Comment
Please, Sign In to add comment