-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParkingOrbit.py
176 lines (118 loc) · 4.67 KB
/
ParkingOrbit.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import krpc
import time
import math
conn = krpc.connect(name='Parking Orbit')
vessel = conn.space_center.active_vessel
print("VESSEL INFO")
print("")
print "Vessel name: ", vessel.name
print vessel.type
print vessel.situation
print ("")
print("PRE FLIGHT CHECK")
print("")
if vessel.comms.has_connection:
print "The ship has a flight computer"
else:
print "The ship doesn't have a flight computer"
vessel.control.sas = False
print "SAS disengaged"
vessel.control.rcs = False
print "RCS disengaged"
print ("")
# Reference frames
KCKF = vessel.orbit.body.reference_frame
# Kerbin-Centered, Kerbin-Fixed reference frame.
# Although this actually works for whatever body
# the ship is orbiting.
# Streams
ut = conn.add_stream(getattr, conn.space_center, 'ut')
stage1resources = vessel.resources_in_decouple_stage(stage=1, cumulative=False)
stage1fuel = conn.add_stream(stage1resources.amount, "LiquidFuel")
stage2resources = vessel.resources_in_decouple_stage(stage=0, cumulative=False)
stage2fuel = conn.add_stream(stage2resources.amount, "LiquidFuel")
speed = conn.add_stream(getattr, vessel.flight(KCKF), "speed")
altitude = conn.add_stream(getattr, vessel.flight(KCKF), "mean_altitude")
apo_altitude = conn.add_stream(getattr, vessel.orbit, "apoapsis_altitude")
def twr():
thrust = vessel.thrust
weight = vessel.mass * vessel.orbit.body.surface_gravity
return thrust / weight
# target = raw_input("Enter the desired target altitude in km: ")
# fuel check
go = raw_input("Proceed to launch? (y/n)")
target = 80
supersonic = False
if go.lower() != "y":
print "No go, I repeat, no go"
program = "exit.exe"
else:
program = "launch.exe"
while program != "exit.exe":
###################################################
if program == "launch.exe": # launch sequence
vessel.auto_pilot.engage()
vessel.auto_pilot.target_pitch_and_heading(90, 90)
vessel.control.throttle = 1
print "3..."
time.sleep(1)
print "2..."
time.sleep(1)
vessel.control.activate_next_stage() # main engine ignition
print "1..."
time.sleep(1)
print "aaaand LIFTOFFFFF"
vessel.control.activate_next_stage() # launch clamps release
while speed() <= 50:
pass
program = "pitchover_maneuver.exe"
###################################################
if program == "pitchover_maneuver.exe":
print "Pitchover maneuver"
vessel.auto_pilot.target_pitch_and_heading(80, 90) # pending: asking for target inclination
time.sleep(3)
vessel.auto_pilot.disengage()
print "Auto pilot disengaged"
program = "first_stage.exe"
###################################################
if program == "first_stage.exe":
vessel.control.throttle = 0.9
while stage1fuel() > 0.1:
if speed() >= 340 and not supersonic: # it won't be 340 at that altitude
print "The rocket is now supersonic"
supersonic = True
# else:
# pass
program = "second_stage.exe"
###################################################
if program == "second_stage.exe":
print "SAS engaged"
vessel.control.sas = True
time.sleep(1)
vessel.control.sas_mode = conn.space_center.SASMode.prograde
vessel.control.throttle = 0
print "Main engine cutoff"
vessel.control.activate_next_stage() # MECO
print "Secondary engine activated"
vessel.control.throttle = 0.2
while apo_altitude() <= target * 1000:
pass
vessel.control.throttle = 0
program = "burn_circularization_calculation.exe"
print "Coasting to apoapse"
###################################################
# As this program is currently written it must be run once the ship is
# outside the atmosphere or the calculation will be slightly wrong.
if program == "burn_circularization_calculation.exe":
print "Calculating circularization maneuver"
mu = vessel.orbit.body.gravitational_parameter
r = vessel.orbit.apoapsis
a1 = vessel.orbit.semi_major_axis
a2 = r
v1 = math.sqrt(mu*((2./r)-(1./a1)))
v2 = math.sqrt(mu*((2./r)-(1./a2)))
delta_v = v2 - v1
node = vessel.control.add_node(ut()+vessel.orbit.time_to_apoapsis, prograde=delta_v)
# check if there's enough fuel
# if not, inform of the predicted resulting periapse
program = "exit.exe"