Last active
March 25, 2021 20:20
-
-
Save stmobo/825e61963d81d92d204172a2efe63bef to your computer and use it in GitHub Desktop.
Yet another hoverslam script for kOS. Only works in vacuum.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare function print_line { | |
parameter text. | |
parameter y. | |
print text:padright(terminal:width) at (0, y). | |
} | |
declare function fmt_number { | |
parameter n. | |
return round(n, 2):tostring:padleft(7). | |
} | |
clearscreen. | |
list parts in partList. | |
set lp to 0. //lowest part height | |
set hp to 0. //hightest part height | |
for p in partList { | |
set cp to facing:vector * p:position. | |
if cp < lp | |
set lp to cp. | |
else if cp > hp | |
set hp to cp. | |
} | |
set height to hp - lp. | |
lock true_radar_alt to alt:radar - height. | |
lock asl_target to (ship:altitude - true_radar_alt) + 50. | |
print "Target altitude: " + round(asl_target, 2) + "m". | |
lock avail_acc to ship:availablethrust / ship:mass. | |
lock vsrf to ship:velocity:surface. | |
lock v_vert_srf to (vsrf * ship:up:vector) / ship:up:vector:mag. // Vertical velocity in surface frame | |
lock vec_horiz_srf to vectorexclude(ship:up:vector, vsrf). // Horizontal velocity in surface frame | |
lock v_horiz_srf to vec_horiz_srf:mag. | |
if alt:radar > 10000 { | |
print "Waiting for < 10k altitude...". | |
wait until alt:radar < 10000. | |
} | |
if v_vert_srf > -5 { | |
print "Waiting for negative vertical velocity...". | |
wait until v_vert_srf < -5. | |
} | |
print "Initiating hoverslam.". | |
sas off. | |
rcs off. | |
set navmode to "surface". | |
// Steer to opposite of horizontal component of velocity: | |
lock steering to -vec_horiz_srf. | |
lock steering_angle_err to vectorangle(ship:facing:vector, -vec_horiz_srf). | |
lock throttle to 0.0. | |
until steering_angle_err < 5.0 { | |
print_line("Waiting for steering...", 0). | |
print_line("Angle error = " + fmt_number(steering_angle_err) + " degrees", 1). | |
wait 0. | |
} | |
// Cancel horizontal velocity: | |
clearscreen. | |
lock throttle to 1.0. | |
until abs(v_horiz_srf) < 0.75 { | |
print_line("Killing horizontal velocity...", 0). | |
print_line("Current surface hvel = " + fmt_number(v_horiz_srf) + " m/s", 1). | |
wait 0. | |
} | |
clearscreen. | |
lock throttle to 0.0. | |
// Do the actual burn: | |
lock dist_to_target to ship:altitude - asl_target. | |
lock cur_grav_acc to ship:body:mu / ((ship:altitude + ship:body:radius)^2). | |
lock cur_net_acc to avail_acc - cur_grav_acc. | |
lock cur_burn_time to abs(v_vert_srf) / cur_net_acc. | |
lock cur_burn_distance to (v_vert_srf * cur_burn_time) + (0.5 * cur_net_acc * (cur_burn_time^2)). | |
lock burn_start_target to asl_target + abs(cur_burn_distance). | |
lock distance_to_burn to ship:altitude - burn_start_target. | |
set burn_started to false. | |
until abs(v_vert_srf) < 2.5 and true_radar_alt < 100 { | |
print ("Descending - target altitude = " + fmt_number(asl_target) + " m ASL"):padright(terminal:width) at (0, 0). | |
print_line(" Distance to target altitude : " + fmt_number(dist_to_target) + " m", 2). | |
print_line(" Available engine acc. : " + fmt_number(avail_acc) + " m/s^2", 3). | |
print_line(" Current grav. acc : " + fmt_number(cur_grav_acc) + " m/s^2", 4). | |
print_line(" Burn net acceleration : " + fmt_number(cur_net_acc) + " m/s^2", 5). | |
print_line(" Burn time : " + fmt_number(cur_burn_time) + " seconds", 7). | |
print_line(" Burn distance : " + fmt_number(cur_burn_distance) + " m", 8). | |
if not burn_started { | |
print_line(" Distance to burn : " + fmt_number(distance_to_burn) + " m", 10). | |
if distance_to_burn >= 10 { | |
lock throttle to 0.0. | |
print_line("Starting burn in " + fmt_number(distance_to_burn / abs(v_vert_srf)) + " seconds...", 11). | |
} else { | |
lock throttle to 1.0. | |
lights on. | |
set burn_started to true. | |
} | |
} else { | |
lock throttle to 1.0. | |
print_line(" Vertical velocity : " + fmt_number(v_vert_srf) + " m/s", 10). | |
print_line(" Altitude : " + fmt_number(true_radar_alt) + " m", 11). | |
print_line("Burning.", 12). | |
} | |
wait 0. | |
} | |
// Final descent - attempt to limit downwards velocity to between -1.25 and -2.5 m/s until we hit the ground: | |
clearscreen. | |
lock steering to ship:up. | |
lock target_throttle to (ship:mass * cur_grav_acc) / ship:availablethrust. | |
legs on. | |
until true_radar_alt < 0.5 { | |
print_line("Final descent - radar altitude = " + fmt_number(true_radar_alt) + " m", 0). | |
print_line("Vertical velocity: " + fmt_number(v_vert_srf) + " m/s", 1). | |
if v_vert_srf < -2.5 { | |
lock throttle to 1.0. | |
} else if v_vert_srf < -1.25 { // v_vert_srf is between -2.5 and -1.25 m/s | |
lock throttle to target_throttle. | |
} else { // v_vert_srf is >= -1.25 m/s | |
lock throttle to 0. | |
} | |
wait 0. | |
} | |
unlock throttle. | |
set ship:control:pilotmainthrottle to 0. | |
clearscreen. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment