Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Project 2 :-Implement the Code for controlling the retraction and extension of Airplanes landing gear .
In this project, controlling the retraction and extension of Airplane’s landing gear can be implemented using Finite State Machine (FSM). FSM is the most efficient algorithm which is mathematical model of computation.
Objective:
Here we are going to control the Airplanes landing gear.
What is Finite state Machine:
A finite state machine (sometimes called a finite state automaton) is a computation model that can be implemented with hardware or software and can be used to simulate sequential logic and some computer programs. Finite state automata generate regular languages. Finite state machines can be used to model problems in many fields including mathematics, artificial intelligence, games, and linguistics.

A system where particular inputs cause particular changes in state can be represented using finite state machines. This example describes the various states of a turnstile. Inserting a coin into a turnstile will unlock it, and after the turnstile has been pushed, it locks again. Inserting a coin into an unlocked turnstile, or pushing against a locked turnstile will not change its state.
Implementation of the code:-
Undestanding the logic of working of retraction and extension of airplane landing gear is very much important..
The logic is explianed as follows:-




As per the above mentioned control logic and given state machine logic we have to implement the code

The above flow chart represent condition of the state and condition required to jump into next state.
Code:-
First create the function we want to carry out the process or controlling sets of gear under different condition as per the above chart.
acess the function using folloiwng commnad

Create enu type structure to creating the alise number to our function which is easy to carrry out comparison and easy way giving inputs.

Create object name for all 4 enum structures and also state machine table

create the object named as statemachine which is an array in which it contain six different type of condtional output:
Create the main function to implement the code

In the main function calling the function name to initiate state machine function.
While and if selse are used to execute landing gear at following condition
GEAR_DOWN,
CHECKING_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR
In the gear down function we are changing the current state to gear down
And getting the pilot inputs if the pilot give inputs as raising anf switch on it means ready to takes off and changing th state as checking before takeooff state
And then printing current indication and light.


Gear down and pilot lever, squat switch checking before take off with the help of if else as shwon in thr above image


In the checking before takeoff function we are getting the input from pilot for liver,squat switch.
If the liver is falling it goes to the geardown fuction part.
otherwise when the liver is raising and switch is up the current state is changes to next state that is Raising gear
Print the direction of valve,light,indication statemachine.
NEXT STATE IS RAISING GEAR:

In the raising gear function getting input from the pilot as lever is falling and switch is on it means it change state is check before landing and print the respectine value.If the lever is up then state changes to gear up and following output we will get.

NEXT FUNCTION IS CHECKING BEFORE LANDING

In this function we are pilot input as lever is falling and haydraulis finction as same then state changes to lowering gear and show corresponding output from the statemachine array.
If the lever is falling and hydralics not working it shows the gas pressure system.
THE LAST FUNCTION Lowering the Gear

If we are getting input as lever falling and limit switch is on then state is checking before landing and it is output is shown from state machine.
If we are getting input as lever raising and limit switch is off then state is gear down and it is output is shown from state machine.
If limit switch is off the it goes to lowering gear function itself in.

CODE
#include <stdio.h>
void Initiate_State_Machine();
void GearDown();
void CheckingBeforeTakeOFF();
void RaisingGear();
void GearUp();
void CheckBeforeLanding();
void LoweringGear();
static void(*statetable[])(void)={GearDown, CheckingBeforeTakeOFF, RaisingGear, GearUp, CheckBeforeLanding, LoweringGear};
typedef enum State
{
GEAR_DOWN,
CHECKING_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR,
}State_Type;
typedef enum Switch
{
on,
off//plane is on ground
}Switch_status;
typedef enum pilot_lever
{
Raising,
falling
}pilot_lever;
typedef enum hydraulic_mechanism
{
working,
not_working
}hydraulic_mechanism;
static volatile Switch_status squat_switch;
static volatile Switch_status limit_switch;
static volatile pilot_lever pl;
static volatile hydraulic_mechanism hm;
State_Type current_state;
typedef struct
{
char* current_state_indication;
char* light;
char* direction_valve_status;
char* landing_gear_hydraulic_control;
char* Gas_Pressurized_Spring_system_status[2];
}State_Table;
static State_Table State_Machine[6]={{"GearDown","Green","Down","Enabled",{NULL,NULL}},
{"CheckingBeforeTakeOFF","Green","Down","Enabled",{NULL,NULL}},
{"RaisingGear","Red","Up","Enabled",{NULL,NULL}},
{"GearUp","Off","NULL","Disabled",{NULL,NULL}},
{"CheckBeforeLanding","Red","Down","Enabled",{NULL,NULL}},
{"LoweringGear","Green","Down","Enabled",{"Disabled","Enabled"}}
};
int main()
{
Initiate_State_Machine();
while(1)
{
statetable[current_state]();
}
}
void GearDown(void)
{
current_state=GEAR_DOWN;
printf("Enter the status of pilot's lever and squat switch");
fflush(stdout);
scanf("%d %d",&pl,&squat_switch);
if(pl==Raising && squat_switch==on)
{
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();
}
}
//CheckingBeforeTakeOFF
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",&pl,&squat_switch,&hm);
if(pl==falling&&squat_switch==off)
{
GearDown();
}
else if(pl==Raising && squat_switch==on && hm==working)
{
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);
}
else if(pl==Raising && squat_switch==on && hm==not_working)
{
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 pressurised Spring System:%s\n",State_Machine[current_state].Gas_Pressurized_Spring_system_status);
}
}
//RaisingGear
void RaisingGear(void)
{
current_state=RAISING_GEAR;
printf("Enter the status of pilot's lever and limit switch:");
fflush(stdout);
scanf("%d %d",&pl,&limit_switch);
if(pl==falling && limit_switch==on)
{
current_state=CHECK_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_control);
}
else if(pl==Raising && limit_switch==off)
{
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_control);
}
else
{
RaisingGear();
}
}
//GearUp
void GearUp(void)
{
current_state=GEAR_UP;
printf("Enter the status of pilot's lever:");
fflush(stdout);
scanf("%d",&pl);
if(pl==falling)
{
current_state=CHECK_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_control);
}
else
{
GearUp();
}
}
//CheckBeforeLanding
void CheckBeforeLanding(void)
{
current_state=CHECK_BEFORE_LANDING;
printf("Enter the status of pilot's lever and hydraulic mechanism status:");
fflush(stdout);
scanf("%d %d",&pl,&hm);
if(pl==falling && hm==working)
{
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);
}
else if(pl==falling && hm==not_working)
{
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].Gas_Pressurized_Spring_system_status);
}
else if(pl==(Raising))
{
CheckBeforeLanding();
}
}
//LoweringGear.
void LoweringGear(void)
{
current_state=LOWERING_GEAR;
printf("Enter the status of pilot's lever and limit switch:");
fflush(stdout);
scanf("%d %d",&pl,&limit_switch);
if(pl==falling && limit_switch==on)
{
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 if(pl==Raising && limit_switch==off)
{
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_control);
}
if(limit_switch==off)
{
LoweringGear();
}
}
//Initiate_State_Machine
void Initiate_State_Machine()
{
current_state==GEAR_DOWN;
printf("The Finite State Machine is Intialized and current the program is in""GearDown State & Light is:%s\n",State_Machine[current_state].light);
}