User Interfaces for Simulink Real-Time
You can design apps with App Designer to tune parameters and monitor signals from real-time applications that are running on a Speedgoat target computer. See how Instrument Objects can simplify gathering data. You can automatically retrieve data from the target computer and display that data in an app. Learn about the commands and how to use them. An example model and demonstration are shown.
2020b was a major release for Simulink Real-Time. For more general information, see the released video for Simulink Real-Time in 2020b. This video is more specifically about user interfaces for Simulink Real-Time. We're going to take a look at App Designer integration using what we call instrumentation objects.
So App Designers the recommended environment for building apps in MATLAB. Therefore, App Designer is also the recommended environment for building apps or user interfaces in MATLAB. On the left we show a typical, or at least an example app. In the middle here we show a picture representing the development computer, which is running that app, and on the right, a spigot target computer, which is running your model in real time connected to hardware. Whereas the development computer and target computer are connected via ethernet, which is how data is transferred between the two.
So if you have an app with say some numerical displays and some line plots, you have to periodically get that data from the spigot and update the app. That's what instrumentation objects are for. They take care of all that for you. Once you create the object, it periodically gets the data from the spigot and updates the line plot. No need for timer functions or anything else, makes it really simple and easy.
Some of the key commands that are used are slrealtime.Instrument to create an instrument object, connectCallback to have your own function, which is called every time there's new data, connectLine to take a signal from the spigot and display it on a line plot, or connectScalar to take the current value of a signal, and put it in something like a numerical display or a gauge for example.
Let's look at an example of this. So here we have a model of a closed-loop control system running a PID controller on a DC motor. You can see the physical DC motor here in this video, along connected to the spigot target computer, which-- will be running this model in real time. You can see this model has a constant function, which creates the commanded position, the signal called command, and the-- feedback signal of the motor is named feedback.
So let's go ahead and run this model, where we've connected both the commanded signal and the feedback signal to this axis. And as we use the slider to change the position, we can see the motor move in the video, as well as seeing the data update in the plot. So how do we create this app? Well, we used App Designer, and the way we did it, this is a simple app, so it's just a few components that we grabbed. You can see there's lots of choices here of components. We simply grabbed an axes, a button, an axes and a button, as well as a slider. And so if we go to the code view and take a look at this, all the code here in gray was automatically generated for us. So there's not much code that's needed to get this to work. So in the setup instrumentation function here, we used slrealtime.Instrument to create our instrumentation object, and we used a connect line to connect that command signal, as well as the feedback signal to the user interface axes in our app.
Other than that, we have a startup function, which creates a Simulink real-time object, stops the spigot target computer if it's running, and loads or model. And we have a function here for the slider, and every time the value changes, we simply use the setparam command to change the value of the commanded position by changing that constant block to the value. Now you might notice here that the instrumentation object is not needed for the slider, and that's because we only need to change a single value or a parameter on the spigot target computer when the slider is changed. So there's an event that causes that to happen, whereas on the plots we want that update continuously as it's running.
And the only other code is for our Start/Stop button, since we use one button for both. When we stop the model, we change the button to say start, and when we start the model, we change the button to say stop, and that's about it. So let's see this running in action one more time. I restart this, and bring in the video and the model back into view, change these positions. So that's it. Nice and simple. Makes it easy to retrieve data from the target computer automatically.
Some really cool time instrument, here are some of the key commands you might want to look up in the documentation. That's a real-time instrument for creating instrument, connect callback for retrieving or creating your own callback whenever a new data is retrieved, connect line for continuously updating something like a line plot, and then connect scalar for any type of display like a gauge, numeric display that has only a current value display, getting the current value and automatically updating that display.
For more information or to find an example, you can search for "Add App Designer App to Inverted Pendulum Model" inside the documentation, and then it'll bring you to an example, or you can look at the code and how it was done in that example. You can also get there sort of searching by going to Simulink Real-Time section under Control and Instrumentation. Go to the Real-Time Application Instruments with Instrument Objects, and you'll see the example there as well as the reference for all the commands. This example is also linked below. Thank you very much.
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.