Repository navigation
Expand file tree
/
Copy pathRocket_Simulator.py
More file actions
357 lines (280 loc) · 12.9 KB
/
Copy pathRocket_Simulator.py
File metadata and controls
357 lines (280 loc) · 12.9 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
## There is progress, but one issue is, after burn time, every alternative Acceleration calculation is coming out to be 0.0 for some reason.
#EVERYTHING WORKS EXCEPT FOR THE MAX ACCELERATION. MAX ACC IS ALMOST 10M/S^2 LESS THAN OPEN ROCKET
from this import d
from turtle import color
import matplotlib.pyplot as plt
import csv
import time as timee
import numpy as np
from scipy.misc import ascent
import math
from mpl_toolkits import mplot3d
#Setting up the constants
#Barrowman equations
"""
rocket_mass(mr)
engine case mass(me)
propellant mass(mp)
impulse(im)
thrust(th)
burn time(tb)
air density(rho)
drag coefficient(cd)
rocket diameter(dr)
rocket area(a)
time increment(dt)
mass decrement(dm)
gravitational coefficient(g)
"""
# For Trajectory plotting, we need to plot altX, altY, altZ in a 3D plot. This will show the trajectory of the rocket.
# Taking estes F-15 as rocket motor and the rocket I made in openRocket for the other values
#Wet mass of rocket = 721g
G = 6.67408 * pow(10,-11) # Gravitational constant
ME = 5.972 * pow(10,24) # Earth mass in Kg
Re = 6371 * pow(10,3) # Radius of earth in m
cg = 0.528 #in cm
cp = 0.728 #in cm
mr = 0.618 #Mass without casing or propellant in kg
me = 0.043
mp = 0.060
im = 49.61 #in N-s
th = 14.5 #Avg thrust in N(use this)
mth = 25.3 #Max Thrust in N
tb = 3.42 #in s (im/th)
rho = 1.22 #in Kg/m^3, Drops by 10% for each 1000m of altitude for the first 14km or we can use the formula = 1.22 * 0.9^(Altitude/1000)
cd = 0.75 #Avg Cd, but we can calculate it based on our rocket dimensions.
dr = 0.045 #in m
a = 3.14 * pow(dr/2,2) #Area of the rocket
dt = 0.01 #in s
dm = (mp/tb)*dt#mass flow rate
g = 9.8 #m/s
tmass = mr + me + mp #Total Mass in kg
alt = []
vel = []
accel = []
time = []
t = 0
v = 0
# v = 6.48
h = 0
diameter = 0.86 #diameter of parachute in m
parachuteFlag = 0
parachuteFlag1 = 0
ascentFlag = 0 # This shows whether the rocket has started its ascent or not.
prevHeight = 0
t2apogee = 0
theta = 20 #in degrees from x axis.
thetaV = 0 # Velocity directions
# Currently, we are considering that the angle remains the same throughout the entire flight.
count = 0
altX = []
altY = []
altZ = []
velX = []
velY = []
velZ = []
accelX = []
accelY = []
accelZ = []
hx = 0
hy = 0
# using the style for the plot
plt.style.use('dark_background')
thrust_curve_x = [0,0.148,0.228,0.294,0.353,0.382,0.419,0.477,0.520,0.593,0.688,0.855,1.037,1.205,1.423,1.452,1.503,1.736,1.955,2.210,2.494,2.763,3.120,3.382,3.404,3.418,3.450]
thrust_curve_y = [0,7.638,12.253,16.391,20.210,22.756,25.260,23.074,20.845,19.093,17.500,16.225,15.427,14.948,14.627,15.741,14.785,14.623,14.303,14.141,13.819,13.338,13.334,13.013,9.352,4.895,0.000]
#Table for getting the change in rho values based on altitude
density = [1.347,1.225,1.112,1.007,0.9093,0.8194,0.7364,0.6601,0.5900,0.5258,0.4671,0.4135,0.1948,0.08891,0.04008,0.01841,0.003996,0.001027,0.0003097,0.00008283,0.00001846]
altitudeRho = [-1000,0,1000,2000,3000,4000,5000,6000,7000,8000,9000,10000,15000,20000,25000,30000,40000,50000,60000,70000,80000]
# plt.plot(thrust_curve_x,thrust_curve_y)
# plt.grid()
# plt.show()
# while t<=tb:
# print(np.interp(t, thrust_curve_x,thrust_curve_y),t)
# t += dt
def getThrust(t):
if t<tb:
return np.interp(t, thrust_curve_x,thrust_curve_y)
else:
return 0
def getDrag(v):
d = 0.5 * rho * cd * a * pow(v,2)
return d
def getParachuteDrag(v):
d = (cd * rho * 3.14 * pow(diameter,2) * pow(v,2))/8 #(diameter /2) ^ 2 ==> (4) and then the entire formula is 1/2 * .... ==> 1/8
return d
def getAcc(tmass,th,v,velY,altY,t,h,parachuteFlag,ascentFlag):
# In the beginning, acceleration is -9.8(Pointing downwards), and then it gradually increases. Until, it reaches a +ve value,
# The rocket stays on the launch pad with v = 0, alt = 0 and d = 0.
if parachuteFlag == 0 and len(velY) > 0 and velY[-1]<0 and len(altY) > 0 and altY[-1]>0:
parachuteFlag = 1
if parachuteFlag == 1: # After parachute is deployed
d = getParachuteDrag(v) + getDrag(v)
thh = getThrust(t)
acceleration = (d-(tmass*g))/tmass # Sin theta not considered because, once the parachute is deployed, the rocket is going to fall straight down. So the angle is not going to be considered.
print("Status : Parachute Deployed(Descent)","Time : ",t,"Thrust : ",thh,"Drag : ", d,"Weight : ",tmass*g,"Force : ",(d - (tmass*g) ),"Mass : ",tmass,"Acceleration",acceleration)
else: # Before Parachute is deployed
# If parachute hasn't deployed, then either the rocket is still on the launch pad, or it is in the ascent phase.
thh = getThrust(t)
d = getDrag(v)
acceleration = (thh - ((tmass*g)*math.sin(math.radians(theta))) - d)/tmass # math.sin considers radians, hence theta is first converted to radians, then passed into math.sin
# The reason, why we are considering mg.sin(theta) here is because, we are considering that the rocket is going to be launched at a launch angle theta. So, when calculating the net force, at that angle, mg.sin(theta) is used.
print("Status : Ascent, Theta : ",theta,"Time : ",t,"Thrust : ",thh,"Drag : ", d,"Weight : ",((tmass*g)*math.sin(math.radians(theta))),"Force : ",(thh - ((tmass*g)*math.sin(math.radians(theta))) - d),"Mass : ",tmass,"Acceleration",acceleration)
if ascentFlag == 0 and acceleration >= 0: # If the rocket hasn't begun its ascent phase and the acceleration is +ve, meaning ascent phase is beginning
ascentFlag = 1 # Ascent has begun
elif ascentFlag == 1 and acceleration<0: #If the rocket has finished its ascent phase and the acceleration becomes -ve, meaning pointing downwards, means that the ascent phase has ended and the descent phase has begun
ascentFlag = 2 # Ascent has ended and Descent has begun
return [acceleration,ascentFlag]
def isCondition(altY,t,parachuteFlag): # Condition to check whether the rocket has landed or not
if len(altY) > 0 and altY[-1]<=0 and t > 0 and parachuteFlag == 1:
return 0
return 1
while isCondition(altY,t,parachuteFlag1):
print("*(***************************",len(velY),len(altY))
if len(velY) != 0 and len(altY) != 0:
print("VelY : ", velY[-1], " AltY : ", altY[-1],"Parachute Flag : ", parachuteFlag1)
if parachuteFlag1 == 0 and velY[-1]<0 and altY[-1]>0: # Apogee condition, Checking if the last vertical velocity < 0 , and last vertical altitude is > 0
parachuteFlag1 = 1 #Parachute is deployed
t2apogee = t # Time to apogee set at apogee
# Gravity modelling(Change in acceleration due to gravity)
# g = (G*ME)/(h + Re)
try:
theta = math.degrees(math.atan(velY[-1]/velX[-1])) # Theta is the angle at which the rocket is going to be launched.
print("theta:",theta)
except:
print("Exception in theta calculations")
print("g : " , g)
# Accel, Vel, Height calculations
inp = getAcc(tmass,th,v,velY,altY,t,h,parachuteFlag1,ascentFlag)
acc = float(inp[0])
ascentFlag = int(inp[1])
if ascentFlag == 0:
v = 0
h = 0
velY.append(v)
accelY.append(acc)
else:
if parachuteFlag1 == 1: # Parachute is deployed
print("ENTERED PARACHUTE FLAG ***********************************")
# Breaking acc into its x and y vectors
theta = 90 # Considering that when the parachute is deployed, the rocket will fall straight down. So theta is 90 degrees from x axis and 0 from y
# acc_x = acc * math.cos(math.radians(theta))
acc_x = 0
acc_y = acc * math.sin(math.radians(theta))
print("COS 90 : " , math.cos(math.radians(90)),"SIN 90 : ", math.sin(math.radians(90)),"ACC_X : ",acc_x,"ACC_Y : ",acc_y)
# Breaking previous velocity and altitude into its x and y vectors
# v_x = v * math.cos(math.radians(theta)) # Previous velocity x components
v_x = 0
v_y = v * math.sin(math.radians(theta)) # Previous velocity y components
# h_x = h * math.cos(math.radians(theta)) # Previous altitude x components
# h_y = h * math.sin(math.radians(theta)) # Previous altitude y components
print("V_X : ",v_x,"V_Y : ",v_y)
# Calculating the current x and y vectors of velocity and altitude
# vx = v_x + acc_x * dt
# vy = v_y + acc_y * dt
v += acc_y * dt
v_y = v
print("++++++++++++++++++v_y : ",vy,"acc_y : ",acc_y ,"dt : ",dt,"acc_y * dt : ",acc_y * dt,"v_y + acc_y * dt : ",v_y + acc_y * dt)
# v = math.sqrt(pow(vx,2) + pow(vy,2))
# print(acc,dt,pow(dt,2),acc*pow(dt,2))
h += v_y*dt + (0.5*acc_y*pow(dt,2)) # Because altitude is basically distance in the y axis
hx += v_x * dt
altX.append(hx)
altY.append(h)
altZ.append(0)
velX.append(vx)
velY.append(vy)
velZ.append(0)
accelX.append(acc_x)
accelY.append(acc_y)
accelZ.append(0)
# thetaV = math.degrees(math.atan(vy/vx)) # math.atan gives a radian value, so converting it to degrees
print("acc_x : ",acc_x,"acc_y : ",acc_y,"vx : ",vx,"vy : ",vy,"hx : ",hx,"hy : ",h)
else: # Parachute hasn't been deployed yet
# Breaking acc into its x and y vectors
acc_x = acc * math.cos(math.radians(theta))
acc_y = acc * math.sin(math.radians(theta))
# Breaking previous velocity and altitude into its x and y vectors
v_x = v * math.cos(math.radians(theta)) # Previous velocity x components
v_y = v * math.sin(math.radians(theta)) # Previous velocity y components
# Calculating the current x and y vectors of velocity and altitude
vx = v_x + acc_x * dt
vy = v_y + acc_y * dt
v = math.sqrt(pow(vx,2) + pow(vy,2))
h += v_y*dt + (0.5*acc_y*pow(dt,2)) # Because altitude is basically distance in the y axis
hx += v_x * dt
altX.append(hx)
altY.append(h)
altZ.append(0)
velX.append(vx)
velY.append(vy)
velZ.append(0)
accelX.append(acc_x)
accelY.append(acc_y)
accelZ.append(0)
# thetaV = math.degrees(math.atan(vy/vx)) # math.atan gives a radian value, so converting it to degrees
print("acc_x : ",acc_x,"acc_y : ",acc_y,"vx : ",vx,"vy : ",vy,"hx : ",hx,"hy : ",h)
# v += acc * dt
# h += v * dt
print("Mass : ",tmass,"Acceleration : ",acc,"Velocity : ",v,"Altitude : ",h)
# Appending the values to the lists at each dt
accel.append(acc)
vel.append(v)
alt.append(h)
time.append(t)
t+=dt
if t<tb: # T < Burn time, then current mass = mass of the rocket body + engine casing + propellant mass
if tmass<=mr+me: # If the mass, goes below rocket body mass + engine casing mass(Which is not possible), then the mass = rocket body mass + engine casing mass
tmass = mr + me
else: # current mass = current mass - mass flow rate
tmass-=dm
else: # If T > Burn time, then current mass = mass of the rocket body + engine casing + engine casing
tmass = mr + me
# timee.sleep(0.40)
# if parachuteFlag1 == 1:
# timee.sleep(0.25)
vel1 = []
accel1 = []
for i in vel:
vel1.append(i)
for i in accel:
accel1.append(i)
vel = [abs(ele) for ele in vel]
accel = [abs(ele) for ele in accel]
print("Avg vel : " , (sum(vel)/len(vel)))
print("Avg accel : " , (sum(accel)/len(vel)))
print("Max vel : ",max(vel))
print("Max alt : ",max(alt))
print("Max Accel : ",max(accel))
print("Stability : ",(cp-cg)/(dr),"Calibres")
print("Time to apogee : ",t2apogee)
print("Total Time : ",t)
print(len(vel))
print(len(accel))
print(len(alt))
with open('./flightSimulatorResults.csv' , mode='w') as file:
csvWriter = csv.writer(file,delimiter=',')
csvWriter.writerow(["Time","Velocity","Acceleration","Altitude","Thrust"])
for i in range(len(vel)):
csvWriter.writerow([time[i],vel1[i],accel1[i],alt[i],th])
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# ax1 = plt.subplot()
# l1, = ax1.plot(time,alt, color='red',label = "Altitude")
# ax2 = ax1.twinx()
# l2, = ax2.plot(time,accel1, color='blue',label = "Total Acceleration")
# l3, = ax2.plot(time,vel1, color='green',label = "Total Velocity")
# # l2, = ax2.plot(time,accelY, color='blue',label = "Vertical Acceleration")
# # l3, = ax2.plot(time,velY, color='green',label = "Vertical Velocity")
# plt.legend([l1, l2, l3], ["Altitude", "Acceleration", "Velocity"])
fig = plt.figure()
# ax = plt.axes(projection='3d')
ax = plt.axes()
ax.set_xlabel('Altitude in X')
ax.set_ylabel('Altitude in Y')
# ax.set_zlabel('Altitude in Z')
# ax.plot3D(altX,altY,altZ, 'red')
ax.plot(altX,altY,'red')
for i in range(len(altY)):
print(altX[i]," , ",altY[i]," , ",altZ[i])
plt.grid()
plt.show()