Designing Your Model: Model-Code Correspondence : Statecharts : Statechart - Generated Functions

Statechart - Generated Functions

In general, functions generated from statecharts will resemble the following (the provided line numbers are used in explanations of the code below):

1 void
2 cgDo_A11_CTRLcnt1(void)
3 {
4 StateInfo_A11_CTRLcnt1 nextState_A11_CTRLcnt1 = 0;
5 if (currentState_A11_CTRLcnt1 == 0) {
6 nextState_A11_CTRLcnt1 = FS_A11_CTRLst2;
7 }
8
9 else
10 {
11 … The rest of the Statechart logic
12 }
13 if (nextState_A11_CTRLcnt1 != 0) {
14 if (currentState_A11_CTRLcnt1 !=
nextState_A11_CTRLcnt1)
15 cgGlobalFlags |= BITSUPERSTEP_TASK1;
16 currentState_A11_CTRLcnt1 =
nextState_A11_CTRLcnt1;
17 }
18 }

In line 4, the nextState variable is reset. This variable will be set only if a transition has been made, and will hold the statechart's new state configuration.

Lines 13 and 14 check the nextState variable to determine whether a transition was made, and whether to enforce another step in the task holding the statechart.

Line 16 advances the statechart configuration a step to hold the configuration of the next step.

In your statechart, lines 5 to 12 will be replaced with specific code resulting from the specified statechart logic. For example, in many cases, two additional functions will be generated here - entry actions and exit actions. If the statechart logic requires entering/exiting reactions, the functions will resemble the following.

void
cgEnterActions_A11_CTRLcnt1(void)
{
… entering reactions code
}


void
cgExitActions_A11_CTRLcnt1(void)
{
… exiting reactions code
}

When either of these function are needed, the following changes to cgDo_… will also be made:

void cgDo_A11_CTRLcnt1(void)
{

StateInfo_A11_CTRLcnt1 nextState_A11_CTRLcnt1 = 0;

staySame_A11_CTRLcnt1 = 0;

if (currentState_A11_CTRLcnt1 == 0) {

nextState_A11_CTRLcnt1 =

FS_DefaultOf_Chart_A11_CTRL;

}

else

{

… The rest of the Statechart logic

}

if (nextState_A11_CTRLcnt1 != 0) {

cgExitActions_A11_CTRLcnt1();

cgEnterActions_A11_CTRLcnt1();

if (currentState_A11_CTRLcnt1 !=

nextState_A11_CTRLcnt1)

cgGlobalFlags |= BITSUPERSTEP_TASK1;

currentState_A11_CTRLcnt1 = nextState_A11_CTRLcnt1;

}

}