|On this page…|
To implement the interface yourself, work through the exercises in this section. Otherwise, open the supplied model by entering this command at the MATLAB® prompt:
addpath(fullfile(docroot, 'toolbox', 'stateflow', 'gs', 'examples')) Stage1Interface
The following sections describe the rationale for the input and output of the Stateflow® chart.
Rationale. The purpose of the chart is to control the air temperature in a physical plant. The goal is to maintain an ideal temperature of 120 degrees by activating one or two cooling fans if necessary. The chart must check the plant temperature over time to determine the amount of cooling required.
|Size||Inherit from Simulink® input signal for compatibility|
|Data type||Inherit from Simulink input signal for compatibility|
Rationale. When the Simulink subsystem determines the temperature of the physical plant over time, it needs to account for the speed of the airflow. Airflow speed is directly related to the amount of cooling activity generated by the fans. As more fans are activated, cooling activity increases and air flows faster. To convey this information, the Stateflow chart outputs a value that indicates whether 0, 1, or 2 fans are running. The Simulink subsystem uses this value as an index into a multiport switch, which outputs a cooling activity value, as described in A Look at the Physical Plant.
|Data type||8-bit unsigned integer (uint8)|
(The values can be only 0, 1, or 2.)
To begin building your Stateflow chart, you will add a Stateflow block to a partially built Simulink model called sf_aircontrol_exercise, which contains the Physical Plant subsystem, described in A Look at the Physical Plant.
To add a Stateflow block to an existing Simulink model:
Open the Simulink model by typing sf_aircontrol_exercise at the MATLAB command prompt.
The model opens on your desktop:
The model is incomplete because it does not include the Stateflow chart that you will build as you work through the exercises in this guide. Instead, the model contains several nonfunctional blocks: the Terminator, Inport, and Annotation blocks.
Delete the nonfunctional blocks and their connectors.
Your model should now look like this:
Save the model as Stage1Interface:
Create a new local folder for storing your working model.
In the Simulink model window, select File > Save As.
Navigate to the new folder.
Enter Stage1Interface as the file name.
Leave the default type as Simulink Models.
On the toolbar of the Simulink model, click the Library Browser icon:
The Simulink Library Browser opens on your desktop:
Add the Stateflow Chart block to the Simulink model:
In the left scroll pane of the Library Browser, select Stateflow.
Drag the first block, called Chart, into your model.
The model should now look like this:
Click the label Chart under the Stateflow block and rename it Air Controller.
Change the action language of the chart to C:
Double-click the block to open the chart.
Right click in an empty area of the chart and select Properties.
From the Action Language box, select C.
Inputs and outputs are data elements in a Stateflow chart that interact with the parent Simulink model. To define inputs and outputs for your chart, follow these steps:
Double-click the Air Controller block in the Simulink model Stage1Interface to open the Stateflow chart.
The Stateflow Editor opens on your desktop:
Add a data element to hold the value of the temperature input from the Simulink model:
In the editor menu, select Chart > Add Inputs & Outputs > Data Input From Simulink.
The Data properties dialog box opens on your desktop with the General tab selected:
The default values in the dialog box depend on the scope — in this case, a data input.
In the Name field, change the name of the data element to temp.
Leave the following fields at their default values in the General tab because they meet the design requirements:
|Field||Default Value||What It Means|
|Scope||Input||Input from Simulink model. The data element gets its value from the Simulink signal on the same input port.|
|Size||-1||The data element inherits its size from the Simulink signal on the same port.|
|Complexity||Off||The data element does not contain any complex values.|
|Type||Inherit: Same as Simulink||The data element inherits its data type from the Simulink signal on the same output port.|
In the General tab, select Add to watch window.
The Stateflow Breakpoints and Watch Data window lets you examine the value of temp during breakpoints in simulation. You will try this in Setting Simulation Parameters and Breakpoints.
Click OK to apply the changes and close the dialog box.
Add a data element to hold the value of the airflow output from the Air Controller chart:
In the editor menu, select Chart > Add Inputs & Outputs > Data Output To Simulink.
The Data properties dialog box opens on your desktop, this time with different default values, associated with the scope Output:
In the Name field of the Data properties dialog box, change the name of the data element to airflow.
In the Type field, select uint8 (8-bit unsigned integer) from the submenu.
Look at the Initial value field.
The initial value is a blank expression, which indicates a default value of zero, based on the data type. This value is consistent with the model design, which specifies that no fans are running when the chart wakes up for the first time.
Make the following changes for other properties in the General tab:
|Property||What to Specify|
|Limit range||Enter 0 for Minimum and 2 for Maximum.|
|Add to watch window||Select this to add airflow to the Watch Data tab of the Stateflow Breakpoints and Watch Data window.|
Click OK to apply the changes and close the dialog box.
Go back to the Simulink model by clicking the up-arrow button in the Stateflow Editor toolbar.
Notice that the input temp and output airflow have been added to the Stateflow block:
Tip There are several ways to add data objects to Stateflow charts. You used the Stateflow Editor, which lets you add data elements to the Stateflow chart that is open and has focus. However, to add data objects not just to a chart, but anywhere in the Stateflow design hierarchy, you can use a tool called the Model Explorer. This tool also lets you view and modify the data objects you have already added to a chart. For more information, see Stateflow Hierarchy of Objects and How to Add Data Using the Model Explorer in the Stateflow User's Guide. You can also add data objects programmatically using the Stateflow API, as described in Create Stateflow Objects in the Stateflow API Guide.
Now that you have defined the inputs and outputs for the Stateflow Air Controller block, you need to connect them to the corresponding signals of the Simulink Physical Plant subsystem. Follow these steps:
In the model Stage1Interface, connect the output airflow from Air Controller to the corresponding input in Physical Plant:
Place your pointer over the output port for airflow on the right side of the Air Controller block.
The pointer changes in shape to crosshairs.
Hold down the left mouse button and move the pointer to the input port for airflow on the left side of the Physical Plant block.
Release the mouse.
The connection should look something like this:
Connect the output temp from the Physical Plant to the corresponding input in Air Controller by drawing a branch line from the line that connects temp to the Scope:
Place your pointer on the line where you want the branch line to start.
While holding down the Ctrl key, press and hold down the left mouse button.
Drag your pointer to the input port for temp on the left side of the Air Controller block.
Release the mouse button and the Ctrl key.
Reposition the connection so that it looks like this:
Tip To reposition connections, move your cursor over the end of the line. When the cursor changes to a circle, select the end of the line with the left mouse button and drag the line to a new location. For more information, see Connect Blocks.
Where to go next. Now you are ready to model the operating modes with states. See Implementing the States to Represent Operating Modes.