How To Use Switch In App Designer Matlab
Introduction
I recently started to play around with an idea for a model using Stateflow®. This model would be controlled through an interactive user interface, for which I could use MATLAB's App Designer.
Unfortunately this was another case of me trying to reverse engineer a MATLAB example to figure out how to do this. In this article I will try to present the minimum setup needed to do this using MATLAB R2021b step by step.
The basic pattern
Basically I want to be able to interact with a Stateflow model (used to build state machines) using a graphical interface. I'd like to influence state transitions by selecting a value from a drop down list.
Explanation
The graphical interface can be built with App Designer, but a mechanism to send messages to the state machine model must exist.
To enable this, a Start-up function is added to the App that will have a parameter with a reference to the Stateflow model. Upon initialization of the simulation, the Stateflow will instantiate the app and keep a reference to it as a local variable. Doing so will enable the two-ways communication between the state machine and the graphical interface.
What now follows is a detailed description of building this from scratch. There are some setup steps that are important and unless performed the model won't work as desired.
Building the Stateflow model
The model built for this example is a simplified version of a traffic light system. It will consist of a single lamp that, starting lit up in red colour, will transition to green, amber and back to red and so on. There is also the possibility to select a different mode that will have the amber light flashing.
Therefore, the model will be a hierarchical one that presents two outer states ("On" and "Off"). When "On", it transitions as explained above. When "Off" it will make the amber light to blink. I will control such transitions from a drop down list.
First thing to notice here is that Stateflow charts can use either MATLAB or C as the action language to implement state transitions or/and actions.
By default a chart created in Simulink will use MATLAB, we want to use C here. I explain why further down below. For now, let's change this from MATLAB to C. This can be done from the Model Explorer. Open the Model Explore and select the chart, on the right-hand side under the General tab, select C in the Action Language menu:
Inside the model, notice how the app is instantiated immediately after the entry event on the outer state.
app = ml.simpleModelApp(this);
The namespace "ml" can be used to call MATLAB functions or access variables. To be able to use it, the state chart action language must be set to C.
I did have some issues with resolving the variable app in Stateflow. For me it would automatically set the variable size to -1 (inherit). This will result in an error when trying to run the simulation as MATLAB cannot compute the variable size. To overcome this, set its size to 1 from the Model Explorer window.
This was all the basic setup needed on the Simulink side, let's take a look at the App side.
Building the App
After placing the drop-down UI control with the desired values I turn to setting up an input parameter for the App. Remember that this parameter will contain a reference to the state chart.
So the first step is to create a property where to store the reference inside the app. In the Code Browser left-hand side pane there's a tab called Properties, click on the green cross to add a property. I will call it chart.
Secondly I proceed to set up the input argument. Select the root node with the app name under the Component Browser pane on the right-hand side. Then populate the input arguments with a name (we are specifying only one input in this case).
Now I can create the startup function, and because I set the input argument first, the App Designer will automatically generate the function for us. To create the function select the "Callbacks" tab (you can see it in the image just above). Now enter a name in the StartupFcn field.
This will create a shell startup function, it is in here that I populate it as follows:
The call to assignin is optional. It is a handy tip to know that we can store it in the base workspace so it can be accessed elsewhere in the model. Particularly to close the App once the simulation ends.
In the Stateflow model there was a local variable called mode, initialized to "On" and used to determine the transitions between the "On" and "Off" states. All that is left to do is to add a callback function to the drop-down value change event so mode can also change accordingly.
And that finishes setting up both the model and the app. A quick run of the simulation below show the results.
Simulation
Conclusion
This article focused on describing, step by step, how to build a Stateflow model which can be interacted with from an external graphical interface made with App Designer. It wasn't my intention to show in detail how the actual models were built but to explain how the connection mechanism between state machine model and App is established. In essence, this is achieved by:
- Creating an App that allows a parameter to be pass down during startup-up that will contain a reference to the state machine. Then storing away the reference inside a member variable in the app so charts variables can be accessed from it.
- Creating a Stateflow chart using C as Action Language. The chart implements a wrapper state used to instantiate the app, at which point we pass the reference to the state machine.
References
I included the main references throughout the article. Nonetheless I think it is important to stress out that the original model that inspire me to do this and which I reversed engineer is this one from the MATLAB Stateflow series on Finite States Machine modeling.
How To Use Switch In App Designer Matlab
Source: https://www.linkedin.com/pulse/how-build-interactive-model-stateflow-matlab-app-daniel-hern%C3%A1ndez
Posted by: mcleandentelf1969.blogspot.com
0 Response to "How To Use Switch In App Designer Matlab"
Post a Comment