Sub-Orbital Flight

This introductory tutorial uses kRPC to send some Kerbals on a sub-orbital flight, and (hopefully) returns them safely back to Kerbin. It covers the following topics:

  • Controlling a rocket (activating stages, setting the throttle)

  • Using the auto pilot to point the vessel in a specific direction

  • Using events to wait for things to happen in game

  • Tracking the amount of resources in the vessel

  • Tracking flight and orbital data (such as altitude and apoapsis altitude)

Note

For details on how to write scripts and connect to kRPC, see the Getting Started guide.

This tutorial uses the two stage rocket pictured below. The craft file for this rocket can be downloaded here.

This tutorial includes source code examples for the main client languages that kRPC supports. The entire program, for your chosen language can be downloaded from here:

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

../_images/SubOrbitalFlight.png

Part One: Preparing for Launch

The first thing we need to do is open a connection to the server. We can also pass a descriptive name for our script that will appear in the server window in game:

10        var conn = new Connection ("Sub-orbital flight");

Next we need to get an object representing the active vessel. It’s via this object that we will send instructions to the rocket:

12        var vessel = conn.SpaceCenter ().ActiveVessel;

We then need to prepare the rocket for launch. The following code sets the throttle to maximum and instructs the auto-pilot to hold a pitch and heading of 90° (vertically upwards). It then waits for 1 second for these settings to take effect.

14        vessel.AutoPilot.TargetPitchAndHeading (90, 90);
15        vessel.AutoPilot.Engage ();
16        vessel.Control.Throttle = 1;
17        System.Threading.Thread.Sleep (1000);

Part Two: Lift-off!

We’re now ready to launch by activating the first stage (equivalent to pressing the space bar):

19        Console.WriteLine ("Launch!");
20        vessel.Control.ActivateNextStage ();

The rocket has a solid fuel stage that will quickly run out, and will need to be jettisoned. We can monitor the amount of solid fuel in the rocket using an event that is triggered when there is very little solid fuel left in the rocket. When the event is triggered, we can activate the next stage to jettison the boosters:

23            var solidFuel = Connection.GetCall(() => vessel.Resources.Amount("SolidFuel"));
24            var expr = Expression.LessThan(
25                conn, Expression.Call(conn, solidFuel), Expression.ConstantFloat(conn, 0.1f));
26            var evnt = conn.KRPC().AddEvent(expr);
27            lock (evnt.Condition) {
28                evnt.Wait();
29            }

In this bit of code, vessel.resources returns a Resources object that is used to get information about the resources in the rocket. The code creates the expression vessel.resources.amount('SolidFuel') < 0.1 on the server, using the expression API. This expression is then used to drive an event, which is triggered when the expression returns true.

Part Three: Reaching Apoapsis

Next we will execute a gravity turn when the rocket reaches a sufficiently high altitude. The following uses an event to wait until the altitude of the rocket reaches 10km:

36            var meanAltitude = Connection.GetCall(() => vessel.Flight(null).MeanAltitude);
37            var expr = Expression.GreaterThan(
38                conn, Expression.Call(conn, meanAltitude), Expression.ConstantDouble(conn, 10000));
39            var evnt = conn.KRPC().AddEvent(expr);
40            lock (evnt.Condition) {
41                evnt.Wait();
42            }

In this bit of code, calling vessel.flight() returns a Flight object that is used to get all sorts of information about the rocket, such as the direction it is pointing in and its velocity.

Now we need to angle the rocket over to a pitch of 60° and maintain a heading of 90° (west). To do this, we simply reconfigure the auto-pilot:

45        Console.WriteLine ("Gravity turn");
46        vessel.AutoPilot.TargetPitchAndHeading (60, 90);

Now we wait until the apoapsis reaches 100km (again, using an event), then reduce the throttle to zero, jettison the launch stage and turn off the auto-pilot:

32        {
33            var apoapsisAltitude = Connection.GetCall(() => vessel.Orbit.ApoapsisAltitude);
34            var expr = Expression.GreaterThan(
35                conn, Expression.Call(conn, apoapsisAltitude), Expression.ConstantDouble(conn, 100000));
36            var evnt = conn.KRPC().AddEvent(expr);
37            lock (evnt.Condition) {
38                evnt.Wait();
39            }
40        }
41
42        Console.WriteLine ("Launch stage separation");
43        vessel.Control.Throttle = 0;
44        System.Threading.Thread.Sleep (1000);
45        vessel.Control.ActivateNextStage ();
46        vessel.AutoPilot.Disengage ();

In this bit of code, vessel.orbit returns an Orbit object that contains all the information about the orbit of the rocket.

Part Four: Returning Safely to Kerbin

Our Kerbals are now heading on a sub-orbital trajectory and are on a collision course with the surface. All that remains to do is wait until they fall to 1km altitude above the surface, and then deploy the parachutes. If you like, you can use time acceleration to skip ahead to just before this happens - the script will continue to work.

64        {
65            var srfAltitude = Connection.GetCall(() => vessel.Flight(null).SurfaceAltitude);
66            var expr = Expression.LessThan(
67                conn, Expression.Call(conn, srfAltitude), Expression.ConstantDouble(conn, 1000));
68            var evnt = conn.KRPC().AddEvent(expr);
69            lock (evnt.Condition) {
70                evnt.Wait();
71            }
72        }
73
74        vessel.Control.ActivateNextStage ();

The parachutes should have now been deployed. The next bit of code will repeatedly print out the altitude of the capsule until its speed reaches zero – which will happen when it lands:

76        while (vessel.Flight (vessel.Orbit.Body.ReferenceFrame).VerticalSpeed < -0.1) {
77            Console.WriteLine ("Altitude = {0:F1} meters", vessel.Flight ().SurfaceAltitude);
78            System.Threading.Thread.Sleep (1000);
79        }
80        Console.WriteLine ("Landed!");
81        conn.Dispose();

This bit of code uses the vessel.flight() function, as before, but this time it is passed a ReferenceFrame parameter. We want to get the vertical speed of the capsule relative to the surface of Kerbin, so the values returned by the flight object need to be relative to the surface of Kerbin. We therefore pass vessel.orbit.body.reference_frame to vessel.flight() as this reference frame has its origin at the center of Kerbin and it rotates with the planet. For more information, check out the tutorial on Reference Frames.

Your Kerbals should now have safely landed back on the surface.