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.
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.
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:
In the next section, we dive deeper into this process.
The process begins in MATLAB. MATLAB starts and pauses the Simulink rocket model, opens a local TCP server, and waits for 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.
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:
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.
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.
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.
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.
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: