%% Modeling a Launch Abort System
% This example shows how to model a launch abort system.  An aircraft is launched into
% outer space.  If an anomaly or fault occurs during the launch, the
% operation is aborted, and the aircraft is sent back down to Earth.

% Copyright 2007-2013 The MathWorks, Inc.

%% Description
% The Launch Abort Controller state chart is used to control the mode logic
% and launch abort behavior.   The state chart consists of two parallel
% states: ModeLogic and Abort.  Within the Mode Logic state, the aircraft
% can be in either one of four states:
%
% * RTLS: Return to Launch Site
% * DRL: Downrange Landing
% * AOA: Abort Once Around
% * ATO: Abort to Orbit
%
% The aircraft transitions from one mode to the next based on the altitude
% of the aircraft (a simplification).  

clear
close all;
bdclose all;
open_system('sf_launchabort')
r = sfroot;
m = r.find('-isa', 'Stateflow.Machine', 'Name', 'sf_launchabort');
c = m.find('-isa', 'Stateflow.Chart', 'Name', 'launchAbortController');
c.Visible=1;

%%
% The second parallel state controls the behavior of the aircraft if an
% anomaly is detected.  If an anomaly occurs, the system transitions to the
% abortLogic substate and the following 3 actions can occur, depending on
% the current state of the system:
%
% * Dump the fuel
% * Release the tanks
% * Release the stage

s = c.find('-isa', 'Stateflow.State', 'Name', 'abortLogic');
sf('Open', s.Id);

%%
% Although Stateflow(R) is used to schedule when these events occur, the fuel,
% tank, and stage subsystems are modeled in Simulink(R).  Since this is a
% simplified example, the dynamics of the subsystems are not modeled here.
% However, a visualization of the fuel dump, tank release, and stage
% release can be seen through the use of Simulink(R) 3D Animation(TM).

% set VR figure to show viewpoint with runway overview
open_system([gcs '/VR Sink']);
wname = get_param([gcs '/VR Sink'], 'WorldFileName');
myworld = vrworld(wname);
myfig = get(myworld, 'Figures');
% there should be exactly one vrfigure open at this time
myfig = myfig(1);
set(myfig, 'Antialiasing', 'on');
set(myfig, 'NavPanel', 'none');
set(myfig, 'Viewpoint', 'Launch Site');
vrdrawnow;
drawnow;

%% Reference
% Nelson, D. K., Bradford, J. E., Olds, J. R., "Abortability Metrics:
% Quantifying Intact Abort Mode Availability for Reusable Launch Vehicles,"
% AIAA-2006-7293, Space 2006, San Jose, California, September 19-21, 2006.
