Skip to main content

Control Systems with Kerbal Space Program

· 5 min read
Josh Kaplan

Feedback Control, Control Theory, Sensors and Controls; Whatever you call it, control systems is a difficult topics. For me, part of that difficult was because it was all theoretical. Here I will introduce control theory and show how it can be applied using one of my favorite games, Kerbal Space Program.

Kerbal Space Program

If you've never played the game and are even remotely interested in engineering, physics, or space stuff, you should try Kerbal Space Program (KSP) immediately. It'll set you back a one time fee of $40 (at the time of this writing), but you'll get many hours of entertainment out of it.

Why Kerbal?

  1. It's a game. It's fun.
  2. It's got a pretty solid physics engine. It takes place in a made up solar system, but all the concepts, theory, and math you learn in high school physics still apply.
  3. You get to take the concepts from the classroom and apply them in virtual world.

Control Theory

If you're a mechanical, aerospace, electrical, or other engineering student, there's a good chance you've taken or will have to take some sort of control systems class. And if you're like many other engineering students it will be one of your least favorite classes and Brian Douglas' Control Lectures will be the only thing that gets your through it.

But I believe that the course I took in college was only boring because it was so heavy on theory with absolutely no application. I'd like to try to remedy that. Before we dive into creating our own control systems with KSP, you'll need a few things:

  • Python - We'll be writing everything in Python. If you're not familiary with Python (or any programming), don't worry. We'll take it real slow.
  • kRPC - A Kerbal add-on and Python library that allows you to make remote procedure calls from Python scripts to KSP. This post describes how to install add-ons for KSP.

kRPC has a pretty good getting started section to walk you through setting it up. If you're totally unfamiliar with Python and/or programming, I'd suggest starting here. You don't need to become and expert in Python (or even good at all at this point), just make sure you know how to create a Python script and execute it.

Our First Control System

At a high level, control is all about doing something, seeing how well you did it, then adjusting based on that feedback. The classic first examples in a controls class are a thermostat or cruise control.

We'll start even simpler. Let's say we have a single stage rocket. It has a a primary liquid fuel engine and two solid boosters on the side. We know the boosters will run out of fuel before our liquid engine does and we want to automate booster separation. You can follow along using my craft file and python script.

The process will go something like this:

  1. Launch. Ignite all engines.
  2. Check how much solid fuel we have left.
  3. If we still have fuel, go back to step 2. Otherwise, proceed.
  4. We're out of fuel, separate the boosters.

The end. We've automated booster separation.

Let's see what that looks like in Python. Here's part 1 of our Python program:

import krpc
import time

# Connect to Kerbal and our vessel
conn = krpc.connect(name='Sub-orbital flight script')
vessel = conn.space_center.active_vessel

# Initiate Autopilot
vessel.auto_pilot.target_pitch_and_heading(90,90)
vessel.auto_pilot.engage()
vessel.control.throttle = 0.4
time.sleep(1)

# Launch
print('3 ... '); time.sleep(1)
print('2 ... '); time.sleep(1)
print('1 ... '); time.sleep(1)
print('Launch!')
vessel.control.activate_next_stage()

The above code is just some of the basics we need to start. The two import lines include the kRPC and time libraries (time is built-in to Python, you need to make sure you install kRPC, see above for details).

We then connect to KSP, grab the active vessel, and initiate our autopilot. Then we count down and launch.

Now comes the actual control part:

# Sleep 1 second while we still have solid fuel
while vessel.resources.amount('SolidFuel') > 0.1:
time.sleep(1)

# Once we're out of fuel, separate the boosters.
print('Booster separation')
vessel.control.activate_next_stage()

# Disengage autopilot
vessel.auto_pilot.disengage()

Here we use a while loop to continuously check how much solid fuel we have left, while it's greater than 0.1, we sleep or wait for 1 second.

Once we're out of fuel (or more precisely once our fuel is below our 0.1 threshold), we activate our rocket's next stage, which in our case is the booster separation. Lastly, we disengage the autopilot and you can takeover the flight from there.

Conclusion

This was a very brief intro to the idea of what control systems do. I plan to make this the beginning of a series of posts on using Kerbal to study control systems. This is both for my own learning and to share with others.

I'd like to dive progressively deeper into more advanced control systems to do things such as automate space launches, orbital maneuvers, even land a rocket vertically (like SpaceX!). I'd like to not only do these things, but do them well. We'll dive into PID control a learn how to build robust control systems. I'll try to balance both the theory and the application and see where it goes.

I hope you enjoy.