Main Content

Activate a Stateflow Chart by Sending Input Events

Simulink® models contain Stateflow® charts, but those charts are not necessarily active for the duration of model execution. One way for a chart to become active is by receiving an input event from Simulink. Once Stateflow charts become active, the chart executes its actions.

An input event occurs outside of a Stateflow chart. A Simulink model, Simulink function, or another Stateflow chart can generate input events.

Define an Input Event

To define an input event:

  1. Add an event to the Stateflow chart. For more information, see Define Events in a Chart.

  2. In the Property Inspector, set the Scope property for the event to Input from Simulink. A single trigger port appears at the top of the Stateflow block in the Simulink model.

An input event can activate a Stateflow chart through a change in a control signal or a function call from a Simulink block. The change in a control signal is also known as an edge trigger. You cannot use both edge-triggered and function-call input events in the same Stateflow chart. Using these input events together results in a compile-time error.

Activate a Stateflow Chart by Using Edge Triggers

An edge-triggered input event causes a Stateflow chart to execute during the current time step of simulation. With this type of input event, a change in a control signal acts as a trigger.

To specify a function-call input event, set the Trigger property to Function call.

Edge Trigger Type

Description

Rising

Rising edge trigger. Chart is activated when the control signal changes from either zero or a negative value to a positive value.

Falling

Falling edge trigger. Chart is activated when the control signal changes from a positive value to either zero or a negative value.

Either

Either rising or falling edge trigger. Chart is activated when the control signal crosses zero as it changes in either direction.

In all cases, the value of the control signal must be equal to or cross zero to be a valid edge trigger. For example, a signal that changes from -1 to 1 is a valid rising edge trigger. A signal that changes from 1 to 2 is not a valid rising edge trigger. A signal that changes from 0 to 2 is a valid rising edge trigger.

When to Use Edge-Triggered Input Events

Use an edge-triggered input event to activate a chart when your model requires regular or periodic chart execution. For example, in this model, an edge-triggered input event activates the Edge to Function chart at regular intervals.

sf_loop_scheduler_img.png

Behavior of Multiple Edge-Triggered Input Events

At every time step, Stateflow checks input events in ascending order based on their port numbers. The chart wakes up once for each valid event. For edge-triggered input events, multiple edges can occur in the same time step, waking the chart more than once in that time step.

Activate a Stateflow Chart by Using Function Calls

A function-call input event causes a Stateflow chart to execute during the current time step of simulation.

When to Use Function-Call Input Events

Use a function-call input event to activate a chart when your model requires access to output data from the chart in the same time step as the function call. For example, in this model, a function-call input event activates the Looping Scheduler chart.

sf_loop_scheduler_img.png

Behavior of Multiple Function-Call Input Events

For function-call input events, only one trigger event exists. The event caller explicitly calls and executes the chart.

Association of Input Events with Control Signals

When you define one or more input events in a chart, a single trigger port appears on the top side of the chart block. Multiple external Simulink blocks can trigger the input events through a vector of signals connected to the trigger port. The Port property of an input event specifies the index into the control signal vector that connects to the trigger port.

By default, Port values appear in the order that you add input events. You can change these assignments by modifying the Port property of the events. When you change the Port property for an input event, the Port values for the remaining input events automatically renumber.

Data Types Allowed for Input Events

For multiple input events to a trigger port, all signals must have the same data type. Using signals of different data types as input events results in an error during simulation. For example, you can mux two input signals of type double to use as input events to a chart.

Simulink model that uses two signals of type double as input events to a Stateflow chart.

You cannot mux two input signals of different data types, such as boolean and double.

Simulink model that uses a signal of type boolean and a signal of type double as input events to a Stateflow chart. This configuration produces a run-time error.

Related Topics