Skip to content

Instantly share code, notes, and snippets.

@djungelorm
Created March 16, 2016 05:52
Show Gist options
  • Save djungelorm/01b597163491a3ed0bce to your computer and use it in GitHub Desktop.
Save djungelorm/01b597163491a3ed0bce to your computer and use it in GitHub Desktop.
kRPC hover script
import krpc
import time
conn = krpc.connect()
vessel = conn.space_center.active_vessel
control = vessel.control
flight = vessel.flight(vessel.orbit.body.reference_frame)
target = 15 # target altitude above the surface, in meters
g = 9.81
while True:
alt_error = target - flight.surface_altitude
# compute the desired acceleration:
# g to counteract gravity
# -v to push the vertical speed towards 0
# e to push the altitude error towards 0
a = g - flight.vertical_speed + alt_error
# Compute throttle setting using newton's law F=ma
F = vessel.mass * a
control.throttle = F / vessel.available_thrust
time.sleep(0.01)
@kparaju
Copy link

kparaju commented Apr 9, 2021

Wow, thank you for posting this! This is such an elegant solution that it makes you feel dumb.

I first tried the naive solution of incrementing and decrementing the vessel.control.throttle by 0.1 based on flight.mean_altitude. Then when it was just bouncing back and forth I tried to limit the flight.vertical_speed for a smoother flight. And then I started looking into approximating the total distance based on the past N velocities, almost like an approximated integral. Never thought to use one of the first formulas you learn in Physics class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment