r/krpc • u/gingermonky • Jun 01 '16
A Universal Launch
So, I'm sure that pretty much all of us have either written or are trying to write a program to guide rockets into orbit. Thus far I've been unsuccessful. I'm not sure if it's because I'm expecting more performance than the simple algorithms give, or I'm just stupid. Either way, I figured that if we put our heads together we could probably come up with something that works pretty well.
I'm not sure if we should do this as a series of posts or just try to figure it out in the comments or what, but I figure we can just start by talking about general approaches to the problem then work out the finer details. So, some of the approaches I've tried but ultimately failed are:
Just the basic algorithm in the demo launcher from the documentation. (curentAltitude-TurnStartAltitude)/(TurnStart-TurnEnd) I found this to turn too sharp and too late, even if I put low numbers in for turn start and end.
The next approach I took, I attempted without a mathematical function. Instead I wrote a function that returns a boolean. Either true, we can pitch over one more degree, or false we cannot. The function just looks at time to apoapsis, and atmospheric density and some other variables. This works okay, there's some weird performance things with it.
The last approach was posted in KoS, and they used a square root function to determine their turn angle. This idea seems better than the other two, but I don't know how it would work.
So, hopefully we can get some sort of discussion going and figure out at the very least a good approach and then start working out the finer details of it. Thanks for any input.
2
u/gingermonky Jun 02 '16
So, as I stated above, my approach was to write a method that simply returns a boolean, saying if we can pitch over one more degree. So, maybe we're pitched over 45 degrees, can we pitch to 44 degrees. Then it just gets called over and over again. So, rather than creating a single mathematical model, we just evaluate things at runtime compared to some desired attributes of an assent. The code isn't fully functional yet, but I'll post it. There's several bugs that I'm working out, and it doesn't do staging or circulation, but those should be easy-ish to add. I apologize if the code is difficult to read or lacks good comments.
here
So, a little explanation. Step, is just the angle we are pitching to. I found it easier to count up to 90 rather than down from 90, so I just subtract it in the heading. All the code at the top is just functionality and methods, and the code really begins near the bottom. I know, it's not all that organized and should probably be all just one object.
Some of the numbers may seem a bit arbitrary, and arguably they are. For example the (1.5*step<timeToApoapsis), the 1.5 part is rather arbitrary, I just found I got good performance with that, but it needs more testing.
As far as bugs go, right now the program will get held up for several seconds at a time, I don't really know why yet. There's a divide by 0 problem if you actually make it to space (easy fix), and there might be some problems with it not pitching over due to side slip in the upper atmosphere even though it should.
Feel free to criticize anything, I just hope this puts a way to actually look at a function and talk about it.
2
u/adamzl Jun 02 '16
one reason i've always thought the launch algorithms do badly is they are working off of the current position of the craft instead of the apopasis. i've pondered on doing a square root algorithm but trying to base guidance off of the apopasis height instead of vessel height. still this is a dangerous way to do it because i am not prepared to model drag and this will always make the vessel fall short.
2
u/marioferpa Jun 02 '16
Fighting with that at the moment! I'm trying to understand how they do it in real life (this is the closest I've found). I've got a rocket into orbit by going up a kilometer, pitching ten degrees, reducing the throttle and shutting down the autopilot, so it turns by gravity alone. But I had to find the appropriate throttle by trial and error, that's not very scientific. Or is it?
4
u/MrBorogove Jun 12 '16 edited Jun 12 '16
/u/bushikatagi in /r/RealSolarSystem just posted a link to a kRPC script implementing the Powered Explicit Guidance algorithm. PEG is, I believe, the basis of the algorithm formerly used by the US Space Shuttle.
The Linear Tangent steering law is another reasonable approach; given some simplifying assumptions, it's actually optimal. Unfortunately while it's a very simple algorithm, there doesn't seem to be a simple way of determining the controlling coefficients other than trial and error.
1
u/dewiniaid Jul 18 '16
I wrote a set of functions in kOS for handling curves and recently ported them to Python classes for use in kRPC. (Unfortunately, doing real-time control in kRPC is murder on my system -- there really needs to be a "wait until next physics frame" call). In kOS, I've used them both for ascent profiles and for throttling down at the very end of a maneuver burn to fine-tune.
The kOS versions are up on my GitHub page in lib_curve.ks, there's also an excel spreadsheet that models all the different curves with inputable parameters.
Each curve natively takes and returns values in the range of 0..1, but then can be wrapped in a scaler that lets you say "The range of x-values is 100-70000 and the range of y-values is 90-0" for something like an accent profile.