Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Overview of the Project:
- In this project, controlling the retraction and extension of the Airplane’s landing gear can be implemented using Finite State Machine (FSM). FSM is the most efficient algorithm which is a mathematical model of computation.
Goals:
- Controlling the direction of Valve.
- Controlling the retraction and extension of the Airplane’s landing gear.
Key Highlights of the Project:
- A direction valve is controlled by the software to allow the hydraulic pressure to either raise or lower the landing gear.
Program Code:
#include <stdio.h> //We have to include stdio.h (standard input output library for C language)to use the print function
void Init_State_Machine(); //Initializing Initial State Machine Function
void Gear_Down(); //Initializing Gear Down Function
void Check_Before_Takeoff(); //Initializing Check Before Take off Function
void Raising_Gear(); //Initializing Raising Gear Function
void Gear_Up(); //Initializing Gear Up Function
void Check_Before_Landing(); //Initializing Check Before Landing Function
void Lowering_Gear(); //Initializing Lowering Gear Function
typedef enum State //Initializing Enumerated State
{
GEAR_DOWN, //Gear Down State Type
CHECK_BEFORE_TAKEOFF, //Check Before Take Off State Type
RAISING_GEAR, //Raising Gear State Type
GEAR_UP, //Gear Up State Type
CHECK_BEFORE_LANDING, //Check Before Landing State Type
LOWERING_GEAR //Lowering Gear State Type
}State_Type; //Naming State as State Type
typedef enum Switch //Initializing Enumerated Switch
{
Open, //Turning ON a Switch
Close //Turning OFF a Switch
}Switch_Status; //Naming Switch as Switch State
typedef enum Pilot_Lever //Initializing Enumerated Pilot Lever
{
Raising, //Raising Function for Lever
Falling, //Falling Function for Lever
Up, //Up Function for Lever
Down //Down Function for Lever
}Pilot_Lever_Status; //Naming Pilot Lever as Pilot Lever Status
typedef enum Hydraulic_Mechanism //Initializing Hydraulic Mechanism
{
Working, //Turning ON Hydraulics
Malfunctioning //Fault in Hydraulics
}Hydraulic_Mechanism_Status; //Naming Hydraulic Mechanism as Hydraulic Mechanism Status
static volatile Switch_Status Squat_Switch; //Reading Data from Switch Status for Squat Switch
static volatile Switch_Status Limit_Switch; //Reading Data from Switch Status for Limit Switch
static volatile Pilot_Lever_Status Pilot_Lever; //Reading Data from Pilot Lever Status for Pilot Lever
static volatile Hydraulic_Mechanism_Status Hydraulic_Mechanism; //Reading Data from Hydraulic Mechanism Status for Hydraulic Mechanism
State_Type Current_State; //Defining Current State in State Types
typedef struct //Initializing Structure
{
char* Current_State_Status; //Initializing Character as Current Status
char* Light_Status; //Initializing Character as Light Status
char* Direction_Valve_Status; //Initializing Character as Direction Valve Status
char* Hydraulic_Pump_Control_Status; //Initializing Character as Hydraulic Pump Control Status
char* Gas_Pressurized_Spring_Status[2]; //Initializing Character Array as Gas Pressurized Spring Status
}State_Table; //Naming Structure as State Table
//Initializing and Defining State Machine Function
static State_Table State_Machine[6]={
{"Gear_Down","Green","Down","Enabled",{0,0}},
{"Check_Before_Take_Off","Green","Down","Enabled",{0,0}},
{"Raising_Gear","Red","Up","Enabled",{"Disabled","Enabled"}},
{"Gear_Up","Off","Null","Disabled",{0,0}},
{"Check_Before_Landing","Red","Down","Enabled",{0,0}},
{"Lowering_Gear","Green","Down","Enabled",{"Disabled","Enabled"}}
};
//Initializing State Table Function
static void(*state_table[])(void) = {Gear_Down, Check_Before_Takeoff, Raising_Gear, Gear_Up, Check_Before_Landing, Lowering_Gear};
int main() //Starting of the Main Function
{
Init_State_Machine(); //Initial Stage of State Machine
while(1) //Starting of Never Ending While Loop
{
state_table[Current_State](); //Execution of the State Table
}
}
void Init_State_Machine() //Defining Initial State of State Machine
{
Current_State = GEAR_DOWN; //Current State is Gear Down State
printf("\nState Machine is Initialized."); //Started Execution of the State Machine
printf("\nAirplane is in Gear_Down State."); //Airplane is in Gear Down State
printf("\nLight Status: %s.\n",State_Machine[Current_State].Light_Status); //Light Shows the State of the Gear
}
void Gear_Down() //Defining Gear Down State
{
Current_State = GEAR_DOWN; //Gear Down is the Current State
printf("\nEnter the Pilot's Lever Position: "); //Asking Pilot Lever Position
fflush(stdout); //Carries Output to Console
scanf("%d",&Pilot_Lever); //Storing Pilot Lever Value
printf("Enter the Squat Switch Status: "); //Asking Squat Switch Status
fflush(stdout); //Carries Output to Console
scanf("%d",&Squat_Switch); //Storing Squat Switch Value
if(Pilot_Lever == Raising && Squat_Switch == Open) //Compare Input from the Required Condition
{
Current_State = CHECK_BEFORE_TAKEOFF; //If Input Matches Current State Will be Check Before Take Off State
printf("\nCurrent State: %s\n",State_Machine[Current_State].Current_State_Status); //Printing Airplane Current Status
printf("Light Status:%s\n", State_Machine[Current_State].Light_Status); //Printing State of Gear from Light Indication
printf("Direction Valve Status:%s\n", State_Machine[Current_State].Direction_Valve_Status); //Printing Direction Valve Status
}
else //If Input Doesn't Matches Return to Gear Down State
{
Gear_Down(); //Calling Gear Down Function
}
}
void Check_Before_Takeoff() //Defining Check Before Take Off State
{
Current_State = CHECK_BEFORE_TAKEOFF; //Check Before Take Off is the Current State
printf("\nEnter the Pilot's Lever Position: "); //Asking Pilot Lever Position
fflush(stdout); //Carries Output to Console
scanf("%d",&Pilot_Lever); //Storing Pilot Lever Value
printf("Enter the Squat Switch Status: "); //Asking Squat Switch Status
fflush(stdout); //Carries Output to Console
scanf("%d",&Squat_Switch); //Storing Squat Switch Value
printf("Enter the Hydraulic Mechanism Status: "); //Asking Hydraulic Mechanism Status
fflush(stdout); //Carries Output to Console
scanf("%d",&Hydraulic_Mechanism); //Storing Hydraulic Mechanism Value
printf("\nWait for 4 seconds\n"); //Printing Wait for 4 Seconds
if(Pilot_Lever == Up && Squat_Switch == Open && Hydraulic_Mechanism == Working) //Compare Input from the Required Condition
{
Current_State = RAISING_GEAR; //If Input Matches, Current State is Raising Gear
printf("\nCurrent State :%s\n",State_Machine[Current_State].Current_State_Status); //Printing Airplane Current Status
printf("Light Status: %s\n", State_Machine[Current_State].Light_Status); //Printing State of Gear from Light Indication
printf("Direction Valve Status:%s\n", State_Machine[Current_State].Direction_Valve_Status); //Printing Direction Valve Status
printf("Hydraulic Control for Landing Gear Status:%s\n",State_Machine[Current_State].Hydraulic_Pump_Control_Status); //Printing Hydraulic Status
}
else if(Pilot_Lever == Up && Squat_Switch == Open && Hydraulic_Mechanism == Malfunctioning) //Compare Input from the Required Condition
{
Current_State = RAISING_GEAR; //If Input Matches, Current State is Raising Gear
printf("\nCurrent State :%s\n",State_Machine[Current_State].Current_State_Status); //Printing Airplane Current Status
printf("Light Status: %s\n", State_Machine[Current_State].Light_Status); //Printing State of Gear from Light Indication
printf("Direction Valve Status:%s\n", State_Machine[Current_State].Direction_Valve_Status); //Printing Direction Valve Status
printf("Gas Pressurized Spring System Status:%s\n",State_Machine[Current_State].Gas_Pressurized_Spring_Status[Hydraulic_Mechanism]); //Printing Gas Spring Status
}
else //If Input Doesn't Matches Return to Check Before Take Off State
{
Check_Before_Takeoff(); //Calling Check Before Take Off Function
}
}
void Raising_Gear() //Defining Raising Gear State
{
Current_State = RAISING_GEAR; //Raising Gear is the Current State
printf("\nEnter the Pilots Lever Status: "); //Asking Pilot Lever Position
fflush(stdout); //Carries Output to Console
scanf("%d",&Pilot_Lever); //Storing Pilot Lever Value
printf("Enter the Limit Switch Status: "); //Asking Limit Switch Status
fflush(stdout); //Carries Output to Console
scanf("%d",&Limit_Switch); //Storing Limit Switch Value
if(Pilot_Lever == Falling && Limit_Switch == Open) //Compare Input from the Required Condition
{
Current_State = CHECK_BEFORE_LANDING; //If Input Matches, Current State is Check Before Landing
printf("\nCurrent State: %s\n",State_Machine[Current_State].Current_State_Status); //Printing Airplane Current Status
printf("Light Status:%s\n",State_Machine[Current_State].Light_Status); //Printing State of Gear from Light Indication
printf("Direction Valve Status:%s\n", State_Machine[Current_State].Direction_Valve_Status); //Printing Direction Valve Status
printf("Hydraulic Control for Landing Gear Status:%s\n",State_Machine[Current_State].Hydraulic_Pump_Control_Status); //Printing Hydraulic Status
}
else if(Limit_Switch == Close && Pilot_Lever == Up) //Compare Input from the Required Condition
{
Current_State = GEAR_UP; //If Input Matches, Current State is Gear Up State
printf("\nCurrent State: %s\n",State_Machine[Current_State].Current_State_Status); //Printing Airplane Current Status
printf("Light Status: %s\n", State_Machine[Current_State].Light_Status); //Printing State of Gear from Light Indication
printf("Direction Valve Status:%s\n", State_Machine[Current_State].Direction_Valve_Status); //Printing Direction Valve Status
printf("Hydraulic Control for Landing Gear Status:%s\n", State_Machine[Current_State].Hydraulic_Pump_Control_Status); //Printing Hydraulic Status
}
else //If Input Doesn't Matches Return to Raising Gear State
{
Raising_Gear(); //Calling Raising Gear Function
}
}
void Gear_Up() //Defining Gear Up State
{
Current_State = GEAR_UP; //Gear Up is the Current State
printf("\nEnter the Pilot's Lever Position: "); //Asking Pilot Lever Position
fflush(stdout); //Carries Output to Console
scanf("%d",&Pilot_Lever); //Storing Pilot Lever Value
if(Pilot_Lever == Falling) //Compare Input from the Required Condition
{
Current_State = CHECK_BEFORE_LANDING; //If Input Matches, Current State is Check Before Landing State
printf("\nCurrent State: %s\n",State_Machine[Current_State].Current_State_Status); //Printing Airplane Current Status
printf("Light Status:%s\n", State_Machine[Current_State].Light_Status); //Printing State of Gear from Light Indication
printf("Hydraulic Control for Landing Gear Status:%s\n",State_Machine[Current_State].Hydraulic_Pump_Control_Status); //Printing Hydraulic Status
}
else //If Input Doesn't Matches Return to Gear Up State
{
Gear_Up(); //Calling Gear Up Function
}
}
void Check_Before_Landing() //Defining Check Before Landing State
{
Current_State = CHECK_BEFORE_LANDING; //Check Before Landing is the Current State
printf("\nEnter the Pilot's Lever Position: "); //Asking Pilot Lever Position
fflush(stdout); //Carries Output to Console
scanf("%d",&Pilot_Lever); //Storing Pilot Lever Value
printf("Enter the Status of Hydraulic Mechanism: "); //Asking Hydraulic Mechanism Status
fflush(stdout); //Carries Output to Console
scanf("%d",&Hydraulic_Mechanism); //Storing Hydraulic Mechanism Value
if(Pilot_Lever == Down && Hydraulic_Mechanism == Working) //Compare Input from the Required Condition
{
Current_State = LOWERING_GEAR; //If Input Matches, Current State is Lowering Gear State
printf("\nCurrent State:%s\n", State_Machine[Current_State].Current_State_Status); //Printing Airplane Current Status
printf("Light Status:%s\n", State_Machine[Current_State].Light_Status); //Printing State of Gear from Light Indication
printf("Direction Valve Status:%s\n", State_Machine[Current_State].Direction_Valve_Status); //Printing Direction Valve Status
printf("Hydraulic Control for Landing Gear Status:%s\n",State_Machine[Current_State].Hydraulic_Pump_Control_Status); //Printing Hydraulic Status
}
else if(Pilot_Lever == Down && Hydraulic_Mechanism == Malfunctioning) //Compare Input from the Required Condition
{
Current_State = LOWERING_GEAR; //If Input Matches, Current State is Lowering Gear State
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Status); //Printing Airplane Current Status
printf("Light Status:%s\n", State_Machine[Current_State].Light_Status); //Printing State of Gear from Light Indication
printf("Direction Valve Status:%s\n", State_Machine[Current_State].Direction_Valve_Status); //Printing Direction Valve Status
printf("Gas Pressurized Spring System Status:%s\n",State_Machine[Current_State].Gas_Pressurized_Spring_Status[Hydraulic_Mechanism]); //Printing Gas Spring Status
}
else if(Pilot_Lever == (Up || Raising || Falling)) //Compare Input from the Required Condition
{
Check_Before_Landing(); //Calling Gear Down Function
}
}
void Lowering_Gear() //Defining Lowering Gear State
{
Current_State = LOWERING_GEAR; //Lowering Gear is the Current State
printf("\nEnter the Limit Switch Status: "); //Asking Limit Switch Status
fflush(stdout); //Carries Output to Console
scanf("%d",&Limit_Switch); //Storing Limit Switch Value
printf("Enter the Pilots Lever Status: "); //Asking Pilot Lever Position
fflush(stdout); //Carries Output to Console
scanf("%d",&Pilot_Lever); //Storing Pilot Lever Value
if(Limit_Switch == Close && Pilot_Lever == Raising ) //Compare Input from the Required Condition
{
Current_State = CHECK_BEFORE_TAKEOFF; //If Input Matches, Current State is Check Before Take Off State
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Status); //Printing Airplane Current Status
printf("Light Status:%s\n",State_Machine[Current_State].Light_Status); //Printing State of Gear from Light Indication
printf("Direction Valve Status:%s\n", State_Machine[Current_State].Direction_Valve_Status); //Printing Direction Valve Status
}
else if(Limit_Switch == Open && Pilot_Lever == Down) //Compare Input from the Required Condition
{
Current_State = GEAR_DOWN; //If Input Matches, Current State is Gear Down State
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Status); //Printing Airplane Current Status
printf("Light Status:%s\n",State_Machine[Current_State].Light_Status); //Printing State of Gear from Light Indication
printf("Direction Valve Status:%s\n", State_Machine[Current_State].Direction_Valve_Status); //Printing Direction Valve Status
}
else //If Input Doesn't Matches Return to Lowering Gear State
{
Lowering_Gear(); //Calling Lowering Gear Function
}
}
--------------------------------------------------------------------------------------------
Initializing States and State Table :


