Main Content

Create a Flight Animation from Trajectory Data

This example shows how to create a flight animation for a trajectory using a FlightGear Animation object.

When running this example within the product, you must customize the example with your FlightGear installation and uncomment the GenerateRunScript, system, and play commands. You must also copy the HL20 folder into the $FLIGHTGEAR/data/Aircraft/ folder. The HL20 folder is located in the working folder of this example, or under the following folder:

fullfile(matlabroot, "toolbox", "aero", "animation")

Note: This example is not supported for MATLAB Online.

Load Recorded Flight Trajectory Data

The flight trajectory data for this example is stored in a comma separated value formatted file. Use readmatrix to read the data from the file.

tdata = readmatrix('asthl20log.csv');

Create a Time Series Object from Trajectory Data

Use the MATLAB® timeseries command to create the time series object, ts, from the latitude, longitude, altitude, and Euler angle data along with the time array in tdata. To convert the latitude, longitude, and Euler angles from degrees to radians, use the convang function.

ts = timeseries([convang(tdata(:,[3 2]),'deg','rad') ...
                 tdata(:,4) convang(tdata(:,5:7),'deg','rad')],tdata(:,1));

You can create imported data from this data using other valid formats, such as 'Array6DoF'. For example:

ts = [tdata(:,1) convang(tdata(:,[3 2]),'deg','rad') tdata(:,4) ... convang(tdata(:,5:7),'deg','rad')];

and 'Array3DoF'.

ts = [tdata(:,1) convang(tdata(:,3),'deg','rad') tdata(:,4) ... convang(tdata(:,6),'deg','rad')];

Use FlightGearAnimation Object to Initialize Flight Animation

Open a FlightGearAnimation object.

h = Aero.FlightGearAnimation;

Set FlightGearAnimation object properties for timeseries.

h.TimeseriesSourceType = 'Timeseries';
h.TimeseriesSource = ts;

Set FlightGearAnimation object properties about FlightGear.

These properties include the path to the installation folder, the aircraft geometry model, and the network information for FlightGear flight simulator.

h.FlightGearBaseDirectory = 'C:\Program Files\FlightGear';
h.GeometryModelName = 'HL20';
h.DestinationIpAddress = '127.0.0.1';
h.DestinationPort = '5502';

Set the desired initial conditions (location and orientation) for FlightGear flight simulator.

h.AirportId = 'KSFO';
h.RunwayId = '10L';
h.InitialAltitude = 7224;
h.InitialHeading = 113;
h.OffsetDistance = 4.72;
h.OffsetAzimuth = 0;

Enable "just in time" scenery installation for FlightGear flight simulator. Required scenery will be downloaded while the simulator is running. For Windows® systems, you may encounter an error message while launching FlightGear with this option enabled. For more information, see Installing Additional FlightGear Scenery.

h.InstallScenery = true;

Disable FlightGear Shaders.

h.DisableShaders = true;

Set the seconds of animation data per second of wall-clock time.

h.TimeScaling = 5;

Use get(h) to check the FlightGearAnimation object properties and their values.

get(h)
             OutputFileName: 'runfg.bat'
    FlightGearBaseDirectory: 'C:\Program Files\FlightGear'
          GeometryModelName: 'HL20'
       DestinationIpAddress: '127.0.0.1'
            DestinationPort: '5502'
                  AirportId: 'KSFO'
                   RunwayId: '10L'
            InitialAltitude: 7224
             InitialHeading: 113
             OffsetDistance: 4.7200
              OffsetAzimuth: 0
             InstallScenery: 1
             DisableShaders: 1
               Architecture: 'Default'
                TimeScaling: 5
            FramesPerSecond: 12
                     TStart: NaN
                     TFinal: NaN
           TimeSeriesSource: [1x1 timeseries]
       TimeSeriesSourceType: 'Timeseries'
          TimeSeriesReadFcn: @TimeseriesRead

Create a Run Script to Launch FlightGear Flight Simulator

To start FlightGear with the desired initial conditions (location, date, time, weather, and operating modes), create a run script with the GenerateRunScript command. By default, GenerateRunScript saves the run script as a text file named 'runfg.bat'.

GenerateRunScript(h)

You do not need to generate this file each time the data is viewed. Generate it only when the desired initial conditions or FlightGear information changes.

Start FlightGear Flight Simulator

To start FlightGear from the MATLAB command prompt, type the system command to execute the run script created by GenerateRunScript.

system('runfg.bat &');

Tip: With the FlightGear window in focus, press the V key to alternate between the different aircraft views: cockpit view, helicopter view, and chase view.

Play the Flight Animation of Trajectory Data

Once FlightGear is up and running, the FlightGearAnimation object can start to communicate with FlightGear. To display the flight animation with FlightGear, use the play command.

play(h)

To display a screenshot of the flight animation, use the MATLAB image command.

image(imread('astfganim01.png','png'));
axis off;
set(gca,'Position',[ 0 0 1 1 ]);
set(gcf,'MenuBar','none');

See Also

Related Topics