MATLAB Examples

View Differences Between Stateflow Messages, Events, and Data

This example shows the difference in behavior of a Stateflow message compared to events or data.

Contents

Sender Charts

This model has three sender charts, DataSender, EventSender, and MessageSender. Each sender chart has one state, A. In the entry action of state A, each chart assigns an output data value to 1, sends a function-call event, or sends a message.

Receiver Charts

For each of the sender charts, there is a corresponding receiving chart. Each receiver chart has a state diagram with states A0, A1, A2, and A3. Each receiver chart has a transition from A0 to A1 after 3 seconds. The data, event, or message from the corresponding sender chart guards the transitions between A1, A2, and A3.

Scope Output

Each receiver chart has active state output enabled, with the output port connected to a scope. On the scope output, you see which states are active for each time step. This output highlights the difference in behavior between output data, event, and message.

Behavior of Data

In the entry action of chart DataSender, the output data M is assigned a value of 1. M connects as input to the chart DataReceiver. M holds this assigned value throughout the simulation. Because there is no input event, the chart DataReceiver executes once every time step. In chart DataReceiver, initially state A0 is active. At t=3, the chart takes the transition to A1. On the scope, you see the state change from A0 to A1. In the next time step, t=4, the transition tests if M equals 1. This condition is true, so the chart takes the transition to A2. At t=5, M still equals 1. The chart transitions from A2 to A3.

After data is assigned a value, it remains valid. Therefore, each time a transition evaluates M, you see DataReceiver transition to a new state.

Behavior of Event

The chart EventSender uses the syntax send(M) to send an event as an entry action in state A. This function-call event is an input event to wake up the chart EventReceiver. The chart EventReceiver executes only when the input event M wakes the chart up. When event M wakes up chart EventReceiver, state A0 is active. The chart checks for a valid transition from state A0. The transition from A0 is based on absolute-time temporal logic, and is not valid at t=0. A0 remains as the active state, and the chart goes back to sleep. The chart EventReceiver does not wake up again, because chart EventSender only sends event M once. On the scope, you see that EventReceiver never transitions out of A0.

Because events do not remain valid across time steps, the chart has only one chance to respond to the event. When EventSender sent the event, EventReceiver was not in a state to respond to it. The ability for EventReceiver to transition in response to the event is lost.

Behavior of Message

The chart MessageSender uses the syntax send(M) to send one message through the message output port. The message goes in the message input receiving queue for the chart, MessageReceiver. The message waits until MessageReceiver evaluates it. Because there is no input event, the chart MessageReceiver executes once every time step. In MessageReceiver, initially, state A0 is active. The chart transitions to state A1 at t=3. In the next time step, t=4, the transition determines if M is present. The chart removes M from the receiver input queue. Because M is valid, the chart takes the transition to A2. The chart destroys M at the end of the time step t=4. At t=5, another message is not present in the queue, so the chart does not transition to A3. A2 remains the active state.

Unlike events, messages are queued. The receiving chart can choose to respond to a message anytime after it was sent. Unlike data, the message does not remain valid across time steps. If the number of messages the chart receives overflows the input queue, then a message overflow error occurs.