Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include
#include
void GearDown();
void CheckingBeforeTakeoff();
void RaisingGear();
void GearUp();
void CheckingBeforeLanding();
void LoweringGear();
typedef enum State
{
GEAR_DOWN,
CHECKING_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECKING_BEFORE_LANDING,
LOWERING_GEAR,
}state_type;
typedef enum Switch
{
On,
Off
}switch_status;
typedef enum pilot_lever
{
Raising,
Falling,
Up,
Down,
}pilot_lever;
typedef enum hydraulic_mechanism
{
working,
not_working,
}hydraulic;
static volatile switch_status squat_switch;
static volatile switch_status limit_switch;
static volatile pilot_lever Pilot_Lever;
static volatile hydraulic HYDRAULIC;
static state_type current_state;
typedef struct{
char* currentstate;
char* LIGHT;
char* direction;
char* hydraulic_control;
}state_TABLE;
static void(*Finite_Machine[])(void)={
GearDown,
CheckingBeforeTakeoff,
RaisingGear,
GearUp,
CheckingBeforeLanding,
LoweringGear
};
static state_TABLE state_Machine[6]={
{"GEARDOWN","GREEN","DOWN","ENABLED"},
{"CHECKINGBEFORETAKEOFF","GREEN","DOWN","ENABLED"},
{"RAISINGGEAR","RED","UP","ENABLED"},
{"GEARUP","OFF","NULL","DISABLED"},
{"CHECKINGBEFORELANDING","RED","DOWN","ENABLED"},
{"LOWERINGGEAR","GREEN","DOWN","ENABLED"}
};
int main()
{
current_state=GEAR_DOWN;
while(1)
{
Finite_Machine[current_state]();
}
}
void GearDown()
{
current_state=GEAR_DOWN;
printf("Enter the state of pilot switch and squat switch:");
fflush(stdout);
scanf("%d %d",&Pilot_Lever,&squat_switch);
if (Pilot_Lever==Raising && squat_switch==Off)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("current state is in :%s\n",state_Machine[current_state].currentstate);
printf("light is:%s\n",state_Machine[current_state].LIGHT);
printf("direction valve is:%s\n",state_Machine[current_state].direction);
printf("hydraulic gear is:%s\n",state_Machine[current_state].hydraulic_control);
}
else
{
GearDown();
}
}
void CheckingBeforeTakeoff()
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("Enter the state of pilot lever and hydralic :");
fflush(stdout);
scanf("%d %d",&Pilot_Lever,&HYDRAULIC);
if (Pilot_Lever==Falling && squat_switch==Off)
{
current_state=GEAR_DOWN;
printf("current state is in:%s\n",state_Machine[current_state].currentstate);
printf("light is :%s\n",state_Machine[current_state].LIGHT);
}
if (Pilot_Lever==Raising && HYDRAULIC==working)
{
current_state=RAISING_GEAR;
printf("current state is in :%s\n",state_Machine[current_state].currentstate);
printf("light is :%s\n",state_Machine[current_state].LIGHT);
printf("direction valve is:%s\n",state_Machine[current_state].direction);
printf("hydraulic gear is:%s\n",state_Machine[current_state].hydraulic_control);
}
else
{
CheckingBeforeTakeoff();
}
}
void RaisingGear()
{
current_state=RAISING_GEAR;
printf("Enter the state of pilot lever and limit switch:");
fflush(stdout);
scanf("%d %d",&Pilot_Lever,&limit_switch);
if (Pilot_Lever==Falling)
{
current_state=CHECKING_BEFORE_LANDING;
printf("current state is:%s\n",state_Machine[current_state].currentstate);
printf("light is :%s\n",state_Machine[current_state].LIGHT);
printf("direction valve is:%s\n",state_Machine[current_state].direction);
printf("hydraulic gear is:%s\n",state_Machine[current_state].hydraulic_control);
}
if (limit_switch==Off)
{
current_state=GEAR_UP;
printf("current state is :%s\n",state_Machine[current_state].currentstate);
printf("light is:%s\n",state_Machine[current_state].LIGHT);
printf("direction valve is:%s\n",state_Machine[current_state].direction);
printf("hydraulic gear is:%s\n",state_Machine[current_state].hydraulic_control);
}
else
{
RaisingGear();
}
}
void GearUp()
{
current_state=GEAR_UP;
printf("Enter the state of pilot lever :");
fflush(stdout);
scanf("%d ",&Pilot_Lever);
if(Pilot_Lever==Falling)
{
current_state=CHECKING_BEFORE_LANDING;
printf("current state is:%s\n",state_Machine[current_state].currentstate);
printf("light is:%s\n",state_Machine[current_state].LIGHT);
printf("direction valve is:%s\n",state_Machine[current_state].direction);
printf("hydraulic gear is:%s\n",state_Machine[current_state].hydraulic_control);
}
else
{
GearUp();
}
}
void CheckingBeforeLanding()
{
current_state=CHECKING_BEFORE_LANDING;
printf("Enter the state of pilot lever and hydralic:");
fflush(stdout);
scanf("%d %d",&Pilot_Lever,&HYDRAULIC);
if(Pilot_Lever==Down && HYDRAULIC==not_working)
{
current_state=LOWERING_GEAR;
printf("current state is :%s\n",state_Machine[current_state].currentstate);
printf("light is :%s\n",state_Machine[current_state].LIGHT);
printf("direction valve is :%s\n",state_Machine[current_state].direction);
printf("hydraulic gear is:%s\n",state_Machine[current_state].hydraulic_control);
}
else
{
CheckingBeforeLanding();
}
}
void LoweringGear()
{
current_state=LOWERING_GEAR;
printf("Enter the state of pilot lever and limit switch:");
fflush(stdout);
scanf("%d %d",&Pilot_Lever,&limit_switch);
if (limit_switch==On )
{
current_state=GEAR_DOWN;
printf("current state is:%s\n",state_Machine[current_state].currentstate);
printf("light is :%s\n",state_Machine[current_state].LIGHT);
printf("direction valve is :%s\n",state_Machine[current_state].direction);
printf("hydraulic gear is:%s\n",state_Machine[current_state].hydraulic_control);
}
if (Pilot_Lever==Raising)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("current state is :%s\n",state_Machine[current_state].currentstate);
printf("light is :%s\n",state_Machine[current_state].LIGHT);
printf("direction valve is :%s\n",state_Machine[current_state].direction);
printf("hydraulic gear is:%s\n",state_Machine[current_state].hydraulic_control);
}
else
{
LoweringGear();
}
}