Postings on science, wine, and the mind, among other things.

Fly Me to the Mun (and Minmus)

Watch an autopilot programmed in kOS take an autonomous journey in Kerbal Space Program

Kerbal Space Programming (KSP) is a computer game focused on simulated rocket building and space flight. Unlike most games based in space, KSP's simulation adheres quite closely to the laws of physics as we know them, particularly in the domain of orbital mechanics. It uses an approximation known as patched conics to simulate the trajectories of space vessels and heavenly bodies. Although this approximation isn't perfect - it doesn't allow model Lagrange points, for instance - it's orders of magnitudes beyond the "fighter planes in space" physics model prevalent in many games (and movies, etc.) set in space.

The accuracy of the physics simulation in KSP is one of features which makes the game such a fun and unique challenge. While playing, one actually learns about - and indeed develops an intuitive sense for - orbital mechanics. Indeed, for this among other reasons, a number of organizations including NASA have partnered with Squad (KSP's developer) to development content for educational and scientific outreach purposes.

One of KSP's greatest assets is its enthusiastic, supportive, and highly creative community. It's worth perusing the subreddit devoted to the game to get a sense for the fantastic imagination and positivity of many of its players. One of this community's best products is a rich ecosystem of "mods" one can add to the base game. A mod, for the unfamiliar, is short for "modification" and typically represents an independent third-party contribution with which one can amend a game. Various games and developers are more or less open to modders, but KSP makes it fairly easy to add content. This has included everything from faithful replicas of historical rockets like the Saturn V, to Alcubierre warp drives, to the topic of this post - the Kerbal Operating System (kOS).

kOS is a mod that provides a scriptable autopilot for programming rockets (and other vehicles) in Kerbal Space Program. Just to emphasize: people have modded a programming language into this game. Kerbonauts have put it to use building SpaceX style recoverable rockets, sophisticated quadcopter drones, and "starships." I had been playing KSP for years before I ever picked up kOS, but eventually I could no longer resist attempting to achieve my own feats of automation.

The video embedded above is the result. A collection of scripts - which you can view and download here on my github account - send a rocket into orbit around Kerbin (the Kerbol System Earth-analog) and then land it on Kerbin's two moons - Mun and Minmus - before safely returning. All of this occurs without any intervention from me (other than the brilliant camera work) after I initialize the program just before launch. Separate routines deal with launching, orbital adjustments, inclination matching, intercepting, escaping, and landing. The video was recorded in a single take using OBS. All of the time warping happens in game, and was itself automated using kOS.

The credit for this is not all mine: in addition to kOS, the other mods I used, and KSP itself, I am indebted to many posters in r/KerbalSpaceProgram, r/kOS, and the kOS wiki. Much of my code was indirectly inspired by, or very directly cut-and-pasted from, the work of these fine people. Having implicated them, let me be quick to add that any mistakes you may discover in the code are mine alone. Indeed, the code is not particularly well structured or commented, and while the individual parts are generally solid, I doubt the whole is robust across all reasonable mission profiles. It's also worth noting that the actual flight plan followed in the video is far from the most efficient conceivable in terms of fuel, cost, or flight time. That said, I would argue that the ability to land on and return from multiple other heavenly bodies with but a single command is invaluable in itself.