Embedded Systems Design With UML State Machines
Embedded Systems Design With UML State Machines
INTRODUCTION
·
FSM's can be used to describe situations or scenarios of your
application ,
like for example, modelling the life
cycle of a
·
FSMs are helpful to model complex applications that involve
lots of decision making producing different outputs or actions
, ,
and
processing various events .
Zager Altrai
Moore Machine
input events
.
①
②
Zager Altrai
<behavior-type-label>[‘/’ <behavior-expression>]
{<trigger>}['[' <guard>']'] [/<behavior-expression>]
1. Nested Switch Approach:
• Structure:
This approach uses nested switch-case statements. The outer switch
statement typically handles the current state, and the inner switch or
if-else statements handle the events or inputs.
• Implementation:
Each case within the outer switch corresponds to a state. Inside each
case, another switch or series of if-else statements determine the
action to take based on the current input or event.
• Advantages:
• Easy to implement and understand, especially for simple state machines.
Directly maps the state machine diagram to code.
• Disadvantages:
• Can become complex and hard to manage with a large number of states and
transitions.
• Modifying the state machine can be error-prone, as changes might need to
be made in multiple places.
• Use Case:
Best for simple state machines with a limited number of states and
transitions.
• Structure:
This approach uses a table (array) to represent states and transitions.
Each row in the table represents a state, and each column represents an
event or input.
• Implementation:
The table entries typically store information about the next state and
the action to take. When an event occurs, the system looks up the
current state and event in the table to determine the next state and
action.
• Advantages:
• Scalable for complex state machines with many states and transitions.
• Easier to read and modify, as the state transition logic is centralized
in the table.
• Can be data-driven, allowing for dynamic changes to the state machine.
• Disadvantages:
• Can consume more memory, especially with a large number of states and
events.
• Might be less intuitive for simple state machines or for developers
unfamiliar with the approach.
• Use Case:
Ideal for complex state machines where scalability and maintainability
are important.
3. State Handler Approach:
• Structure:
This approach uses a function (or method) for each state. Each function
handles the events or inputs for its specific state and returns the next
state.
• Implementation:
The state machine has a current state variable. Based on this state, the
corresponding state handler function is called. The function executes
the actions for its state and returns the next state based on the inputs
or events.
• Advantages:
• Provides clear separation of the logic for each state.
• Easy to add or modify states, as each state has its own function.
• Can improve readability and maintainability, especially with well-named
state functions.
• Disadvantages:
• Might involve more overhead in terms of function calls, especially with
deeply nested states.
• The overall flow can be less visible compared to the nested switch or
state table approaches.
• Use Case:
Suitable for medium to complex state machines where clarity and
maintainability of state-specific logic are priorities.
Detailed Explanation:
• Flat State Machines: In a flat state machine, all states are on the same
level. This can lead to complexity and redundancy, especially in large
systems where many states share similar behaviors.
• Hierarchical State Machines (HSMs): HSMs introduce the concept of nested
states. States can inherit behavior from their parent states, leading to
a more organized and manageable structure.