Ruby Programming

Orbital Rocket Guidance with Ruby

Orbital Rocket Guidance with Ruby

by Nate Berkopec

In the video titled "Orbital Rocket Guidance with Ruby," Nate Berkopec discusses how to program a simulated spacecraft computer using the Ruby programming language within the context of Kerbal Space Program, a physics-based space simulation game. The session, held at RubyConf 2017, explores both historical and modern approaches to spacecraft guidance, ultimately demonstrating how to launch a Saturn V rocket into orbit. Key points discussed in the video include:

  • Introduction of the Project: Nate introduces himself as the Mission Control Director for the Ruby Space Program, highlighting the emphasis on programming a space computer using Ruby despite its lack of aerospace relevance.

  • Using Kerbal Space Program (KSP): The session demonstrates how KSP serves as a robust testing environment, leveraging a mod called Realism Overhaul that replicates the actual solar system. The use of the KRPC library allows Ruby scripts to interact with the KSP API, enabling control over the spacecraft.

  • Key Requirements for Space Computers: Berkopec outlines three critical features that space computers must satisfy: real-time operation, high reliability with redundancy, and simplicity in design.

  • Historical Context: He touches on the evolution of space computing, referencing the Apollo Guidance Computer (AGC) and its innovative methods, such as multitasking and redundancy for reliability.

  • Programming with Ruby: Nate provides examples of how to structure Ruby programs to handle the essential tasks necessary for launching a rocket, including managing fuel levels and staging sequences.

  • Focus on Guidance Systems: The video emphasizes the implementation of guidance systems for trajectory optimization during launch, detailing the processes behind ignition sequences, thrust monitoring, and gravity turns—a technique used by the Saturn V to transition from atmospheric flight to orbital insertion.

  • Takeaways from the Session: The demonstration culminates in launching the simulated Saturn V rocket, showcasing how even simple programming can achieve complex results akin to historical space missions. Berkopec emphasizes that while the focus was on coding aspects, the principles explored reflect genuine challenges faced by early space missions.

Overall, Berkopec advocates for the use of KSP as a fun and engaging tool for learning programming concepts, especially in emulating the past achievements in space exploration. Participants are encouraged to delve further into programming and space simulation using the concepts introduced throughout the session.

00:00:10.730 We are keeping an eye on that power transfer at T-minus 50 seconds. The S-IVB propellants are now all pressurized. The S-IVB propellant is the third stage of the Saturn V. We are at one minute and 15 seconds and counting. The spacecraft equipment is now on its own internal cooling. It has been sharing its cooling from an external power source up to this time.
00:00:32.450 We are now approaching the T-minus one-minute mark. T-minus one minute and humanity encounters buffeting today because of our countdown. We anticipate quite a shake in 30 seconds. They'll keep it settled down. In fact, it is interesting to see if they play slow to get past the T-minus 50 seconds mark, when the power transfer takes place—that's first stage, second stage, and third stage, along with the instrument unit, going to internal power.
00:01:09.560 At T-minus 37 seconds, our count continues to go well. We're now looking for the ignition of those five first-stage engines at the T-minus 8.9 second mark. We've passed T-minus 30 seconds, T-minus 25 seconds and counting, and Apollo 13 is go. T-minus 20 seconds, T-minus 20 seconds and counting. 17 guidance released—15, 14, 13, 12, 11, 10, 9, 8.
00:01:34.429 Ignition sequence has started. 6, 5, 4, 3.
00:01:50.990 My name is Nate Berkopec, and I am the Mission Control Director for the Ruby Space Program. As you may know, we have had a lot of budget cuts over the last couple of years, so you are here today to learn how to program a spacecraft computer in the Ruby programming language.
00:02:27.160 Obviously, Ruby is not known for its aerospace applications, but we're not going to let that stop us. Like I said, my name is Nate, and I know nothing about space. I have no background in space at all; in fact, all I do is write about how to make Ruby web applications faster. I have no qualifications for this, but that doesn't stop me from teaching this session.
00:02:49.310 We are going to be using a program called Kerbal Space Program. Kerbal Space Program is a video game that simulates spacecraft and space launches. It is more known for its quirky, wacky scenarios. This game is really just a physics sandbox, and people perform lots of crazy experiments in it. However, it is also an extremely accurate physics simulator and can model real-world space launches.
00:03:39.360 We'll use a mod compilation called Realism Overhaul, which transforms Kerbal Space Program's universe into a model of the actual solar system. Today, we're going to launch a Saturn V rocket into orbit with a program written in Ruby. The Ruby part is managed by a library called KRPC, which stands for Kerbal Remote Procedure Call. This library turns any running Kerbal Space Program session into an API that you can make calls to.
00:04:50.840 With the help of Google Protobufs, there exists a robust Ruby client library. Basically, you can control anything in Kerbal Space Program that you'd normally control with the keyboard or joystick. Here’s an example program for KRPC written in Ruby: we first require it, connect to a running Kerbal Space Program with KRPC installed, and then we have a vessel object that will hold all of our telemetry, such as speed and orientation.
00:05:19.840 Next, we will create a control object that allows us to change the throttle, activate stages, and perform other essential tasks. I've used Pry to open up a console for interactively working with different objects provided by KRPC. Now, let's discuss how we are going to design a program to help us reach orbit.
00:06:50.650 Space computers need to fulfill three vital requirements. First, they must operate in real time because a rocket launch is fast-paced. These computers have to perform calculations quickly and interact with astronauts in real time. Back in the 1960s, you might input a punch card into a machine and receive results ten minutes later. So, the concept of real-time computing was revolutionary at that time.
00:08:14.880 Space computers also have to be extremely reliable. For instance, the Apollo Guidance Computer had all components triplicated. If one sensor malfunctioned, it would take results from the other two. This redundancy was necessary to ensure functionality in the harsh environment of space, characterized by a vacuum and wildly varying temperatures. Failure of the computer could cost lives.
00:09:25.360 Finally, space computers must be simple. Complicated computers are difficult to manage in the unpredictable environment of space, which is why simple designs are prioritized. The origins of space computing trace back to Charles Draper, who led the Draper MIT Lab.
00:09:45.210 In the 1950s, he successfully demonstrated a computer capable of flying a plane autonomously from the East Coast to the West Coast without relying on external landmarks, relying only on gyroscopes and accelerometers. This was vital for space guidance as there are no external reference points in space.
00:10:09.660 In the 1950s, sounding rockets such as the Aerobee were constructed for upper atmosphere research. The common design had two stages with a liquid-fueled sustainer stage on top and a solid-fuel booster stage. The booster stage would ignite first and last only five seconds, enough to lift the rocket off the pad.
00:11:49.420 We can observe an Aerobee rocket launch where the rocket is adjusted to penetrate outer space on a photographic mission. The view from 50 miles up is breathtaking!
00:12:19.760 As the rocket ascends, the view from a hundred miles becomes increasingly impressive. Most sounding rockets were spin-stabilized, meaning that fins were canted slightly to impart a spin as they ascended. This design enhances stability, akin to how spinning tops maintain their upright positions.
00:12:52.880 Now, in our Ruby program to launch a sounding rocket into the upper atmosphere, we need to accomplish three important objectives. First, we must detach from the launch tower when thrust is at maximum. Most rockets do not achieve immediate full thrust upon ignition; it takes time for the thrust to build-up. If we detach from the launch pylon too soon, the rocket may tip over.
00:14:59.580 Next, just before the booster stage cuts out, we must ignite the second stage, ensuring that both stages fire at the same time. This is crucial because if the first stage burns out before the second stage is ignited, fuel sloshing in the second stage tank could prevent the second engine from lighting. This process is known as hot staging.
00:15:59.710 To simulate this in Kerbal Space Program, we will create a client object from KRPC to connect, wait for the first-stage ignition until it reaches 60% of maximum thrust, detaching the launch pylon once that threshold is met. Then, we will manage fuel levels carefully, activating the sustainer rocket when there’s about 10% fuel remaining in the first stage.
00:17:11.360 Manned space flight demonstrates a significant difference in computer roles. In the Soviet space program, astronauts were almost like passengers, as most tasks were automated. Electromechanical computers, such as the globus I am unit, were employed instead of digital ones, representing a simpler architecture that couldn't fail.
00:18:56.500 On the other hand, the Apollo Guidance Computer (AGC) marks a significant advancement in computer technology. Weighing 70 pounds and roughly the size of a contemporary PC tower, it contained critical software with limited system resources. Despite these limitations, it successfully guided missions by relying on meticulous programming, which was often handcrafted.
00:20:43.090 The AGC operated in a multitasking environment using what's called priority-based multitasking. It had several 'corsets,' which were areas assigned for different jobs, but only a limited number of tasks could run simultaneously. Managing those corsets required cooperation among jobs to ensure everything ran smoothly.
00:22:51.979 In our Ruby program, we'll use classes for our jobs and implement a structure to track priorities. When a task is completed, we'll yield control back to the executive system, allowing for seamless multitasking. This model will help us understand how the AGC could manage its tasks efficiently.
00:24:43.620 To make this effective, our program will also incorporate features from the AGC’s wait list method. This approach enables scheduling work efficiently by manipulating task priorities and executing them at specified intervals.
00:26:49.550 Additionally, the AGC required the capability to restart at any time due to unpredictable power fluctuations. We’ll emulate this checking system with a 'night watchman' method in Ruby, automatically checking task execution at predetermined intervals.
00:27:20.530 The Apollo 11 landing exemplified how crucial restart protocols were. During the descent to the lunar surface, the AGC raised a 1202 alarm caused by the landing radar power issue. The astronauts kept rebooting the computer, which allowed them to continue their descent successfully despite ongoing computer overload.
00:29:50.940 Now, we are going to write our launch program, focusing on guidance, which determines the precise ascent path to achieve orbit. Utilizing the inertial measurement unit (IMU) and gyroscopes, we will maintain correct positioning while maneuvering the rocket.
00:31:20.700 In essence, we’ll implement basic guidance for vertical ascent and horizontal acceleration to achieve orbit around 200 kilometers above the Earth. The Saturn V performed a gravity turn, utilizing an open-loop guidance system that followed pre-defined commands.
00:31:59.930 During launch, we’ll monitor rocket parameters, ensuring we remain oriented correctly. For example, once out of the atmosphere, the guidance system can make adjustments based on real-time data. This process is crucial to transitioning from an atmospheric launch to orbital travel.
00:34:50.100 Monitoring pitch angles and acceleration profiles allows us to optimize trajectory planning. As we near the end of our ascent, we'll prepare to detach the second stage and engage our sustainer rocket to finalize our orbital insertion.
00:36:33.940 In Kerbal Space Program, our program performs the ignition sequence, waits for the necessary thrust, then activates the gravity turn task—and we carefully manage staging to ensure smooth transitions through each phase of the launch.
00:37:54.320 As we execute a successful launch, our trajectory shifts seamlessly, ensuring we reach orbit effectively while employing the principles reminiscent of space flight operations from the Apollo era.
00:39:06.470 This is how we launch into orbit using Ruby! Thank you for your attention! If you have questions, I am available at the provided GitHub address.
00:40:35.020 Did I ever turn off the garbage collector? No, I didn’t go that deep. Any other questions about the Apollo Guidance Computer?
00:40:44.929 I don't know if I have time for a lengthy discussion on it, but I can tell you that it was an elegant system with fewer than seventy thousand lines of code, nothing compared to contemporary computing!
00:40:59.599 Regarding recreating other Apollo missions, this was just the launch phase, which does not involve the AGC directly. Landing and many other operations were managed by distinct systems. The Apollo missions were far more complicated.
00:41:17.170 However, this is an engaging way to utilize Kerbal Space Program and explore how simple programming can mimic aspects of real space missions!