Advertisement
oz1sej

Model Rocket Simulator

Mar 20th, 2020
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.39 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. from numpy import sign
  3.  
  4. # Motors
  5.  
  6. EstesA8  = { "t1": 0.22 , "t2": 0.30 , "t3": 0.65 , "t4": 0.72 , "fmax": 10.0 , "fsus":  2.2 }
  7. EstesB6  = { "t1": 0.18 , "t2": 0.30 , "t3": 0.80 , "t4": 0.86 , "fmax": 12.0 , "fsus":  4.5 }
  8. EstesC6  = { "t1": 0.18 , "t2": 0.24 , "t3": 1.80 , "t4": 1.84 , "fmax": 14.6 , "fsus":  4.5 }
  9. EstesD12 = { "t1": 0.35 , "t2": 0.40 , "t3": 1.60 , "t4": 1.70 , "fmax": 30.0 , "fsus": 10.0 }
  10. KlimaA6  = { "t1": 0.35 , "t2": 0.70 , "t3": 0.75 , "t4": 0.75 , "fmax":  7.0 , "fsus":  0.0 }
  11. KlimaB4  = { "t1": 0.55 , "t2": 0.70 , "t3": 1.00 , "t4": 1.25 , "fmax":  8.0 , "fsus":  4.0 }
  12. KlimaC2  = { "t1": 0.55 , "t2": 0.70 , "t3": 4.80 , "t4": 5.05 , "fmax":  5.0 , "fsus":  2.0 }
  13. KlimaD3  = { "t1": 0.65 , "t2": 1.00 , "t3": 5.50 , "t4": 6.00 , "fmax":  9.0 , "fsus":  3.0 }
  14. KlimaD9  = { "t1": 0.25 , "t2": 0.60 , "t3": 1.80 , "t4": 2.15 , "fmax": 25.0 , "fsus":  9.0 }
  15.  
  16. # Environment
  17. g   = 9.8 # m/s^2
  18. rho = 1.2 # kg/m^3
  19. dt  = 0.001 # s
  20. rod = 1 # m - rod length
  21.  
  22. # Rocket
  23. diam  = 0.042 # m
  24. m     = 0.100  # kg
  25. cd    = 0.65
  26. chute = 0.35  # m
  27.  
  28. motor = EstesC6
  29. delay = 5
  30.  
  31. def getThrust(t):
  32.     if t<=motor["t1"]:
  33.         thrust = motor["fmax"]*t/motor["t1"]
  34.     if motor["t1"]<t<motor["t2"]:
  35.         thrust = motor["fmax"] + ( (t-motor["t1"])/(motor["t2"]-motor["t1"]) * (motor["fsus"]-motor["fmax"]) )
  36.     if motor["t2"]<=t<=motor["t3"]:
  37.         thrust = motor["fsus"]
  38.     if motor["t3"]<t<=motor["t4"]:
  39.         thrust = motor["fsus"] - ( (t-motor["t3"])/(motor["t4"]-motor["t3"]) * motor["fsus"] )
  40.     if t>motor["t4"]:
  41.         thrust=0
  42.     return thrust
  43.  
  44. h=0
  45. v=0
  46. i=0
  47. t=0
  48. hold=0
  49. tmax=100
  50.  
  51. liftoff = False
  52. rodclr  = False
  53. burnout = False
  54. apogee  = False
  55. deploy  = False
  56. landing = False
  57.  
  58. lt  = []
  59. lh  = []
  60. lv  = []
  61. la  = []
  62. lq  = []
  63. lFr = []
  64. lFd = []
  65. lFg = []
  66. lFtot = []
  67.  
  68. while t<tmax and h>=0:
  69.     Fr = getThrust(t)
  70.  
  71.     Fg = -m*g
  72.  
  73.     q  = 0.5 * rho * v**2
  74.     Fd = q * 3.14159 * (diam/2)**2 * cd * -sign(v)
  75.  
  76.     Ftot = Fr + Fg + Fd
  77.     a = Ftot/m
  78.  
  79.     v = v + a*dt
  80.     h = h + v*dt
  81.    
  82.     if h<0 and not liftoff:
  83.         h=0
  84.         v=0
  85.  
  86.     if h>0 and not liftoff:
  87.         h=0
  88.         print("Liftoff at T+ %.2f" % t + " s")
  89.         liftoff=True
  90.  
  91.     if h>rod and not rodclr:
  92.         print("Rod clr at T+ %.2f" % t + " s, v=%.2f" % abs(v) + " m/s")
  93.         rodclr=True
  94.  
  95.     if t>motor["t4"] and not burnout:
  96.         print("Burnout at T+ %.2f" % t + " s, h=%.2f" % h + " m, v=%.2f" % v + " m/s")
  97.         burnout=True
  98.  
  99.     if h<hold and not apogee:
  100.         print("Apogee  at T+ %.2f" % t + " s, h=%.2f" % h + " m")
  101.         apogee=True
  102.     hold = h
  103.  
  104.     if t>motor["t4"]+delay and not deploy:
  105.         print("Deploy  at T+ %.2f" % t + " s, h=%.2f" % h + " m, v=%.2f" % abs(v) + " m/s")
  106.         diam = chute
  107.         deploy=True
  108.         if not apogee:
  109.             print("Deployed before apogee.")
  110.  
  111.     if h<0 and apogee:
  112.         print("Landing at T+%.2f" % t + " s, v=%.2f" % abs(v) + " m/s")
  113.         if not deploy:
  114.             print("Landed before deployment.")
  115.  
  116.     lt.append(t)
  117.     lh.append(h)
  118.     lv.append(v)
  119.     la.append(a/g)
  120.     lq.append(q)
  121.     lFr.append(Fr)
  122.     lFd.append(Fd)
  123.     lFg.append(Fg)
  124.     lFtot.append(Ftot)
  125.  
  126.     i=i+1
  127.     t=t+dt
  128.  
  129. print()
  130.  
  131. plt.subplot(411)
  132. plt.plot(lt,lh)
  133. plt.ylabel('Altitude / [m]')
  134. plt.grid(True)
  135.  
  136. plt.subplot(412)
  137. plt.plot(lt,lv)
  138. plt.ylabel('Speed / [m/s]')
  139. plt.grid(True)
  140.  
  141. plt.subplot(413)
  142. plt.plot(lt,la)
  143. plt.ylabel('Acceleration / [G]')
  144. plt.grid(True)
  145.  
  146. plt.subplot(414)
  147. plt.plot(lt,lFr)
  148. plt.xlabel('Time / [s]')
  149. plt.ylabel('Dynamic Pressure / [Pa]')
  150. plt.grid(True)
  151.  
  152. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement