Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include<stdio.h> //headerfile declaration
//states declaration
void Gear_Down();
void checking_before_Take_OFF();
void Raising_Gear();
void Gear_Up();
void checking_before_Landing();
void lowering_Gear();
// function pointer to the function to call in each state
static void(*State_table[])(void)={Gear_Down, checking_before_Take_OFF, Raising_Gear, Gear_Up, checking_before_Landing, lowering_Gear};
//states are assuming as structure for calling of function
typedef enum state
{
GEAR_DOWN,
CHECKING_BEFORE_TAKE_OFF,
RAISING_GEAR,
GEAR_UP,
CHECKING_BEFORE_LANDING,
LOWERING_GEAR
}type_state;
type_state current_state;
// userdefined datatype to assume the values to call
typedef enum swich
{
on,
off
}switch_status;
typedef enum pilot_lever1
{
raising ,
falling ,
Up ,
Down
}pilot_lever1;
typedef enum direction_valve_status1
{
null,
down,
up
}direction_valve_status1;
//userdefined data type for state table variables to assume values call
typedef struct
{
char* current_state_indication;
char* light;
char* direction_valve_status;
char* Gas_pressurized_spring_system_status[2];
char* landing_Gear_hydraulic_control;
}state_table;
static volatile switch_status squat_switch;
static volatile switch_status limit_switch;
static volatile pilot_lever1 pilot_lever;
static volatile direction_valve_status1 direction_valve_status;
//assuming the state table based on the state diagram
static state_table state_machine[6]=
{
{"Gear_Down","Green","Down","Enabled",{NULL,NULL}},
{"checking_before_Take_OFF","Green","Down","Enabled",{NULL,NULL}},
{"Raising_Gear","Red","Up","Enabled",{"Disabled","Enabled"}},
{"Gear_Up","off","null","Disabled",{NULL,NULL}},
{"checking_before_Landing","Red","Down","Enabled",{NULL,NULL}},
{"lowering_Gear","Green","Down","Enabled",{"Disabled","Enabled"}}
};
// Initialiging the state machine for the first stage where aeroplane is in grounded
void initial_state_machine()
{
current_state=GEAR_DOWN;
printf("\n current_state of finite state machine is in Gear down state and Light is %s",state_machine[current_state].light);
}
//state of aeroplane gears are down position and gears are locked
void Gear_Down()
{
current_state=GEAR_DOWN;
// the values are enterd to go next state by assuming the pilot lever and switch conditions
printf("\n 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_TAKE_OFF;
printf("\n current_state is %s",state_machine[current_state].current_state_indication);
printf("\n light is %s",state_machine[current_state].light);
}
else
{
Gear_Down();
}
}
// aeroplane is in checking to take off state
void checking_before_Take_OFF()
{
current_state=CHECKING_BEFORE_TAKE_OFF;
// the values are enterd to go next state by assuming the pilot lever and switch conditions and direction of valve
printf("\n enter the status of pilot's lever, squat switch and direction valve status");
fflush(stdout);
scanf("%d %d %d",&pilot_lever,&squat_switch,&direction_valve_status);
if(pilot_lever==falling && squat_switch==off)
{
Gear_Down();
}
else if(pilot_lever==Up && direction_valve_status==up)
{
current_state=RAISING_GEAR;
printf("\n current_state is %s",state_machine[current_state].current_state_indication);
printf("\n light is %s",state_machine[current_state].light);
printf("\n direction valve status is %s",state_machine[current_state].direction_valve_status);
}
else
{
checking_before_Take_OFF();
}
}
// aeroplane is in raising gear state wher gears of the aeroplane gear are going up
void Raising_Gear()
{
current_state=RAISING_GEAR;
// the values are enterd to go next state by assuming the pilot lever and switch conditions
printf("\n enter the status of pilot's lever and Limit switch:");
fflush(stdout);
if(pilot_lever==falling && limit_switch==on)
{
current_state=CHECKING_BEFORE_LANDING;
printf("\n current_state is %s",state_machine[current_state].current_state_indication);
printf("\n light is %s",state_machine[current_state].light);
printf("\n Landing Gear Hyraulic Control is %s",state_machine[current_state].landing_Gear_hydraulic_control);
}
else if(pilot_lever==raising && limit_switch==off)
{
/* Once all 3 legs are up, go to the GEAR_UP state.*/
current_state=GEAR_UP;
printf("\n current_state is %s",state_machine[current_state].current_state_indication);
printf("\n light is %s",state_machine[current_state].light);
printf("\n Landing Gear Hyraulic Control is %s",state_machine[current_state].landing_Gear_hydraulic_control);
}
else
{
Raising_Gear();
}
}
// aeroplane is in gear up state where aeroplane is bringing gear up
void Gear_Up()
{
current_state=GEAR_UP;
// the values are enterd to go next state by assuming the pilot lever
printf("\n enter the status of pilot's lever ");
fflush(stdout);
scanf("%d",&pilot_lever);
if(pilot_lever==falling)
{
current_state=CHECKING_BEFORE_LANDING;
printf("\n current_state is %s",state_machine[current_state].current_state_indication);
printf("\n light is %s",state_machine[current_state].light);
printf("\n Landing Gear Hyraulic Control is %s",state_machine[current_state].landing_Gear_hydraulic_control);
}
else
{
Gear_Up();
}
}
// aeroplane is in checking before landing state where aeroplane is ready to land
void checking_before_Landing()
{
current_state=CHECKING_BEFORE_LANDING;
// the values are enterd to go next state by assuming the pilot lever and switch conditions and direction of valve
printf("\n enter the status of pilot's lever and direction_valve_status");
fflush(stdout);
scanf("%d %d",&pilot_lever,&direction_valve_status);
if(pilot_lever==Down && direction_valve_status==down)
{
current_state=LOWERING_GEAR;
printf("\n current_state is %s",state_machine[current_state].current_state_indication);
printf("\n light is %s",state_machine[current_state].light);
printf("\n direction valve status is %s",state_machine[current_state].direction_valve_status);
}
else
{
checking_before_Landing();
}
}
// aeroplane is in lowering gear state where aeroplane gear is coming down
void lowering_Gear()
{
current_state=LOWERING_GEAR;
// the values are enterd to go next state by assuming the pilot lever and switch conditions
printf("\n 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)
{
/*There is a possibility that the plane is about to take off and an emergency comes where pilot has to abort the take off When taking off,to ensure that its right time to raising the gear that squat switch is open */
current_state=CHECKING_BEFORE_TAKE_OFF;
printf("\n current_state is %s",state_machine[current_state].current_state_indication);
printf("\n light is %s",state_machine[current_state].light);
}
else if(pilot_lever==raising && limit_switch==off)
{
current_state=GEAR_DOWN;
printf("\n current_state is %s",state_machine[current_state].current_state_indication);
printf("\n light is %s",state_machine[current_state].light);
printf("\nLandingGearHyraulicControlis%s",state_machine[current_state].landing_Gear_hydraulic_control);
}
else
{
lowering_Gear();
}
}
int main()
{
initial_state_machine();
while(1)
{
State_table[current_state]();
}
}