Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Solution:-
#include<stdio.h> // Preprocessor Declaration
//Global Function Declaration
void Default_State();
void Gear_Down_State();
void Checking_Before_Takeoff_State();
void Raising_Gear_State();
void Gear_Up_state();
void Checking_Before_Landing_state();
void Lowering_Gear_state();
typedef enum state{
Gear_Down,
Checking_Before_Takeoff,
Raising_Gear,
Gear_Up,
Checking_Before_Landing,
Lowering_Gear,
}Plane_state; // In Airplane has 6 state at different condition
typedef enum LSwitch{
Limit_Switch_Open,
Limit_Switch_Closed,
} Limit_switch_state; // Define enumerator with typedef for the Limit switch Condition
typedef enum SSwitch{
Squate_Switch_Open,
Squate_Switch_Closed,
}Squate_switch_state; // Define enumerator with typedef for the Squate switch Condition
typedef enum PLever{
Gear_Down_Falling,
Gear_Up_Raising,
}Pilot_lever; // Define enumerator with typedef for the Pilot lever Condition
typedef enum DValue{
Direction_Value_Up,
Direction_Value_Down,
}Direction_value_status; // Define enumerator with typedef for the Direction Value Condition
typedef enum HState{
Hydraulic_not_working,
Hydraulic_working,
}Hydraulic_status; // Define enumerator with typedef for the Hydraulic Condition
static volatile Limit_switch_state Limit_Switch_State;
static volatile Squate_switch_state Squate_Switch_State;
static volatile Pilot_lever Pilot_Lever;
static volatile Direction_value_status Direction_Value_Status;
Plane_state current_state;
typedef struct
{
char* Current_State_Indication;
char* Gear_Light_Indication;
int time;
char* Landing_Gear_Control;
char* Direction_Value_Status;
char* Gas_pressurised_Spring[2];
char* Hydrulic_Control[2];
}State_Table;
// State_Table State machine declare
State_Table StateMachine[6]=
{
{"Gear_Down","Green",0,"Enabled","Direction_Value_Down",{"Disabled","Disabled"},{"Enabled","Enabled"} },
{"Checking_Before_Takeoff","Green",4,"Enabled","Direction_Value_Down",{"Disabled","Disabled"},{"Enabled","Enabled"}},
{"Raising_Gear","Red",4,"Enabled","Direction_Value_Up",{"Disabled","Disabled"},{"Enabled","Enabled"}},
{"Gear_Up","Off",0,"Disabled","Direction_Value_No_Change",{"Enabled","Enabled"},{"Disabled","Disabled"}},
{"Checking_Before_Landing","Red",3,"Enabled","Direction_Value_Down",{"Disabled","Disabled"},{"Enabled","Enabled"}},
{"Lowering_Gear","Green",0,"Enabled","Direction_Value_Down",{"Disabled","Disabled"},{"Enabled","Enabled"}}
};
// Default Function or Initial condition function declared
void Default_State()
{
current_state = Gear_Down;
printf("This Default or Initial condition where State machine start \n ");
} // Default Function Declare
// Gear Down State Function declared
void Gear_Down_State()
{
current_state = Gear_Down;
printf("Enter the status of Pilot's lever and Squat switch position : for Raising 1,Open 0 \n ");
fflush(stdout);
scanf("%d %d", &Pilot_Lever,&Squate_Switch_State); // Taking input from User
if(Pilot_Lever == Gear_Up_Raising && Squate_Switch_State == Squate_Switch_Open)
{
current_state = Checking_Before_Takeoff;
printf("Current State of Airplane is : %s \n ",StateMachine[current_state].Current_State_Indication);
fflush(stdout);
printf("Lever Gear_Light_Indication is : %s \n",StateMachine[current_state].Gear_Light_Indication );
fflush(stdout);
printf(" Direction_Value_Status is : %s \n",StateMachine[current_state].Direction_Value_Status );
fflush(stdout);
}
else
{
printf("Again Enter value for state in Gear down state ");
Gear_Down_State();
}
}
// Checking before takeoff state function declared
void Checking_Before_Takeoff_State()
{
current_state = Checking_Before_Takeoff;
printf("Enter the status of Pilot's lever, Squat switch position, Direction Value \n ");
fflush(stdout);
scanf("%d %d %d",&Pilot_Lever,&Squate_Switch_State,&Direction_Value_Status);
if(Pilot_Lever == Gear_Up_Raising && Squate_Switch_State == Squate_Switch_Open && Direction_Value_Status == Direction_Value_Up )
{
current_state = Raising_Gear;
printf("Current State of Airplane is : %s \n ",StateMachine[current_state].Current_State_Indication);
fflush(stdout);
printf("Lever Gear_Light_Indication is : %s \n",StateMachine[current_state].Gear_Light_Indication );
fflush(stdout);
printf("Direction value status is : %s \n",StateMachine[current_state].Direction_Value_Status);
fflush(stdout);
printf("Time delay is : %d \n",StateMachine[current_state].time);
fflush(stdout);
}
else if(Pilot_Lever == Gear_Down_Falling && Squate_Switch_State == Squate_Switch_Closed && Direction_Value_Status == Direction_Value_Down )
{
current_state = Gear_Down;
printf("Current State of Airplane is : %s \n ",StateMachine[current_state].Current_State_Indication);
fflush(stdout);
printf("Lever Gear_Light_Indication is : %s \n",StateMachine[current_state].Gear_Light_Indication );
fflush(stdout);
printf("Direction value status is : %s \n",StateMachine[current_state].Direction_Value_Status);
fflush(stdout);
}
else
{
printf("Again Enter the value in Checking before take off state \n");
Checking_Before_Takeoff_State();
}
}
// Raising Gear state function declared
void Raising_Gear_State()
{
current_state = Raising_Gear;
printf("Enter the status of Pilot's lever and Limit switch position- for rising 1 & falling 0, Open 0 & Closed 1 \n");
fflush(stdout);
scanf("%d %d",&Pilot_Lever,&Limit_Switch_State);
if(Pilot_Lever == Gear_Up_Raising && Limit_Switch_State == Limit_Switch_Closed)
{
current_state = Gear_Up;
printf("Current State of Airplane is : %s \n ",StateMachine[current_state].Current_State_Indication);
fflush(stdout);
printf("Lever Gear_Light_Indication is : %s \n",StateMachine[current_state].Gear_Light_Indication );
fflush(stdout);
printf("Gear Control status is : %s \n",StateMachine[current_state].Landing_Gear_Control);
fflush(stdout);
printf("Gas pressure spring status is : %s \n",StateMachine[current_state].Gas_pressurised_Spring[current_state]);
fflush(stdout);
printf("Hydraulic Control status is : %s \n",StateMachine[current_state].Hydrulic_Control[current_state]);
fflush(stdout);
}
else if(Pilot_Lever == Gear_Down_Falling && Limit_Switch_State == Limit_Switch_Open)
{
current_state = Checking_Before_Landing;
printf("Current State of Airplane is : %s \n ",StateMachine[current_state].Current_State_Indication);
fflush(stdout);
printf("Lever Gear_Light_Indication is : %s \n",StateMachine[current_state].Gear_Light_Indication );
fflush(stdout);
printf("Direction value status is : %s \n",StateMachine[current_state].Direction_Value_Status);
fflush(stdout);
printf("Gas pressure spring status is : %s \n",StateMachine[current_state].Gas_pressurised_Spring[current_state]);
fflush(stdout);
printf("Hydraulic Control status is : %s \n",StateMachine[current_state].Hydrulic_Control[current_state]);
fflush(stdout);
}
else
{
printf("Again Enter value in Raising Gear state \n");
Raising_Gear_State();
}
}
// Gear up state function declared
void Gear_Up_state()
{
current_state = Gear_Up;
printf("Enter the status of Pilot's lever:- for rising 1 & falling \n");
fflush(stdout);
scanf("%d",&Pilot_Lever);
if(Pilot_Lever == Gear_Down_Falling)
{
current_state = Checking_Before_Landing;
printf("Current State of Airplane is : %s \n ",StateMachine[current_state].Current_State_Indication);
fflush(stdout);
printf("Lever Gear_Light_Indication is : %s \n",StateMachine[current_state].Gear_Light_Indication );
fflush(stdout);
printf("Gear Control status is : %s \n",StateMachine[current_state].Landing_Gear_Control);
fflush(stdout);
}
else
{
printf("Again Enter value in GearUp state state \n");
Gear_Up_state();
}
}
// Cheching before landing State function declared
void Checking_Before_Landing_state()
{
current_state = Checking_Before_Landing;
printf("Enter the status of Pilot's lever,Direction Value - for rising 1 & falling 0, up 0 & down 1 \n");
fflush(stdout);
scanf("%d %d",&Pilot_Lever,&Direction_Value_Status);
if(Pilot_Lever == Gear_Down_Falling && Direction_Value_Status == Direction_Value_Down )
{
current_state = Lowering_Gear;
printf("Current State of Airplane is : %s \n ",StateMachine[current_state].Current_State_Indication);
fflush(stdout);
printf("Lever Gear_Light_Indication is : %s \n",StateMachine[current_state].Gear_Light_Indication );
fflush(stdout);
printf("Direction value status is : %s \n",StateMachine[current_state].Direction_Value_Status);
fflush(stdout);
}
else
{
printf("Again Enter value in Checking before landing state \n");
Checking_Before_Landing_state();
}
}
// Lowering Gear State Declared function
void Lowering_Gear_state()
{
current_state = Lowering_Gear;
printf("Enter the status of Pilot's lever,Direction Value - for rising 1 & falling 0, up 0 & down 1 \n");
fflush(stdout);
scanf("%d %d", &Pilot_Lever, &Limit_Switch_State);
if(Pilot_Lever == Gear_Down_Falling && Limit_Switch_State == Limit_Switch_Open )
{
current_state = Gear_Down;
printf("Current State of Airplane is : %s \n ",StateMachine[current_state].Current_State_Indication);
fflush(stdout);
printf("Lever Gear_Light_Indication is : %s \n",StateMachine[current_state].Gear_Light_Indication );
fflush(stdout);
printf("Direction value status is : %s \n",StateMachine[current_state].Direction_Value_Status);
fflush(stdout);
}
else if(Pilot_Lever == Gear_Up_Raising && Limit_Switch_State == Limit_Switch_Closed )
{
current_state = Checking_Before_Takeoff;
printf("Current State of Airplane is : %s \n ",StateMachine[current_state].Current_State_Indication);
fflush(stdout);
printf("Lever Gear_Light_Indication is : %s \n",StateMachine[current_state].Gear_Light_Indication );
fflush(stdout);
printf("Direction value status is : %s \n",StateMachine[current_state].Direction_Value_Status);
fflush(stdout);
}
{
printf("Again Enter value Lowering Gear State \n");
Lowering_Gear_state();
}
}
static void(*statetable[])(void) = { Gear_Down_State,Checking_Before_Takeoff_State,Raising_Gear_State,Gear_Up_state,Checking_Before_Landing_state,Lowering_Gear_state};
// Main Function Declaration
int main()
{
Default_State(); // Calling the initial function or default starting function
while(1)
{
statetable[current_state](); // State Table Current value calling
}
}
Screenshots:-











Result:-
