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:
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);
1import krpc
2
3conn = krpc.connect(name="Launch into orbit")
4
5sc = conn.space_center
6mj = conn.mech_jeb
7ascent = mj.ascent_autopilot
8
9#All of these options will be filled directly into Ascent Guidance window and can be modified manually during flight.
10ascent.desired_orbit_altitude = 100000
11ascent.desired_inclination = 6
12
13ascent.force_roll = True
14ascent.vertical_roll = 90
15ascent.turn_roll = 90
16
17ascent.autostage = 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}
18ascent.enabled = True
19sc.active_vessel.control.activate_next_stage() #launch the vessel
20
21with conn.stream(getattr, ascent, "enabled") as enabled:
22 enabled.rate = 1 #we don't need a high throughput rate, 1 second is more than enough
23 with enabled.condition:
24 while enabled():
25 enabled.wait()
26
27print("Launch complete!")
28conn.close()
The rocket should now be in a circular 100km orbit with 6° inclination.