Jump to content

kOS: Looking for SpaceX landing script


Rdivine

Recommended Posts

Inspired by the multiple successful landings of the first stage of Falcon 9 of SpaceX, i've decided to make my own rocket. I've split the landing into 2 parts ; Boostback and Final Descent.

Here is the boostback script:

Spoiler

clearscreen.

set ag8 to false.
set ag9 to false.
set ag10 to false.
set boostback to 0.
set hdg to 0.
set roll to 0.
set pitch to 0.
set targhdg to 0.
set targpitch to 0.
set targfuel to 0.
set timepitch to 0.
set payloadmass to 0.
set cutvel to 0.
set cutheight to 0.
set launch to 0.

wait 0.1.
set payloadmass to mass - 158.75.
print"Payload Mass: " + round(payloadmass,1) + "t   ".

set cutvel to 0.0000018*(payloadmass^6) + 8*payloadmass + 680.
set cutheight to (cutvel+10.416)/0.029025.

when boostback >= 0 then {
    set hdg to ship:bearing.
    if hdg <= 0 {
        set hdg to (-1*hdg).
    }
    else if hdg > 0 {
        set hdg to 360 - hdg.
    }.
    set roll to 90 - vectorangle(up:vector,ship:facing:starvector).
    set pitch to 90 - vectorangle(ship:facing:vector,up:vector).
    print"      pitch:" + round(pitch,1) + "deg   " at (0,3).
    print"       roll:" + round(roll,1) + "deg   " at (0,4).
    print"        hdg:" + round(hdg,1) + "hdg   " at (0,5).
    print"    targhdg:" + round(targhdg,1) + "hdg   " at (0,6).
    print"  targpitch:" + round(targpitch,1) + "deg   " at (0,7).
    print"   targfuel:" + round(targfuel,1) + "   " at (0,8).
    print"payloadmass:" + round(payloadmass,1) + "t   " at (0,9).
    print"     cutvel:" + round(cutvel,1) + "m/s   " at (0,10).
    print"  cutheight:" + round(cutheight,1) + "m   " at (0,11).
    print"shipbearing:" + round(ship:bearing,1) + "deg   " at (0,12).
    preserve.
}.

wait until verticalspeed > 100.
set launch to 1.
when launch = 1 then {
    if ship:velocity:surface:mag >= cutvel and
        ship:altitude >= cutheight {
        set targhdg to hdg + 180.
        if targhdg > 360 {
            set targhdg to targhdg - 360.
        }.
        set launch to 2.
    }.
        if stage:liquidfuel <= 350 {
        set targhdg to hdg + 180.
        if targhdg > 360 {
            set targhdg to targhdg - 360.
        }.
        set launch to 2.
    }.
    preserve.
}.
when launch = 2 then {
    set ship:control:pilotmainthrottle to 0.
    lock throttle to 0.
    print"Pressing for stage separation." at (0,2).
    if mass < 62 {
        set launch to 3.
    }.
    preserve.
}.

wait until launch = 3.
sas on.
wait 0.05.
sas off.
gear on.
wait 0.05.
gear off.
brakes on.
wait 0.05.
brakes off.
set boostback to 1.
unlock throttle.
set ship:control:pilotmainthrottle to 0.
ag6 on.

set targpitch to 70 - 0.01*stage:liquidfuel.

rcs on.
unlock steering.
wait 0.05.
sas on.
wait 0.05.
set sasmode to "retrograde".
wait 8.
sas off.
wait 0.05.
lock steering to heading(targhdg,targpitch).
wait 12.
if stage:liquidfuel < 400 {
    set boostback to 3.
}
else if stage:liquidfuel >= 400 {
    set boostback to 2.
    set targfuel to 173 + 0.37*stage:liquidfuel.
}.
wait 0.1.
rcs off.
when boostback = 2 then {
    if stage:liquidfuel > targfuel {
        lock throttle to 1.
    }
    else if stage:liquidfuel - 10 < targfuel {
        set ship:control:pilotmainthrottle to 0.
        lock throttle to 0.
        set boostback to 3.
    }.
    preserve.
}.

wait until boostback = 3.
unlock steering.
unlock throttle.
set ship:control:pilotmainthrottle to 0.
sas off.
wait 0.1.
sas on.
wait 0.1.
rcs on.
set sasmode to "retrograde".

reboot.
wait until mass < 0.

And here's the landing script:

Spoiler

clearscreen.
print"Falcon 9 landing software V4.31.".

set land to 16.
set decentheight to 0.
set maxacc to 0.
set maxthr to 0.
set trueacc to 0.
set acctarget to 0.
set throttdif to 0.
set altkm to 0.
set pressure to 0.
set proppc to 0.
set basepc to stage:liquidfuel + 0.00000001.
set refh to 0.
set oceanc to 0.
set terrainheight to 0.

set vmone to 0.
set vmtwo to 0.
set vmthree to 0.
set targh to 0.

set stamp1 to 0.
set stamp2 to 0.
set tburn to 0.

set engconfig to 0.
set terraintype to "OCEAN".

set ship:control:pilotmainthrottle to 0.
print"Stage 1 Telemetry----------" at (0,6).

log "                                                            " to land.txt.
log "------------------Landing Telemetry Begin-------------------------------" to land.txt.
log "LANDING ATTEMPT:" + shipname to land.txt.
log "LANDING SOFTWARE VERSION: V4.31" to land.txt.
    
when land = 16 then {
    set maxacc to (ship:maxthrust/mass) - 9.81.
    print"      Max Thrust:" + round(ship:maxthrust,0) + "kN          " at (0,7).
    print"            Mass:" + round(mass,2) + "t               " at (0,8).
    print"Max Acceleration:" + round(maxacc,2) + "m/s^2            " at (0,9).
    print"    Decentheight:" + round(decentheight,0) + "m           " at (0,10).
    preserve.
}.
    
when land > 0 and
    land < 16 then {
    set terrainheight to ship:altitude - alt:radar.
    set proppc to (100*stage:liquidfuel)/basepc.
    set altkm to ship:altitude/1000.
    set pressure to 1.01321*(2.7182818^(-(altkm + 0.001*verticalspeed)/5.6)).
    set maxthr to ship:availablethrustat(pressure).
    set maxacc to (ship:maxthrust/mass) - 9.81.
    set trueacc to ((ship:control:pilotmainthrottle*ship:availablethrustat(pressure))/mass) - 9.81.
    if (maxacc - 6.5) <= 0 {
        set vmone to 65.
        set vmtwo to 37.
        set vmthree to 28.
    }
    else if (maxacc - 6.5) > 0 {
        set vmone to 12*(maxacc - 6.5) + 65.
        set vmtwo to 3.5*(maxacc - 6.5) + 37.
        set vmthree to 28.
    }.
    print" Terrain Height:" + round((ship:altitude-alt:radar),0) + "m           " at (0,7).
    print"       Altitude:" + round((alt:radar - 26.0825),0) + "m               " at (0,8).
    print"       Velocity:" + round(airspeed,0) + "m/s         " at (0,9).
    print"    Groundspeed:" + round(groundspeed,0) + "m/s          " at (0,10).
    print"     Propellant:" + round(stage:liquidfuel,0) + " (" + round(proppc,0) + "%)   " at (0,11).
    print"  Engine Thrust:" + round((ship:availablethrustat(pressure)*ship:control:pilotmainthrottle),0) + "/" + round(maxthr,0)+ "kN (" + round(ship:control:pilotmainthrottle,2) + ")   " at (0,12).
    print"           Mass:" + round(1000*mass,0) + "kg          " at (0,13).
    print"   Decentheight:" + round(decentheight,0) + "m           " at (0,14).
    print"         Maxacc:" + round(maxacc,3) + "m/s^2         " at (0,15).
    print"           refh:" + round(refh,1) + "m       " at (0,16).
    print"          targh:" + round(targh,1) + "m         " at (0,17).
    print"      acctarget:" + round(acctarget,1) + "m/s^2          " at (0,18).
    print"        trueacc:" + round(trueacc,1) + "m/s^2         " at (0,19).
    print"           land:" + round(land,0) + " state        " at (0,20).
    preserve.
}.

when land = 3 then {
    set decentheight to 8.3*ship:velocity:surface:mag - 700*(1/(1+2.71828^(-2*((maxacc - 6.5)+0.63)))) + 610 - 0.07*(ship:altitude-alt:radar) - (9/maxacc)*verticalspeed.
    if (maxacc - 6.5) <= 0 {
        set decentheight to decentheight - 45.5*((maxacc - 6.5)^3).
    }
    else if (maxacc - 6.5) > 0 {
        set decentheight to decentheight - (3*((maxacc - 6.5)^2) + 24.5*(maxacc - 6.5)).
    }.
    if (alt:radar + verticalspeed - 1000) < decentheight {
    set warp to 0.
    }.
    if (alt:radar + verticalspeed) < decentheight {
        set stamp1 to time:second.
        set land to 4.
    }.
    preserve.
}.
when land = 4 then {
    set acctarget to ((verticalspeed^2)/(2*(alt:radar - refh - targh))).
    set refh to (ship:control:pilotmainthrottle-0.9)*(1.9+(0.07*maxacc))*(alt:radar - 26.0825) + 36.
    if refh > 0 {
        set refh to 0.
    }.
    set throttdif to 0.2*((acctarget - trueacc)^3).
    if throttdif > 0.05 {
        set throttdif to 0.05.
    }
    else if throttdif < -0.05 {
        set throttdif to -0.05.
    }.
    if (alt:radar - 25.4) <= 0.2 {
        set ship:control:pilotmainthrottle to 0.3.
        set land to 5.
    }
    else if (alt:radar - 25.4) > 0.2 and
    verticalspeed < 0 {
        set ship:control:pilotmainthrottle to ship:control:pilotmainthrottle + throttdif.
    }.
    if verticalspeed >= 0 {
        set ship:control:pilotmainthrottle to 0.3.
        set land to 5.
    }.
    preserve.
}.

wait until verticalspeed < 0.
rcs off.
ag6 on.
unlock steering.
brakes off.
wait 0.1.
sas off.
wait 0.1.
sas on.
gear off.
wait 0.1.
rcs on.
set sasmode to "retrograde".
wait until (alt:radar - 26.0825) < 28000.
brakes on.
wait until (alt:radar - 26.0825) < 15000.
wait 0.1.
rcs off.
set sasmode to "retrograde".
set land to 1.

wait until (alt:radar - 26.0825) < 8100.
if maxacc <= 0 {
set warp to 0.
}.
wait until (alt:radar - 26.0825) < 8000.
if maxacc <= 0 {
    set ship:control:pilotmainthrottle to 1.
    wait 0.3.
    set ship:control:pilotmainthrottle to 0.
}.
wait until (alt:radar - 26.0825) < 7000.
set land to 3.
wait 0.2.

set basepc to stage:liquidfuel + 0.000000001.

wait until ship:control:pilotmainthrottle > 0.
log "---LANDING BURN TELEMETRY:" to land.txt.
log "     max acceleration: " + round(maxacc,5) + "m/s^2" to land.txt.
log "        decent height: " + round(decentheight,2) + "m" to land.txt.
log "        burn velocity: " + round(ship:velocity:surface:mag,2) + "m/s" to land.txt.
log "     burn groundspeed: " + round(groundspeed,2) + "m/s" to land.txt.
log "   burn verticalspeed: " + round(verticalspeed,2) + "m/s" to land.txt.
set sasmode to "radialout".
wait until ship:velocity:surface:mag <= vmone.
if maxthrust <= 800 {
    set targh to 25.4.
    set engconfig to 1.
}
else if maxthrust > 800 {
    set targh to 40-(0.605*maxacc).
    set engconfig to 3.
}.
set sasmode to "retrograde".
wait until ship:velocity:surface:mag <= vmtwo.
log "   Gear Deploy Height: " + round(vmtwo,2) + "m" to land.txt.
rcs on.
gear on.
wait until ship:velocity:surface:mag <= vmthree.
set sasmode to "radialout".
wait until ship:velocity:surface:mag <= 0.54*maxacc.
if maxthrust > 800 {
    log "  2-Engine Cut Height: " + round(alt:radar,2) + "m" to land.txt.
    ag4 on.
    set targh to 25.4.
}.

wait until land = 5.
set stamp2 to time:second.
set tburn to stamp2 - stamp1.
if tburn < 0 {
    set tburn to 0-tburn.
}.
if terrainheight <= 0.5 {
    set ship:control:pilotmainthrottle to (1-0.037*maxacc).
    wait 0.5.
    set ship:control:pilotmainthrottle to 0.6.
    wait 0.02.
    set ship:control:pilotmainthrottle to 0.1.
    wait 0.04.
    set ship:control:pilotmainthrottle to 0.
}
else if terrainheight > 0.5 {
    set ship:control:pilotmainthrottle to 0.2.
    wait 0.03.
    set ship:control:pilotmainthrottle to 0.6.
    wait 0.02.
    set ship:control:pilotmainthrottle to 0.1.
    wait 0.04.
    set ship:control:pilotmainthrottle to 0.
}.
clearscreen.
print"Falcon 9 has landed.".

set basepc to basepc - 0.000000001.
if (ship:altitude - alt:radar) < 0.5 {
    set terraintype to "OCEAN".
    rcs on.
    }
else if (ship:altitude - alt:radar) >= 0.5 and
(ship:altitude - alt:radar) <= 10 {
    set terraintype to "COAST".
    rcs off.
    }
else if (ship:altitude - alt:radar) > 10 and
(ship:altitude - alt:radar) <= 120 {
    set terraintype to "LOWLAND".
    rcs off.
    }
else if (ship:altitude - alt:radar) > 120 and
(ship:altitude - alt:radar) <= 3000 {
    set terraintype to "GRASSLAND".
    rcs off.
    }
else if (ship:altitude - alt:radar) > 3000 {
    set terrtaintype to "MOUNTAINS".
    rcs off.
    }.
log "---TOUCHDOWN TELEMETRY:" to land.txt.
log "      touchdown speed: " + round(ship:velocity:surface:mag,2) + "m/s" to land.txt.
log "touchdown groundspeed: " + round(groundspeed,2) + "m/s" to land.txt.
log "landing verticalspeed: " + round(verticalspeed,2) + "m/s" to land.txt.
log "          liquid fuel: " + round(stage:liquidfuel,0) + "/" + round(basepc,0) to land.txt.
log "    liquid fuel spent: " + round(basepc - stage:liquidfuel,0) to land.txt.
log "        burn duration: " + round(tburn,3) + "s" to land.txt.
log " Engine configuration: " + round(engconfig,0) to land.txt.
log "             latitude: " + round(latitude,5) to land.txt.
log "            longitude: " + round(longitude,5) to land.txt.
log "        Terrainheight: " + round(ship:altitude - alt:radar,2) + "m (" + terraintype + ")" to land.txt.
log "-------------------Landing Telemetry end--------------------------------" to land.txt.
print"burn duration:" + round((tburn),3) + "s" at (0,24).
print"     latitude:" + round(latitude,5) at (0,25).
print"    longitude:" + round(longitude,5) at (0,26).
wait until mass < 0.

I have had SOME success with it. The landing script almost always work perfectly, in all variants of terrain height. Here's a view of me, manually guiding the rocket back to the pad. The throttle and guidance is all controlled by kOS. Touchdown speed is between 0m/s to 3m/s. Throttle stays above 80%.

So i have 2 very important questions.

1. How do i refine the landing using a graphical method? I know that displacement is the area under the velocity/time graph, but how do i account for decreasing fuel and drag? How do i graph a velocity/displacement graph, accounting for all the factors?

2. How do i guide the vessel to a specific location? What guidance concept would i use? I have no clue how to start on getting the vessel to a specific location, let's say a barge on the ocean. I would appreciate any help or advice given to me :) 

Link to comment
Share on other sites

So, is your boostback script working (more or less...)?

Or are you still working on that? I've also worked on a script like this, the landing wirks perfectly, it can even control the descend and land the rocket at the exact latlng, but I failed to get a proper boostback script, so i have to steer it close enough to the landing zone so thst it can account for the position error and still land at the target.

Link to comment
Share on other sites

On 6/20/2016 at 5:19 AM, Kartoffelkuchen said:

So, is your boostback script working (more or less...)?

Or are you still working on that? I've also worked on a script like this, the landing wirks perfectly, it can even control the descend and land the rocket at the exact latlng, but I failed to get a proper boostback script, so i have to steer it close enough to the landing zone so thst it can account for the position error and still land at the target.

My boostback script is working, but doesn't adjust it's burn to a specific latlng. Neither does my landing burn. Is it possible if you could share your method on landing on the exact latlng?

Link to comment
Share on other sites

  • 2 years later...
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...