STK 11 Aviator Connect Example

« Go Back

Information

 
Notes/Code
This MATLAB script will connect to STK, create a an aircraft and use Aviator to create the different procedures during the route.  This script uses Connect commands to create the aircraft.

How to use:
Attached to this article is a *.m file with the below code. Download the file, or copy the code into a MATLAB and save as a *.m file.
Run the *.m file, it should not require any additional setup. As written, this MATLAB example requires STKv11 with the Integration and Aviator license.
 
close all
clear all
clc

try
    %Grab an existing instance of STK
    uiapp = actxGetRunningServer('STK11.application');
    root = uiapp.Personality2;
    checkempty = root.Children.Count;
    if checkempty == 0
        %If a Scenario is not open, create a new scenario
        uiapp.visible = 1;
        root.NewScenario('MATLAB_Aviator_Connect');
        scenario = root.CurrentScenario;
    else
        %If a Scenario is open, prompt the user to accept closing it or not
        rtn = questdlg({'Close the current scenario?',' ','(WARNING: If you have not saved your progress will be lost)'});
        if ~strcmp(rtn,'Yes')
            return
        else
            root.CurrentScenario.Unload
            uiapp.visible = 1;
            root.NewScenario('MATLAB_Aviator_Connect');
            scenario = root.CurrentScenario;
        end
    end

catch
    %STK is not running, launch new instance
    %Launch a new instance of STK11 and grab it
    uiapp = actxserver('STK11.application');
    root = uiapp.Personality2;
    uiapp.visible = 1;
    root.NewScenario('MATLAB_Aviator_Connect');
    scenario = root.CurrentScenario;
end

%Create an aircraft route with a default UAV performance model
root.ExecuteCommand('New / */Aircraft Aircraft1');
root.ExecuteCommand('SetPropagator */Aircraft/Aircraft1 MissionModeler');
root.ExecuteCommand('MissionModeler */Aircraft/Aircraft1 Aircraft Copy "Basic UAV"');
root.ExecuteCommand('MissionModeler */Aircraft/Aircraft1 Aircraft Choose "Basic UAV Copy"');

%Defining the first waypoint (Takeoff)
root.ExecuteCommand('MissionModeler */Aircraft/Aircraft1 Procedure Add AsFirst SiteType Runway ProcedureType "Takeoff"');
root.ExecuteCommand('MissionModeler */Aircraft/Aircraft1 Site 1 SetValue Latitude 36 deg');
root.ExecuteCommand('MissionModeler */Aircraft/Aircraft1 Site 1 SetValue Longitude -90 deg');

%Define the second waypoint (Enroute)
root.ExecuteCommand('MissionModeler */Aircraft/Aircraft1 Procedure Add After 1 SiteType Waypoint ProcedureType "Enroute"');
root.ExecuteCommand('MissionModeler */Aircraft/Aircraft1 Site 2 SetValue Latitude 40 deg');
root.ExecuteCommand('MissionModeler */Aircraft/Aircraft1 Site 2 SetValue Longitude -80 deg');

%Create a new Phase
root.ExecuteCommand('MissionModeler */Aircraft/Aircraft1 Phase Add After 1 "Phase 2"');

%Create a new performance model for cruise flight
try
    
root.ExecuteCommand(['MissionModeler */Aircraft/Aircraft1 Aircraft AddPerfModel "Cruise"',' "AGI Cruise Model" ','"MyCruise"']);

catch
    
end

root.ExecuteCommand('MissionModeler */Aircraft/Aircraft1 Phase SetActiveModel 2 Cruise "MyCruise"');
root.ExecuteCommand('MIssionModeler */Aircraft/Aircraft1 Aircraft PerfModel "Cruise" "MyCruise" SetValue ProfileItem 1 TAS 600 nm/hr');
root.ExecuteCommand('MIssionModeler */Aircraft/Aircraft1 Aircraft PerfModel "Cruise" "MyCruise" SetValue ProfileItem 2 TAS 600 nm/hr');

%Define the third waypoint (Landing)
root.ExecuteCommand('MissionModeler */Aircraft/Aircraft1 Procedure Add After 2 SiteType Runway ProcedureType "Landing"');
root.ExecuteCommand('MissionModeler */Aircraft/Aircraft1 Procedure MoveToNextPhase 3');
root.ExecuteCommand('MissionModeler */Aircraft/Aircraft1 Site 3 SetValue Latitude 36 deg');
root.ExecuteCommand('MissionModeler */Aircraft/Aircraft1 Site 3 SetValue Longitude -90 deg');

root.ExecuteCommand('MissionModeler */Aircraft/Aircraft1 ConfigureAll');
root.ExecuteCommand('MissionModeler */Aircraft/Aircraft1 CalculateAll');
root.ExecuteCommand('MissionModeler */Aircraft/Aircraft1 SendNtfUpdate');

You can learn more using STK and MATLAB together in the STK Help at STK Help > Integrating with STK > MATLAB Interface and in the Integrating STK and MATLAB FAQ.

You can learn more about using Connect Commands in the STK Help at STK Help > Integrating with STK > Connect Command Library and the Connect Commands for Aviator specifically at: STK Help > Integrating with STK > Connect Command Library > Listing by Module > Aviator (MissionModeler) Connect.