Created
March 16, 2016 05:52
-
-
Save djungelorm/01b597163491a3ed0bce to your computer and use it in GitHub Desktop.
kRPC hover script
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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 onflight.mean_altitude
. Then when it was just bouncing back and forth I tried to limit theflight.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.