Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
1)program
#include<stdio.h>
void initialize_state_machine();
void geardown();
void checkingbeforetakeoff();
void raisinggear();
void gearup();
void checkbeforelanding();
void loweringgear();
static void(*statetable[])(void) = {geardown , checkingbeforetakeoff , raisinggear , gearup , checkbeforelanding , loweringgear};
typedef enum state
{
gear_down,
checking_before_takeoff,
raising_gear,
gear_up,
check_before_landing,
lowering_gear,
}State_type;
typedef enum Switch
{
on,
off
}
Switch_status;
typedef enum pilot_lever
{
raising,
falling
}
pilot_lever;
typedef enum hydraulic_mechanism
{
working,
not_working
}
hydraulic_mechanism;
static volatile Switch_status squat_Switch;
static volatile Switch_status limit_Switch;
static volatile pilot_lever pl;
static volatile hydraulic_mechanism hm;
State_type current_state;
typedef struct
{
char* current_state_indication;
char* light;
char* direction_valve_status;
char* landing_gear_hydraulic_control;
char* gas_pressurised_spring_system_status[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",{NULL,NULL}},
{"gearup","off","NULL","disabled",{NULL,NULL}},
{"checkbeforelanding","red","down","enabled",{"disabled","enabled"}}
};
int main()
{
initiate_state_machine();
while(1)
{
statetable[current_state]();
}
}
void geardown(void)
{
current_state=gear_down;
printf("enter the status of pilot lever and squat switch");
fflush(stdout);
scanf("%d %d",&pl,&squat_Switch);
if(pl==raising && squat_Switch==on)
{
current_state=checking_before_takeoff;
printf("current state is %s/n" , state_machine[current_state].current_state_indication);
printf("light is %s/n",state_machine[current_state].light);
}
else
{
geardown();
}
}
void checkingbeforetakeoff(void)
{
current_state=checking_before_takeoff;
printf("enter the status of pilot lever and squat switch and hydraulic mechanism");
fflush(stdout);
scanf("%d %d",&pl,&squat_Switch,&hm);
if(pl==falling&&squat_Switch==off)
{
geardown();
}
else if(pl==raising && squat_Switch==on && hm==working)
{
current_state=raising_gear;
printf("current state is %s/n",state_machine[current_state].current_state_indication);
printf("light is %s/n",state_machine[current_state].light);
printf("status of direction valve is %s/n",state_machine[current_state].direction_valve_status);
}
else if(pl==raising && squat_Switch==on && hm==working)
{
current_state=raising_gear;
printf("current state is %s/n",state_machine[current_state].current_state_indication);
printf("light is %s/n",state_machine[current_state].light);
printf("status of direction valve is %s/n",state_machine[current_state].direction_valve_status);
printf("status of gas pressurized spring system is %s/n", state_machine[current_state].gas_pressurised_spring_system_status);
}
}
void raisinggear(void)
{
current_state=raising_gear;
printf("enter the status of pilot lever and limit switch");
fflush(stdout);
scanf("%d %d , &pl,&limit_switch");
if(pl==falling && limit_Switch==on)
{
current_state=check_before_landing;
printf("current state is %s/n",state_machine[current_state].current_state_indication);
printf("light is %s/n",state_machine[current_state].light);
printf("landing gear hydraulic control is %s/n",state_machine[current_state].landing_gear_hydraulic_control);
}
else if(pl==raising && limit_Switch==off)
{
current_state=gear_up;
printf("current state is %s/n",state_machine[current_state].light);
printf("landing gear hydraulic control is %s/n",state_machine[current_state].landing_gear_hydraulic_control);
}
else
{
raisinggear();
}
}
void gearup(void)
{
current_state=gear_up;
printf("enter the status of pilot lever");
fflush(stdout);
scanf("%d",&pl);
if(pl==falling)
{
current_state=check_before_landing;
printf("current state is %s/n",state_machine[current_state].current_state_indication);
printf("light is %s/n",state_machine[current_state].light);
printf("landing gear hydraulic control is %s/n",state_machine[current_state].landing_gear_hydraulic_control);
}
else
{
gearup();
}
}
void checkbeforelanding(void)
{
current_state=check_before_landing;
printf("enter the status of pilot lever and hydraulic mechanism status ");
fflush(stdout);
scanf("%d %d,&pl,&hm");
if(pl==falling && hm==working)
{
current_state=lowering_gear;
printf("current state is %s/n",state_machine[current_state].current_state_indication);
printf("light is %s/n",state_machine[current_state].light);
printf("status of direction valve is %s/n",state_machine[current_state].direction_valve_status);
}
else if(pl==falling && hm==not_working)
{
current_state=lowering_gear;
printf("current state is %s/n",state_machine[current_state].current_state_indication);
printf("light is %s/n",state_machine[current_state].light);
printf("status of direction valve is %s/n",state_machine[current_state].direction_valve_status);
printf("status of gas pressurised spring system is %s/n",state_machine[current_state].gas_pressurised_spring_system_status);
}
else if(pl==(raising))
{
checkbeforelanding();
}
}
void loweringgear(void)
{
current_state=lowering_gear;
printf("enter the status of pilot liver and limit switch");
fflush(stdout);
scanf("%d %d ,&pl,&limit_switch");
if(pl==falling && limit_Switch==on)
{
current_state=checking_before_takeoff;
printf("current_state is %s/n",state_machine[current_state].current_state_indication);
printf("light is %s/n",state_machine[current_state].light);
}
else if(pl==raising && limit_Switch==off)
{
current_state=gear_down;
printf("current_state is %s/n",state_machine[current_state].current_state_indication);
printf("light is %s/n",state_machine[current_state].light);
printf("landing gear hydraulic control is %s/n",state_machine[current_state].landing_gear_hydraulic_control);
}
if(limit_Switch==off)
{
loweringgear();
}
}
void initiate_state_machine()
{
current_state==gear_down;
printf("the finite state machine is initialized and current the program is in geardown state & light is %s/n",state_machine[current_state].light);
}