Code Example

Tutorial

In this section we’re going to build our very first script that uses FALL module. It’s assumed that you’re using stock KSP aerodynamic model and know the basics of Kerbal Operating System mod.


Goals and requirements

Mission sequence:

  • Liftoff
  • Pitchover at velocity of 90 m/s, by 10 degrees with 3 degrees aoa
  • Ascent until apoapsis > 8000 meters and then turn off the engine
  • Land safely

Rocket should:

  • Have RCS thrusters, preferably at the top
  • Be able to turn retrograde when falling

What do we need?

Because our goal is to perform an ascent and then safely touchdown, we’re going to use only two structures:

  • ascentController
  • hoverslamModel

Implementation

First let’s import the structures we need:

runoncepath("0:/fall/controllers/ascentController").
runoncepath("0:/fall/models/hoverslamModel").

Now we initialize new ascentController object with previously defined parameters:

// -- [ Prelaunch ] -------------
local ascent is ascentController(8000, 90, 10, 3).

Short launch sequence(assuming the rocket is held by clamps) can be written as:

// -- [ Launch ] ----------------
stage. // turn on engines
lock throttle to 1.
stage. // release clamps

Rocket is now lifting off, but isn’t steered in any way. Let’s give full control to our ascent object:

// -- [ Ascent ] ----------------
ascent["passControl"]().

At this stage the rocket will perform ascent until apoapsis(8000 meters) goal is reached. Then the engine will shutdown. Oh no! Don’t panic. First we will turn retrograde, and wait until the rocket is falling.

// -- [ Shutdown ] --------------
RCS ON.
lock steering to ship:srfretrograde.
wait until ship:verticalspeed < 0. // waiting for descent

And now we will safely land thanks to the magic(not really) of hoverlsamModel object:

// -- [ Landing ] ---------------

// number 10 comes from the height of the rocket in meters
// it's advised to overshoot it a little bit(by say 3-5 meters)
local hoverslam is hoverslamModel(10).
lock throttle to hoverslam["getThrottle"]().

wait until ship:status = "splashed" or ship:status="landed".
// We've landed!

Code in its entirety looks like this:

runoncepath("0:/fall/controllers/ascentController").
runoncepath("0:/fall/models/hoverslamModel").

// -- [ Prelaunch ] -------------
local ascent is ascentController(8000, 90, 10, 3).

// -- [ Launch ] ----------------
stage.
lock throttle to 1.
stage.

// -- [ Ascent ] ----------------
ascent["passControl"]().

// -- [ Shutdown ] --------------
RCS ON.
lock steering to ship:srfretrograde.
wait until ship:verticalspeed < 0. // waiting for descent


// -- [ Landing ] ---------------
local hoverslam is hoverslamModel(10).
lock throttle to hoverslam["getThrottle"]().


wait until ship:status = "splashed" or ship:status="landed".
// We've landed!

More Tutorials