Launch into Orbit#

This tutorial launches the AeroEquus, one of the game’s stock craft, into a 150km circular orbit. Load it in the Vehicle Assembly Building and launch it to the pad before you run the program.

The program is available in a variety of languages:

Python, C#, C++, C, Java, Lua, JavaScript

Preparing for Launch#

The following code connects to the server, gets the active vessel, sets up a bunch of streams to get flight telemetry then prepares the rocket for launch. The C and Lua clients do not support streams, so their scripts read the telemetry with a call each time.

import math
import time

import krpc

turn_start_altitude = 250
turn_end_altitude = 45000
target_altitude = 150000

conn = krpc.connect(name="Launch into orbit")
vessel = conn.space_center.active_vessel

# Set up streams for telemetry
ut = conn.add_stream(getattr, conn.space_center, "ut")
altitude = conn.add_stream(getattr, vessel.flight(), "mean_altitude")
apoapsis = conn.add_stream(getattr, vessel.orbit, "apoapsis_altitude")
stage_5_resources = vessel.decouple_stage_at(stage=5).resources(
    cumulative=False
)
stage_3_resources = vessel.decouple_stage_at(stage=3).resources(
    cumulative=False
)

# Pre-launch setup
vessel.control.sas = False
vessel.control.rcs = False
vessel.control.throttle = 1.0

Ascent#

The next part of the program launches the rocket on its four solid boosters. It then adds an event for each of the two lower stages, which fires when the stage runs out of fuel, and a callback that the event calls. The callback activates two stages: one drops the empty stage, and the next lights the engine above it. It also removes its event, as the stage it watched is gone.

The callbacks run alongside the main loop. On every pass between 250 m and 45 km, the loop sets the auto-pilot’s target pitch, which gradually turns the rocket towards the horizon. It pauses for 50 ms between passes, so it does not flood the server with calls. The JavaScript program waits for the next altitude update in place of the pause. The loop exits when the rocket’s apoapsis is close to the target apoapsis.

The C and Lua clients have no events, so their loops also watch the fuel in the current stage, and activate the stages the same way.

# Activate the first stage
vessel.auto_pilot.engaged = True
vessel.auto_pilot.target_pitch_and_heading(90, 90)
vessel.control.activate_next_stage()


# Separate each stage when it runs out of fuel
def separate(event, name):
    event.remove()
    vessel.control.activate_next_stage()
    vessel.control.activate_next_stage()
    print(name, "stage separated")


first_stage_empty = conn.add_event(
    lambda: stage_5_resources.amount("SolidFuel") < 0.1
)
first_stage_empty.add_callback(lambda: separate(first_stage_empty, "First"))
second_stage_empty = conn.add_event(
    lambda: stage_3_resources.amount("LiquidFuel") < 0.1
)
second_stage_empty.add_callback(
    lambda: separate(second_stage_empty, "Second")
)

# Main ascent loop
while True:
    # Gravity turn
    if altitude() > turn_start_altitude and altitude() < turn_end_altitude:
        frac = (altitude() - turn_start_altitude) / (
            turn_end_altitude - turn_start_altitude
        )
        turn_angle = frac * 90
        vessel.auto_pilot.target_pitch_and_heading(90 - turn_angle, 90)

    # Stop the ascent near the target apoapsis
    if apoapsis() > target_altitude * 0.9:
        print("Approaching target apoapsis")
        break

    time.sleep(0.05)

Leaving the Atmosphere#

Next, the program fine tunes the apoapsis using 25% thrust, then waits until the rocket has left Kerbin’s atmosphere. Each wait is an event, which the server checks every physics tick. See Server Side Functions for how events work.

The Python, C# and JavaScript clients compile the event’s condition from a lambda, and the C++ and Java clients build it with the expression API. The C and Lua programs poll in a loop with a short pause, as those clients have no events.

# Fine tune the apoapsis at 25% thrust
vessel.control.throttle = 0.25
event = conn.add_event(
    lambda: vessel.orbit.apoapsis_altitude >= target_altitude
)
with event.condition:
    event.wait()
