Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
/* FSM: Moore State Machine(Input & Output are Independent In This Method) */
#include<stdio.h> //Preprocessor
//Declare Function as Void (With No Argument) No Return
void Initiate_State_Machine();
void GearDown();
void CheckingBeforeTakeOff();
void RaisingGear();
void GearUp();
void CheckingBeforeLanding();
void LoweringGear();
//Creating Enum type Structure States
typedef enum State
{
Gear_Down,
Checking_Before_TakeOff,
Raising_Gear,
Gear_Up,
Checking_Before_Landing,
Lowering_Gear,
}State_Type;
typedef enum Switch1
{
Opened,//0
Closed,//1
}Switch_Status1;
typedef enum Switch2
{
Open,//0
Close,//1
}Switch_Status2;
typedef enum Pilot_Lever
{
Raising,
Falling
}Pilot_Lever;
typedef enum Hydraulic_Mechanism
{
Raise,
Lower
}Hydraulic_Mechanism;
//Create Object Name For The Each Enum Structure as Static Volatile
static volatile Switch_Status1 squat_switch;
static volatile Switch_Status2 limit_switch;
static volatile Pilot_Lever pilot_lever;
static volatile Hydraulic_Mechanism hydraulic_pressure;
State_Type current_state; //Create a Variable Using Enum(Current State)
//Creating Structure For Different Condition In Landing Gear Control
typedef struct
{
char* current_state_indication;
char* light;
char* direction_valve_status;
char* landing_gear_hydraulic_Pump_Control;
char* Excerting_Gas_pressure_System[2];
}landing_Gear_state;
//Create State Table Using array With Multiple Dimension For an Conditional Output
static landing_Gear_state State_Machine[6] =
{
{"GearDown","Green","Down","Enabled",{NULL,NULL}},
{"CheckingBeforeTakeOff","Green","Down","Enabled",{NULL,NULL}},
{"RaisingGear","Red","Up","Enabled",{"Disabled","Enabled"}},
{"GearUp","Off","NULL","Disabled",{NULL,NULL}},
{"CheckingBeforeLanding","Red","Down","Enabled",{NULL,NULL}},
{"LoweringGear","Green","Down","Enabled",{"Disabled","Enabled"}}
};
//Entering The Function Into the array of 6 Element to access the Function Based Requirement in The name of StateTable
static void(*stateTable[])(void)={GearDown, CheckingBeforeTakeOff, RaisingGear, GearUp, CheckingBeforeLanding, LoweringGear};
int main()//Main Class
{
Initiate_State_Machine(); //Function Calling With Initiate The Initial state
while(1) //Infinite Loop
{
stateTable[current_state](); //Function Call StateTable With The argument of Current State
}
}
//All of Function Definition Represent The State Diagram with Extension and Retraction of Airplan's Landing Gear
void GearDown(void)
{
current_state = Gear_Down;
printf("Enter Pilot's Lever Status and Squat Switch Status: ");
fflush(stdout);
scanf("%d %d",&pilot_lever,&squat_switch);
if(pilot_lever == Raising && squat_switch == Opened)
{
current_state = Checking_Before_TakeOff;
printf("current state: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is : %s\n",State_Machine[current_state].light);
}
else
{
GearDown();//User Get In Inputs Not Properly The Function Call Itself
}
}
void CheckingBeforeTakeOff(void)
{
current_state = Checking_Before_TakeOff;
printf("Enter the Status of Pilot's Lever and Squat Switch and hydraulic mechanism: ");
fflush(stdout);
scanf("%d %d %d",&pilot_lever,&squat_switch,&hydraulic_pressure);
if(pilot_lever == Falling && squat_switch == Closed)
{
GearDown();
}
else if(pilot_lever == Raising && squat_switch == Opened && hydraulic_pressure == Raise)
{
current_state = Raising_Gear;
printf("current state: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is : %s\n",State_Machine[current_state].light);
printf("Status of Direction Valve : %s\n",State_Machine[current_state].direction_valve_status);
printf("Status of gas pressurized Spring System: %s\n",State_Machine[current_state].Excerting_Gas_pressure_System[1]);
}
else if(pilot_lever == Raising && squat_switch == Opened && hydraulic_pressure == Lower)
{
current_state = Raising_Gear;
printf("current state: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is : %s\n",State_Machine[current_state].light);
printf("Status of Direction Valve : %s\n",State_Machine[current_state].direction_valve_status);
printf("Status of gas pressurized Spring System: %s\n",State_Machine[current_state].Excerting_Gas_pressure_System[0]);
}
}
void RaisingGear(void)
{
current_state = Raising_Gear;
printf("Enter the Status of Pilot's Lever and Limit Switch: ");
fflush(stdout);
scanf("%d %d",&pilot_lever,&limit_switch);
if(pilot_lever == Falling && limit_switch == Open)
{
current_state = Checking_Before_Landing;
printf("current state: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is : %s\n",State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control : %s\n",State_Machine[current_state].landing_gear_hydraulic_Pump_Control);
}
else if(pilot_lever == Raising && limit_switch == Close)
{
current_state = Gear_Up;
printf("current state: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is : %s\n",State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control : %s\n",State_Machine[current_state].landing_gear_hydraulic_Pump_Control);
}
else
{
RaisingGear();
}
}
void GearUp(void)
{
current_state = Gear_Up;
printf("Enter Pilot's Lever Status: ");
fflush(stdout);
scanf("%d",&pilot_lever);
if(pilot_lever == Falling)
{
current_state = Checking_Before_Landing;
printf("current state: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is : %s\n",State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control : %s\n",State_Machine[current_state].landing_gear_hydraulic_Pump_Control);
}
else
{
GearUp();
}
}
void CheckingBeforeLanding(void)
{
current_state = Checking_Before_Landing;
printf("Enter the Status of Pilot's Lever and hydraulic mechanism: ");
fflush(stdout);
scanf("%d %d",&pilot_lever,&hydraulic_pressure);
if(pilot_lever == Falling && hydraulic_pressure == Raise)
{
current_state = Lowering_Gear;
printf("current state: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is : %s\n",State_Machine[current_state].light);
printf("Status of Direction Valve : %s\n",State_Machine[current_state].direction_valve_status);
printf("Status of gas pressurized Spring System: %s\n",State_Machine[current_state].Excerting_Gas_pressure_System[1]);
}
else if(pilot_lever == Falling && hydraulic_pressure == Lower)
{
current_state = Lowering_Gear;
printf("current state: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is : %s\n",State_Machine[current_state].light);
printf("Status of Direction Valve : %s\n",State_Machine[current_state].direction_valve_status);
printf("Status of gas pressurized Spring System: %s\n",State_Machine[current_state].Excerting_Gas_pressure_System[0]);
}
else
{
CheckingBeforeLanding();
}
}
void LoweringGear(void)
{
current_state = Lowering_Gear;
printf("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 == Close)
{
current_state = Checking_Before_TakeOff;
printf("current state: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is : %s\n",State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control : %s\n",State_Machine[current_state].landing_gear_hydraulic_Pump_Control);
}
else if(pilot_lever == Falling && limit_switch == Open)
{
current_state = Gear_Down;
printf("current state: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is : %s\n",State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control : %s\n",State_Machine[current_state].landing_gear_hydraulic_Pump_Control);
}
else
{
LoweringGear();
}
}
void Initiate_State_Machine()//Called Function With Initiate The Initial state
{
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);
}