Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A simple kerboscript code to launch a rocketship to orbit
- // Assumes a standard rocket design with a single stage and a fairing
- // Adjust the target altitude, inclination, and heading as needed
- // Set the target altitude in meters
- set targetAlt to 100000.
- // Set the target inclination in degrees
- set targetInc to 0.
- // Set the launch heading in degrees
- set launchHeading to 90 - targetInc * cos(LATITUDE).
- // Set the initial pitch angle in degrees
- set pitchAngle to 90.
- // Set the pitch program parameters
- set pitchStartAlt to 1000. // The altitude to start pitching down
- set pitchEndAlt to 45000. // The altitude to end pitching down
- set pitchEndAngle to 10. // The final pitch angle
- // Set the steering parameters
- set maxSteer to 0.05. // The maximum steering error allowed
- set steerFactor to 0.5. // The steering correction factor
- // Set the throttle parameters
- set maxThrottle to 1. // The maximum throttle level
- set minThrottle to 0.1. // The minimum throttle level
- set throttleFactor to 0.1. // The throttle correction factor
- // Set the circularization parameters
- set circBurnAlt to 70000. // The altitude to start circularization burn
- set circBurnTolerance to 10. // The tolerance for circularization burn
- // Lock the throttle and steering
- lock throttle to maxThrottle.
- lock steering to heading(launchHeading, pitchAngle).
- // Wait for launch clamps to release
- wait until STAGE:READY.
- // Start the main engine and stage
- stage.
- // Print some information
- print "Launching rocketship to orbit.".
- print "Target altitude: " + round(targetAlt) + " m.".
- print "Target inclination: " + round(targetInc) + " deg.".
- print "Launch heading: " + round(launchHeading) + " deg.".
- // Main ascent loop
- until apoapsis > targetAlt {
- // Update the pitch angle according to the pitch program
- if altitude > pitchStartAlt and altitude < pitchEndAlt {
- set pitchAngle to map(altitude, pitchStartAlt, pitchEndAlt, 90, pitchEndAngle).
- } else if altitude >= pitchEndAlt {
- set pitchAngle to pitchEndAngle.
- }
- // Update the steering according to the prograde vector and the pitch angle
- 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)).
- // Update the throttle according to the apoapsis error and the throttle factor
- lock throttle to max(minThrottle, min(maxThrottle, maxThrottle - throttleFactor * (apoapsis - targetAlt))).
- // Print some information
- print "Altitude: " + round(altitude) + " m." at (0,0).
- print "Apoapsis: " + round(apoapsis) + " m." at (0,1).
- print "Pitch angle: " + round(pitchAngle) + " deg." at (0,2).
- print "Throttle: " + round(throttle * 100) + " %" at (0,3).
- }
- // Cut off the engine and coast to the circularization burn altitude
- lock throttle to 0.
- print "Coasting to circularization burn altitude.".
- // Wait until the circularization burn altitude is reached
- wait until altitude > circBurnAlt.
- // Calculate the circularization burn delta-v and time-to-node
- set circNode to node(time:seconds + eta:apoapsis, 0, 0, sqrt(body:mu / apoapsis) - ship:orbit:velocityat(time:seconds + eta:apoapsis):orbit:mag).
- set circDV to circNode:deltav:mag.
- set circBurnTime to circDV / ship:maxthrust * ship:mass.
- set circTimeToNode to circNode:eta - circBurnTime / 2.
- // Orient the ship towards the node and wait for the node time
- lock steering to circNode.
- wait until circTimeToNode < 0.
- // Start the circularization burn and loop until the delta-v is close enough
- lock throttle to maxThrottle.
- print "Starting circularization burn.".
- until circNode:deltav:mag < circBurnTolerance {
- // Print some information
- print "Delta-v: " + round(circNode:deltav:mag) + " m/s." at (0,4).
- print "Time to node: " + round(circTimeToNode) + " s." at (0,5).
- }
- // Cut off the engine and remove the node
- lock throttle to 0.
- remove circNode.
- print "Circularization burn complete.".
- // Deploy the fairing and unlock the steering
- if ship:fairing.
- stage.
- print "Fairing deployed.".
- unlock steering.
- // Print the final orbit information
- print "Orbit achieved.".
- print "Apoapsis: " + round(apoapsis) + " m.".
- print "Periapsis: " + round(periapsis) + " m.".
- print "Inclination: " + round(inclination) + " deg.".
- // End of script
Add Comment
Please, Sign In to add comment