-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathorbit.py
More file actions
132 lines (110 loc) · 4.69 KB
/
orbit.py
File metadata and controls
132 lines (110 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import time
from datetime import datetime, timedelta
import krpc
import helpers
import settings
"""
This script takes a rocket and launches it into orbit from Kerbin.
"""
HEADING_NORTH = 0
HEADING_EAST = 90
HEADING_SOUTH = 180
HEADING_WEST = 270
def launch(connection, vessel, heading, target_altitude):
"""
Launch a given vessel into orbit at a given target altitude.
:params connection: A krpc connection
:params vessel: A vessel object
:params heading: The heading of the orbit
:params target_altitude: The target apoapsis and periapsis altitude in meters
"""
# Set up telemetry streams
altitude = connection.add_stream(getattr, vessel.flight(), "mean_altitude")
apoapsis = connection.add_stream(getattr, vessel.orbit, "apoapsis_altitude")
periapsis = connection.add_stream(getattr, vessel.orbit, "periapsis_altitude")
time_to_apoapsis = connection.add_stream(getattr, vessel.orbit, "time_to_apoapsis")
# Setup heading, control and throttle
vessel.auto_pilot.engage()
vessel.auto_pilot.target_pitch_and_heading(90, heading)
vessel.control.throttle = 1
# Countdown...
print("3...")
time.sleep(1)
print("2...")
time.sleep(1)
print("1...")
time.sleep(1)
print("Launch!")
vessel.control.activate_next_stage()
start_time = datetime.now()
# When fuel is empty in the current stage, automatically move to the next
# one
helpers.enable_auto_stage(connection, vessel)
running = True
while running:
# Start gravity turn - we start pointing up (90 degrees) and gradually
# pitch until we're at 45 degrees.
min_altitude = 0
max_altitude = 10000
start_pitch = 90
end_pitch = 45
if apoapsis() < target_altitude and int(vessel.auto_pilot.target_pitch) != end_pitch:
adjusted_pitch = start_pitch - ((altitude() - min_altitude) / (
max_altitude - min_altitude
)) * end_pitch
vessel.auto_pilot.target_pitch = adjusted_pitch
# Approaching the target apoapsis altitude
if apoapsis() > target_altitude * 0.9:
print(f"Approaching target apoapsis: {apoapsis()} / {target_altitude}")
# Get rid of the solid boosters if they're still in use, as we're
# approaching the apoapsis
fuel_in_current_stage = vessel.resources_in_decouple_stage(
stage=vessel.control.current_stage - 1, cumulative=False
)
if "SolidFuel" in fuel_in_current_stage.names:
vessel.control.activate_next_stage()
solid_fuel_separated = True
print("Near target apoapsis - separating solid fuel boosters early")
break
# Reduce throttle and boost until we reach the target orbit altitude
vessel.control.throttle = 0.25
while apoapsis() < target_altitude:
pass
# Reached target apoapsis - shut down engines
print(f"Reached target apoapsis: {apoapsis()} / {target_altitude}")
vessel.control.throttle = 0
vessel.auto_pilot.target_pitch = 0
# Circularise the orbit.
starting_periapsis = periapsis()
max_apoapsis = apoapsis() * 0.7
while periapsis() < apoapsis() * 0.99:
max_time_to_apoapsis = 25
min_time_to_apoapsis = 15
if time_to_apoapsis() <= max_time_to_apoapsis:
# Adjust the throttle - start at full throttle and gradually reduce
# as the periapsis matches the apoapsis.
adjusted_throttle = 1 - (periapsis() - starting_periapsis) / (
max_apoapsis - starting_periapsis
)
if adjusted_throttle > 0.1:
vessel.control.throttle = adjusted_throttle
# Adjust pitch based on how close we are to the apoapsis - i.e., pitch
# up by 10 degrees if we're close to the apoapsis, and pitch down by 10
# degrees the further away we are.
# Pitching up will "move" the apoapsis further away, while pitching down
# will "move" the apoapsis closer to us.
adjusted_pitch = -10 + (1 - (time_to_apoapsis() - min_time_to_apoapsis) / (
max_time_to_apoapsis - min_time_to_apoapsis
)) * 20
vessel.auto_pilot.target_pitch = adjusted_pitch
# In stable orbit
vessel.control.throttle = 0
vessel.control.rcs = False
vessel.auto_pilot.disengage()
helpers.disable_auto_stage()
launch_duration = datetime.now() - start_time
print(f"Stable orbit achieved in {launch_duration}")
if __name__ == "__main__":
connection = krpc.connect(address=settings.krpc_ip_address)
vessel = connection.space_center.active_vessel
launch(connection, vessel, HEADING_EAST, 75000)