%% Automatically Generate MATLAB App for Deploying Simulink Models
% This example shows how a MATLAB App can be automatically generated for
% deploying a Simulink Model.

% Copyright 2019 The MathWorks, Inc.

%% Model Overview
% The model RoadSuspensionInteractionIn3DOF shown below simulates the
% vehicle dynamics based on the road - suspension interaction for different
% road profiles. The vehicle dynamics are captured in three degrees of
% freedom: vertical displacement, roll, and pitch. The road profiles for
% the left and right tires can be passed into the model as external inputs.
% The Road-Suspension Interaction subsystem calculates the suspension
% forces on the vehicle at the four tire locations based on the road data
% and the current vehicle state. In the Body Dynamics subsystem these
% forces and the resulting pitch and roll moments are used to determine the
% vehicle motion in three degrees of freedom: vertical displacement, roll,
% and pitch.
%%
model = "RoadSuspensionInteractionIn3DOF";
open_system(model);

%% Automatically Generate MATLAB App for the Model
% We will now generate a MATLAB App for the model. Note that in order to
% run simulations road profiles are required. Instead of having to create a
% road profile everytime for running a similation, we will specify a set of
% road profiles to be automatically available in the app to help users
% explore the app initially. These road profiles are saved in the file,
% |RoadProfiles.mat|. The app also provides a way to bring in additional
% road profiles.
%
% In addition to road profiles, the app can also be configured to have a
% set of initial states that can be used for simulations. These initial
% states are saved in the file, |InitialConditions.mat|. An initial
% state can be selected from the app for simulation.
%%
load RoadProfiles.mat;
load InitialConditions.mat;
simulink.compiler.genapp(bdroot, "InputMATFiles", ["RoadProfiles.mat", "InitialConditions.mat"]);

%%
% <<../RoadSuspensionInteractionIn3DOFApp.png>>

%% Launching the App
% The app will automatically launch but it can also be launched manually by
% using the name of the app. By default the name of the app is the name of
% the model with the string |_SLSimApp| appended at the end,
% |RoadSuspensionInteractionIn3DOF_SLSimApp|, for this example. A different
% name can be specified by passing in a value for the |AppName| parameter
% with |simulink.compiler.genapp|.

%% Using the App
% When the app opens, click on the |External Input:| dropdown button to
% select a road profile, for example |Road4|. A different initial state can
% also be selected by clicking on the |Initial State:| dropdown button.
% Click on the |Simulate| button to run the simulation. Signals that have
% logging enabled, will automatically be available under |Logged Signals|
% for plotting.

%%
% <<../RoadSuspensionInteractionIn3DOFResults.png>>

%% Compile the App for Deployment
% To compile the app, use the |mcc} command, followed by the name of the
% generated app.

  mcc -m RoadSuspensionInteractionIn3DOF_SLSimApp