Documentation Center

  • Trial Software
  • Product Updates

Implementing the Interface with Simulink

Build It Yourself or Use the Supplied Model

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

Design Considerations for Defining the Interface

The following sections describe the rationale for the input and output of the Stateflow® chart.

Inputs Required from Simulink Model

Type of Input.  Temperature of the physical plant

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.

Properties of Input.  The properties of the temperature input are as follows:

PropertyValue
Nametemp
ScopeInput
SizeInherit from Simulink® input signal for compatibility
Data typeInherit from Simulink input signal for compatibility
Port1
Watch in debuggerEnable

Outputs Required from Stateflow Chart

Type of Output.  Speed of airflow, based on how many fans are operating

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.

Properties of Output.  The properties of the airflow output are as follows:

PropertyValue
Nameairflow
ScopeOutput
Data type8-bit unsigned integer (uint8)

(The values can be only 0, 1, or 2.)

Port1
Watch in debuggerEnable

Adding a Stateflow Block to a Simulink Model

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:

  1. 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.

  2. Delete the nonfunctional blocks and their connectors.

      Tip   Hold down the Shift key to select multiple objects, and then press Delete.

    Your model should now look like this:

  3. Save the model as Stage1Interface:

    1. Create a new local folder for storing your working model.

    2. In the Simulink model window, select File > Save As.

    3. Navigate to the new folder.

    4. Enter Stage1Interface as the file name.

    5. Leave the default type as Simulink Models.

    6. Click Save.

  4. On the toolbar of the Simulink model, click the Library Browser icon:

    The Simulink Library Browser opens on your desktop:

  5. Add the Stateflow Chart block to the Simulink model:

    1. In the left scroll pane of the Library Browser, select Stateflow.

    2. Drag the first block, called Chart, into your model.

    The model should now look like this:

  6. Click the label Chart under the Stateflow block and rename it Air Controller.

  7. Change the action language of the chart to C:

    1. Double-click the block to open the chart.

    2. Right click in an empty area of the chart and select Properties.

    3. From the Action Language box, select C.

    4. Select OK.

 Shortcut for adding a Stateflow block to a new Simulink model

Defining the Inputs and Outputs

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:

  1. Double-click the Air Controller block in the Simulink model Stage1Interface to open the Stateflow chart.

    The Stateflow Editor opens on your desktop:

  2. Add a data element to hold the value of the temperature input from the Simulink model:

    1. 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.

    2. In the Name field, change the name of the data element to temp.

    3. Leave the following fields at their default values in the General tab because they meet the design requirements:

      FieldDefault ValueWhat It Means
      ScopeInputInput from Simulink model. The data element gets its value from the Simulink signal on the same input port.
      Size-1The data element inherits its size from the Simulink signal on the same port.
      ComplexityOffThe data element does not contain any complex values.
      TypeInherit: Same as SimulinkThe data element inherits its data type from the Simulink signal on the same output port.

        Note   Ports are assigned to inputs and outputs in the order they are created. Because temp is the first input you created, it is assigned to input port 1.

    4. In the General tab, select the Watch in debugger check box.

      Enabling Watch in debugger lets you examine the value of temp during breakpoints in simulation. You will try this in Setting Simulation Parameters and Breakpoints.

    5. Click OK to apply the changes and close the dialog box.

  3. Add a data element to hold the value of the airflow output from the Air Controller chart:

    1. 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:

        Note:   Because airflow is the first output you created, it is assigned to output port 1.

    2. In the Name field of the Data properties dialog box, change the name of the data element to airflow.

    3. In the Type field, select uint8 (8-bit unsigned integer) from the submenu.

    4. 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.

    5. Make the following changes for other properties in the General tab:

      PropertyWhat to Specify
      Limit rangeEnter 0 for Minimum and 2 for Maximum.
      Watch in debuggerSelect the check box to enable this option.

    6. Click OK to apply the changes and close the dialog box.

  4. 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   You might need to enlarge the Air Controller block to see the input and output clearly. To change the size of the block:

      1. Select the block and move your pointer over one of the corners until it changes to this shape:

      2. Hold down the left mouse button and drag the block to the desired size.

  5. Save Stage1Interface.

    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.

Connecting the Stateflow Block to the Simulink Subsystem

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:

  1. In the model Stage1Interface, connect the output airflow from Air Controller to the corresponding input in Physical Plant:

    1. 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.

    2. 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.

    3. Release the mouse.

      The connection should look something like this:

      Tip   You can use a shortcut for automatically connecting blocks. Select the source block, and then hold down the Ctrl key and left-click the destination block.

  2. 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:

    1. Place your pointer on the line where you want the branch line to start.

    2. While holding down the Ctrl key, press and hold down the left mouse button.

    3. Drag your pointer to the input port for temp on the left side of the Air Controller block.

    4. Release the mouse button and the Ctrl key.

    5. 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.

  3. Save Stage1Interface.

Where to go next.  Now you are ready to model the operating modes with states. See Implementing the States to Represent Operating Modes.

Was this topic helpful?