Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include<stdio.h>
#include<stdlib.h>
void initial_state(); //function decleration
void gear_down();
void checking_before_takeoff();
void raising_gear();
void lowering_gear();
void checking_before_landing();
void gear_up();
enum states //differnt state of the airplane
{
geardown,
checkingbefore_takeoff,
raisinggear,
gearup,
checkingbefore_landing,
loweringgear
};
enum pilot_lever_state //pilot level state raising = 0, falling = 1, up = 2,down = 3
{
raising,
falling,
up,
down
};
enum switch_state //switch state close = 0 and open=1
{
close,
open
};
enum hyd_mech //hydraulic machanism working = 0, malfunction = 1
{
working,
malfunction
};
int squat_switch; //squat switch only used in nose gear
int limit_switch;//limit switch are used in left, right and nose gear
int pilot_lever;
int hyd_machanism;
enum states current_state;
struct airplane
{
char* current_status;
char* light_status;//light status is depending upon the input the light is green or red or off
char* direaction_valve_status;//depending upon the input the direaction valve is up or down
char* hydraulic_pump_status;
char* gas_pressurized;
};
struct airplane state_machine[6]=
{
{"gear down","green","down","enabled"},
{"checkingbefore_takeoff","green","down","enabled"},
{"raising_gear","red","up","enabled"},
{"gear_up","off","null","disabled"},
{"checkingbefore_landing","red","down","enabled"},
{"lowering_gear","green","down","enabled"}
};
void gear_down() //function declaration
{
current_state=geardown;
printf("\n enter the pilot lever state and squat switch state");
scanf("%d %d",&pilot_lever,&squat_switch);
if(pilot_lever==raising && squat_switch==open)//if this condition is satisfying it will go to the next state other wise it is in same state
{
current_state=checkingbefore_takeoff;
printf("\n the current state is :%s",state_machine[current_state].current_status);//print what is current state
printf("\n light status is %s",state_machine[current_state].light_status);//print what is light status
}
else
{
gear_down();
}
}
void checking_before_takeoff()
{
current_state=checkingbefore_takeoff;//current status
printf("\n enter the pilot lever state and the squat switch state ");
scanf("%d %d",&pilot_lever,&squat_switch);
if(pilot_lever == up && squat_switch==open)//if the condition is satisfying it will go to the next state
{
printf("\n the current state is %s",state_machine[current_state].current_status);
printf("\n light status is :%s",state_machine[current_state].light_status);
printf("\n direaction valve state :%s",state_machine[current_state].direaction_valve_status);//it will print the direaction valve status up or down
}
}
void raising_gear()
{
current_state=raisinggear;//now the current state is raisng gear
printf("\n enter the limit switch state");
scanf("%d",&limit_switch);
if(limit_switch==close)//if the switch is closed it will go to the next state
{
current_state==gearup;
printf("\n the current state is :%s",state_machine[current_state].current_status);
printf("\n light states is :%s",state_machine[current_state].light_status);
printf("\n direaction valve state is :%s",state_machine[current_state].direaction_valve_status);
printf("\n hydraaulic_pump_status is :%s",state_machine[current_state].hydraulic_pump_status);
}//if the above condition is not satisfying it will come to this loop
printf("\n enter the pilot lever state");
scanf("%d",&pilot_lever);
if(pilot_lever==falling)//if the pilot lever is falling it will go to the checking before landing
{
current_state=checkingbefore_landing;
printf("\n the current state is :%s",state_machine[current_state].current_status);
printf("\n light states is :%s",state_machine[current_state].light_status);
printf("\n direaction valve state is : %s",state_machine[current_state].direaction_valve_status);
printf("\n hydraulic_pump_status :%s",state_machine[current_state].hydraulic_pump_status);
}
}
void gear_up()
{
current_state=gearup;
printf("\n enter the pilot_valve_state");
scanf("%d",&pilot_lever);
if(pilot_lever==falling)//if pilot lever is falling it will go to the next state
{
current_state=checkingbefore_landing;//current state
printf("\n the current state is :%s",state_machine[current_state].current_status);
printf("\n light states is : %s",state_machine[current_state].light_status);
printf("\n direaction valve status is :%s",state_machine[current_state].direaction_valve_status);
printf("\n hydraulic pump status is :%s",state_machine[current_state].hydraulic_pump_status);
}
}
void checking_before_landing()
{
current_state=checkingbefore_landing;
printf("\n enter pilot lever state");
scanf("%d",&pilot_lever);
if(pilot_lever==falling)//if the pilot lever is falling it will go to the next state
{
current_state=loweringgear;
printf("\n the current state is : %s",state_machine[current_state].current_status);
printf("\n light state is :%s",state_machine[current_state].light_status);
printf("\n direaction valve status is :%s",state_machine[current_state].direaction_valve_status);
printf("\n hydraulic pump status is :%s",state_machine[current_state].hydraulic_pump_status);
}
}
void lowering_gear()
{
current_state=loweringgear;//current status
printf("\n limit switch state");//input from user
scanf("%d",&limit_switch);
if(limit_switch==open)//if the limit switch is open it will go the next state
{
current_state=geardown;//current status
printf("\n the current state is :%s",state_machine[current_state].current_status);
printf("\n light states is :%s",state_machine[current_state].light_status);
printf("\n direaction valve status is :%s",state_machine[current_state].direaction_valve_status);
printf("\n hydraulic pump status is :%s",state_machine[current_state].hydraulic_pump_status);
}//in this above condition is not satisfying it will come in to this loop
printf("\n enter the pilot lever status");
scanf("%d",&pilot_lever);
if(pilot_lever==raising)//pilot lever is falling the current state will change in to checking before take of state
{
current_state=checkingbefore_takeoff;
printf("\n the current state is :%s",state_machine[current_state].current_status);
printf("\n light states is :%s",state_machine[current_state].light_status);
printf("\n direaction valve status is :%s",state_machine[current_state].direaction_valve_status);
printf("\n hydraulic pump status is :%s",state_machine[current_state].hydraulic_pump_status);
}
}
void initial_state()//initial function
{
current_state=geardown;//in initial state the state is geardown
}
void(*airplane[])(void)={gear_down,checking_before_takeoff,raising_gear,gear_up,checking_before_landing,lowering_gear};//array pointer
int main()
{
//initial_state();
while(1)//it will run untill the conditon statisfying
{
airplane[current_state]();
}
return 0;
}