execute and unit test stateflow chart objects -凯发k8网页登录

main content

execute and unit test stateflow chart objects

a standalone stateflow® chart is a matlab® class that defines the behavior of a finite state machine. standalone charts implement classic chart semantics with matlab as the action language. you can program the chart by using the full functionality of matlab, including those functions that are restricted for code generation in simulink®. for more information, see create stateflow charts for execution as matlab objects.

example of a standalone stateflow chart

the file sf_chart.sfx contains the standalone stateflow chart sf_chart. the chart has local data u, x, and y.

this example shows how to execute this chart from the stateflow editor and in the matlab command window.

execute a standalone chart from the stateflow editor

to unit test and debug a standalone chart, you can execute the chart directly from the stateflow editor. during execution, you enter data values and broadcast events from the user interface.

  1. open the chart in the stateflow editor:

    edit sf_chart.sfx
  2. in the symbols pane, enter a value of u = 1 and click run . the chart executes its default transition and:

    • initializes x to the value of 0.

    • makes state a the active state.

    • assigns y to the value of 1.

    • increases the value of x to 1.

    the chart animation highlights the active state a. the symbols pane displays the values u = 1, x = 1, and y = 1. the chart maintains its current state and local data until the next execution command.

    chart animation showing that state a is active.

  3. click step . because the value of u does not satisfy the condition [u<0] to transition out of state a, this state remains active and the values of x and y increase to 2. the symbols pane now displays the values u = 1, x = 2, and y = 2.

    chart animation showing that state a is active.

  4. in the symbols pane, enter a value of u = −1 and click step . the negative data value triggers the transition to state b. the symbols pane displays the values u = −1, x = 1, and y = 3.

    chart animation showing that state b is active.

  5. you can modify the value of any chart data in the symbols pane. for example, enter a value of x = 3. the chart will use this data value in the next time execution step.

  6. enter a value of u = 2 and click step . the chart transitions back to state a. the symbols pane displays the values u = 2, x = 4, and y = 5.

    chart animation showing that state a is active.

  7. to stop the chart animation, click stop .

to interrupt the execution and step through each action in the chart, add breakpoints before you execute the chart. for more information, see debug a standalone stateflow chart.

execute a standalone chart in matlab

you can execute a standalone chart in matlab without opening the stateflow editor. if the chart is open, then the editor highlights active states and transitions through chart animation.

  1. open the chart in the stateflow editor. in the matlab command window, enter:

    edit sf_chart.sfx
  2. create the stateflow chart object by using the name of the sfx file for the standalone chart as a function. specify the initial value for the data u as a name-value pair.

    s = sf_chart(u=1)
    stateflow chart
       execution function
        y = step(s)
       local data
          u         : 1
          x         : 1
          y         : 1
       active states: {'a'}
    this command creates the chart object s, executes the default transition, and initializes the values of x and y. the stateflow editor animates the chart and highlights the active state a.

    chart animation showing that state a is active.

  3. to execute the chart, call the step function. for example, suppose that you call the step function with a value of u = 1:

    step(s,u=1)
    disp(s)
    stateflow chart
       execution function
        y = step(s)
       local data
          u         : 1
          x         : 2
          y         : 2
       active states: {'a'}
    because the value of u does not satisfy the condition [u<0] to transition out of state a, this state remains active and the values of x and y increase to 2.

    chart animation showing that state a is active.

  4. execute the chart again, this time with a value of u = −1:

    step(s,u=-1)
    disp(s)
    stateflow chart
       execution function
        y = step(s)
       local data
          u         : -1
          x         : 1
          y         : 3
       active states: {'b'}
    the negative data value triggers the transition to state b. the value of x decreases to 1 and the value of y increases to 3.

    chart animation showing that state b is active.

  5. to access the value of any chart data, use dot notation. for example, you can assign a value of 3 to the local data x by entering:

    s.x = 3
    stateflow chart
       execution function
        y = step(s)
       local data
          u         : -1
          x         : 3
          y         : 3
       active states: {'b'}
    the standalone chart will use this data value in the next time execution step.
  6. execute the chart with a value of u = 2:

    step(s,u=2)
    disp(s)
    stateflow chart
       execution function
        y = step(s)
       local data
          u         : 2
          x         : 4
          y         : 5
       active states: {'a'}
    the chart transitions back to state a and modifies the values of x and y.

    chart animation showing that state a is active.

  7. to stop the chart animation, delete the stateflow chart object s:

    delete(s)

execute multiple chart objects

you can execute multiple chart objects defined by the same standalone chart. concurrent chart objects maintain their internal state independently, but remain associated to the same chart in the editor. the chart animation reflects the state of the chart object most recently executed. executing multiple chart objects while the stateflow editor is open can produce confusing results and is not recommended.

see also

| |

related topics

网站地图