Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Project: FINITE STATE MACHINE
Problem Statement:
Implement the Code for controlling the retraction and
extension of Airplane’s landing gear
Proble FSM:

Program:
#include //Header file
#include
void Gear_Down(); //Function declaretion
void Checking_before_Take_off();
void Gear_Raising();
void Gear_UP();
void Checking_before_Landing();
void Gear_Lowering();
void Initial_State();
//Function Pointer declaretion
static void(*state_Table[])(void)=
{
Gear_Down,
Checking_before_Take_off,
Gear_Raising,
Gear_UP,
Checking_before_Landing,
Gear_Lowering,
};
//Create enum variable for function state
typedef enum
{ //As per enum structure enum position are
GEAR_DOWN, //GEAR_DOWN=0
CHECKING_BEFORE_TAKE_OFF, //CHECKING_BEFORE_TAKE_OFF=1,
GEAR_RAISING, //GEAR_RAISING=2,
GEAR_UP, //GEAR_UP=3,
CHECKING_BEFORE_LANDING, //CHECKING_BEFORE_LANDING=4,
GEAR_LOWERING, //GEAR_LOWERING=5
}State_Landing;
//Create enum variable for pilot lever position
typedef enum
{ //As per enum structure enum position are
UP, //UP=0,
DOWN, //DOWN=1,
RISING, //RISING=2,
LOWERING, //LOWERING=3,
FALLING, //FALLING=4
}Pilot_lever;
//Create enum variable for Limit switch position
typedef enum //As per enum structure enum position are
{
OPEN, //OPEN=0,
CLOSE, //CLOSE=1
}Limit_switch;
//Create enum variable for squat switch position
typedef enum
{ //As per enum structure enum position are
OPEN_E, //OPEN_E=0,
CLOSE_E, //CLOSE_E=1,
}squat_switch;
typedef enum
{ //As per enum structure enum position are
UP_D, //UP_D=0,
DOWN_D, //DOWN_D=1,
}direction_valve;
State_Landing current_state; //Name the state landing variable as current_state
static volatile Pilot_lever Pilot_lever_state; //Name the Pilot lever enum variable as Pilot_lever_state
static volatile squat_switch squat_switch_state; //Name the Squat switch enum variable as squat_switch_state
static volatile Limit_switch Limit_switch_state; //Name the Limit switch enum variable as Limit_switch_state
static volatile direction_valve direction_valve_state;
//Create structure pointer charecter type array
typedef struct
{
char* machine_current_state;
char* State_of_Landing_Gear;
char* State_of_Airplane;
char* Light_state;
}state_Table1;
//Define pointer array eliment asper pointr structure
state_Table1 state_Machine[6]=
{
{"Gear_Down","RISING","LANDED","GREEN"},
{"Checking_before_Take_off","UP","TAKEO","RED"},
{"Gear_Raising","FALLING","TAKEOF","RED"},
{"Gear_UP","FALLING","FLY","RED"},
{"Checking_before_Landing","DOWN","LANDING","RED"},
{"Gear_Lowering","DOWN","LANDING","GREEN"},
};
//Main function
int main()
{
Initial_State(); //Call Initial state for initial condition
while (1)
{
state_Table[current_state](); //Call state table functions as per logic sequance
}
}
void Gear_Down(void) //Function for Gear Down state
{
current_state=GEAR_DOWN;
printf("Enter your Pilot lever and Squat_switch status:\n");
fflush(stdout);
scanf("%d %d",&Pilot_lever_state,&squat_switch_state);
if (Pilot_lever_state==RISING && squat_switch_state==CLOSE_E)
{
current_state=CHECKING_BEFORE_TAKE_OFF;
printf("current status:%s\n",state_Machine[current_state].machine_current_state);
printf("Lightis: %s\n",state_Machine[current_state].Light_state);
}
else
{
Gear_Down();
}
}
void Checking_before_Take_off(void) //Function for Take off before checking state
{
current_state=CHECKING_BEFORE_TAKE_OFF;
printf("Enter your Pilot lever and Squat_switch status:\n");
fflush(stdout);
scanf("%d %d %d",&Pilot_lever_state,&squat_switch_state,&direction_valve_state);
if (direction_valve_state==UP_D)
{
current_state=GEAR_RAISING2
;
printf("current status:%s\n",state_Machine[current_state].machine_current_state);
printf("Lightis: %s\n",state_Machine[current_state].Light_state);
}
else if (Pilot_lever_state==FALLING || squat_switch_state==CLOSE_E)
{
current_state=GEAR_DOWN;
printf("current status:%s\n",state_Machine[current_state].machine_current_state);
printf("Lightis: %s\n",state_Machine[current_state].Light_state);
}
else
{
Checking_before_Take_off();
}
}
void Gear_Raising(void) //Function for Take Gear Raising state
{
current_state=GEAR_RAISING;
printf("Enter your Limit switch status:\n");
fflush(stdout);
scanf("%d %d",&Limit_switch_state,&Pilot_lever_state);
if (Limit_switch_state==CLOSE)
{
current_state=GEAR_UP;
printf("current status:%s\n",state_Machine[current_state].machine_current_state);
printf("Lightis: %s\n",state_Machine[current_state].Light_state);
}
else if (Pilot_lever_state==FALLING)
{
current_state=CHECKING_BEFORE_LANDING;
printf("current status:%s\n",state_Machine[current_state].machine_current_state);
printf("Lightis: %s\n",state_Machine[current_state].Light_state);
}
else
{
Gear_Raising();
}
}
void Gear_UP(void) //Function for Gear up state
{
current_state=GEAR_UP;
printf("Enter your Pilot lever:GEAR_UP\n");
fflush(stdout);
scanf("%d",&Pilot_lever_state);
if (Pilot_lever_state==FALLING)
{
current_state=CHECKING_BEFORE_LANDING;
printf("current status:%s\n",state_Machine[current_state].machine_current_state);
printf("Lightis: %s\n",state_Machine[current_state].Light_state);
}
else
{
Gear_UP();
}
}
void Checking_before_Landing(void) //Function for Checking Before Landing state
{
current_state=CHECKING_BEFORE_LANDING;
printf("Enter your Pilot lever:\n");
fflush(stdout);
scanf("%d",&Pilot_lever_state);
if (Pilot_lever_state==DOWN )
{
current_state=GEAR_LOWERING;
printf("current status:%s\n",state_Machine[current_state].machine_current_state);
printf("Lightis: %s\n",state_Machine[current_state].Light_state);
}
else
{
Checking_before_Landing();
}
}
void Gear_Lowering(void) //Function for Gear lowering state
{
current_state=GEAR_LOWERING;
printf("Enter your Pilot lever and Limit switch state:\n");
fflush(stdout);
scanf("%d %d",&Pilot_lever_state,&Limit_switch_state);
if (Pilot_lever_state==RISING)
{
current_state=CHECKING_BEFORE_TAKE_OFF;
printf("current status:%s\n,",state_Machine[current_state].machine_current_state);
printf("Lightis: %s\n",state_Machine[current_state].Light_state);
}
else if (Limit_switch_state==OPEN)
{
current_state=GEAR_DOWN;
printf("current status:%s\n",state_Machine[current_state].machine_current_state);
printf("Lightis: %s\n",state_Machine[current_state].Light_state);
}
else
{
Gear_Lowering();
}
}
void Initial_State(void) ////Function for initial state state
{
current_state=GEAR_DOWN;
printf("Plane at initial condition:\n");
printf("current status:%s\n",state_Machine[current_state].machine_current_state);
printf("Lightis: %s\n",state_Machine[current_state].Light_state);
}
List of enum parameter:
Gear State State Position
GEAR_DOWN 0
CHECKING_BEFORE_TAKE_OFF 1
RAISING_GEAR 2
GEAR_UP 3
CHECKING_BEFORE_LANDING 4
LOWERING_GEAR 5
Pilot Lever State Position
UP 0
DOWN 1
RISING 2
LOWERING 3
FALLING 4
Limit Switch State Position
OPEN 0
CLOSE 1
Squat Switch State Position
OPEN_E 0
CLOSE _E 1
Direction valve State Position
UP_D 0
DOWN_D 1
OUTPUT ONE:

OUTPUT TWO:
