Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
To write a C program using Finite State Machine(FSM) for controlling the retraction and extension of Airplane's landing gear.
ALGORITHM:
Step 1: We need to create a project and source file in the STM cube IDE.
Step 2: We need to declare required header files and the required function prototypes like "Gear_Down", "Checking_Before_Take_Off", "Raising_Gear", "Gear_Up", "Checking_Before_Landing" and "Lowering_Gear".
Step 3: Using user defined datatypes like typedef and enum we are declaring 4 enumerated datatypes named as "State_type", "Switch", "Pilot_lever" and "Hydraulic_mechanism".
Step 4: The enumerated variable "State_type" has predefined states like "GEAR_DOWN", "CHECKING_BEFORE_TAKE_OFF", "RAISING_GEAR", "GEAR_UP", "CHECKING_BEFORE_LANDING", "LOWERING_GEAR" and the enumerated variable "Hydraulic_mechanism" has predefined states like "WORKING" and "NOT_WORKING".
Step 5: The enumerated variable "Switch" has predefined states like "OFF", "ON" and the enumerated variable "Pilot_lever" has predefined states like "RAISING", "FALLING", "UP" and "DOWN".
Step 6: In this step we are creating two variables of enumerated datatype "Switch" with type qualifier as "volatile" and storage class as "static" and the variables are named as "Squat_Switch" and "Limit_Switch".
Step 7: Similar to Step 6 we are creating two variables of enumerated datatype "Pilot_lever" with type qualifier as "volatile" and storage class as "static" and the variables are named as "PL".
Step 8: Similar to Step 6 we are creating two variables of enumerated datatype "Hydraulic_mechanism" with type qualifier as "volatile" and storage class as "static" and the variables are named as "HM".
Step 9: A structure is created with alias name of "State" and inside structure we are creating 4 string variables like "Currentstate", "Light", "Direction_valve" and "Hydraulic_control".
Step 10: We are creating array of function pointers in order to store the 6 different functions and it is created with the storage class of "static", return type and arguments as "void".
Step 11: Now we are creating an array of structure with storge class as "static" and it is named as "State_Machine" with the array size of 6 and each index of array will be storing current state, light status, Direction valve and Hydraulic control status as outputs.
Step 12: In main function we are assigning the "GEAR_DOWN" state as the current state and Using infinite while loop we are calling the array of function pointers(State_Table[Current_state]()).
Step 13: In function definition of Gear_Down() we are assigning the "GEAR_DOWN" state as the current state and using scanf() function we are getting the values of Pilot lever and Squat_Switch.
Step 14: Using If condition we are checking the Pilot lever is equal to "RAISING" and Squat_Switch is equal to "ON" and if the condition is true then we are assigning the "CHECKING_BEFORE_TAKE_OFF" as the current state.
Step 15: Using printf() function we are printing the values of Currentstate, Light, Direction_valve and Hydraulic_control values. When the IF condition fails we are calling the same function "Gear_Down()".
Step 16: In function definition of Checking_Before_Take_Off() we are assigning the "CHECKING_BEFORE_TAKE_OFF" state as the current state and using scanf() function we are getting the values of Pilot lever, Squat_Switch and Hydraulic_mechanism.
Step 17: Using If condition we are checking the Pilot lever is equal to "FALLING" and Squat_Switch is equal to "OFF" and if the condition is true then we are assigning the "GEAR_DOWN" as the current state and printing the required outputs as mentioned in Step 15.
Step 18: In Else If part we are checking the Pilot lever is equal to "UP" and Hydraulic_mechanism is equal to "WORKING" and if the condition is true then we are assigning the "RAISING_GEAR" as the current state and printing the required outputs as mentioned in Step 15 and in else part we are calling the same function "Checking_Before_Take_Off()".
Step 19: In function definition of Raising_Gear() we are assigning the "RAISING_GEAR" state as the current state and using scanf() function we are getting the values of Pilot lever,Limit_Switch.
Step 20: Using If condition we are checking the Pilot lever is equal to "FALLING" and if the condition is true then we are assigning the "CHECKING_BEFORE_LANDING" as the current state and printing the required outputs as mentioned in Step 15.
Step 21: In Else If part we are checking the Limit_Switch is equal to "OFF" and if the condition is true then we are assigning the "GEAR_UP" as the current state and printing the required outputs as mentioned in Step 15 and in else part we are calling the same function "Raising_Gear()".
Step 22: In function definition of Gear_Up() we are assigning the "GEAR_UP" state as the current state and using scanf() function we are getting the values of Pilot lever.
Step 23: Using If condition we are checking the Pilot lever is equal to "FALLING" and if the condition is true then we are assigning the "CHECKING_BEFORE_LANDING" as the current state and printing the required outputs as mentioned in Step 15 and in else part we are calling the same function "Gear_Up()".
Step 24: In function definition of Checking_Before_Landing() we are assigning the "CHECKING_BEFORE_LANDING" state as the current state and using scanf() function we are getting the values of the pilot lever and Hydraulic_mechanism.
Step 25: Using If condition we are checking the Pilot lever is equal to "DOWN" OR Hydraulic_mechanism is equal to NOT_WORKING if the condition is true then we are assigning the "LOWERING_GEAR" as the current state and printing the required outputs as mentioned in Step 15 and in else part we are calling the same function "Checking_Before_Landing()".
Step 26: In function definition of Lowering_Gear() we are assigning the "LOWERING_GEAR" state as the current state and using scanf() function we are getting the values of the Pilot lever and Limit Switch.
Step 27: Using If condition we are checking the Limit_Switch is equal to "ON" and if the condition is true then we are assigning the "GEAR_DOWN" as the current state and printing the required outputs as mentioned in Step 15.
Step 28: In Else If part we are checking the Pilot lever is equal to "RAISING" and if the condition is true then we are assigning the "CHECKING_BEFORE_TAKE_OFF" as the current state and printing the required outputs as mentioned in Step 15 and in else part we are calling the same function "Lowering_Gear()".
CODE:
/*
* Gear.c
*
* Created on: 12-Sep-2022
* Author: Jagadesh R
*/
#include
#include
void Gear_Down(void);
void Checking_Before_Take_Off(void);
void Raising_Gear(void);
void Gear_Up(void);
void Checking_Before_Landing(void);
void Lowering_Gear(void);
typedef enum
{
GEAR_DOWN,
CHECKING_BEFORE_TAKE_OFF,
RAISING_GEAR,
GEAR_UP,
CHECKING_BEFORE_LANDING,
LOWERING_GEAR
}State_type;
typedef enum
{
OFF,
ON
}Switch;
typedef enum
{
RAISING,
FALLING,
UP,
DOWN
}Pilot_lever;
typedef enum
{
WORKING,
NOT_WORKING
}Hydraulic_mechanism;
static volatile Switch Squat_Switch;
static volatile Switch Limit_Switch;
static volatile Pilot_lever PL;
static volatile Hydraulic_mechanism HM;
State_type Current_state;
typedef struct
{
char* Currentstate;
char* Light;
char* Direction_valve;
char* Hydraulic_control;
}State;
static void(*State_Table[])(void)={Gear_Down,Checking_Before_Take_Off,Raising_Gear,Gear_Up,Checking_Before_Landing,Lowering_Gear};
static State State_Machine[6]={
{"Gear Down","Green","Down","Enabled"},{"Checking Before Take Off","Green","Down","Enabled"},
{"Raising Gear","Red","Up","Enabled"},{"Gear Up","off","Null","Disabled"},
{"Checking Before Landing","Red","Down","Enabled"},{"Lowering Gear","Green","Down","Enabled"}
};
int main()
{
Current_state = GEAR_DOWN;
while(1)
{
State_Table[Current_state]();
}
return 0;
}
Gear_Down()
{
Current_state = GEAR_DOWN;
printf("\nEnter the Pilot lever and Squat_Switch Values:");
fflush(stdout);
scanf("%d %d",&PL,&Squat_Switch);
if((PL==RAISING)&&(Squat_Switch==ON))
{
Current_state = CHECKING_BEFORE_TAKE_OFF;
printf("\nCurrent state: %s",State_Machine[Current_state].Currentstate);
printf("\nLight Status: %s",State_Machine[Current_state].Light);
printf("\nDirection_valve Status: %s",State_Machine[Current_state].Direction_valve);
printf("\nHydraulic_control Status: %s",State_Machine[Current_state].Hydraulic_control);
}
else
{
Gear_Down();
}
}
Checking_Before_Take_Off()
{
Current_state = CHECKING_BEFORE_TAKE_OFF;
printf("\nEnter the Pilot lever, Squat_Switch and Hydraulic_mechanism Values:");
fflush(stdout);
scanf("%d %d %d",&PL,&Squat_Switch,&HM);
if((PL==FALLING)&&(Squat_Switch==OFF))
{
Current_state = GEAR_DOWN;
printf("\nCurrent state: %s",State_Machine[Current_state].Currentstate);
printf("\nLight Status: %s",State_Machine[Current_state].Light);
printf("\nDirection_valve Status: %s",State_Machine[Current_state].Direction_valve);
printf("\nHydraulic_control Status: %s",State_Machine[Current_state].Hydraulic_control);
}
else if((PL==UP)&&(HM==WORKING))
{
Current_state = RAISING_GEAR;
printf("\nCurrent state: %s",State_Machine[Current_state].Currentstate);
printf("\nLight Status: %s",State_Machine[Current_state].Light);
printf("\nDirection_valve Status: %s",State_Machine[Current_state].Direction_valve);
printf("\nHydraulic_control Status: %s",State_Machine[Current_state].Hydraulic_control);
}
else
{
Checking_Before_Take_Off();
}
}
Raising_Gear()
{
Current_state = RAISING_GEAR;
printf("\nEnter the Pilot lever,Limit_Switch Values:");
fflush(stdout);
scanf("%d %d",&PL,&Limit_Switch);
if(PL==FALLING)
{
Current_state = CHECKING_BEFORE_LANDING;
printf("\nCurrent state: %s",State_Machine[Current_state].Currentstate);
printf("\nLight Status: %s",State_Machine[Current_state].Light);
printf("\nDirection_valve Status: %s",State_Machine[Current_state].Direction_valve);
printf("\nHydraulic_control Status: %s",State_Machine[Current_state].Hydraulic_control);
}
else if(Limit_Switch==OFF)
{
Current_state = GEAR_UP;
printf("\nCurrent state: %s",State_Machine[Current_state].Currentstate);
printf("\nLight Status: %s",State_Machine[Current_state].Light);
printf("\nDirection_valve Status: %s",State_Machine[Current_state].Direction_valve);
printf("\nHydraulic_control Status: %s",State_Machine[Current_state].Hydraulic_control);
}
else
{
Raising_Gear();
}
}
Gear_Up()
{
Current_state = GEAR_UP;
printf("\nEnter the Pilot lever Values:");
fflush(stdout);
scanf("%d",&PL);
if(PL==FALLING)
{
Current_state = CHECKING_BEFORE_LANDING;
printf("\nCurrent state: %s",State_Machine[Current_state].Currentstate);
printf("\nLight Status: %s",State_Machine[Current_state].Light);
printf("\nDirection_valve Status: %s",State_Machine[Current_state].Direction_valve);
printf("\nHydraulic_control Status: %s",State_Machine[Current_state].Hydraulic_control);
}
else
{
Gear_Up();
}
}
Checking_Before_Landing()
{
Current_state = CHECKING_BEFORE_LANDING;
printf("\nEnter the Pilot lever and Hydraulic_mechanism Values:");
fflush(stdout);
scanf("%d%d",&PL,&HM);
if((PL==DOWN)||(HM==NOT_WORKING))
{
Current_state = LOWERING_GEAR;
printf("\nCurrent state: %s",State_Machine[Current_state].Currentstate);
printf("\nLight Status: %s",State_Machine[Current_state].Light);
printf("\nDirection_valve Status: %s",State_Machine[Current_state].Direction_valve);
printf("\nHydraulic_control Status: %s",State_Machine[Current_state].Hydraulic_control);
}
else
{
Checking_Before_Landing();
}
}
Lowering_Gear()
{
Current_state = LOWERING_GEAR;
printf("\nEnter the Pilot lever and Limit Switch Values:");
fflush(stdout);
scanf("%d%d",&PL,&Limit_Switch);
if((Limit_Switch==ON))
{
Current_state = GEAR_DOWN;
printf("\nCurrent state: %s",State_Machine[Current_state].Currentstate);
printf("\nLight Status: %s",State_Machine[Current_state].Light);
printf("\nDirection_valve Status: %s",State_Machine[Current_state].Direction_valve);
printf("\nHydraulic_control Status: %s",State_Machine[Current_state].Hydraulic_control);
}
else if(PL==RAISING)
{
Current_state = CHECKING_BEFORE_TAKE_OFF;
printf("\nCurrent state: %s",State_Machine[Current_state].Currentstate);
printf("\nLight Status: %s",State_Machine[Current_state].Light);
printf("\nDirection_valve Status: %s",State_Machine[Current_state].Direction_valve);
printf("\nHydraulic_control Status: %s",State_Machine[Current_state].Hydraulic_control);
}
else
{
Lowering_Gear();
}
}
RESULT:
Thus the C program is written using Finite State Machine(FSM) for controlling the retraction and extension of Airplane's landing gear.