Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

The Background: About Me

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

About me:

You can get my details from my linkedin page. (Nitin Skandan)


I would be happy to provide any little help you might need.

The background
The acceptance of model based design across the embedded community has led to more and more systems being
developed in Simulink. The complexities of simulink models are increasing with more and more features being
incorporated to controllers. To analyse the outputs of simulink models we generally rely on scope and display
blocks. But, when we work with large models it is no longer possible to understand what is happening in the
system with scope and display block alone. It would have been better if you have a customizable GUI which
consists of a panel of buttons, checkboxes etc which feeds all the necessary data required to the model and there
is a similar GUI which displays the output of the model use plots, fields etc. This assists you to test and analyse
the model more efficiently and in turn provides a facility to do extensive functional testing as you do in
ControlDesk in a HIL rig. As you are already aware simulink/matlab provide features to make this possible
namely GUIDE & S-function. What many people have difficulty is how to link both. This is what I want to
address here.

Through a simple exercise I would try to explain how a GUI could be made and how it can be linked to
SIMULINK model.

Tutorial is written assuming that you have basic idea of GUIDE, S-function etc.

AIM
To make 2 GUI's
1. Feeds information to simulink model
2. Collects information from simulink model and displays it

Components
The following are the components and their description

No Name Description
1. InpGUI.fig Fig file generated from GUIDE for Input GUI

2. InpGUI.m M file generated from GUIDE for Input GUI

3. Inpgui_sf.m M file S function which collects information from Input GUI and outputs to
mode
4. Ingui_test.mdl Test model with GUI, S function integrated

5. OpGUI.fig Fig file generated from GUIDE for Output GUI

6. OpGUI.m M file generated from GUIDE for Output GUI

7. Opgui_sf.m M file S function which collects information from model and outputs to
Output GUI
8. Opgui_test.mdl Test model with GUI, S function integrated
Step by step process
1. Creating the GUI
This is simple and essentially drag and drop work using GUIDE.

Input GUI

This is what I made

The following were done with property manager


1. All components were given specific tags for easy identification
2. Button Enable is to Inactive. This is done to create different button behaviour than the default one.
The default behaviour is that when button is pressed released once a value is set. But usually in
embedded applications we need push button whose value should be set when pressed and is reset when
released. So we will be doing some additional things to get this behaviour.
3. Button ButtonDownFcn is set to inpgui_sf([],[],[],'pushBtnDwn'), this will cause GUI to call the
inpgui s-function when button is pushed down.

Output GUI
-All components were given specific tags for easy identification

2. How data flow takes place?


Before I go to next step its important to give an overview of how data flow takes place i.e from GUI to simulink
model and vice versa.

Some information
In each Figure there is a property called "Userdata" where you can store any information. The
information stored here is retained even if figure is closed.
In each simulink block ObjectParameters" there is a field call "Userdata" where you can store any
information. The information stored here is retained even when model is closed.
Figure has a handle hObject by means of which all properties of the figure can be accessed
Each block has a handle by means of which all block properties could be accessed

GUI data flow

Model

Inp GUI Figure


Block sets fig properties S function Block
based inputs from model

From model To model


UserData
Block access user data
and passes to model

hObject Handle of fig is stored UserData


in block
The handle of figure is stored in the 'userdata' of s-function block.
S-function can access the data that is stored in the GUI and pass to model i.e. from Input GUI to model.
Using the handle of figure the block can set and change the properties of GUI such as change the plots,
set the values of edit field and so on i.e, from model to Output GUI.

3. Updating GUI M file


Input GUI (inpgui.m)
The GUI needs to be modified to facilitate data flow as described in figure above and also data update based on
user inputs in the GUI screen. To understand the modifications it is divided into various parts.

Part 1- This section of code is inserted in figure create function


Creates sets initial values for the GUI fields
Stores the handle of GUI to the s-function block which invokes the GUI
Part 2 This section of code is inserted in edit field callback and edit field create function
Code to show the default values in edit field when GUI opens
Code to read the new values entered into the edit field and store it in UserData for access from S-
function
Part 3 This section of code is inserted in radio button callback and radio button create function
Code to show the default setting of radio button when GUI opens
Code to read the new setting for radio button and store it in UserData for access from S-function
Part 4 This section of code is inserted in check box callback and check box create function
Code to show the default setting of check box when GUI opens
Code to read the new setting of check box and store it in UserData for access from S-function

Output GUI (opgui.m)


Part 1- This section of code is inserted in figure create function
Stores the handle of GUI to the s-function block which invokes the GUI

4. M file s function
Input GUI S function (inpgui_sf.m)
The S-function read the GUI and generates outputs that goes to SIMULINK.
The important functions that you can find inside the s-function, what it does and what modification is to be done
inside them to integrate with GUI are described in sections below.

Switch flag switch case structure:-


When block executes the s-function the simulink block execution framework calls the s-functions with
arguments (t,x,u,flag,Ts).
t- simulation time
x state
u block inputs
flag which operation model is doing
Ts sampling time
Based on the operation the model is doing the appropriate function will be called by the switch case structure.

The button processing operation should be done here as follows.


When button is pressed update the GUI settings data structure; specify the button release function (this will in
turn cause GUI to call this function when button is released), commands to change color of button to indicate
that button is pressed.
When button is released - update the GUI settings data structure stored in figure; commands to change color of
button to indicate that button is released
MdlInitialize function
- Specify the number of outputs the s- function has (number of outputs from GUI to Simulink)
- All other settings can be usually kept at default values
- Call the InpGUI figure (this cause the figure M-script to be executed, which will in turn cause GUI to
appear)

MdlUpdate function
No change needed

Mdloutputs function
Here the current GUI settings is read from figure and S-function block outputs are updated accordingly
The GUI settings data structure element value are written to S-function block output ports.

Output GUI S function (opgui_sf.m)


The s-function reads the inputs from Simulink and updates the GUI figure.

Switch flag switch case structure:-


None

MdlInitialize function
- Specify the number of inputs the s- function has (number of inputs from Simulink to GUI)
- All other settings can be usually kept at default values
- Call the OpGUI figure (this cause the figure M-script to be executed, which will in turn cause GUI to
appear)

MdlUpdate function
The following operations are to be done here
- get the figure handle which is stored in blocks 'UserData'
- Search and find the handles of various objects in the figure
- Update the GUI objects based on S-function block inputs using the figure handle

Mdloutputs function
No change here as there are no outputs.

The models are now ready to run.

The input model (inpguitest.mdl) will read InpGUI and pass it to SIMULINK
The output model (opguitest.mdl) will read from SIMULINK and pass it to OpGUI.

You might also like