OPENFLIGHT GitHub repository : ย ย Openflight-sim/OPENFLIGHT: Educational Flight Simulatorย
OPENFLIGHT GitHub repository : ย ย Openflight-sim/OPENFLIGHT: Educational Flight Simulatorย
This document summarizes the high-level architecture and data flow of the OpenFlight flight simulator, as illustrated in the accompanying interactive flow diagram. The simulator consists of two primary components: a Julia backend responsible for the physics simulation and a JavaScript frontend handling user interaction and 3D visualization. These components communicate in real-time via WebSockets.
Julia Backend (Orange Blocks):
Physics Engine: Calculates the aircraft's motion based on aerodynamic forces, propulsive forces, gravity, and control inputs using a Runge-Kutta 4th order (RK4) integrator.
Aerodynamic & Propulsion Models: Loads aircraft-specific data (from YAML files) and calculates forces (Lift, Drag, Sideforce) and moments (Roll, Pitch, Yaw) based on the current flight state (Mach, Alpha, Beta, angular rates) and control surface deflections. Includes models for thrust generation.
Actuator Dynamics: Simulates the time delay and rate limits of control surfaces and engine thrust response.
Data Management: Handles loading configuration files, aerodynamic databases, and recording flight telemetry data to CSV files.
WebSocket Server: Manages the connection with the JavaScript frontend, receiving state/control data and sending back simulation results.
JavaScript Frontend (Green Blocks):
3D Visualization: Uses Babylon.js to render the 3D environment, including the aircraft model (default or loaded GLB), terrain, sky, runway, and other scenery objects.
User Interface (GUI): Displays flight data (altitude, speed, attitude, etc.) and provides interactive elements (buttons for pause, loading models) using Babylon.GUI.
Input Handling: Captures keyboard and gamepad inputs, translating them into control demands (pitch, roll, yaw, thrust) for the simulation.
WebSocket Client: Connects to the Julia backend, sends user inputs and current state, and receives updated simulation data to refresh the visualization and GUI.
WebSocket Communication (Purple Block):
Acts as the real-time bridge between the Julia backend and the JavaScript frontend.
JS sends its current perceived state, user control demands, and the time elapsed since the last frame (deltaTime) to Julia.
Julia sends back the newly calculated state (position, velocity, orientation, angular rates), resulting forces, attained control positions (after actuator dynamics), and the official simulation time (server_time) to JS.
Initialization:
Julia: Starts first, loads necessary packages, reads the default_mission.yaml configuration, finds an available network port, loads the specified aircraft's aerodynamic data, calculates initial parameters (like inertia), and modifies the JavaScript initializations.js file to inject the port number and mission parameters. It then starts the WebSocket server and waits for a connection.
JavaScript: The browser loads the HTML and the (potentially modified) initializations.js. Babylon.js engine and scene are created. Initial scenery, default aircraft, GUI, and cameras are set up. It reads the synced parameters (like the port number) and establishes a WebSocket connection to the Julia server. The main render loop begins.
Main Simulation Loop (Continuous):
JS (Input & Send): The render loop reads current keyboard/gamepad inputs, determining the pilot's demanded control settings. It packages these demands along with the aircraft's current 3D position, orientation, velocity, angular velocity, and the measured deltaTime into a JSON object and sends it to Julia via WebSocket.
Julia (Receive & Calculate): The WebSocket server receives the JSON data. It updates its internal simulation time (sim_time) using the client's deltaTime. It passes the demanded controls through the actuator dynamics model to get attained control values. It then calls the RK4 integrator, which repeatedly calls the Equations of Motion function. The EOM function uses the aero/propulsion models (which query the loaded aero database) to calculate forces and moments, ultimately determining the aircraft's linear and angular accelerations. Flight data is optionally recorded.
Julia (Send Update): Julia packages the newly calculated state vector (position, velocity, orientation, angular rates), resulting global forces, aerodynamic angles, attained control values, and the authoritative sim_time (as server_time) into a JSON object and sends it back to JS via WebSocket.
JS (Receive & Update): The WebSocket client receives the updated data. It updates the global JavaScript variables representing the aircraft's state. It stores the server_time. These updated variables are then used by the render loop to:
Update the 3D position and rotation of the aircraft model.
Update the trajectory markers and force/velocity vectors.
Update the text values displayed in the GUI.
The frame is rendered, and the loop repeats.
Optional GLB Loading: The user can click the "Load Aircraft" button (GUI Setup) which triggers the file input. The GLB Loader script handles reading the file, applying specific transformations based on filename, loading the model into the scene, disabling the default model, and repositioning lights/propeller.
This cycle ensures that the computationally intensive physics simulation runs on the backend (Julia), while the frontend (JavaScript) handles the user interaction and visual representation, synchronized via WebSockets.