-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGreenShepard.py
264 lines (168 loc) · 6.88 KB
/
GreenShepard.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#C:\Users\Mario>C:\Python27\python.exe C:\Users\Mario\Dropbox\Python\Kerbal\GreenShepard.py
import krpc, time
from math import log
conn = krpc.connect(name='Green Shepard')
vessel = conn.space_center.active_vessel
print("VESSEL INFO")
print("")
print "Vessel name: ", vessel.name
print ("")
#if vessel.name != "Green Shepard":
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
vessel.control.rcs = False
print "SAS and RCS off."
for landing_leg in vessel.parts.landing_legs:
landing_leg.deployed = False
print "Landing legs retracted."
supersonic = False
print ("")
print "Program sequence: launch.exe -> first_stage.exe ->"
print "second_stage.exe -> coasting.exe ->"
print "suicide_burn_vertical.exe -> exit.exe"
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')
stage0resources = vessel.resources_in_decouple_stage(stage=0, cumulative=False)
stage0fuel = conn.add_stream(stage0resources.amount, "LiquidFuel")
stage2resources = vessel.resources_in_decouple_stage(stage=2, cumulative=False)
stage2fuel = conn.add_stream(stage2resources.amount, "LiquidFuel")
stage3resources = vessel.resources_in_decouple_stage(stage=3, cumulative=False)
stage3fuel = conn.add_stream(stage3resources.amount, "LiquidFuel")
speed = conn.add_stream(getattr, vessel.flight(KCKF), "speed")
v_speed = conn.add_stream(getattr, vessel.flight(KCKF), "vertical_speed")
altitude = conn.add_stream(getattr, vessel.flight(KCKF), "mean_altitude")
surface_altitude = conn.add_stream(getattr, vessel.flight(KCKF), "surface_altitude")
apo_altitude = conn.add_stream(getattr, vessel.orbit, "apoapsis_altitude")
## FUNCTIONS
def twr():
thrust = vessel.thrust
weight = vessel.mass * vessel.orbit.body.surface_gravity
return thrust / weight
go = raw_input("Request launch (y/n) or a specific program: ")
#go = "Y"
if go.lower() == "y":
print "Initiating launch sequence"
time.sleep(3)
program = "launch.exe"
elif go.lower() == "n":
print "No go"
program = "exit.exe"
else:
program = go
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)
print "1..."
time.sleep(1)
print "Liftoff!"
vessel.control.activate_next_stage() #all engines start
while speed() <= 50:
pass
program = "first_stage.exe"
###################################################
if program == "first_stage.exe":
print "First stage."
while stage3fuel() >= 1:
pass
print "Staging boosters."
vessel.control.activate_next_stage() #booster ejection
program = "second_stage.exe"
###################################################
if program == "second_stage.exe":
print "Second stage"
while apo_altitude() <= 80000:
pass
print "Boost complete, throttle killed."
vessel.control.throttle = 0
time.sleep(1)
vessel.control.activate_next_stage() #booster ejection
time.sleep(1)
vessel.control.activate_next_stage() #landing engines activated
program = "coasting.exe"
###################################################
if program == "coasting.exe":
print "Waiting for apoapse"
while v_speed() > 0:
pass
print "Maximum altitude reached.",
program = "suicide_burn_vertical.exe"
###################################################
if program == "suicide_burn_vertical.exe":
print "Pointing retrograde"
vessel.auto_pilot.engage()
vessel.control.sas = True
time.sleep(1)
#vessel.control.sas_mode = conn.space_center.SASMode.retrograde
vessel.auto_pilot.target_pitch_and_heading(90,90)
#Max thrust. This kills the kerbal.
while altitude() >= 23000:
pass
print "This is fine."
active_engines = filter(lambda e: e.active and e.has_fuel, vessel.parts.engines)
max_thrust = sum(engine.available_thrust for engine in active_engines)
isp = max_thrust / sum(engine.available_thrust / engine.kerbin_sea_level_specific_impulse for engine in active_engines) #aprox. constant
g = vessel.orbit.body.surface_gravity
delta_t = 0.1 #time step for the integration
while True:
critical_altitude = 0
m_i = vessel.mass
v = speed()
while v >= 0:
a_b = (max_thrust / m_i) - g
m_f = m_i - (max_thrust * delta_t)/(g * isp)
delta_v = g * isp * log(m_i / m_f)
delta_h = v * (delta_v / a_b) - ((delta_v ** 2)/(2 * a_b))
critical_altitude += delta_h
v -= delta_v
m_i = m_f
# print "Crit h = %s, m_f = %s, delta_v = %s, delta_h = %s" % (critical_altitude, m_f, delta_v, delta_h)
if surface_altitude() <= critical_altitude:
print "Critical altitude reached: %s" % surface_altitude()
break
print "Burn!!"
while abs(v_speed()) >= 30.0:
vessel.control.throttle = 1
for landing_leg in vessel.parts.landing_legs:
landing_leg.deployed = True
print "Landing legs deployed."
vessel.control.throttle = 0.1
landing_v = -10.0
kp = 0.2
ki = 0.00002
kd = 0.45
reset = 0
lasterror = 0
# PID loop
while str(vessel.situation) != "VesselSituation.landed":
error = landing_v - v_speed()
reset = reset + error
pid = kp * error + ki * reset + kd * (error - lasterror) #o al reves?
lasterror = error
# print "Error = %s, reset = %s, pid = %s" % (error, reset, pid)
if pid + vessel.control.throttle > 1:
vessel.control.throttle = 1
elif pid + vessel.control.throttle < 0:
vessel.control.throttle = 0
else:
vessel.control.throttle = pid
vessel.control.throttle = 0
print "PHEW"
program = "exit.exe"