Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include<stdio.h>
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
{
functioning,
malfunctioning
}hydraulic_mechanism;
typedef struct
{
char* current_state_indication;
char* light_status;
char* direction_valve;
char* hydraulic_pump;
char* gas_pressurized_spring_system[2];
}State_Table;
static State_Table;
State_Machine[6]=
{
{"GearDown","Green","Down","Enabled",{Null,Null}},
{"CheckingBeforeTakeOff","Green","Down","Enabled",{Null,Null}},
{"RaisingGear","Red","Up","Enabled",{"Disabled","Enabled"}},
{"GearUp","Off","Null","Disabled",{Null,Null}},
{"CheckingBeforeLanding","Red","Down","Enabled",{Null,Null}},
{"LoweringGear","Green","Down","Enabled",{"Disabled","Enabled"}}
};
//accessing functions by an array of functions
Static void(*State_Table[])(void) = {Gear_Down, Checking_Before_TakeOff, Raising_Gear, Gear_Up, Checking_Before_Landing, Lowering_Gear};
int main()
{
//system initialized
Init_State_Machine();
While(1)
{
State_Table[current_State]();
}
//Geardown
void Gear_Down()
{
current_state=Gear_Down;
printf("Enter the status of pilot's lever and squat switch");
fflush(stdout);
scanf("%d%d",&pilot_lever,&squat_switch);
if(pilot_lever==raising && squat_switch==On)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("current state:%s\n",State_Machine[current_state].current_state_indication);
printf("light is in :%s\n",State_Machine[current_state].light_status);
}
else
{
GearDown();
}
}
//checkingbeforetakeoff
void Checking_Before_Takeoff()
{
current_state=Checking_before_Takeoff;
printf("Enter the status of pilot's lever and squat switch and hydraulic mechanism");
fflush(stdout);
scanf("%d%d",&pilot_lever,&squat_switch,&hydraulic_mechanism);
if(pilot_lever==falling && squat_switch==Off)
{
GearDown;
}
else if(pilot_lever==raising && squat_switch==On && hydraulic_mechanism==functioning)
{
current_state=RAISING_GEAR;
printf("current state:%s\n",State_Machine[current_state].current_state_indication);
printf("light state:%s\n",state_Machine[current_state].light_status);
printf("direction valve state:%s\n",State_Machine[current_state].direction_valve_status);
}
else if(pilot_lever==raising && squat_switch==On && hydraulic_mechanism==malfunctioning)
{
current_state=RAISING_GEAR;
printf("current state:%s\n",State_Machine[current_state].current_state_indication);
printf("light state:%s\n",state_Machine[current_state].light_status);
printf("direction valve state:%s\n",State_Machine[current_state].direction_valve_status);
printf("Gas presurrized spring system:%s\n",State_Machine[current_state].Gas_presurrized_spring_system);
}
}
//RaisingGear
void Raising_Gear()
{
current_state=RAISING_GEAR;
printf("Enter the status of pilot's lever and limit switch");
fflush(stdout);
scanf("%d%d",&pilot_lever,&limit_switch);
if(pilot_lever==falling && limit_switch==On)
{
current_state=CHECK_BEFORE_LANDING;
printf("current state:%s\n",State_Machine[current_state].current_state_indication);
printf("Light is in:%s\n",State_Machine[current_state].light_status);
printf("Hydraulic mechanism:%s\n",State_Machine[current_state].hydraulic_mechanism);
}
else if(pilot_lever=raising && limit_switch=Off)
{
current_state=GEAR_UP;
printf("current state:%s\n",State_Machine[current_state].current_state_indication);
printf("Light is in:%s\n",State_Machine[current_state].light_status);
printf(" Hydraulic mechanism:%s\n",State_Machine[current_state].hydraulic_mechanism);
}
else
{
RaisingGear();
}
}
//GearUp
void Gear_Up()
{
current_state=GEAR_UP;
printf("Enter the status of pilot's lever");
fflush(stdout);
scanf("%d",&pilot_lever);
if(pilot_lever=falling)
{
current_state=CHECKING_BEFORE_LANDING;
printf("current state:%s\n",State_Machine[current_state].current_state_indication);
printf("Light is in:%s\n",State_Machine[current_state].light_status);
printf("hydraulic mechanism:%s\n",State_Machine[current_state].hydraulic_mechanism);
}
else
{
GearUp();
}
}
//checkingbeforelanding
void Checking_Before_Landing()
{
current_state=CHECKING_BEFORE_LANDING;
printf("Enter the status of pilot's lever and hydraulic mechanism");
fflush(stdout);
scanf("%d%d",&pilot_lever,&hyrdaulic_mechanism);
if(pilot_lever==falling && hydraulic_mechanism==functioning)
{
current_state=LOWERING_GEAR;
printf("current state:%s\n",State_Machine[current_state].current_state_indication);
printf("light is in:%s\n",State_Machine[current_state].light_status);
printf("status of Direction valve:%s\n",State_Machine[current_state].direction_valve_status);
}
else
{
CheckingBeforeLanding();
}
}
//LoweringGear
void Lowering_Gear()
{
current_status=LOWERING_GEAR;
printf("Enter the status of pilot's lever and limit switch");
fflush(stdout);
scanf("%d%d",&pilot_lever,&limit_switch);
if(pilot_lever==raising && limit_switch==On)
{
current_state=CHECK_BEFORE_TAKEOFF;
printf("current state:%s\n",State_Machine[current_state].current_state_indication);
printf("Light is in:%s\n",State_Machine[current_state].light_status);
}
else if(pilot_lever==raising && limit_switch==Off)
{
current_state=GEAR_DOWN;
printf("current state:%s\n",State_Machine[current_state].current_state_indication);
printf("Light is in:%s\n",State_Machine[current_state].light_status);
printf("hydraulic mechanism:%s\n",State_Machine[current_state].hydraulic_mechanism);
}
else
{
LoweringGear();
}
}
void Init_state_machine()
{
current_state=GEAR_DOWN;
printf("The finite state machine is Initialized and the program is in""GearDown state and light indication is:%s\n",State_Machine[current_state].Light_status);
}
}