-
Notifications
You must be signed in to change notification settings - Fork 9
/
maneuver.py
328 lines (242 loc) · 11.6 KB
/
maneuver.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import math
from vector3 import *
class maneuver():
def __init__(self, name, vessel, mnv_type):
self.name = name
self.vessel = vessel
self.type = mnv_type
def get_vessel(self):
return self.vessel
def get_type(self):
return self.type
class maneuver_impulsive(maneuver):
def __init__(self, name, vessel, frame_body, orientation, delta_v, t_perform):
super().__init__(name, vessel, "impulsive")
self.frame_body = frame_body
self.orientation = orientation
self.orientation_input = orientation
if type(self.orientation_input) != str and type(self.orientation_input) != list:
self.orientation_input = orientation.tolist()
self.delta_v = delta_v
self.t_perform = t_perform
self.done = False
self.draw_point = None
def set_orientation(self):
if not type(self.orientation) == type(vec3()):
self.orientation = self.vessel.get_orientation_rel_to(self.frame_body, self.orientation)
def perform_maneuver(self, current_time, delta_t):
if (not self.done) and current_time >= self.t_perform:
self.set_orientation()
self.vessel.set_vel(self.vessel.get_vel() + self.orientation * self.delta_v)
self.done = True
self.draw_point = self.vessel.get_draw_pos()
# Ok, here is a hack. Since this is an instantaneous maneuver, we don't deal with accelerations.
# So we let this function update the velocity and return a null acceleration so the solver doesn't
# apply anything (because this function already does what needs to be done).
def get_accel(self, current_time, delta_t):
if (not self.done) and current_time >= self.t_perform:
self.set_orientation()
self.vessel.set_vel(self.vessel.get_vel() + self.orientation * self.delta_v)
self.done = True
self.draw_point = self.vessel.get_draw_pos()
return vec3(0, 0, 0)
def update_mass(self, dummy1, dummy2):
pass
def is_performing(self, current_time):
return False
def get_state(self, current_time):
if self.done:
return "Completed"
else:
return "Pending"
def get_duration(self):
return 0
def get_name(self):
return self.name
def get_draw_point(self):
return self.draw_point
def clear_draw_vertices(self):
self.draw_point = None
def get_params_str(self):
output = "Vessel: " + self.vessel.get_name() + "\n"
if type(self.orientation_input) == str:
output += "Orientation: " + self.orientation_input + " rel to " + self.frame_body.get_name()
else:
output += "Orientation: " + str(self.orientation) + " rel to global frame"
output += "\nDv: " + str(self.delta_v) + " m/s\n"
output += "Perform Time: " + str(self.t_perform) + " s\n"
return output
class maneuver_const_accel(maneuver):
def __init__(self, name, vessel, frame_body, orientation, accel, t_start, duration):
super().__init__(name, vessel, "const_accel")
self.frame_body = frame_body
self.orientation = orientation
self.orientation_input = orientation
if type(self.orientation_input) != str and type(self.orientation_input) != list:
self.orientation_input = orientation.tolist()
self.accel = accel
self.duration = duration
self.t_start = t_start
self.done = False
self.draw_vertices = []
def set_orientation(self):
if not type(self.orientation) == type(vec3()) or (type(self.orientation_input) == str and self.orientation_input[-8:] == "_dynamic"):
self.orientation = self.vessel.get_orientation_rel_to(self.frame_body, self.orientation)
def perform_maneuver(self, current_time, delta_t):
if (not self.done) and current_time >= self.t_start:
self.set_orientation()
self.vessel.update_vel(self.orientation * self.accel, delta_t)
self.draw_vertices.append(self.vessel.get_draw_pos())
# if the orientation is set as "dynamic"
# it needs to be updated every frame
if type(self.orientation_input) == str and self.orientation_input[-8:] == "_dynamic":
self.orientation = self.orientation_input
if (not self.done) and (current_time > (self.t_start + self.duration)):
self.done = True
# basically identical to perform_maneuver but doesn't auto-update the vessel, instead returns the acceleration vector
def get_accel(self, current_time, delta_t):
accel_vec = vec3(0, 0, 0)
if (not self.done) and current_time >= self.t_start:
self.set_orientation()
accel_vec = self.orientation * self.accel
self.draw_vertices.append(self.vessel.get_draw_pos())
# if the orientation is set as "dynamic"
# it needs to be updated every frame
if type(self.orientation_input) == str and self.orientation_input[-8:] == "_dynamic":
self.orientation = self.orientation_input
if (not self.done) and (current_time > (self.t_start + self.duration)):
self.done = True
return accel_vec
def update_mass(self, dummy1, dummy2):
pass
def is_performing(self, current_time):
if not self.done and current_time >= self.t_start:
return True
else:
return False
def get_state(self, current_time):
if self.done:
return "Completed"
elif current_time >= self.t_start:
return "Performing"
else:
return "Pending"
def get_name(self):
return self.name
def get_draw_vertices(self):
return self.draw_vertices
def clear_draw_vertices(self):
self.draw_vertices = []
def get_params_str(self):
output = "Vessel: " + self.vessel.get_name() + "\n"
if not type(self.orientation_input) == type(vec3()):
if type(self.orientation_input) == str and self.orientation_input[-8:] == "_dynamic":
output += "Orientation: " + self.orientation_input[0:-8] + " (dynamic) rel to " + self.frame_body.get_name()
else:
output += "Orientation: " + self.orientation_input[-8:] + " rel to " + self.frame_body.get_name()
else:
output += "Orientation: " + str(self.orientation) + " rel to global frame"
output += "\nAcceleration: " + str(self.accel) + " m/s^2\n"
output += "Start time: " + str(self.t_start) + " s\n"
output += "Duration: " + str(self.duration) + " s\n"
output += "Dv: " + str(self.duration * self.accel) + " m/s\n"
return output
def get_duration(self):
return self.duration
class maneuver_const_thrust(maneuver):
def __init__(self, name, vessel, frame_body, orientation, thrust, mass_init, mass_flow,
t_start, duration):
super().__init__(name, vessel, "const_thrust")
self.frame_body = frame_body
self.orientation = orientation
self.orientation_input = orientation
if type(self.orientation_input) != str and type(self.orientation_input) != list:
self.orientation_input = orientation.tolist()
self.thrust = thrust
self.mass_init = mass_init
self.mass_flow = mass_flow
self.t_start = t_start
self.duration = duration
self.done = False
self.mass = mass_init
self.draw_vertices = []
# calculate Delta-v
v_exhaust = self.thrust/self.mass_flow
m_final = self.mass_init - self.duration * self.mass_flow
self.Dv = v_exhaust * math.log(self.mass_init/m_final)
def set_orientation(self):
if not type(self.orientation) == type(vec3()) or (type(self.orientation_input) == str and self.orientation_input[-8:] == "_dynamic"):
self.orientation = self.vessel.get_orientation_rel_to(self.frame_body, self.orientation)
def perform_maneuver(self, current_time, delta_t):
if (not self.done) and current_time >= self.t_start:
self.set_orientation()
if self.mass <= 0:
self.done = True
return
accel = self.thrust/self.mass
self.mass -= self.mass_flow * delta_t
self.vessel.update_vel(self.orientation * accel, delta_t)
self.draw_vertices.append(self.vessel.get_draw_pos())
# if the orientation is set as "dynamic"
# it needs to be updated every frame
if type(self.orientation_input) == str and self.orientation_input[-8:] == "_dynamic":
self.orientation = self.orientation_input
if (not self.done) and (current_time > (self.t_start + self.duration)):
self.done = True
# basically identical to perform_maneuver but doesn't auto-update the vessel, instead returns the acceleration vector
def get_accel(self, current_time, delta_t):
accel_vec = vec3(0, 0, 0)
if (not self.done) and current_time >= self.t_start:
self.set_orientation()
if self.mass <= 0:
self.done = True
return
accel = self.thrust / self.mass
accel_vec = self.orientation * accel
self.draw_vertices.append(self.vessel.get_draw_pos())
# if the orientation is set as "dynamic"
# it needs to be updated every frame
if type(self.orientation_input) == str and self.orientation_input[-8:] == "_dynamic":
self.orientation = self.orientation_input
if (not self.done) and (current_time > (self.t_start + self.duration)):
self.done = True
return accel_vec
def update_mass(self, current_time, delta_t):
if (not self.done) and current_time >= self.t_start:
self.mass -= self.mass_flow * delta_t
def is_performing(self, current_time):
if not self.done and current_time >= self.t_start:
return True
else:
return False
def get_state(self, current_time):
if self.done:
return "Completed"
elif current_time >= self.t_start:
return "Performing"
else:
return "Pending"
def get_name(self):
return self.name
def get_draw_vertices(self):
return self.draw_vertices
def clear_draw_vertices(self):
self.draw_vertices = []
def get_params_str(self):
output = "Vessel: " + self.vessel.get_name() + "\n"
if not type(self.orientation_input) == type(vec3()):
if type(self.orientation_input) == str and self.orientation_input[-8:] == "_dynamic":
output += "Orientation: " + self.orientation_input[0:-8] + " (dynamic) rel to " + self.frame_body.get_name()
else:
output += "Orientation: " + self.orientation_input[-8:] + " rel to " + self.frame_body.get_name()
else:
output += "Orientation: " + str(self.orientation) + " rel to global frame"
output += "\nThrust: " + str(self.thrust) + " N\n"
output += "Initial mass:" + str(self.mass_init) + " kg\n"
output += "Mass flow: " + str(self.mass_flow) + " kg/s\n"
output += "Start time: " + str(self.t_start) + " s\n"
output += "Duration: " + str(self.duration) + " s\n"
output += "Dv: " + str(round(self.Dv, 3)) + " m/s\n"
return output
def get_duration(self):
return self.duration