0
0
SimulinkHow-ToBeginner · 4 min read

How to Create State Machine in Stateflow: Step-by-Step Guide

To create a state machine in Stateflow, open Simulink, add a Stateflow Chart block, then define states and transitions inside the chart editor. Use graphical states and transitions to model behavior, and simulate to test the state machine.
📐

Syntax

In Stateflow, a state machine is created inside a Stateflow Chart block. The main elements are:

  • State: Represents a condition or mode.
  • Transition: Arrows connecting states, triggered by events or conditions.
  • Entry/During/Exit actions: Code executed when entering, during, or exiting a state.
  • Events: Triggers that cause transitions.

The basic syntax inside a state is:

state_name:
  entry: action;
  during: action;
  exit: action;

Transitions use conditions or events like:

state1 --> state2 [condition] {action}
stateflow
chart MyStateMachine {
  state Idle {
    entry: disp("Entering Idle");
  }
  state Active {
    entry: disp("Entering Active");
  }
  Idle --> Active [start == 1];
  Active --> Idle [stop == 1];
}
💻

Example

This example shows a simple state machine with two states: Idle and Active. The machine switches from Idle to Active when start is 1, and back to Idle when stop is 1.

It demonstrates how to define states, transitions, and entry actions.

matlab
function createSimpleStateMachine()
  % Open a new Simulink model
  modelName = 'simpleStateMachineModel';
  new_system(modelName);
  open_system(modelName);

  % Add Stateflow Chart block
  chartBlock = add_block('sflib/Chart', [modelName '/MyStateMachine']);
  set_param(chartBlock, 'Position', [100 100 300 300]);

  % Open the chart editor
  sfRoot = sfroot;
  charts = sfRoot.find('-isa', 'Stateflow.Chart', '-and', 'Name', 'MyStateMachine');
  if isempty(charts)
    sfChart = sfRoot.addChart;
    sfChart.Name = 'MyStateMachine';
  else
    sfChart = charts(1);
  end

  % Add states
  idleState = sfChart.addState;
  idleState.Name = 'Idle';
  idleState.Position = [50 50 90 60];
  idleState.Entry = 'disp("Entering Idle");';

  activeState = sfChart.addState;
  activeState.Name = 'Active';
  activeState.Position = [200 50 90 60];
  activeState.Entry = 'disp("Entering Active");';

  % Add transitions
  t1 = sfChart.addTransition;
  t1.Source = idleState;
  t1.Destination = activeState;
  t1.Condition = 'start == 1';

  t2 = sfChart.addTransition;
  t2.Source = activeState;
  t2.Destination = idleState;
  t2.Condition = 'stop == 1';

  % Save and open model
  save_system(modelName);
  open_system(modelName);
end
⚠️

Common Pitfalls

  • Not defining initial state: Always set one state as the default (initial) state, or the chart will not run.
  • Incorrect transition conditions: Conditions must be valid expressions; otherwise, transitions won't trigger.
  • Missing events or inputs: Ensure input signals or events used in conditions exist in the model.
  • Overlapping states: Avoid placing states overlapping in the editor to prevent confusion.

Example of missing initial state (wrong):

state1;
state2;
state1 --> state2;

Correct with initial state marked by a default transition:

default --> state1;
state1 --> state2;
stateflow
chart Example {
  % Wrong: no initial state
  state state1;
  state state2;
  state1 --> state2;

  % Right: initial state
  default --> state1;
  state1 --> state2;
}
📊

Quick Reference

ElementDescriptionExample
StateRepresents a mode or conditionstate Idle { entry: disp('Idle'); }
TransitionArrow between states with conditionIdle --> Active [start == 1];
Entry ActionCode run when entering a stateentry: disp('Entering');
During ActionCode run while in a stateduring: count = count + 1;
Exit ActionCode run when leaving a stateexit: disp('Exiting');
Default TransitionInitial state markerdefault --> Idle;
EventTrigger for transitionsevent start;

Key Takeaways

Create a Stateflow Chart block in Simulink to start building a state machine.
Define states with entry, during, and exit actions to model behavior.
Use transitions with conditions or events to switch between states.
Always set an initial state with a default transition to ensure execution.
Check that all events and inputs used in conditions exist in your model.