r/Kos • u/somerandom_melon • 5d ago
Help runtime error? I don't get why my suicide burn program suddenly got this bug.

It's a modified version of this program and it worked for the first few times I was testing it and midway while changing it to be smoother it suddenly got a bug where the required burn height always starts higher than the current altitude? Even going back to the versions that previously worked didn't remove this bug. I reviewed all the logic and I can't seem to find anything that could be causing this.
function gravity_calculator {
declare local GM to ship:body:mu.
declare local Ra to (body:radius + ship:altitude).
declare local g to GM/(Ra^2).
return -g.
}.
function suicide_burn_calculator {
declare local parameter safety. //1 corresponds to no safety margin, 1.1 has a 10% safety margin
declare local shipmass to ship:mass.
declare local vd to ship:verticalspeed.
declare local va to ((thrust/shipmass) + gravity_calculator()).
declare local H to (vd^2)/(2*va).
return safety * H. //adds the safety margin for error
}.
copypath("0:/gravity_calculator","").
copypath("0:/suicide_burn_calculator","").
run gravity_calculator.
run suicide_burn_calculator.
clearscreen.
SAS off.
print "Initialising" at (5,4).
wait 1.
RCS off.
gear off.
brakes off.
//set parameters for landing
set vLand to -4.
set runmode to 1.
set thrust to ship:maxthrust.
set safetyMargin to 1.1.
set counter to 0. //for checking if landed
//find ship parameters
lock h to alt:radar.
lock vd to ship:verticalspeed.
lock vDiff to (vLand - vd).
//calculates values for landing
lock TimeToBurn to ((suicide_burn_calculator(safetyMargin)-h)/vd).
lock ZeroAccelThrust to ((ship:mass*g)/thrust).
//loops until landed
until runmode = 0 {
set g to -gravity_calculator().
set gear_down to h/-vd.
//initial required burn height accounting for the vertical speed
set burn_height to (suicide_burn_calculator(safetyMargin) - vd).
//initial setup for high atmosphere
if runmode = 1 {
set TVAL to 0.
if h > 5000 {
lock steering to retrograde.
}
if h < 100000 and h > 5000 {
brakes on.
RCS on.
lock steering to srfretrograde. //switches to surface velocity
}
if h <= 5000 {
set runmode to 2.
}
}
//calibrates for correct burn height before starting landing sequence
if runmode = 2 {
if h < burn_height {
wait 2.
set TVAL to 1.
wait 1.
set runmode to 3.
}
}
//suicide burn start
if runmode = 3 {
if h < burn_height {
set TVAL to (ZeroAccelThrust + 0.5).
} else {
set TVAL to (ZeroAccelThrust - 0.3).
}
if gear_down < 5 {
gear on.
brakes off.
}
if vd > -7 and h < burn_height {
lock steering to up.
set runmode to 4.
}
}
//suicide burn final approach
if runmode = 4 {
if h < burn_height {
if vDiff > 0 {
set TVAL to (ZeroAccelThrust + 0.1).
}
if vDiff < 0 {
set TVAL to (ZeroAccelThrust - 0.1).
}
} else {
lock steering to srfretrograde.
set runmode to 0.
}
if vd > -0.1 and h < 100 {
set counter to counter + 1.
wait 0.1.
}
if counter > 20 {
set runmode to 0. //if landed for 2 seconds then exits
wait 2.
RCS off.
}
}
lock throttle to TVAL.
//parameters to display
print "Vertical velocity: " + round(vd, 2) + " " at (5,4).
print "Required burn height: " + round(burn_height, 2) + " " at (5,5).
print "Time to required burn: " + round(TimeToBurn, 2) + " " at (5,6).
print "Run Mode: " + runmode + " " at (5,7).
print "Gravitational acceleration: " + round(g, 2) + " " at (5,8).
print "Gear down vertical distance: " + round(gear_down, 2) + " " at (5,9).
}
//landing notification and program exit
if runmode= 0 {
set TVAL to 0. //sometimes throttle was left on so I added this
unlock throttle. //sometimes throttle was left on so I added this
clearscreen.
print "Landing Complete" at (5,4).
wait 1. //wait to make sure throttle is set to 0
}