 My name is Brad Grizyak, and that is how you pronounce it, or if you're in Poland, it's Gieszek, or just Ger, right? I'm the co-founder and CEO of Bendyworks. You can find me on Twitter as Lystrophy. I do have one payload that I worked on that's currently in space, is growing food that astronauts can eat. So basically, the next step towards generation ships, which is pretty cool. But I'm here today to talk about video games and outer space, specifically Kerbal Space Program. So I'm good friends with a woman named Ash Dryden, give it up for Ash. So this is actually a quote from her, like I asked her for this quote, and she gave it to me. Whenever Brad is drinking, you want to make sure you're sitting next to him because instead of being obnoxious, he teaches you orbital mechanics. This is true. You also don't need to say the first part. You can sit next to me and I'll teach you orbital mechanics, whether I've had alcohol or not. I love teaching people about this kind of stuff. Also related to Ash, get your Altar Conf tickets. There's only two left, one of them is in Portland in like a week, and then San Francisco after that. It's a really cool thing. All right. So prerequisites for putting on a talk like this. First of all, you have to buy Kerbal Space Program. It runs for about 40 bucks on Steam. But as of today, thanks to Ben for informing me of this. It's actually on sale through November 1st. So 24 bucks gets you Kerbal Space Program. You need to install CCAN, which is basically CCAN, but for Kerbal, the Comprehensive Kerbal Archive Network. Using CCAN, you can install a thing called KRPC. You might also want to learn a few things about astrodynamics. So I would advise if you want to go to UW Madison, you would take EMA, that's Engineering Mechanics and Astronautics 550, which is Astrodynamics. And then also, you might want to take the Mechanical Engineering 446 Automatic Controls. This was the second lowest achieving class that I took in college. Mostly because it was an online class. Apparently that doesn't work so well for me. Install Ruby correctly, which is probably the hardest thing to do on this slide. And then you want to gem install KRPC. So the KRPC in the third bullet point is this generic add-on mod for Kerbal Space Program. And there's a bunch of libraries that you can then use to connect to Kerbal. And this is just the Ruby version of that. You have to write some Ruby, and you're good to go. So I'd like all of you who would like to get on the local network for, what is it, LMO Wi-Fi and go to this URL. I couldn't be bothered to port forward 80 to 3000. Also this server is running in development mode, so let's hope that it continues to work. But that should take you to this computer right here that's running a Rails server. So first thing that we're going to do today, we'll call it Project Mercury, and we're going to hover a spacecraft. Not quite good to outer space yet, just kind of hover. So that's our problem statement, make a rocket hover. What does that mean? What are some of the key words in here? Well the first is hover, and what we mean by that is we want to make altitude constant and sufficiently above zero so that we're not just standing there, we're actually above the ground. And the other key word here is rocket. So in our situation we're going to have exactly one input, which is the throttle, and it's also going to have a variable mass, which is kind of tricky to deal with because as the throttle goes above zero, you are shooting some of your mass out the business end of the rocket. And so the whole thing is going to get lighter as time progresses forward. So taking all that into account, you get some math, all right, let's do some statics here. No. So we have the force equals ma, and you get all the stuff, and you end up with this last part, which is actually a nonlinear second order differential equation is fronty face. Like you do not want to deal with this, this is terrible, you do not want this. So what are we going to do about this? This is a hard thing to do, and also it only takes into account the two forces that we know of, which is gravity and thrust. What about any sort of perturbations, or what if we don't know the perfect amount of thrust that we're sending out the business end of this rocket? Like you don't know, like is it 3,000 kilonewtons, or is it 3,001 kilonewtons? Like you don't know. So it's really hard to be able to accomplish anything by going with this correct mathematical approach. So what are we going to do instead? We're going to use what's called decontroller. PID stands for proportional integral derivative controller, which again, seems very mathy, very unattractive, like I don't want to touch that, that looks gross, calculus of times, I just don't want to do integrals. The great thing about a PID controller is that you're not actually going to be doing any integration or derivatives, like that's the really, really good news. The hardest mathematical operation you're going to use in a PID controller is division, which is pretty reasonable. So this is kind of the feedback diagram of how this is going to work. We're going to set a throttle, and that throttle goes to a rocket, which turns the throttle amount into an actual altitude. That actual altitude is going to change over time. And we're going to have a sense that detects how high we are, what our actual altitude, shunt that into our PID controller, and we're also going to tell it what our desired altitude is. And it's going to do, again, some simple mathematics, only up to division. And then it's up with a new throttle that we want to use to send back into the rocket. So it's going to automatically change the throttle based on the inputs of the actual altitude, the desired altitude, and any sort of state that it has inside of it. So this is called a negative feedback controller. We like to talk about in programming or like in the management of people we want to have positive feedback systems. And that's, I usually say that's not actually what you want. You generally want a negative feedback system. But when you're talking about a positive feedback system, what you end up with is a diverging situation, right? Which is actually what you would want in a people management of assuming that you're diverging in the positive. You want people to get better and better and better over time. In our situation, however, we don't want to diverge. We want to converge. And so we want a negative feedback system to dial into the actual desired altitude that we want. So this is our PID controller in Ruby. That's it. We do some setup. We check the current time. We do a loop. We sleep like a little bit of amount of time. And then we calculate three things. We calculate error, which is just the difference between where we are now and where we want to be. We keep track of an accumulated error. And so we take that error that we're at right now and multiply that by the difference in time that we slept. And then we take a derivative, which is basically, again, this is where the division comes in, hang with me. And we come up with another number. And then you can see we have the PID. And those are just constants that we can come up with via trial by error. Or you can actually do some really interesting math using Oplacian transforms and spend hours and hours to figure out what you could figure out in a few minutes of trial and error. So that's super exciting. But then you just set the throttle and that's it. And you save some state for later. And with that, you can actually get a rocket to hover, basically. Assuming you have some sort of control to make sure that it doesn't tip over, of course, but we're not going to deal with that. So, would you like to see what that looks like? All right, exit. So this is Kerbal Space Program. Going to start the game. We're going to resume, got a KRPC. Over here, you can see the KRPC server. This is part of the add-on that I have installed. I'm going to start that server. We're going to go to the vehicle assembly building. We're going to load a rocket that I've already created called the small, AA1 small hover. We've got to have a call sign for that, which makes it easy to find. And it's this little guy. It's, isn't it cute? So we're going to launch it with one poor soul inside of it. And her name is Valentina Kerman. So we're at 78 meters, because we're above sea level at this point. So I'm going to pull this over to the side and bring up. This is the local host's version of Space.Fail. It is the same server, but because browsers don't like when 127.0.0.1, and I don't know. That's not my area of expertise. But it works for me, and you should all be on Space.Fail colon 3000. And it just says not active, because we're not actually doing anything yet. So let's go to the terminal. We have one file in this particular folder, appropriately named go. I guess it is the opposite of no go. So we're going to just go ahead and run that. And that's going to do like a countdown three, two, one. Give me enough time to show the appropriate windows. And then here we go. So, all right, three, two, one. There we go. Let's take it off. This probably should have been refreshed. Live demos, everyone. Well, if you have it in front of you, you should be seeing some graphs. Yeah? Great. Yeah, so again, this is running in development mode. So maybe the whole asynchronous aspect isn't quite working. But if you look at the graphs, you'll see one which is the altitude, and it's very pretty, and it kind of goes like this. And then the other graph is the PID variables that change over time. And then you can see the throttle as well and the fuel. And so what we see here is that right here we have the throttle. And up here we have the height. So I had set the desired altitude to 250 meters. And so it basically feathered, oh, there we go. Well, now that all the data is gone, it's kind of boring to watch. But that's what it looks like. And as it's going, you can actually see the throttle down here is going down because the craft is getting lighter. And I didn't program this. All I wrote was basically those couple of lines of determining the error, the accumulated error, and the derivative. And it's just doing it for me. So this is an example of stepping back from what seems like a really, really hard mathematical problem in seeing, oh, someone else has solved this in a much, much easier kind of way. One way to see how this works with the decreasing mass is if we cancel this, the rocket will eventually start moving up. So up here we can see 253, 254. It's going up because it's stuck at the same throttle, but it's getting lighter. So it's just kind of going up until it runs out of fuel. So all right, so that's hovering. Cool. Next up, orbit. Project Gemini, we have a new problem statement we're going to make, a rocket orbit. We already know what a rocket is, but what does orbit mean? What should we define that as? Well, it's basically go really high and then on the way back down just miss. Thanks to Douglas Adams for the inspiration there. That's basically what's happening. You just go really high and you make sure that you're going fast enough that way so that you just kind of miss the earth as you come back down. So let's get a better visualization of what that actually means. Well, we have our rocket, first of all. It's going to be a multi-stage rocket. We have our first stage, which is these two little boosters here. Second stage is the bottom half here, and then we have a third stage. And the reason you have these different stages is because if you just have one big rocket with one stage, all the extra weight of the empty portion of the fuel tank just bogs you down quite a bit. And so you want to release mass so that your rocket gets lighter and so you can get more, you know, the force kind of results in more acceleration. So that's our rocket. And let's talk about what orbit actually means here. So here we have a space station. And let's draw a straight line. We're not going to follow the curvature of the earth. It's hard to visualize because the earth is so big. But let's just draw a horizontal line here, and it just goes off into space. Now, let's pretend that this spaceship is going to zoom really fast in that direction. And then at the same time, gravity is acting on it. And gravity is pulling it towards the center of the earth. So it pulls down in kind of a slightly slanted direction. So it falls towards the earth. So you can imagine that even though we showed actual curvature here, these two things are actually happening at the same time. So what does this mean? And you end up in this curve because they are happening at the same time. You don't get some weird jagged effect of going around the earth. So in relative terms that we can understand here, let's pretend that that's one foot above the ground, and we're going from Austin to Houston. Do you like my drawing of Minute Maid Park? So the curved distance from Austin to Houston as the bird flies is 237 kilometers. So if you go in this yellow straight line, 237 kilometers, starting at one foot above Austin, you end up 4.41 kilometers above Houston. So that's the difference in curvature of the earth between a perfectly flat line at Austin down to Houston. So I guess that means how long does it take for something to drop, 4.41 kilometers? And the answer is about 30 seconds. So now you think, OK, well, that means I guess we have to go from Austin to Houston in 30 seconds. How fast is that? That's 7.9 kilometers per second. That's really, really fast. And that is actually what you need to do in order to achieve orbit. Basically, at one foot at 200 kilometers high, it's immediately 7.8 kilometers per second. But the further away you get from the surface of the earth or really the core of the earth, the slower you have to go to be in orbit. But yeah, so if I were to run straight at Houston, I'd have to be going 7.9 kilometers per second, such that the gravity pulling me down on each step is about as fast as the earth is falling away from me. That's how fast you have to go. So that's pretty fast. What are the energies required to do something like that? Well, to get up into space, say, 200 kilometers, you do some potential energy calculations, and you end up, for every kilogram, it costs about 2 megajoules to get it up 200 kilometers. But to get it going sideways fast enough, it takes 30 megajoules per kilogram to get it going that fast to be in orbit. So we think of how much energy it takes just to get into space. And that's peanuts compared to how fast you actually have to get in order to achieve orbit. It's a difference of 15 times. So how are we going to actually achieve that? Well, we're going to have a checklist, just like NASA does. We're going to have two state machines. One of them controls the different stages, so we're going to be dropping off engines, and they might explode because they hit each other on the way down, and it's going to be really cool. And then we're going to have a second state machine, which is the control aspect, the steering and the throttle aspect. And then on each tick of the 0.1 or 0.2 seconds, we're going to evaluate what we're going to do based on the two different states. And we're just going to have a case statement that determines what to actually do. And then on each state transition, we're going to determine what to do. So for example, when we say we want to transition from stage one to stage two, we're going to tell Kerbal Space Program to activate the next stage, which drops off the engines. So you have two instances of things going on. You have an instance of doing one thing, and then you have the instance of doing it every single tick. And relying on state machines in this situation is absolutely crucial. The fewer if statements to determine what's inside of my state, the fewer of those the better. Because you want to rely really heavily on your state machines. And what we want to do is basically just pitch over so that we're going sideways by the time we get up into space while we're managing the staging of the thing. So let's see how to do that. Exit here. We are going to revert to vehicle assembly. We're going to load this rocket, as you might imagine, is not going to cut it to get into space. So we're going to use the AA-2 Kerbal-1 Launchable, which looks a little bit more like that. And you saw that already in the slides. So we have KRPC going. Valentina's back in there. Great. All right, so we're going to go up to orbit. And here you can see we've got a couple of different Ruby files. We've got a checklist file, a mission control, and then a state factory that just gets all the data out of Kerbal Space Program. So again, we're just going to run go. And this time, instead of doing a countdown, one of the cool things with this mod is that you can play with the UI in Kerbal. So we'll do that. And there's supposed to be a button that shows up, which is not showing up. So we may have to skip this demo. Do you need to restart the server? Do I need to restart the server in Kerbal? Well, probably not. But we can certainly try restarting things. Let's start it up. I doubt this is going to make a difference. You probably have to reload your browsers here. And it's not doing it. No, it needs to happen here. So let me quit out of here and come back in. In all the times that I've tested this out, this situation has never actually happened. So that's exciting. Again, it's probably one of these hitting the wrong IP addresses. Let's see here. Probably means, no, that should work. All right, well, I guess we're going to have to skip that demo. That's a bummer. Sorry, everyone. It works, I promise. Live demo is the worst. So I encourage you to get the game, because it is amazing the first time you get into space. You can look from the cockpit and like, oh, I'm in space. All right, let's go to the moon. And yes, it's spelled that way in the game. So Project Apollo, we're going to land a rocket on the moon. As programmers, we might take this problem statement and make a few interpretations or implications out of this. So for example, the landing speed is not specified in this problem statement. So we're just going to go with it and see what the stakeholder says. So orbital transfers. So what I'm going to teach to you is called the Hohmann transfer orbit. And so we have this spaceship that's in a circular orbit above a body going around in a circle. And what we're going to do is we're going to follow. And then what that's going to do, it's going to lengthen the orbit on the other side. And so it's going to go into that larger orbit. And now we're in this elliptical orbit. And we don't want to be an elliptical orbit. Perhaps we want to be in a circular orbit. So what we do, again, is fire the rockets. And that pushes the other side, the original side, into a larger orbit. And so that's how we get into a larger orbit, at least with regular chemical rockets. If we're talking about ion drives, it's a completely different thing. If you've seen The Martian, what they show in the movie is not correct. They do not need a supercomputer to figure this out. However, in the book, they do it properly. They say that it's an ion and an engine. So it just goes to show that literature is better than movies sometimes. The book was better, honestly. So how are we going to do this with getting to the moon? Well, we're going to have the moon set up. And we want to not just get to that same circle of a larger orbit, but we also want to get to that circle at the same time that the moon is there. So we're going to go there. We're going to fire an engine, go into a larger orbit, and then meet the moon at an unspecified speed. So these are all the different states that we have to do in this setup to make it happen. We've got to determine how big the burn has to be. So that's determining the insertion burn delta v. A lot of jargon here, sorry. And then we need to determine the location of where we're going to have that burn happen, because if we get to the lunar orbit, and the moon isn't there, that's a problem. So we want to determine when we want to fire the insertion burn. Then we're going to wait for the point where we're ready to burn. We're going to, we have a fire near mid here because we're going to use a warp factor in the game, so we don't have to sit here and wait until like basically the end of the conference. We're going to actually do the burn, which basically means instead of doing 100% throttle, we're going to bring it down to like 10% throttle just so that we can get a more precise finish to that. We're going to go into our trans-mooner orbit. We're going to do outer-moon orbit, mid and final approach. And final approach is the last state for obvious reasons. All right, so let's hope this works. We're going to load a save to the moon. We're actually going to start off in orbit, just to avoid the having to get into orbit and everything. So here we are, we're in orbit. There's our, this is like the last stage of a multi-stage rocket. I'm going to zoom out here. And there's the curbin, not Earth curbin. So we're on that. So this is kind of the overview orbital thing here. So this kind of looks familiar from the last couple of slides, right? We are in this tiny orbit and we want to get to this big orbit where the moon is. See, the moon. All right, so I really hope this works this time. And this, if I haven't made the implication already, the directory of where this should probably kind of give it away. All right, so this is again just going to start immediately. And so you can see, so what we're doing is we're adding a planning node that's just creeping its way out by, we're going to tell it like, add a little bit of extra oomph to it. Is that far enough? No, okay, keep going. Now we're at the right distance. But if we had just stopped there, we would have ended up where the moon is now, but not where it's going to be. Hockey and all that, skate to where the moon is going to be. So now we're determining, oh, now we're going to get into the sphere of influence of the moon. And now we've established an orbit that will figure out that will get us there. Probably refresh this. And we have kind of a dashboard. Hopefully some of you are seeing that. Again, you have to restart that, because action cable, whoo. So we're about to do our insertion burn and we're lined up and there it goes. You can see the throttle jump up. It was down here and it went up to here. And so you can see the blue line is slowly growing to where we need it to go. And this dashboard code is all written in Elm too. So if you want to talk about that afterwards, I love to spread the word of Elm. It's using the action cable Elm package that yours truly wrote. So here we go. We're doing this insertion burn. This is how much Delta V that we have left doing the insertion burn. As we get around like 30 or 20, it's going to go into that finalizing insertion burn. I'm going to jump that throttle way down, get to about zero, close enough. All right, cool. So we're going to time more because again, this would take forever. And so the problem with this game is that, well, there are many problems with the game. But one of the major problems of the game is that it doesn't do multi-body physics. It only does like two body physics. And so in the game, let's zoom in here. See, there's the sun, oh, there's the moon. So you can't do a three-body problem situation, which means you can't do Lagrange points, which is kind of unfortunate, but it was just the game a lot harder to program for the folks. So we're coming in real fast, but we're warping, right? So, okay, we're back to normal speed here. And here we go. We want a profile version of this. Coming down, coming down. There's three of them on this one. Jebediah, Bill, and Bob. Hope you enjoy the landing. And boom. And that's it. Thank you, Kerbonauts, for that. And appropriately, my Ruby program crashes once there's no vessel to talk to. So that's it. Thank you very much. If you would like to see a demo of the middle project in person, I'd be more than happy to show it to you, not up on stage. But thanks again, and I hope you enjoy the rest of the conference.