Jump to content

Python based suicide burn autopilot using the KRPC mod


marcnesium-iv

Recommended Posts

Hello everyone!

I'm a casual KSP player and new to this forum. Recently I finished a project which I wanted to share with you. So I saw one of those graceful Falcon 9 booster landings and thought about doing the same in KSP. I stumbled across calculations for the simplified version of a vertical suicide burn but that was not satisfying to me. Basically, it's just simple mechanics so that problem cannot be that hard, right? Turns out it is also not that easy. After a week I was finally convinced that there is no easy analytical solution, but I had an idea on how to tackle that problem. I found the mod KRPC which gives me the possibility to control KSP with a Python script. And in that Python script I can do all the unholy things I need for a suicide burn... :D

What my program does is to evaluate the equations of motion (EOM) of a rocket burning retrograde in a gravitational field numerically. This works pretty good and in the end the autopilot stops my rocket 10 m above the ground within a few meters of the predicted landing site. However, this comes at a cost: The calculations take about 5 min. Unfortunately, Python is not the most efficient programming language but the only one I'm proficient in. But it works! And if you want to try it for yourself here is the code:

https://github.com/marcnesium-iv/SuicideBurnAutopilot

It requires Python 2.7, the Python packages numpy, scipy and krpc, and the KSP mods KRPC and MechJeb2. It should also be fairly easy to translate the code to Python 3.x -- just change print and xrange. Actually I hope that some of you will test it because there are still some minor bugs in it (no critical ones) and it's not that much tested. I wrote a small paragraph about the usage on the github page. The code should also be understandable since I left quite some comments in there.

The basic algorithm looks like this: The EOM first describes a coasting phase until the time t_burn and then the suicide burn. The scipy.integrate.solve_ivp function evaluates the EOM for a given t_burn until either the rocket crashes into the planet or it slows down to zero. scipy.optimize.minimize now tries to optimize t_burn, so the rocket stops at the end of the burn right at the surface. The main bottlenecks are the functions which determine the velocity and the altitude as both have to use KRPC functions which prevents me from using efficient numpy stuff. As I already said: It works but is not very efficient. If you have any ideas on how to speed this algorithm up, please let me know! Also all other kinds of feedback is very much appreciated. :)

Link to comment
Share on other sites

UPDATE

The calculations now take less than 1 minute! :D

I had two ideas this morning and just implemented them. The calculations are now much faster and in the order of 45 seconds. Unfortunately, the calculations are not that precise anymore and I don't know why but I'll look into it. I'll also try to make a video in the next days so you get an idea what it looks like. Since the calculations are now that fast I can use my script as a live autopilot without pausing the game.

Link to comment
Share on other sites

I would not have thought that it was possible to decrease the computation time much more. Today I simulated over 30 suicide burns and optimized the parameters. I'm now at 5 seconds computational time! :)

As I promised I also made a video of one of these landings.

 

In the beginning my lander is in a circular 40 km orbit with a inclination of 15° around Mun. My script is not capable of landing at a specific site as this would include another optimization step. So you have to perform your own deorbit burn. As you can see I chose a comparably flat descent trajectory, however, any trajectory should be fine as long as it is suborbital. And of course you should have enough time left to slow down.

Then I started the script in the console. The output shows how the optimizing algorithm is converging. "t_burn" (the time until the start of the suicide burn) is changed until the "altitude" (at the end of the suicide burn) is slightly positive. In my script I set an offset of 20 m, but for larger ships this should also be larger. After 4.6 seconds the calculations are finished and we get the parameters of the final trajectory. The ship orientates itself and warps to 10 seconds before engine ignition (or t_burn-10). The SAS is set to retrograde (relative to surface!) and we wait for the burn. Well... and then comes the burn.

About 20 m above ground we reach close to zero velocity and the MechJeb untargeted-landing autopilot is activated for the last few meters. If you compare the console output of the calculated coordinates ("final coordinates") and the actual landing coordinates, we see that we are pretty much where we expect to be.

Hope you liked it! :)

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...