--------------------------------------------------------------------------------------
Main Function:

--------------------------------------------------------------------------------------
Initial State:

--------------------------------------------------------------------------------------
Gear Down State:

--------------------------------------------------------------------------------------
Check Before Take Off State:

--------------------------------------------------------------------------------------
Raising Gear State:

--------------------------------------------------------------------------------------
Gear Up State:

--------------------------------------------------------------------------------------
Check Before Landing State:

--------------------------------------------------------------------------------------------
Lowering Gear State:

-------------------------------------------------------------------------------------------
Console Output:
Initial State:

--------------------------------------------------------------------------------------------
Gear Down State ----------> Check Before Take Off State

--------------------------------------------------------------------------------------------
Check Before Take Off ----------> Raising Gear State

--------------------------------------------------------------------------------------------
Raising Gear State ----------> Check Before Landing State

Raising Gear State ----------> Gear Up State

--------------------------------------------------------------------------------------------
Gear Up State ----------> Check Before Landing State

--------------------------------------------------------------------------------------------
Check Before Landing State ----------> Lowering Gear State

--------------------------------------------------------------------------------------------
Lowering Gear State ----------> Check Before Take Off State

Lowering Gear State ----------> Gear Down State

--------------------------------------------------------------------------------------------
Full Code Execution From
Gear Down ---> Check Before Take Off ---> Raising Gear ---> Gear Up ---> Check Before Landing ---> Lowering Gear ---> Gear Down.


-------------------------------------------------------------------------------------------
Full Code Execution From (No Gear Up State)
Gear Down ---> Check Before Take Off ---> Raising Gear ---> Check Before Landing ---> Lowering Gear ---> Gear Down.

--------------------------------------------------------------------------------------------
Full Code Execution From (No Gear Down State)
Gear Down ---> Check Before Take Off ---> Raising Gear ---> Gear Up ---> Check Before Landing
---> Lowering Gear ---> Check Before Take Off.

