Page 877 - Mechatronics with Experiments
P. 877

®
                                                      ®
                                                MATLAB , SIMULINK , STATEFLOW, AND AUTO-CODE GENERATION  863
                                                                      History   Exclusive (OR) state
                                                                     junction
                               Connective     Default transition
                                junction                                     Transition




                                StateA
                                                    Default_transition
                                                                                      StateA2
                                 StateA1                         H
                                                                                       StateA2a
                                                          StateA1c
                                 StateA1a
                                                                          TransitionA1_A2
                                          (Condition 1)
                                                      (Condition 1){func1()}func2()    StateA2b
                                 TransitionA1a_A1b
                                                                          TransitionA2_A1
                                              StateA1b





                             Transition label     Condition  Condition   Transition  Parallel (AND) state
                                                               action     action
                             FIGURE A.37: Graphical components of a Stateflow diagram: state (parallel and exclusive),
                             transitions (normal transitions, default transitions), actions, junctions (connective and history
                             types), conditions.


                             executed. After that, the actions in the “during:” block are executed during the following
                             sampling periods. When a transition occurs from the currently active state, then the actions
                             in the “exit:” block are executed before the program transitions to another state. Multiple
                             “on_EventName” blocks can be defined. This allows us to define a unique set of actions
                             for each event.
                                  In addition to actions, we may need to define data variables (local or persistent) in
                             each state to keep track of data. It is also possible that we only need the input and output
                             data, and no local (or persistent) data may be needed.
                                  Step 4: Define the Transitions Between States. So far we have defined a state
                             hierarchy and actions within those states. We also need to define the logic to make the
                             transitions between those states.
                             Transitions are the logical flow paths between different states. To simplify the logic, it is
                             advisable to exercise modular programming practice and plan transitions between states
                             at the same hierarchy level. Otherwise, the transition logic may look like a “spaghetti
                             code” which means it is difficult to understand and maintain. Each transition may have a
                             condition which must be TRUE for the transition to occur, and an action that is executed
                             during the transition before the control is passed to the next state. These transition conditions
                             and actions are expressed with a notation on the transition path in text format. At every
                             hierarchy of states, there should be a default transition defined. This transition would
                             be used in case of ambiguity over which state to activate. This often happens on start-up
                             conditions. For every level of exclusive OR-states, there should be a default transition
   872   873   874   875   876   877   878   879   880   881   882