Main Content

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 — To show all the available target computers.

  • Connect/disconnect button — To connect or disconnect the target computer chosen in the drop down window.

  • Load button — To load the application to the target computer.

  • Start/stop button — To start or stop the application on the target computer.

  • Stop time edit field — To display and set the stop time of the application loaded on the target computer.

  • Axes — To display an animation for the two inverted pendulum and cart system.

  • Axes — To display signal output for responses to disrupting the pendulum.

  • Nudge button — To apply input (nudge) to the cart that hold the pendulum.

  • Reference position spinner — To change the reference position of the pendulum and cart system.

  • Reference variation pattern knob — To add a variation pattern to the reference position of the pendulum and cart system.

  • Amplitude slider — To adjust the amplitude of the chosen reference variation pattern.

  • Frequency slider — 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.

  1. Start the target computer.

  2. Open the model slrt_ex_pendulum_100Hz.

  3. Connect the development computer to the target computer. Build the slrt_ex_pendulum_100Hz model.

  4. 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');
### Generated code for 'slrt_ex_pendulum_100Hz' is up to date because no structural, parameter or code replacement library changes were found.
### Successful completion of build procedure for: slrt_ex_pendulum_100Hz
### Created MLDATX ..\slrt_ex_pendulum_100Hz.mldatx

Build Summary

0 of 1 models built (1 models already up to date)
Build duration: 0h 0m 3.951s

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.

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');