1. Scene Setup
1.1 Display STK window interface
%打开一个STK应用实例
uiapp = actxserver('STK10.application');
%获取STK用户界面
root = uiapp.Personality2;
%显示界面
uiapp.visible = 1;
It should be noted that the STK version number for creating uiapp should be consistent with the version number installed on this machine. I installed STK10 here .
Note: The root variables appearing in the following code are all root in this code.
Notice
(1) If you need to open multiple STK windows (each window is an STK instance), in order to prevent mutual influence between STK windows, you need to isolate them. The specific method is:
root.Isolate();
This is because the interaction between MATLAB and STK directly interacts with the STK engine, and the data in the window is read from the engine.
(2) The STK window should remain open while running the MATLAB automation code.
1.2 Load the scene
(1) Create a new scene
Create a scene called STK Automation :
% 创建一个新场景
root.NewScenario('STK Automation');
(2) Load an existing scene
root.LoadScenario('D:\Users\Desktop\AirSimualtion.sc');
1.3 Set the physical quantity unit in the scene
The units and optional values of the physical quantities contained in the scene can be viewed by right-clicking the scene in the Object Browser to open its property window:
Example automation code for setting units:
Set time and date unit
root.UnitPreferences.Item('DateFormat').SetCurrentUnit('UTCG');
set distance unit
root.UnitPreferences.Item('Distance').SetCurrentUnit('m');
Other units can be set according to the unit list in the scene property window.
Notice
After setting the unit on the MATLAB command line, open the scene property window in STK , and find that the unit in the interface is not set to the value we selected. This is because when using the MATLAB command line to automate, it directly interacts with the STK engine. Instead of operating UI components, when the property window is opened, the window will use the default value when it is initialized, so we don't see the unit automatically select the value set for us. For example to change the unit to m :
root.UnitPreferences.Item('Distance').SetCurrentUnit('m');
Open the property window to view the unit and find that it is still the default value km:
Get the units using the command line:
distance = root.UnitPreferences.GetCurrentUnitAbbrv("DistanceUnit");
output:
It can be seen that the unit formatting is actually in effect.
1.4 Set the simulation time interval of the scene
Open an STK window and create a scene with time and date in UTCG format:
%设置时间日期格式为UTCG
root.UnitPreferences.Item('DateFormat').SetCurrentUnit('UTCG');
%设置仿真时间区间
root.CurrentScenario.SetTimePeriod('1 Jul 2013 12:00:00.000', '2 Jul 2013 12:00:00.000');
Open the scene properties window, and you will find that the time interval changes accordingly.
Notice
You must first set the time and date format and then set the time interval . If the time and date format does not match the format of the beginning and end of the time interval, an error will be reported.
1.5 Simulation time update settings
(1) Set the time update type
There are three ways to update the simulation time in STK : Time Step , RealTime and X RealTime . Time step updates the simulation clock at a fixed time step, RealTime updates the simulation clock at the clock rate of the computer currently running STK , and X RealTime updates the simulation clock at X times the clock rate of the computer running STK . The currently selected time update mode can be viewed in the scene properties window:
Use MATLAB to set the time update type to fixed step size:
root.CurrentScenario.Animation.set('AnimStepType','eScTimeStep');
AnimStepType optional values are:
(2) Set the time step
The simulation time step can be seen in the lower left corner of the view
You can see that the current time step is 100s , use MATLAB to set the time step:
root.CurrentScenario.Animation.AnimStepValue=50
Now the step size becomes 50s :
2. Model settings
2.1 Aircraft model display
(1) Insert the aircraft in the scene
aircraft = root.CurrentScenario.Children.New('eAircraft', 'MyAircraft');
Now add an aircraft to the currently created scene. The default aircraft path prediction model is GreatArc , and at least two trajectory points need to be set to display the aircraft model on the view.
After setting, the model will be displayed, and now the default aircraft model is displayed.
(2) Control the visibility of the aircraft model
The visibility code to control the aircraft model using MATLAB is:
% 设置为1可见,设置为0不可见
aircraft.Graphics.IsObjectGraphicsVisible=1;
2.2 View aircraft properties
Here are some ways to view common properties in simulation:
(1) Check the waypoint forecast model
aircraft.RouteType
output:
(2) 3D model of the aircraft
aircraft.VO.Model.ModelData.get()
output:
(3) Waypoint information
number of waypoints
aircraft.Route.Waypoints.Count()
Export all waypoints to the MATLAB workspace:
aircraft.Route.Waypoints.ToArray()
2.3 Set the 3D model file of the aircraft
There are two ways to set up the 3D file of the aircraft:
(1) Set by setting the file name
The file name of the 3D model is not an arbitrary string, but a relative path in the STK installation directory. Generally, STK saves the preset 3D aircraft model in the STKData\VO\Models\Air directory of the installation directory, so use the file name to set the aircraft The method of 3D model is:
aircraft.VO.Model.ModelData
.set('Filename','STKData\VO\Models\Air\b-52_stratofortress.mdl')
Running this MATLAB command reveals that the model is set to a B52 bomber from the default aircraft:
(2) Set by absolute path
aircraft.VO.Model.ModelData
.set('FileName',
'C:\Program Files (x86)\AGI\STK 10\STKData\VO\Models\Air\f-22a_raptor.mdl')
Now the 3D model of the aircraft becomes F22a :
3. Waypoint setting
3.1 Setting up the waypoint forecast model
The waypoint prediction model of the aircraft has
Commonly used are GreatArc and RealTime , use MATLAB to set the waypoint forecast model:
%RealTime路径点预报模型
aircraft.SetRouteType('ePropagatorRealtime');
%GreatArc路径点预报模型
aircraft.SetRouteType('ePropagatorGreatArc');
Notice
Set the waypoint forecast model to RealTime on the MATLAB command line , open the aircraft properties window and find that it is still GreatArc , but run the command to view the waypoint forecast model:
aircraft.RouteType
It will be found that the changes have taken effect. This is because MATLAB directly interacts with the STK engine, not with the window. When the property window is opened, it will be automatically selected as the default value. To update the path, you need this:
%设置飞机的路径预报模型为实时
aircraft.SetRouteType('ePropagatorRealtime');
aircraft.Route.Propagate;
3.2 Add trajectory points when the path prediction model is GreatArc
(1) Add a single track point
% IAgAircraft aircraft: Aircraft object
% Set route to great arc, method and altitude reference
%路径点预测模型
aircraft.SetRouteType('ePropagatorGreatArc');
%获取飞机的路径对象
route = aircraft.Route;
%路径生成方法,这里设置的是根据速度确定中间点的加速度和时刻
route.Method = 'eDetermineTimeAccFromVel';
% 高度参考系
route.SetAltitudeRefType('eWayPtAltRefMSL');
% 添加第一个轨迹点
waypoint = route.Waypoints.Add();
waypoint.Latitude = 37.5378;
waypoint.Longitude = 14.2207;
waypoint.Altitude = 5; % km
waypoint.Speed = .1; % km/sec
% 添加第二个轨迹点
waypoint2 = route.Waypoints.Add();
waypoint2.Latitude = 47.2602;
waypoint2.Longitude = 30.5517;
waypoint2.Altitude = 5; % km
waypoint2.Speed = .1; % km/sec
%保存轨迹点
route.Propagate;
The aircraft in the code is the aircraft model inserted above. Run the code to see that the track settings have taken effect:
(2) Add multiple waypoints
% IAgAircraft aircraft: Aircraft object
route = aircraft.Route;
ptsArray = {37.5378,14.2207,5000,20,5;
47.2602,30.5517,5000,20,5;
47.2602,30.5517,5000,20,5;
37.5378,14.2207,5000,20,5;};
route.SetPointsSmoothRateAndPropagate(ptsArray);
%Propagate the route
route.Propagate;
(3) Precautions
(1) When setting multiple waypoints, two or more points must be passed in, otherwise all existing waypoints will be cleared. This means that ptsArray is at least a 2-dimensional array.
(2) When the waypoint is set, it will be automatically deduplicated, and the same point will only be set once.
(3) When setting the aircraft waypoint, the STK unit setting must be consistent with the input data point unit, otherwise the unit conversion will be performed automatically.
4. Use MATLAB to control the simulation
4.1 Control simulation run
Controlling the simulation process with MATLAB can be achieved using a simple for loop:
for i=1:100000
root.StepForward;
end
Run this script in MATLAB, STK will update the scene with the set clock step, pause running this script, STK will also pause the simulation.
4.2 Pausing the simulation
root.Pause
4.3 Reset view
root.Rewind
4.4 Take a step back
root.StepBackward
4.5 Positive and negative clock simulation
A positive clock simulation updates the scene in the direction of increasing time:
root.PlayForward
Negative clock simulation updates the scene in the direction of decreasing time:
root.PlayBackward