Building a Simulink Model with a Stateflow Truth Table

Methods for Adding Truth Tables to Simulink Models

There are several ways to add a Stateflow truth table to a Simulink model:

ProcedureAction Languages SupportedHow To Do It
Add a Truth Table block directly to the modelEmbedded MATLAB only See Truth Table.
Add a Stateflow block that calls a truth table functionStateflow Classic and Embedded MATLABSee Adding a Stateflow Block that Calls a Truth Table Function.

Adding a Stateflow Block that Calls a Truth Table Function

This section describes how to add a Stateflow block to your Simulink model, and then create a chart that calls a truth table function. These topics are covered:

Once you build a model in this section, finish it by programming the truth table with its behavior in Programming a Truth Table.

Creating a Simulink Model

To execute a truth table, you first need a Simulink model that calls a Stateflow block. Later, you will create a Stateflow chart for the Stateflow block that calls a truth table function. In this section, you create a Simulink model that calls a Stateflow block with the following procedure:

  1. At the MATLAB prompt, enter the following command:

    sfnew
    

    An untitled Simulink model with a Stateflow block appears.

  2. Click and drag the Stateflow block to the center of the Simulink window.

    This action makes room for the blocks you add in the steps that follow.

  3. In the Simulink window, select View > Library Browser.

    The Simulink Library Browser window opens with the Simulink node expanded.

  4. Under the Simulink node, select the Sources library.

    The right pane of the Simulink Library Browser window displays the block members of the Sources library.

  5. From the right pane of the Simulink Library Browser window, click and drag the Constant block to the left of the Stateflow block in the Simulink model.

  6. Add two more Constant blocks to the left of the Chart block, and add a Display block (from the Sinks library) to the right of the Chart block.

  7. In the Simulink model window, double-click the middle Constant block.

  8. In the resulting Block Parameters dialog box, change the Constant value field to 0.

  9. Click OK to close the dialog box.

  10. In the Simulink model window, double-click the bottom Constant block.

  11. In the resulting Block Parameters dialog box, change the Constant value field to 0.

  12. Click OK to close the dialog box.

    Your model should now have the following appearance.

  13. In the Simulink model window, from the Simulation menu, select Configuration Parameters.

    The Configuration Parameters dialog box opens.

  14. Set

  15. Click OK to accept these values and close the Configuration Parameters dialog box.

  16. Save the model as first_truth_table.mdl.

Creating a Stateflow Truth Table

You created a Simulink model in Creating a Simulink Model that contains a Stateflow block. Now you need to open the Stateflow chart for the block and specify a truth table for it in these steps:

  1. In the Simulink model, double-click the Stateflow block named Chart.

    An empty Stateflow Editor appears.

  2. In the Stateflow Editor, click the Truth Table drawing tool:

  3. Move your pointer into the empty chart area and notice that it appears in the shape of a box.

  4. Click to place a new truth table as shown.

    A shaded box appears with the title truthtable and a flashing text cursor in the middle of the box.

  5. Enter the signature label

    t = ttable(x,y,z)
    

    and click outside the truth table box.

    You must label a truth table function with its signature. Use the following syntax:

    [return_val1, return_val2,...] = function_name(arg1, arg2,...)
    

    You can specify multiple return values and multiple input arguments, as shown in the syntax. Each return value and input argument can be a scalar, vector, or matrix of values.

Specifying Properties of Truth Table Functions in Stateflow Charts

After you add a truth table function to a Stateflow chart, you can specify its properties by following these steps:

  1. Right-click the truth table function box.

  2. Select Properties from the context menu.

    The Truth Table properties dialog box for the truth table function appears.

The fields in the Truth Table properties dialog box are as follows:

Field

Description

Name

Function name; read-only; click this hypertext link to bring the truth table function to the foreground in its native Stateflow chart.

Breakpoints

Select Function Call to set a breakpoint to pause execution during simulation when the truth table function is called.

Function Inline Option

This option controls the inlining of the truth table function in generated code through the following selections:

  • Auto
    Decides whether or not to inline the truth table function based on an internal calculation.

  • Inline
    Inlines the truth table function as long as it is not exported to other charts and is not part of a recursion. A recursion exists if the function calls itself either directly or indirectly through another called function.

  • Function
    Does not inline the function.

Label

You can specify the signature label for the function through this field. See Creating a Stateflow Truth Table for more information.

Description

Textual description/comment.

Document Link

Enter a URL address or a general MATLAB command. Examples are www.mathworks.com, mailto:email_address, and edit/spec/data/speed.txt.

Calling a Truth Table in a Stateflow Action

In Creating a Stateflow Truth Table, you created the truth table function ttable with the signature

t = ttable(x,y,z)

Now you need to specify a call to the truth table function in the Stateflow chart. Later, when the chart executes during simulation, it calls the truth table.

You can call truth table functions from the actions of any state or transition. You can also call truth tables from other functions, including graphical functions and other truth tables. Also, if you export a truth table, you can call it from any Stateflow chart in the model.

Use these steps to call the ttable function from the default transition of its own Stateflow chart.

  1. Select the Default Transition button from the drawing toolbar.

  2. Move your pointer to a location left of the truth table function and notice that it appears in the shape of a downward-pointing arrow.

  3. Click to place a default transition into a terminating junction.

  4. Click the question mark character (?) that appears on the highlighted default transition.

    A blinking cursor in a text field appears for entering the label of the default transition.

  5. Enter the text

    {d = ttable(a,b,c);}
    

    and click outside the transition label to finish editing it.

    You might want to adjust the label's position by clicking and dragging it to a new location. The finished Stateflow chart should have the following appearance:

    The label on the default transition that you entered provides a condition action that calls the truth table with arguments and a return value. When the Simulink model triggers the Stateflow block during simulation, the default transition is taken and a call to the truth table ttable is made.

    The call to the truth table in Stateflow action language must match the truth table signature. This means that the type of the return value d must match the type of the signature return value t, and the type of the arguments a, b, and c must match the type of the signature arguments x, y, and z. You ensure this with a later step in this section when you create the data that you use in the Stateflow chart.

  6. From the File menu, select Save to save the model.

Creating Truth Table Data in Stateflow Charts and Simulink Models

When you create a truth table with its own signature, you specify data for it in the form of a return value (t) and argument values (x, y, z). When you specify a call to a truth table, as you did in Calling a Truth Table in a Stateflow Action, you specify data that you pass to the return and argument values of the truth table (d, a, b, and c). Now you must define this data for the Stateflow chart in these steps:

  1. Double-click the truth table to open the Truth Table Editor.

  2. In the Truth Table Editor, select the Edit Data/Ports button .

    The Model Explorer appears.

    In the Model Hierarchy pane, the node for the function ttable is highlighted, and the Contents pane displays the output (t) and inputs (x, y, z) for ttable. By default, these data are defined as scalars of type double. If you want to redefine these data with a different array size and type, you do it in the Model Explorer. However, no changes are necessary for this example.

    Notice also in the Model Hierarchy pane that the node above the function ttable is Chart, the name of the Stateflow chart that contains the truth table ttable.

  3. In the Model Hierarchy pane, select Chart.

    Notice that Chart contains no data in the Contents pane. You need to add the return and argument data used in calling ttable.

  4. Select Add > Data.

    A scalar data is added to the chart in the Contents pane of the Model Explorer with the default name data. This data matches argument x in type and size.

  5. In the Contents pane, double-click the entry data in the Name column.

    A small text field opens with the name data highlighted.

  6. In the text field, change the name to a and click Enter.

  7. Click the entry Local under the Scope column.

    A drop-down menu of selectable scopes appears with Local selected.

  8. Select Input.

    The scope Input means that the Simulink model provides the value for this data, which it passes to the Stateflow chart through an input port on the Stateflow block.

    You should now see the new data input a in the Contents pane.

  9. Repeat steps 3 through 7 to add the data b and c with the scope Input, and data d with a scope of Output.

    The scope Output means that the Stateflow chart provides this data and passes it to the Simulink model through an output port on the Stateflow block.

    You should now see the following data in the Model Explorer.

    The data a, b, c, and d match their counterparts x, y, z, and t in the truth table signature in size (scalar) and type (double), but have sources outside the Stateflow block. Notice that input ports for a, b, and c, and an output port for d appear on the Stateflow block in the Simulink model.

  10. Complete connections to the Simulink blocks as shown.

  11. To save the model, select File > Save.

  


 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS