Advertisement
oz1sej

Model rocket simulator

Sep 1st, 2014
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 5.28 KB | None | 0 0
  1. # Prevent Octave from thinking that this # is a function file:
  2. 1;
  3. clear;
  4.  
  5. # Constants
  6.     g   = 9.8; #m/s2
  7.     rho = 1.2; #kg/m3
  8.  
  9. # Motor Data
  10.  
  11.     # Read thrust curve files
  12.         load A8.m
  13.         load B6.m
  14.         load C6.m
  15.         load D12.m
  16.  
  17.     # Motor variable format: Propellant mass - Mass before firing - Mass after firing
  18.    
  19.     # Original Estes Engines
  20.         MotorDataA8   = [ .0033 ; .01635 ; .0102 ];
  21.         MotorDataB6   = [ .0056 ; .01823 ; .0097 ];
  22.         MotorDataC6   = [ .0108 ; .02400 ; .0094 ];
  23.         MotorDataD12  = [ .0211 ; .04260 ; .0160 ];
  24.  
  25.     # Derived / Clustered
  26.         MotorDataTwoDee   = 2*MotorDataD12;
  27.         MotorDataThreeDee = 3*MotorDataD12;
  28.         MotorDataFourDee  = 4*MotorDataD12;
  29.         TwoDee   = [ D12(:,1) , 2*D12(:,2) ];
  30.         ThreeDee = [ D12(:,1) , 3*D12(:,2) ];
  31.         FourDee  = [ D12(:,1) , 4*D12(:,2) ];
  32.  
  33. # Rocket data
  34.                                                                         # KURIOSUM: Et hønseæg vejer i gennemsnit 63 g. Kilde: Danæg
  35.  
  36.     # Rascal
  37.     # m  = 0.0411  kg
  38.     # d  = 0.0250  m
  39.     # d2 = 0.3     m
  40.  
  41.     # Code Red
  42.     # m  = 0.0666  kg
  43.     # d  = 0.034   m
  44.     # d2 = 0.3     m
  45.  
  46.     # Modular Model Rocket
  47.     # m  = 0.095 kg
  48.     # d  = 0.044 m
  49.     # d2 = 0.3   m
  50.  
  51.     m         = 0.060; # Rocket mass in kg without engine
  52.     d         = 0.050; # Rocket frontal diameter in m
  53.     d2        = 0.300; # Frontal diameter after deployment (chute diameter)
  54.  
  55.     cd        = 0.65; # Rocket drag coefficient
  56.     cd2       = 1.00; # Drag coefficient after deployment
  57.  
  58.     # Motor data
  59.         Motor     = B6;
  60.         MotorData = MotorDataB6;
  61.         delay     = 4; # Delay charge burn time
  62.  
  63. # Set simulation timestep
  64.     dt = 0.001;
  65.  
  66. # Interpolate thrust curve
  67.     tf         = [ 0 : dt : max(Motor(:,1)) ];
  68.     Thrust     = abs(interp1(Motor(:,1),Motor(:,2),tf,"cubic"));
  69.     mDelay     = MotorData(2)-MotorData(3)-MotorData(1); # Mass of delay charge, ejection charge and clay cap
  70.     mDelay     = mDelay/2; # Assuming ejection charge and cap together weighs about the same as the delay charge
  71.     dmDelay    = dt/delay*mDelay;
  72.  
  73. ### SIMULATION ###
  74.  
  75. m   = m + MotorData(2);
  76. A   = pi*(d /2)^2;
  77. A2  = pi*(d2/2)^2;
  78.  
  79. runtime = 300; # Maximum simulation runtime in seconds
  80.  
  81. i = 1;
  82. t = 0;
  83. h = 0;
  84. v = 0;
  85. a = 0;
  86. I = 0;
  87.  
  88. done      = false;
  89. ignition  = false;
  90. liftoff   = false;
  91. burnout   = false;
  92. apogee    = false;
  93. deploy    = false;
  94. landing   = false;
  95.  
  96. printf("\n\n*** SIMULATION ***\n\n")
  97.  
  98. PropMass = MotorData(1)
  99. Burntime = max(Motor(:,1))
  100. TotalImp = sum(Thrust)*dt
  101. SpeciImp = TotalImp/(PropMass*g)
  102.  
  103. while (!done)
  104.    
  105.     t = i * dt;
  106.  
  107.     if( t < Burntime )
  108.         thrust = Thrust(i);
  109.         TotalImpulseSoFar = thrust*dt;
  110.     else
  111.         thrust = 0;
  112.     endif
  113.  
  114.     drag   = -0.5 * rho * cd * A * v^2 * sign(v) ;
  115.     q      =  0.5 * rho *          v^2; # Dynamic Pressure https://en.wikipedia.org/wiki/Max_Q
  116.    
  117.     if( ignition && !burnout )
  118.         dm = PropMass*TotalImpulseSoFar/TotalImp;
  119.         m  = m-dm;
  120.     endif
  121.  
  122.     weight = -m*g;
  123.  
  124.     if( burnout && !deploy )
  125.         m  = m-dmDelay;
  126.     endif
  127.  
  128.     F = thrust + drag + weight;
  129.  
  130.     a    = F/m;
  131.  
  132.     if( !liftoff && a<0 )
  133.         a = 0;
  134.     endif
  135.  
  136.     gees = abs(a/g+1);
  137.    
  138.     dv = a * dt;
  139.     v  = v + dv;
  140.  
  141.     dh = v * dt;
  142.     h  = h + dh;
  143.  
  144.     if( h < 0 )
  145.         h=0;
  146.     endif
  147.  
  148.     if( !ignition && thrust > 0 )
  149.         ignition = true;
  150.         printf("\nIgnition time  ")
  151.         t
  152.     endif
  153.  
  154.     if( !liftoff && h > 0 )
  155.         liftoff = true;
  156.         printf("\nLiftoff time   ")
  157.         t
  158.     endif
  159.  
  160.     if( ignition && !burnout && ( thrust == 0 ) )
  161.         burnout = true;
  162.         printf("\nBurnout at     ")
  163.         t
  164.         printf("Burnout height ")
  165.         h
  166.         printf("Burnout speed  ")
  167.         v
  168.     endif
  169.  
  170.     if( !apogee && liftoff && v<0 )
  171.         apogee = true;
  172.         printf("\nApogee at      ")
  173.         t
  174.         printf("Apogee height  ")
  175.         h
  176.     endif
  177.  
  178.     # Deploy condition - time or something else?
  179.  
  180.     if( t == ( Burntime + delay ) )
  181.         deploy = true;
  182.         cd = cd2;
  183.         A  = A2;
  184.         m=m-mDelay;
  185.         printf("\nDeploy at      ")
  186.         t
  187.         printf("Deploy height  ")
  188.         h
  189.         printf("Deploy speed   ")
  190.         v
  191.     endif
  192.  
  193.     if( liftoff && ( h == 0 ) )
  194.         landing = true;
  195.         printf("\nLanding at     ")
  196.         t
  197.         printf("Landing speed  ")
  198.         v
  199.     endif
  200.  
  201.     if( deploy && ( h == 0 ) )
  202.         done = true; # Rocket didn't leave the pad
  203.     endif
  204.  
  205.     if( landing || ( t >= runtime ) )
  206.         done = true;
  207.         if( t>=runtime )
  208.             printf("\nLanding not detected within preset runtime - stopping")
  209.         endif
  210.     endif
  211.  
  212.     t2(i)      = t;
  213.     thrust2(i) = thrust;
  214.     drag2(i)   = drag;
  215.     F2(i)      = F;
  216.     a2(i)      = a;
  217.     v2(i)      = v;
  218.     h2(i)      = h;
  219.     m2(i)      = m;
  220.     q2(i)      = q;
  221.     gees2(i)   = gees;
  222.  
  223.     t = t + dt;
  224.     i = i + 1;
  225.  
  226. endwhile
  227.  
  228. if( burnout && !liftoff )
  229.     printf("\nRocket didn't leave the pad - too heavy!")
  230. endif
  231.  
  232. graphics_toolkit ("gnuplot")
  233.  
  234. # Time Plots
  235.  
  236. plot(t2,h2);
  237. grid on;
  238. xlabel("time / [s]");
  239. ylabel("altitude / [m]");
  240. print h.png
  241.  
  242. plot(t2,a2);
  243. grid on;
  244. xlabel("time / [s]");
  245. ylabel("acceleration / [m/s^2]");
  246. print a.png
  247.  
  248. plot(t2,v2);
  249. grid on;
  250. xlabel("time / [s]");
  251. ylabel("speed / [m/s]");
  252. print v.png
  253.  
  254. plot(t2,drag2);
  255. grid on;
  256. xlabel("time / [s]");
  257. ylabel("drag / [N]");
  258. print drag.png
  259.  
  260. plot(t2,m2);
  261. grid on;
  262. xlabel("time / [s]");
  263. ylabel("mass / [kg]");
  264. print m.png
  265.  
  266. plot(tf,Thrust);
  267. grid on;
  268. xlabel("time / [s]");
  269. ylabel("thrust / [N]");
  270. print thrust.png
  271.  
  272. plot(t2,q2);
  273. grid on;
  274. xlabel("time / [s]");
  275. ylabel("dynamic pressure / [Pa]");
  276. print dynamicpressure.png
  277.  
  278. plot(t2,gees2);
  279. grid on;
  280. xlabel("time / [s]");
  281. ylabel("gees / [G]");
  282. print gees.png
  283.  
  284.  
  285. printf("\n*** END ***\n\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement