Skip to content

Instantly share code, notes, and snippets.

@nmk456
Last active May 3, 2020 04:14
Show Gist options
  • Save nmk456/241f891f1d0e42dff5c6c12cabe932f7 to your computer and use it in GitHub Desktop.
Save nmk456/241f891f1d0e42dff5c6c12cabe932f7 to your computer and use it in GitHub Desktop.
Tune orbital period of spacecraft using kRPC
import krpc, time
target_period = 7435 # The target orbital period in seconds
conn = krpc.connect()
vessel = conn.space_center.active_vessel
period = conn.add_stream(getattr, vessel.orbit, 'period')
vessel.control.throttle = 0.0
autopilot = vessel.auto_pilot
autopilot.engage()
autopilot.reference_frame = vessel.orbital_reference_frame
autopilot.attenuation_angle = (0.1, 0.1, 0.1)
def error():
return target_period - period()
def set_thrust_limit(n):
if 0 <= n <= 1:
for engine in vessel.parts.engines:
engine.thrust_limit = n
while abs(error()) > 0.00001:
if error() > 0:
autopilot.target_heading = 0
else:
autopilot.target_heading = 180
autopilot.wait()
set_thrust_limit(abs(error()) / 100.0)
vessel.control.throttle = 1.0
scale = 1 / error()
last = abs(error())
while vessel.control.throttle > 0.01 and abs(error()) <= last:
vessel.control.throttle = scale * error()
last = abs(error())
print(error())
vessel.control.throttle = 0
time.sleep(0.5)
set_thrust_limit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment