Skip to content

Instantly share code, notes, and snippets.

@jberkman
Created January 9, 2018 23:45
Show Gist options
  • Save jberkman/0c0c468fdf869936b7cb9b582f7ce4e9 to your computer and use it in GitHub Desktop.
Save jberkman/0c0c468fdf869936b7cb9b582f7ce4e9 to your computer and use it in GitHub Desktop.
@lazyglobal off.
function gravMag {
parameter alt,bdy.
return bdy:mu/(bdy:radius+altitude)^2.
}
function gravVec {
parameter self is ship.
return gravMag(self:altitude,self:obt:body)*-self:up:vector:normalized.
}
function thrustVec {
parameter self is ship.
local t is V(0,0,0).
for mod in self:modulesNamed("ModuleEngines") {
print "avail: " + mod:part:availableThrust.
print "facing: " + mod:part:facing:vector:normalized.
set t to t + (mod:part:availableThrust*mod:part:facing:vector:normalized).
print "thrust: " + t.
}
return t.
}
function maxMag {
parameter vec, mag.
if vec:mag<mag return vec.
return mag*vec:normalized.
}
function constrictSteering {
parameter vec, maxGamma is 20.
local vHat is ship:up:vector:normalized.
local vAccV is vHat*vDot(vHat,vec).
local hHat is vCrs(vCrs(vHat,vec),vHat):normalized.
local hDot is vDot(hHat,vec).
local hSign is hDot/abs(hDot).
local hAccV is hSign*hHat*min(abs(vDot(hHat,vec)),vAccV:mag*tan(maxGamma)).
return vAccV+hAccV.
}
function fallTime {
parameter r is ship:altitude, x is 0, b is ship:body.
local xr is (x+b:radius)/(r+b:radius).
local xr2 is sqrt(xr).
return (r+b:radius)^1.5*(constant:degToRad*arccos(xr2)+xr2*sqrt(1-xr))/sqrt(2*b:mu).
}
clearVecDraws().
lock throttle to 0.
if ship:availableThrust=0 stage.
sas off.
rcs on.
lock steering to lookDirUp(ship:up:vector,heading(90,0):vector).
lock throttle to 1.
wait until apoapsis>80000.
lock throttle to 0.
wait until altitude>70000.
local rotRate is 360/ship:body:rotationPeriod.
local launchPad is LatLng(-0.097207933678136,-74.557757572037).
local landingPad is LatLng(-0.0967218940930756,-74.6173570891441).
local posVec is vecDraw(V(0,0,0),V(0,0,0),red,"POS",1,true,0.2).
local t is eta:apoapsis+fallTime(apoapsis).
local currentPos is positionAt(ship,time+t).
local targetPos is LatLng(launchPad:lat,launchPad:lng+rotRate*t*1.005):position.
local dV is (targetPos-currentPos)/fallTime(apoapsis).
local apoV is velocityAt(ship,time+eta:apoapsis):orbit.
local proV is apoV:normalized.
local radV is (positionAt(ship,time+eta:apoapsis)-positionAt(ship:body,time+eta:apoapsis)):normalized.
local nrmV is vCrs(proV,radV).
local nd is Node(time:seconds+eta:apoapsis,vDot(radV,dV),vDot(nrmV,dV),vDot(proV,dV)).
add nd.
lock steering to lookDirUp(nd:deltaV,-ship:up:vector).
wait until vAng(nd:deltaV,ship:facing:vector)<1.
lock bTime to nd:deltaV:mag*ship:mass/ship:availableThrust.
wait until nd:eta/2 < bTime.
lock throttle to min(1,bTime).
wait until vDot(dV,nd:deltaV) < 0 or nd:deltaV:mag < 0.1.
lock throttle to 0.
remove nd.
lock steering to lookDirUp(-LatLng(launchPad:lat,launchPad:lng):position,heading(270,-45):vector).
local hOffset is 0.
local gModifier is 1.
//local lock altRadar to altitude-body:geopositionof(ship:position):terrainheight.
//local lock goal to airSpeed^2/(altRadar-hOffset-5)/2.
//local lock grav to body:mu/((gModifier*altitude+body:radius)^2).
//local lock thst to ship:availableThrust/mass.
//local lock throttleGoal to(goal+grav)/thst.
//wait until throttleGoal>0.99.
wait until altitude<20000 and altitude/10<airSpeed.
until status="LANDED"{
local tgtPos is launchPad:altitudePosition(launchPad:terrainHeight).
local tgtVel is 0.1*tgtPos. // sqrt(tgtPos:mag)*tgtPos:normalized.
local tgtAcc is tgtVel-ship:velocity:surface.
local tAccV is thrustVec()/ship:mass.
local gAccV is gravVec().
local accV is constrictSteering(tgtAcc-gAccV).
lock steering to lookDirUp(accV,heading(270,-45):vector).
local x is vDot(tAccV,accV).
if x<>0 lock throttle to accV:vec:mag^2/x.
set posVec:vec to tgtPos.
//set accVec:vec to accV+gAccV.
wait 0.
}
lock throttle to 0.
lock steering to lookDirUp(ship:up:vector,-ship:north:vector).
wait 30.
unlock steering.
rcs off.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment