_EXTENDED STATE DIAGRAMS AND REACTIVE SYSTEMS_ by Doron Drusinsky Listing One /* C Code-Generation Output File #1 of 1: Generated for Chart Traffic_light by the Better-State Code Generator, Proprietary of R-Active Concepts Cupertino CA, 95014, (408)252-2808 */ /*----- State ID dictionary Use this dictionary to symbolically reference your states (for those States that you gave names in the chart editor). You can also examine the state register, using the mapping provided as a remark. -------*/ #define DUMMY -7 #define DONT_CARE 0 #define St_c_3_P3 6 /* mapped to PS[0] */ #define St_Yellow_All_P2 14 /* mapped to PS[0] */ #define St_Green2Main_P2 24 /* mapped to PS[0] */ #define St_c_0_P3 86 /* mapped to PS[0] */ #define St_c_1_P3 89 /* mapped to PS[0] */ #define St_c_2_P3 90 /* mapped to PS[0] */ #define St_On_P3 73 /* mapped to PS[1] */ #define St_Off_P3 76 /* mapped to PS[1] */ #define St_Shoot_P3 78 /* mapped to PS[1] */ #define St_Red2Main_P2 33 /* mapped to PS[3] */ #define St_On_going_P2 23 /* mapped to PS[4] */ int CHRT_Traffic_light(int BS_Reset) { /* RESETing: calling CHRT_Traffic_light with BS_Reset>0 will reset your controller to it's default composite state, and will execute all on_entry actions for that state. */ static int PS[5]= {St_Yellow_All_P2, DUMMY, DUMMY, DUMMY, DUMMY}; int NS[5]= {0, 0, 0, 0, 0}; int BS_i; if (BS_Reset>0) { /* Reset state assignments */ NS[0]=St_Yellow_All_P2; NS[1]=DUMMY; NS[2]=DUMMY; NS[3]=DUMMY; NS[4]=DUMMY; /* On_entry actions for reset states */ Color_Main=YELLOW;Color_Sec=YELLOW; } else { /*-------*/ if (PS[4]==St_On_going_P2) if ( (NS[0] == DONT_CARE) && (NS[1] == DONT_CARE) && (NS[2] == DONT_CARE) && (NS[3] == DONT_CARE) && (NS[4] == DONT_CARE) ) if (Reset) { NS[0] = St_Yellow_All_P2 ; NS[1] = DUMMY ; NS[2] = DUMMY ; NS[3] = DUMMY ; NS[4] = DUMMY ; Color_Main=YELLOW;Color_Sec=YELLOW; } /*-------*/ if (PS[0]==St_Yellow_All_P2) if ( (NS[0] == DONT_CARE) && (NS[1] == DONT_CARE) && (NS[2] == DONT_CARE) && (NS[3] == DONT_CARE) && (NS[4] == DONT_CARE) ) if (!Reset) { NS[0] = St_Green2Main_P2 ; NS[1] = DUMMY ; NS[2] = DUMMY ; NS[3] = DUMMY ; NS[4] = St_On_going_P2 ; Color_Main=GREEN; Color_Sec=RED; } /*-------*/ if (PS[0]==St_Green2Main_P2) if ( (NS[0] == DONT_CARE) && (NS[1] == DONT_CARE) && (NS[3] == DONT_CARE) ) if (TIMEOUT) { NS[0] = St_c_0_P3 ; NS[1] = St_On_P3 ; NS[3] = St_Red2Main_P2 ; Color_Main=RED; Color_Sec=GREEN; } /*-------*/ if (PS[3]==St_Red2Main_P2) if ( (NS[0] == DONT_CARE) && (NS[1] == DONT_CARE) && (NS[2] == DONT_CARE) && (NS[3] == DONT_CARE) ) if (TIMEOUT) { NS[0] = St_Green2Main_P2 ; NS[1] = DUMMY ; NS[2] = DUMMY ; NS[3] = DUMMY ; Color_Main=GREEN; Color_Sec=RED; } /*-------*/ if (PS[1]==St_On_P3) if ( (NS[1] == DONT_CARE) ) if (Car_in_Junct) { NS[1] = St_Shoot_P3 ; } /*-------*/ if (PS[1]==St_Shoot_P3) if ( (NS[1] == DONT_CARE) ) { NS[1] = St_On_P3 ; } /*-------*/ if (PS[1]==St_On_P3) if ( (NS[1] == DONT_CARE) ) if (Manual_on) { NS[1] = St_Off_P3 ; } /*-------*/ if (PS[1]==St_Off_P3) if ( (NS[1] == DONT_CARE) ) if (!Manual_on) { NS[1] = St_On_P3 ; } /*-------*/ if (PS[0]==St_c_0_P3) if ( (NS[0] == DONT_CARE) && (NS[2] == DONT_CARE) ) if (New_car_waiting) { NS[0] = St_c_1_P3 ; NS[2] = 122 ; } /*-------*/ if (PS[0]==St_c_1_P3) if (NS[0] == DONT_CARE) if (New_car_waiting) { NS[0] = St_c_2_P3 ; } /*-------*/ if (PS[0]==St_c_2_P3) if (NS[0] == DONT_CARE) { NS[0] = St_c_3_P3 ; } /*-------*/ if (PS[0]==St_c_2_P3) if ( (NS[1] == DONT_CARE) ) { NS[1] = St_Shoot_P3 ; } } /* if BS_Reset */ /* Assigning next state to present-state */ for (BS_i=0;BS_i < 5;BS_i++) if (NS[BS_i] != DONT_CARE) {PS[BS_i]=NS[BS_i]; NS[BS_i]=DONT_CARE;} return 1; } /* end of BS controller */ Listing Two /* calling the two controllers in a multi-rate scheme: the TLC runs twice for each cycle of the TM */ for (i=0;i<100; i++) {for (j=0;j<2;j++) CHRT_TLC(0); CHRT_TM(0); } /* calling the charts stochastically */ for (i=0;i<100;i++) {x1=rand(); if (x1/RAND_MAX 0.5) CHRT_TLC(0); x2=rand(); if (x2/RAND_MAX 0.3) CHRT_TM(0); } /* A Round-robin scheduler in an endless execution (infinite-loop) */ while(1) {CHRT_chart1(0); CHRT_chart2(0); CHRT_chart3(0); ... } Listing Three /* Jumping out of an infinite-loop execution of the controller using Terminal states. When the controller reaches a Terminal state (designated as such using the State C-Code Dialog while drawing the state), it returns a 0 value. */ while (1) { if (!CHRT_TLC(0)) break; /* will break if Terminal state has been reached */ }