Capabilities
Launching Rockets with Falcon-Simulink Integration
May 31, 2023
· Written by
Allie O'brien

Falcon, Duality’s Digital Twin Simulator, uses Unreal’s Chaos physics engine. This engine is good for medium fidelity, rigid-body physical simulations, but Falcon also needs a built-in, high-fidelity, general purpose simulator for dynamical systems.

A dynamical system is one where mathematical functions define the state of an object over time. For example: a swinging pendulum, or — a rocket booster launching a projectile into the sky!

A great tool for this is MATLAB: an industry standard software to model dynamical systems for engineering. To obtain a high-fidelity simulation of a dynamical system within Falcon, we discuss how to integrate Simulink, an extension of MATLAB, and use it to launch a rocket.

What are MATLAB and Simulink?

MATLAB is a matrix-based programming language optimized for technical computing. It is popular amongst scientists and engineers for its powerful numeric computing and graphing abilities. MATLAB supports many toolboxes and other downloadable packages which extend its capabilities.

Simulink is one such extension of MATLAB. It is a graphical programming environment used to model and simulate systems. Assuming one exists, you can download a free Simulink model from the MathWorks website for different systems, or you can build one of your own. A MATLAB program can start and step through a Simulink process as well as change the Simulink system’s input values dynamically, which allows us to control the Simulink process and to synchronize it with Falcon’s simulation time steps.

The rocket launch model

To demonstrate the integration of Simulink into Falcon, I chose to model a rocket launch. There are many simple systems which I could have chosen, but I selected this one because I had all of the necessary components. Plus: it’s fun to launch rockets! First, I found a neat Simulink model online which modeled a rocket launch (“Modeling a Thrust Vector Controlled Rocket in Simulink” from the MathWorks Student Competition Team). I modified this model to better suit my needs. Second, I happened to have on hand a 3D model of a spaceship which I created in a VFX class. 

Here is a high-level explanation of the interaction between Falcon and Simulink: 

  1. Falcon spawns a rocket object in the desert map and applies gravity.
  2. At each tick, Falcon sends a request to Simulink for the thrust produced by and the current mass of the rocket. Mass decreases over time as fuel is burned. Falcon sets the rocket’s mass and applies thrust accordingly, sending the rocket launching into the sky, provided that the thrust force is great enough to overcome gravity.
  3. Eventually, the fuel is depleted. At this point, Simulink reports the thrust as zero, and the rocket falls back to the earth.

In the next section, we dive deeper into this process.

Interplay between Falcon and Simulink

The process begins in MATLAB. MATLAB starts and pauses the Simulink rocket model, opens a local TCP server, and waits for a connection.

The Simulink model is paused
The MATLAB command window tells us when the server is ready to receive a connection

Now that the MATLAB server and Simulink model are ready, we can switch over to Falcon. First, we load the rocket into the desert map, and turn on the rocket’s physics, gravity, and collisions. The rocket is ready for liftoff. Next, we connect to the MATLAB server. If the connection is successful, then the Falcon process sends some initial data to signal Simulink to take a step.                   

The rocket is ready for liftoff

Back on the MATLAB side, the program responds to Falcon’s request by commanding the Simulink simulation to take a step. First, Simulink advances its simulation clock by the fixed time step. Then, it calculates the thrust generated by and the current mass of the rocket. 

Thrust is calculated as the product of two constant values, mass flow rate and exhaust velocity. Mass flow rate is the rate at which fuel is burned and expelled from the rocket, in kg/s. Exhaust velocity, the speed at which the vapors produced by the combustion in the rocket booster exit the rocket, is in m/s. Multiplying these two values yields the instantaneous momentum of the fuel in Newtons, better known as the thrust force:

For this demonstration, thrust is a one-dimensional value, not a multi-dimensional vector, since the rocket force is assumed to be pushing straight back.

The current mass of the rocket is calculated as the mass when the rocket was full minus the simulation time multiplied by the mass flow rate:     

mass flow rate

MATLAB returns to Falcon the calculated upward thrust and the current mass of the rocket. 

After receiving the calculated values from MATLAB, Falcon updates the mass of the rocket and applies the thrust. If the thrust is greater than the opposing gravitational force acting on the rocket, then the rocket will accelerate upwards.

Falcon is now ready to request more data. It transmits the elevation of the rocket back to MATLAB along with the next step instruction.

MATLAB sends the rocket elevation reported by Falcon into the Simulink model. Simulink plots the Z-position calculated by the Simulink rocket model on the same graph as the actual elevation of the projectile according to Falcon’s physics engine. Since thrust and gravity are the only forces acting on both models, we see that both calculations yield the same curve, which shows us that the Falcon simulation has successfully replicated the Simulink simulation.

The Falcon and Simulink simulations match

Video: The rocket launches upward until the fuel depletes, at which point gravity pulls the rocket back to the ground

Once the Simulink rocket model determines that the fuel has been completely depleted, it switches off the thrust. When Falcon starts to receive 0 for the value of thrust, the rocket slows its ascent and accelerates back to the earth as gravity pulls it down.

Adding new forces with Falcon’s physics engine

Now that we know that Falcon’s physics engine can replicate our Simulink model, let’s add more forces in Falcon to test our model under more realistic conditions.

First, we will turn on the drag for the rocket. In Falcon, this is accomplished with one line of code which sets the linear damping value of the rocket to a positive number. Let’s also apply a constant drift force to model the effect of wind on the rocket. Again, this is only one extra line of code.

<code>

self.component = self.get_root_component() # Root of Rocket’s 3D hierarchy

self.component.call_function("SetLinearDamping", .1)   # drag force

self.component.call_function("AddForce", [10,-5,0], 'None', False) # wind force

</code>

We can see how these new forces changed the rocket’s trajectory in two ways. First we will view the Falcon simulation, then we will examine the Simulink plots.

We can see in our new Falcon simulation that due to the drag force resisting its acceleration, the rocket does not travel as high, and completes its flight in less time. Also, due to the wind, the rocket drifts in the XY plane (parallel to the ground), and lands a small distance away from where it took off.

Video: The rocket now has drag forces resisting its acceleration, and a drift force that causes the rocket to land a small distance away from where it took off

If we transmit the position of the rocket in three dimensions to MATLAB, then we can plot the new trajectory against the ideal trajectory calculated by Simulink.

The rocket drifted about 4 meters in the positive X direction from the start to end positions
The rocket drifted about 9 meters in the negative Y direction
With drag present, the rocket reached its maximum height less than 3 kilometers off the ground, compared to the nearly 10 kilometers that it flew without drag

What we can take away from this demonstration is that integrating a Simulink model does not limit what we can do in Falcon, it only expands our capabilities. The Simulink rocket model helps us model a dynamical system within a larger Falcon simulation context that includes other factors and can also incorporate high fidelity rendering and sensor simulation. Simulink executes the mathematical model with a clear interface, and it is up to us to decide how to integrate that model within Falcon.

Considerations moving forward

The rocket launch demonstration is a proof-of-concept project. I will conclude with a few ideas for how a MATLAB/Simulink integration in Falcon can advance beyond this demonstration:

  1. In this demonstration, integrating Simulink gave us the ability to automatically generate plots. MATLAB is well known for its large collection of powerful visualization tools; in the future, MATLAB could generate supplementary visualizations of data produced by a Falcon simulation.
  2. This rocket launch demonstration could be improved to more realistically model physics. Currently, thrust is applied to the center of mass of the rocket, which removes the problem of the imbalance of the 3D spaceship model. If the thrust were applied to the location of the booster, the rocket would quickly fall over and careen into the nearby windmills. I designed the spaceship with aesthetics in mind, not aerodynamics! If we wanted to test the balance of a different 3D rocket model, we could do so by applying the thrust accurately to the center of mass and moment of inertia of the model. In this case, the thrust would also need to be changed to component space, not world space, so that the rocket accelerates in the direction extending from the nose of the rocket, not simply perpendicular to the ground.