The First Launch

This tutorial launches any rocket into a 100km circular orbit with 6° inclination.

The complete program is available in a variety of languages:

Java, Python

We start with connecting to the server, getting AscentAutopilot module and setting launch parameters. Parameters which are omitted will be the same as in Ascent Guidance window.

12    public static void main(String[] args) throws IOException, RPCException, StreamException {
13        try(Connection conn = Connection.newInstance("Launch into orbit")) {
14            SpaceCenter sc = SpaceCenter.newInstance(conn);
15            MechJeb mj = MechJeb.newInstance(conn);
16            AscentAutopilot ascent = mj.getAscentAutopilot();
17
18            //All of these options will be filled directly into AscentGuidance window and can be modified manually during flight.
19            ascent.setDesiredOrbitAltitude(100000);
20            ascent.setDesiredInclination(6);
21
22            ascent.setForceRoll(true);
23            ascent.setVerticalRoll(90);
24            ascent.setTurnRoll(90);
25
26            ascent.setAutostage(true);
27            ascent.setEnabled(true);

Next, we engage the autopilot and activate the first stage. After that, the autopilot takes over control of the vessel. The program uses streams to check if the autopilot is still running. When the autopilot stops, the program continues. You can learn more about streams in kRPC documentation for your language: C#, C++, Java, Lua, Python.

28            sc.getActiveVessel().getControl().activateNextStage(); //launch the vessel
29
30            Stream<Boolean> enabled = conn.addStream(ascent, "getEnabled");
31            enabled.setRate(1); //we don't need a high throughput rate, 1 second is more than enough
32            synchronized(enabled.getCondition()) {
33                while(enabled.get())
34                    enabled.waitForUpdate();
35            }
36            enabled.remove();
37
38            System.out.println("Launch complete!");
39        }
40    }
41}

The rocket should now be in a circular 100km orbit with 6° inclination.