Created
May 31, 2025 19:29
-
-
Save GiantWaffleCode/44a472680e9753fa906c9a8a9e47f484 to your computer and use it in GitHub Desktop.
KRPC Code
This file contains hidden or 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 time | |
import math | |
import krpc | |
conn = krpc.connect(name='Hello World') | |
vessel = conn.space_center.active_vessel | |
#CONSTANTS | |
TARGET_APO = 120000 | |
#Streams | |
ut = conn.add_stream(getattr, conn.space_center, 'ut') | |
altitude = conn.add_stream(getattr, vessel.flight(), 'mean_altitude') | |
apoapsis = conn.add_stream(getattr, vessel.orbit, 'apoapsis_altitude') | |
periapsis = conn.add_stream(getattr, vessel.orbit, 'periapsis_altitude') | |
curr_thrust = conn.add_stream(getattr, vessel, 'thrust') | |
curr_mass = conn.add_stream(getattr, vessel, 'mass') | |
#Expressions | |
srb_resources = vessel.resources_in_decouple_stage(stage=4, cumulative=False) | |
srb_fuel_amount = conn.get_call(srb_resources.amount, 'SolidFuel') | |
stage_1_resources = vessel.resources_in_decouple_stage(stage=3, cumulative=False) | |
lf_fuel_amount = conn.get_call(stage_1_resources.amount, 'LiquidFuel') | |
expr1 = conn.krpc.Expression.less_than( | |
conn.krpc.Expression.call(srb_fuel_amount), | |
conn.krpc.Expression.constant_float(0.1)) | |
expr2 = conn.krpc.Expression.less_than( | |
conn.krpc.Expression.call(lf_fuel_amount), | |
conn.krpc.Expression.constant_float(0.1)) | |
#Events | |
srb_low_fuel = conn.krpc.add_event(expr1) | |
lf_low_fuel = conn.krpc.add_event(expr2) | |
#Functions | |
def sep_srbs(): | |
vessel.control.activate_next_stage() | |
print('SRB Booster Separation!') | |
srb_low_fuel.remove() | |
def stage_booster(): | |
vessel.control.activate_next_stage() | |
print('Stage 1 Booster Separation!') | |
lf_low_fuel.remove() | |
def calc_gravity(m1, m2, radius): | |
G = 6.67e-11 | |
return (G*(m1+m2))/(radius**2) | |
def get_twr(mass, thrust, gravity): | |
weight = mass * gravity | |
return thrust / weight | |
def twr_control(curr_twr, min, max): | |
prop = 1 | |
if curr_twr > max: | |
amt = (curr_twr - max) | |
vessel.control.throttle -= prop * amt | |
if curr_twr < min: | |
amt = (min - curr_twr) | |
vessel.control.throttle += prop * amt | |
def gravity_turn(alt): | |
if alt <= 70000: | |
return -10.8**-8*(alt**2)-(0.00091*alt)+90 | |
else: | |
return 0 | |
def calc_circ_dv(): | |
#calc velocity at target apo for perfect circle | |
m_u = vessel.orbit.body.gravitational_parameter | |
body_r = vessel.orbit.body.equatorial_radius | |
target_vel = math.sqrt(m_u/(body_r+apoapsis())) | |
#calc velocity at target apo for current vessel | |
vessel_vel = math.sqrt(m_u*((2/(body_r+apoapsis()))-(1/vessel.orbit.semi_major_axis))) | |
#take difference | |
return target_vel - vessel_vel | |
def calc_burn_time(burn_dv): | |
#Tsiolkovsky rocket equation to get end mass | |
isp = vessel.specific_impulse_at(0) | |
gravity = conn.space_center.bodies['Kerbin'].surface_gravity | |
mass_start = vessel.mass | |
thrust = vessel.available_thrust_at(0) | |
mass_end = mass_start/math.e**(burn_dv/(isp*gravity)) | |
#calc avg acceleration | |
avg_accel = ((thrust/mass_start)+(thrust/mass_end))/2 | |
#calc burn time | |
return burn_dv / avg_accel | |
#Callbacks | |
srb_low_fuel.add_callback(sep_srbs) | |
srb_low_fuel.start() | |
lf_low_fuel.add_callback(stage_booster) | |
lf_low_fuel.start() | |
#1-Prelaunch | |
print(f'Preparing for launch of {vessel.name}...') | |
vessel.control.throttle = 1.0 | |
time.sleep(.1) | |
print(f'Throttle set to: {vessel.control.throttle*100}%') | |
print(f'Ready for Launch!') | |
vessel.auto_pilot.engage() | |
vessel.auto_pilot.target_heading = 90 | |
vessel.auto_pilot.target_pitch = 90 | |
vessel.auto_pilot.target_roll = 0 | |
time.sleep(1) | |
#Countdown | |
print(f'Countdown Started') | |
time.sleep(1) | |
print(f'3...') | |
time.sleep(1) | |
print(f'2...') | |
time.sleep(1) | |
print(f'1...') | |
time.sleep(1) | |
#2-Liftoff | |
vessel.control.activate_next_stage() | |
print(f'Liftoff!') | |
#3-Gravity Turn | |
while TARGET_APO - 1000 > apoapsis(): | |
curr_gravity = calc_gravity(curr_mass(), vessel.orbit.body.mass, vessel.orbit.radius) | |
curr_twr = get_twr(curr_mass(), curr_thrust(), curr_gravity) | |
twr_control(curr_twr, 1.4, 1.6) | |
# print(f'Current TWR: {curr_twr}') | |
target_pitch = gravity_turn(altitude()) | |
vessel.auto_pilot.target_pitch = target_pitch | |
# print(f'Target pitch: {target_pitch}') | |
time.sleep(.05) | |
vessel.control.throttle = 0.05 | |
while TARGET_APO >= apoapsis(): | |
time.sleep(.02) | |
print(f'Target APO Reached') | |
vessel.control.throttle = 0 | |
#4-Circ Burn | |
TARGET_THRUST = .3 | |
circ_dv = calc_circ_dv() | |
print(f'Circ Dv = {circ_dv:.2f}') | |
burn_time = calc_burn_time(circ_dv) | |
mod_burn_time = burn_time / TARGET_THRUST | |
half_burn_time = burn_time / 2 | |
conn.space_center.warp_to(ut()+vessel.orbit.time_to_apoapsis-half_burn_time-15, 50) | |
while vessel.orbit.time_to_apoapsis > half_burn_time: | |
time.sleep(.05) | |
vessel.control.throttle = TARGET_THRUST | |
m_u = vessel.orbit.body.gravitational_parameter | |
body_r = vessel.orbit.body.equatorial_radius | |
target_vel = math.sqrt(m_u/(body_r+apoapsis())) | |
while vessel.orbit.speed < target_vel - 2: | |
time.sleep(.05) | |
vessel.control.throttle = .05 | |
while vessel.orbit.speed < target_vel: | |
time.sleep(.02) | |
vessel.control.throttle = 0 | |
print(f'Circularized') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment