Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
- state.h file-------------------------------------------------------
#ifndef STATE_H_
#define STATE_H_
// Create global variable
void Int_State_Machine();
void Gear_Down();
void Checking_Before_Takeoff();
void Raising_Gear();
void Gear_Up();
void Checking_Before_Landing();
void Lowering_Gear();
typedef enum State // defined six state
{
GR_DWN,
CHK_BFR_TKOF,
RSING_GR,
GR_UP,
CHK_BFR_LND,
LWRNG_GR
}State_Type;
typedef enum Switch // defined switch state input
{
Open, //0
Close //1 plane is ground
}Switch_Status;
typedef enum Pilot_Leyer // defined pilot state input
{
Rising,
Falling,
Up,
Down
}Pilot_Leyer_Status;
typedef enum Hydraulic_Mechanism // defined hydraulic state input
{
Working,
Malfunctioning
}Hydraulic_Mechanism_Status;
/* ------input for changing the state---*/
static volatile Switch_Status squat_switch;
static volatile Switch_Status limit_switch;
static volatile Pilot_Leyer_Status pilot_lever;
static volatile Hydraulic_Mechanism_Status hydraulic_mechanism;
State_Type current_state;
typedef struct // struct defined for state table
{
char* current_state_indication;
char* light_status;
char* direction_valve_status;
char* hydraulic_pump_control_status;
char* gas_pressurized_spring_status[2];
}State_Table;
// Create state table variable
static State_Table 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}},
{"CheckBeforeLanding","Red","Down","Enabled",{NULL,NULL}},
{"LoweringGear","Green","Down","Enabled",{"Disabled","Enabled"}}
};
#endif /* STATE_H_ */
- main.c file------------------------------------------------------
#include
#include
#include"state.h" // header file for define states
void Gear_Down(void) // first state function
{
current_state =GR_DWN; // current state Gear Down
printf("\n Enter the pilot's Lever Position and Squat Switch Status:"); // select input from defined states
fflush(stdout);
scanf("%d %d",&pilot_lever,&squat_switch);
if(pilot_lever == Rising && squat_switch == Open)// select input from defined states
{
current_state=CHK_BFR_TKOF; // current state check before takeoff
printf("\nCurrent State: %s\n",State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n",State_Machine[current_state].light_status);
}
else
{
Gear_Down();
}
}
void Checking_Before_Takeoff() // Second state function
{
current_state=CHK_BFR_TKOF;// current state check before takeoff
printf("\n Enter the Pilot's Lever Position Squat Switch Status and Hydraulic Mechanism Status:"); // select input from defined states
fflush(stdout);
scanf("%d %d %d",&pilot_lever,&squat_switch,&hydraulic_mechanism);
printf("\n Wait for 2 secs\n");
if(pilot_lever==Falling || squat_switch == Close)// select input from defined states
{
Gear_Down();
}
else if(pilot_lever == Up && squat_switch == Open && hydraulic_mechanism == Working)// select input from defined states
{
current_state = RSING_GR; //current state rising gear
printf("\nCurrent State:%s\n",State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n",State_Machine[current_state].light_status);
printf("Direction Valve Status:%s\n",State_Machine[current_state].direction_valve_status);
}
else if(pilot_lever == Up && squat_switch == Open && hydraulic_mechanism == Malfunctioning)// select input from defined states
{
current_state = RSING_GR;// current state rising gear
printf("\nCurrent State:%s\n",State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n",State_Machine[current_state].light_status);
printf("Direction Valve Status:%s\n",State_Machine[current_state].direction_valve_status);
printf("Gas Pressurized Spring System Status:%s\n",State_Machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]);
}
}
void Raising_Gear() // third state function
{
current_state=RSING_GR;
printf("\n Enter the Limite Switch Status Pilots Leaver Status:");// select input from defined states
fflush(stdout);
scanf("%d %d",&limit_switch,&pilot_lever);
printf("\n Wait for 1 sec\n");
if(limit_switch == Open && pilot_lever == Falling)// select input from defined states
{
current_state = CHK_BFR_LND;
printf("\nCurrent State:%s\n",State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n",State_Machine[current_state].light_status);
printf("Hydraulic Control for Landing Gear Status:%s\n",State_Machine[current_state].hydraulic_pump_control_status);
}
else if(limit_switch == Close && pilot_lever == Up)// select input from defined states
{
current_state = GR_UP;
printf("\nCurrent State:%s\n",State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n",State_Machine[current_state].light_status);
printf("Hydraulic Control for Landing Gear Status:%s\n",State_Machine[current_state].hydraulic_pump_control_status);
}
else
{
Raising_Gear();
}
}
void Gear_Up() // forth state function
{
current_state=GR_UP;
printf("\n Enter the Pilot's Lever Position:");// select input from defined states
fflush(stdout);
scanf("%d",&pilot_lever);
if(pilot_lever == Falling)// select input from defined states
{
current_state=CHK_BFR_LND;
printf("\nCurrent State:%s\n",State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n",State_Machine[current_state].light_status);
printf("Hydraulic Control for Landing Gear Status:%s\n",State_Machine[current_state].hydraulic_pump_control_status);
}
else
{
Gear_Up();
}
}
void Checking_Before_Landing() // fifth state function
{
current_state=CHK_BFR_LND;
printf("\n Enter the Pilot's Lever Position and Status of Hydraulic Mechanism:");
fflush(stdout);
scanf("%d %d",&pilot_lever,&hydraulic_mechanism);
if(pilot_lever == Down && hydraulic_mechanism == Working)// select input from defined states
{
current_state=LWRNG_GR;
printf("\nCurrent State:%s\n",State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n",State_Machine[current_state].light_status);
printf("Direction Valve Status:%s\n\n",State_Machine[current_state].direction_valve_status);
}
else if(pilot_lever == Down && hydraulic_mechanism == Malfunctioning)// select input from defined states
{
printf("\nCurrent State:%s\n",State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n",State_Machine[current_state].light_status);
printf("Direction Valve Status:%s\n\n",State_Machine[current_state].direction_valve_status);
printf("Gas Pressurized Spring System Status:%s\n",State_Machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]);
}
if(pilot_lever==(Up||Rising||Falling))// select input from defined states
{
Checking_Before_Landing();
}
}
void Lowering_Gear() // sixth state function
{
current_state=LWRNG_GR;
printf("\n Enter the limit Switch Status Pilots Leaver Status:");// select input from defined states
fflush(stdout);
scanf("%d %d",&limit_switch,&pilot_lever);
if(limit_switch == Open && pilot_lever == Rising )// select input from defined states
{
current_state = CHK_BFR_TKOF;
printf("\nCurrent State:%s\n",State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n",State_Machine[current_state].light_status);
}
if(limit_switch == Close && pilot_lever == Rising )// select input from defined states
{
current_state=GR_DWN;
printf("\nCurrent State:%s\n",State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n",State_Machine[current_state].light_status);
printf("Hydraulic Control for Landing Gear Status:%s\n",State_Machine[current_state].hydraulic_pump_control_status);
}
else
{
Lowering_Gear();
}
}
// Assigning state table address
static void(*stateTable[])(void)=
{Gear_Down,Checking_Before_Takeoff,Raising_Gear,Gear_Up,Checking_Before_Landing,Lowering_Gear};
void Int_State_Machine()
{ // initailising function of state machine
current_state=GR_DWN; //assigning current state as state initial
printf("The Finite State Machine is Initialized and current the program is in""GearDown state & Light is:%s\n",State_Machine[current_state].light_status);
}
//main function continues loop
int main()
{Int_State_Machine();
while(1)
{
stateTable[current_state]();
}
}
output----------------------------------------------------------------------------------------
The Finite State Machine is Initialized and current the program is inGearDown state & Light is:Green
Enter the pilot's Lever Position and Squat Switch Status:0 0
Current State: CheckingBeforeTakeOFF
Light Status:Green
Enter the Pilot's Lever Position Squat Switch Status and Hydraulic Mechanism Status:2 0 1
Wait for 2 secs
Current State:RaisingGear
Light Status:Red
Direction Valve Status:Up
Gas Pressurized Spring System Status:Enabled
Enter the Limite Switch Status Pilots Leaver Status:1 2
Wait for 1 sec
Current State:GearUp
Light Status:Off
Hydraulic Control for Landing Gear Status:Disabled
Enter the Pilot's Lever Position:1
Current State:CheckBeforeLanding
Light Status:Red
Hydraulic Control for Landing Gear Status:Enabled
Enter the Pilot's Lever Position and Status of Hydraulic Mechanism:3 1
Current State:CheckBeforeLanding
Light Status:Red
Direction Valve Status:Down
Gas Pressurized Spring System Status:(null)
Enter the Pilot's Lever Position and Status of Hydraulic Mechanism:3 0
Current State:LoweringGear
Light Status:Green
Direction Valve Status:Down
Enter the limit Switch Status Pilots Leaver Status:1 0
Current State:GearDown
Light Status:Green
Hydraulic Control for Landing Gear Status:Enabled