print("Target apoapsis reached")
vessel.control.throttle = 0.0

# Wait until out of atmosphere
print("Coasting out of atmosphere")
event = conn.add_event(lambda: vessel.flight().mean_altitude >= 70500)
with event.condition:
    event.wait()

Planning the Circularization Burn#

It is now time to plan the circularization burn. First, we calculate the delta-v required to circularize the orbit using the vis-viva equation. We then calculate the burn time needed to achieve this delta-v, using the Tsiolkovsky rocket equation.

Note

The per-stage values that feed this calculation are also available directly from the staging API: Vessel.stage_at() returns a Stage object with properties such as Stage.delta_v, Stage.specific_impulse and Stage.burn_time. The derivation is shown here because we need the burn time for a specific delta-v, rather than for burning the whole stage.

# Plan circularization burn (using vis-viva equation)
print("Planning circularization burn")
mu = vessel.orbit.body.gravitational_parameter
r = vessel.orbit.apoapsis
a1 = vessel.orbit.semi_major_axis
a2 = r
v1 = math.sqrt(mu * ((2.0 / r) - (1.0 / a1)))
v2 = math.sqrt(mu * ((2.0 / r) - (1.0 / a2)))
delta_v = v2 - v1
node = vessel.control.add_node(
    ut() + vessel.orbit.time_to_apoapsis, prograde=delta_v
)

# Calculate burn time (using rocket equation)
F = vessel.available_thrust
Isp = vessel.specific_impulse * 9.82
m0 = vessel.mass
m1 = m0 / math.exp(delta_v / Isp)
flow_rate = F / Isp
burn_time = (m0 - m1) / flow_rate

Waiting for the Burn#

Next, we need to rotate the craft and wait until the circularization burn. We point the ship along the node’s remaining burn vector, in Kerbin’s non-rotating reference frame, then time warp to 5 seconds before the burn.

# Orientate ship along the burn
print("Orientating ship for circularization burn")
frame = vessel.orbit.body.non_rotating_reference_frame
vessel.auto_pilot.reference_frame = frame
vessel.auto_pilot.target_direction = node.remaining_burn_vector(frame)
vessel.auto_pilot.wait()

# Wait until burn
print("Waiting until circularization burn")
burn_ut = ut() + vessel.orbit.time_to_apoapsis - (burn_time / 2.0)
lead_time = 5
conn.space_center.warp_to(burn_ut - lead_time)

Executing the Burn#

Finally, the program waits until half the burn time before apoapsis, then starts the burn. A loop steers along the remaining burn vector, which turns as the vessel moves around its orbit. It burns at full throttle, then at 5% for the last 5 m/s.

The burn ends when less than 0.1 m/s remains. It also ends if the remaining delta-v starts to grow, which means the burn has overshot:

# Execute burn
print("Ready to execute burn")
event = conn.add_event(
    lambda: vessel.orbit.time_to_apoapsis <= burn_time / 2.0
)
with event.condition:
    event.wait()
print("Executing burn")
remaining_burn = conn.add_stream(node.remaining_burn_vector, frame)
remaining_delta_v = conn.add_stream(getattr, node, "remaining_delta_v")
lowest_delta_v = remaining_delta_v()
while remaining_delta_v() > 0.1:
    # Steer along the remaining burn, which turns as the vessel moves
    # around its orbit
    vessel.auto_pilot.target_direction = remaining_burn()
    # Full throttle, then throttle down for the last few m/s
    vessel.control.throttle = 1.0 if remaining_delta_v() > 5 else 0.05
    # Stop if the burn overshoots, as the remaining delta-v then grows
    if remaining_delta_v() > lowest_delta_v + 0.1:
        break
    lowest_delta_v = min(lowest_delta_v, remaining_delta_v())
    time.sleep(0.05)
vessel.control.throttle = 0.0
node.remove()

print("Launch complete")

The rocket should now be in a circular 150km orbit above Kerbin.