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 modelslrt_ex_pendulum_100Hz. The instrument panel contains these App Designer components:

  • Target selector dropdown list — 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.

  • Status message box — To display target computer status information.

  • 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 cart button — To apply input (nudge) to the cart that hold the pendulum.

  • Reference position knob — 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.

Open Example and Load Model

openExample('SlrtAddAppDesignerAppToInvertedPendulumModelExample');
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 modelslrt_ex_pendulum_100Hz.

  3. Connect the development computer to the target computer. Build theslrt_ex_pendulum_100Hzmodel.

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

In the MATLAB Command Window, type:

model ='slrt_ex_pendulum_100Hz'; set_param(model,'RTWVerbose','off'); tg = slrealtime; evalc('slbuild(model)'); load(tg,model);

Run App Designer Instrument Panel App

The App Designer instrument panel appslrt_ex_pendulumAppprovides controls to start and interact with the real-time applicationslrt_ex_pendulum_100Hz.

1.Run the app. To start the App Designer appslrt_ex_pendulumApp.mlappand create the handleapp, in the MATLAB Command Window, type:

app = slrt_ex_pendulumApp;

2.To connect with an available target computer, click theconnectbutton. The text on the button will switch to 'disconnect' and theloadbutton will be enabled.

3.To load the application to the target computer, click theloadbutton. After the application is loaded on the target computer, thestartbutton andstop timeedit field will be enabled.

4.To set the stop time of the application, type your preferred stop time in the edit field and hitenterbutton.

5.To start running the application, click thestartbutton.

6.To disrupt the equilibrium of the pendulum on each cart, click theNudgebutton. 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 appslrt_ex_pendulumApp.mlappdescribe the callback operations and programming suggestions. To view the callback code, openslrt_ex_pendulumApp.mlappin the App Designer, and then click theCode Viewtab. 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 ofconnectLinefunctions in thesetupInstrumentation(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.

信号被放置在获得团体基于桑普le rate and decimation such that all signals in an Acquire Group have the same time vector.

Update Axes and Animation by Using Acquire Groups

In the callback code, this processing is visible asAcquireGroupDatasignal groups in theupdateAnimationCallbackfunction.

Close the App and Models

The instrument panel app handleappprovides 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');