Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include
#include
//function prototypes of states
void GEARDOWN();
void CHECKBEFORETAKEOFF();
void RAISINGGEAR();
void GEARUP();
void CHECKBEFORELNDING();
void LOWERINGGEAR();
//declaring states with enum datatype
typedef enum
{
Gear_Down,//0
Check_Before_Takeoff,//1
Raising_Gear,//2
Gear_Up,//3
Check_Before_Landing,//4
Lowering_Gear,//5
}State_Type;//6
//declaring switch/Button inputs with enum datatype
typedef enum
{
Closed,//0
Opened,//1
}Button;
//declaring PilotLever inputs with enum datatype
typedef enum
{
Falling,//0
Raising,//1
DN,//2
UP,//3
}PilotLever;
//declaring DirectionValve inputs with enum datatype
typedef enum
{
Down,//0
Up,//1
}Direction_Valve;
//instantiation of objects for Enum variables with static volatile datatype
static volatile Button squat_switch;
static volatile Button Limit_switch;
static volatile PilotLever P_L;
static volatile Direction_Valve D_L;
//instantiation of current_state for Enum State_Type
State_Type current_state;
//declaring structure state with members
typedef struct{
char* currentstate;
char* Light;
char* PilotLever;
char* Directionvalue;
}state;
//declaring and initializaton of structure variable that is state_machine.
state state_machine[6]={
{"Gear Down", "Green", "Down", "Enabled"},
{"Checking before takeoff", "Green","Down", "Enabled"},
{"Raising Gear", "Red", "Up", "Enabled"},
{"Gear up", "off", "Null", "Disabled"},
{"Checking before landing", "Red","Down", "Enabled"},
{"Lowering gear", "Green", "Down", "Enabled"}};
//Array of function assigned to a pointer where it has no return type and data type(void).
static void (*state_table[])(void)={GEARDOWN,CHECKBEFORETAKEOFF,RAISINGGEAR,GEARUP,CHECKBEFORELNDING,LOWERINGGEAR};
int main()
{
current_state=Gear_Down;
printf("Currently you are at:");
printf("%s\nLight Status:%s\nPilot_Lever Status:%s\nDirection_Value Status:%s",state_machine[current_state].currentstate,state_machine[current_state].Light,state_machine[current_state].PilotLever,
state_machine[current_state].Directionvalue);
//infinite loop to get the current_state input continously
while(1)
{
state_table[current_state]();
}
}
//below are the function definitions.
//each function will check the condition if it statisfies then nextstate will be assigned to current_state.
//printing respective structure values as outputs for each function.
//if not satisfies the condition then else blcok will be executed where it remains in same state by calling its function.
void GEARDOWN()
{
current_state=Gear_Down;
printf("\nEnter P_L & squat_switch Values:");
scanf("%d%d",&P_L,&squat_switch);
if(P_L == Raising && squat_switch == Opened)
{
current_state=Check_Before_Takeoff;
printf("\nCurrent_State Status:%s\nLight Status:%s\nPilot_Lever Status:%s\nDirection_Value Status:%s",state_machine[current_state].currentstate,state_machine[current_state].Light,state_machine[current_state].PilotLever,
state_machine[current_state].Directionvalue);
}
else
{
GEARDOWN();
}
}
void CHECKBEFORETAKEOFF()
{
current_state=Check_Before_Takeoff;
printf("\nEnter P_L, D_L & squat_switch Values:");
scanf("%d%d%d",&P_L,&D_L,&squat_switch);
if(P_L == UP || D_L == Up)
{
current_state=Raising_Gear;
printf("\nCurrent_State Status:%s\nLight Status:%s\nPilot_Lever Status:%s\nDirection_Value Status:%s",state_machine[current_state].currentstate,state_machine[current_state].Light,state_machine[current_state].PilotLever,
state_machine[current_state].Directionvalue);
}
else if(squat_switch == Closed || P_L == Falling)
{
current_state=Gear_Down;
printf("\nCurrent_State Status:%s\nLight Status:%s\nPilot_Lever Status:%s\nDirection_Value Status:%s",state_machine[current_state].currentstate,state_machine[current_state].Light,state_machine[current_state].PilotLever,
state_machine[current_state].Directionvalue);
}
else
{
CHECKBEFORETAKEOFF();
}
}
void RAISINGGEAR()
{
current_state=Raising_Gear;
printf("\nEnter Limit_switch & P_L Values:");
scanf("%d%d",&Limit_switch,&P_L);
if(Limit_switch == Closed)
{
current_state=Gear_Up;
printf("\nCurrent_State Status:%s\nLight Status:%s\nPilot_Lever Status:%s\nDirection_Value Status:%s",state_machine[current_state].currentstate,state_machine[current_state].Light,state_machine[current_state].PilotLever,
state_machine[current_state].Directionvalue);
}
else if(P_L == Falling)
{
current_state=Check_Before_Landing;
printf("\nCurrent_State Status:%s\nLight Status:%s\nPilot_Lever Status:%s\nDirection_Value Status:%s",state_machine[current_state].currentstate,state_machine[current_state].Light,state_machine[current_state].PilotLever,
state_machine[current_state].Directionvalue);
}
else
{
RAISINGGEAR();
}
}
void GEARUP()
{
current_state=Gear_Up;
printf("\nEnter P_L Value:");
scanf("%d",&P_L);
if(P_L == Falling)
{
current_state=Check_Before_Landing;
printf("\nCurrent_State Status:%s\nLight Status:%s\nPilot_Lever Status:%s\nDirection_Value Status:%s",state_machine[current_state].currentstate,state_machine[current_state].Light,state_machine[current_state].PilotLever,
state_machine[current_state].Directionvalue);
}
else
{
GEARUP();
}
}
void CHECKBEFORELNDING()
{
current_state=Check_Before_Landing;
printf("\nEnter P_L & D_L Values:");
scanf("%d%d",&P_L,&D_L);
if(P_L == DN || D_L == Down)
{
current_state=Lowering_Gear;
printf("\nCurrent_State Status:%s\nLight Status:%s\nPilot_Lever Status:%s\nDirection_Value Status:%s",state_machine[current_state].currentstate,state_machine[current_state].Light,state_machine[current_state].PilotLever,
state_machine[current_state].Directionvalue);
}
else
{
CHECKBEFORELNDING();
}
}
void LOWERINGGEAR()
{
current_state=Lowering_Gear;
printf("\nEnter Limit_switch and P_L Values:");
scanf("%d%d",&Limit_switch,&P_L);
if(Limit_switch == Opened)
{
current_state=Gear_Down;
printf("\nCurrent_State Status:%s\nLight Status:%s\nPilot_Lever Status:%s\nDirection_Value Status:%s",state_machine[current_state].currentstate,state_machine[current_state].Light,state_machine[current_state].PilotLever,
state_machine[current_state].Directionvalue);
}
else if(P_L == Raising)
{
current_state=Check_Before_Takeoff;
printf("\nCurrent_State Status:%s\nLight Status:%s\nPilot_Lever Status:%s\nDirection_Value Status:%s",state_machine[current_state].currentstate,state_machine[current_state].Light,state_machine[current_state].PilotLever,
state_machine[current_state].Directionvalue);
}
else
{
LOWERINGGEAR();
}
}