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 slrtpendulum. This model contains referenced models that produce the signals that are streamed and plotted. The instrument panel contains these App Designer components:

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

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

  • Nudge buttons — To apply input (nudges) to the carts that hold the pendulums.

To stream signal and parameter data between the real-time application and the instrument panel app, the app uses the instrumentation object.

open_system(docpath(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','slrtpendulum')));

load_system(docpath(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','slrtpendulum_100Hz')));

load_system(docpath(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','slrtpendulum_200Hz')));

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 slrtpendulum.

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

  4. Deploy the real-time application to the target computer.

In the MATLAB Command Window, type:

set_param('slrtpendulum', 'RTWVerbose', 'off');
tg = slrt('TargetPC1');
rtwbuild('slrtpendulum');
load(tg,'slrtpendulum');
### Starting Simulink Real-Time build procedure for model: slrtpendulum_100Hz

### Starting Simulink Real-Time build procedure for model: slrtpendulum_200Hz

### Starting Simulink Real-Time build procedure for model: slrtpendulum
### Generated code for 'slrtpendulum' is up to date because no structural, parameter or code replacement library changes were found.
### Successful completion of build procedure for model: slrtpendulum
### Created MLDATX ..\slrtpendulum.mldatx

Run App Designer Instrument Panel App

The App Designer instrument panel app Pendulum provides controls to start and interact with the real-time application slrtpendulum.

1. Run the app. To start the App Designer app Pendulum.mlapp and create the handle app, in the MATLAB Command Window, type:

app = Pendulum;
Acquire Group  1
  DiscreteInterval = 0.010
  SampleTimeString = 0.01
  HistoryFlag      = 1
  signals:
    slrtpendulum/Reference1:1
    cartposition1
    pendposition1
Acquire Group  2
  DiscreteInterval = 0.005
  SampleTimeString = 0.005
  HistoryFlag      = 1
  signals:
    slrtpendulum/Reference2:1
    cartposition2
    pendposition2

As the app starts, it displays the output of the view AcquiredList command. This view displays the signal hierarchy in the instrumentation object in the app.

2. To disrupt the equilibrium of the pendulum on each cart, click the Nudge buttons . You can adjust the nudge size by using the value selection next to each button.

3. Observe the plot reactions to each nudge. When the nudge value gets too large, the pedulum cannot recover its equilibrium.

App Callback Code

The instrument panel app functionality is provided by callback code. For more information about adding app components and inserting callback code, see Add App Designer Instrument Panel App to Tank Model.

Comments in the callback code in the instrument panel app Pendulum.mlapp describe the callback operations and programming suggestions. To view the callback code, open Pendulum.mlapp in the App Designer, and then click the Code View tab. In the Command Window, type:

edit Pendulum

Specify Block Paths for Signals in Referenced Models

To stream data from signals in referenced models, the connectLine and addSignal functions for the instrumentation object use a cell array to pass the block path.

For examples, see the setupInstrumentation(app) function in the app.

updatePlotAxes Function

This function is a callback with three arguments.

The function uses eventData.ExecTime to get the current time on the target computer and use that time to wrap the XLims of the PlotAxes.

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

Often, models have multiple sample rates.

To update plot data and plot animation, the instrumentation object groups data by AcquireGroups. For the slrtpendulum example, the two AcquireGroups are at different sample rates.

In the callback code, this processing is visible as AcquireGroupData signal groups in the updateAnimationCallback function. The app displays these groups in the instrumentation object by using the view command.

app.hInst.AcquireList.view

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