%% Add App Designer App to Inverted Pendulum Model
%
%% 
% This example shows how to stream signal signals to an App Designer instrument 
% panel app from a Simulink Real-Time application. The example builds the real-time 
% application from the model |slrt_ex_pendulum_100Hz|. The instrument panel contains 
% these App Designer components:
% 
% * Drop down window &mdash; To show all the available target computers.
% * Connect/disconnect button &mdash; To connect or disconnect the target computer chosen in the drop down window.
% * Load button &mdash; To load the application to the target computer.
% * Start/stop button &mdash; To start or stop the application on the target computer.
% * Stop time edit field &mdash; To display and set the stop time of the application loaded on the target computer.
% * Axes &mdash; To display an animation for the two inverted pendulum and cart system.
% * Axes &mdash; To display signal output for responses to disrupting the pendulum.
% * Nudge button &mdash; To apply input (nudge) to the cart that hold the pendulum.
% * Reference position spinner &mdash; To change the reference position of the pendulum and cart system.
% * Reference variation pattern knob &mdash; To add a variation pattern to the reference position of the pendulum and cart system.
% * Amplitude slider &mdash; To adjust the amplitude of the chosen reference variation pattern.
% * Frequency slider &mdash; To modify the frequency of the chosen reference variation pattern.
%
% To stream signal and parameter data between the real-time application and 
% the instrument panel app, the app uses the instrumentation object.
%
%%
load_system(fullfile(matlabroot,'toolbox','slrealtime','examples','slrt_ex_pendulum_100Hz'));

%% Start Target Computer and Build Real-Time Application
% These tasks generate the real-time application that streams data to the App Designer instrument panel app. 
%
% # Start the target computer.
% # Open the model <matlab:open_system(fullfile(matlabroot,'toolbox','slrealtime','examples','slrt_ex_pendulum_100Hz')) |slrt_ex_pendulum_100Hz|>.
% # Connect the development computer to the target computer. Build the 
% |slrt_ex_pendulum_100Hz| model. 
% # Deploy the real-time application to the target computer. 
% 
% In the MATLAB Command Window, type:
% 
set_param('slrt_ex_pendulum_100Hz', 'RTWVerbose', 'off');
tg = slrealtime;
rtwbuild('slrt_ex_pendulum_100Hz');
load(tg,'slrt_ex_pendulum_100Hz');
%% Run App Designer Instrument Panel App
% The App Designer instrument panel app |slrt_ex_pendulumApp| provides controls to start
% and interact with the real-time application |slrt_ex_pendulum_100Hz|. 
%
% *1.* Run the app. To start the App Designer app |slrt_ex_pendulumApp.mlapp| and create the handle |app|, in 
% the MATLAB Command Window, type: 
%
%  openExample('SlrtAddAppDesignerAppToInvertedPendulumModelExample');
%  app = slrt_ex_pendulumApp;
%%
%
% *2.* To connect with an available target computer, click the *connect* 
% button. The text on the button will switch to 'disconnect' and the *load* button will be enabled.
%
% *3.* To load the application to the target computer, click the *load* 
% button. After the application is loaded on the target computer, the *start* button and *stop time* edit field will be enabled.
%
% *4.* To set the stop time of the application, type your preferred stop time in the edit field and hit *enter*
% button. 
%
% *5.* To start running the application, click the *start* button.
%
% *6.* To disrupt the equilibrium of the pendulum on each cart, click the *Nudge* 
% button. You can adjust the nudge magnitude by using the value selection next
% to the button, hange the reference position by adjusting the
% value of reference position spinner, or choose a variation pattern for
% the reference position. 
% 
% <<../Pendulum_mlapp_start_up.png>>
%
%% App Callback Code
% The instrument panel app functionality is provided by callback code. 
% 
% Comments in the callback code in the instrument panel app |slrt_ex_pendulumApp.mlapp| 
% describe the callback operations and programming suggestions. To view the
% callback code, open |slrt_ex_pendulumApp.mlapp| in the App Designer, and then click the *Code
% View* tab. In the Command Window, type:
% 
%  edit slrt_ex_pendulumApp
% 
% *Specify Block Paths for Signals in Referenced Models*
% 
% To stream data from signals in the model, see the use of |connectLine| functions  
% in the |setupInstrumentation(app)| function in the app.
% 
% *updateAnimationCallback Function*
% 
% For each AcquireGroup, this function checks whether there is fresh data 
% since the last time the callback was called.  If there is data, the function 
% updates the animation objects.
% 
% *Update Axes and Animation by Using Acquire Groups*
% 
% In the callback code, this processing is visible as |AcquireGroupData| signal 
% groups in the |updateAnimationCallback| function. 
% 
%% Close the App and Models
% The instrument panel app handle |app| provides access to close the app. 
% 
% Close the app. In the MATLAB Command Window, type: 
%
%  close(app.UIFigure)
%%
% Close the open models. In the Command Window, type:
% 
bdclose ('all');
%% 
% Copyright 2020 The MathWorks, Inc.

