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 Airplane’s landing gear:
AIM:
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 an Airplane 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 problem:




From the above input we are creating six different conditions and function. In this function we are determine or controlling the land gear in different conditions.

CODING:
First, we are creating seven function we want to carry out the process or controlling sets of gear under different condition. Based on above chart.
The functions are:

And then we are entering this function into the array of 6 element to access the function based on requirement. In the name of static table.

And we are creating Enum type structure to creating the alise number to our function which is easy to carry out comparison and easy way giving inputs.
We are creating four types of Enum in program:

And then we are creating the object name for the 4 Enum structures:

And we mentioned that as static type because we can use this anywhere in the program volatile means it values are changeable:
This step is very important we are creating the structure where we are giving the different condition where how to control the landing gears.

We are giving the name to structure as state_table.
we are creating the object named as state machine which is an array in which it contains six different types of conditional output:
we can access the inside array one element using dot sign.

In main function we are calling the function name is Initiate State machine function:
with no argument
In the function part:

Inside we are changing the current state to Gear down which means it is 0 in the Enum function And printing the statemachine[0]. light status which is in statetable.

It returns output like above.
Next step:
It enters into the while loop

In the while loop we have function statetable with the argument of current table.
Now argument as 0 which means current state is gear down:

In the gear down function, we are changing the current state to gear down
And getting the pilot inputs if the pilot gives inputs as raising and switch on it means ready to takes off and changing the state as checking before take-off state and then printing current indication and light.

Now current state changes to checking before take-off in current state:

In the checking before take-off function, we are getting the input from pilot for liver, squat switch. If the liver is falling it goes to the gear down function 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 respective 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 hydraulics function as same then state changes to lowering gear and show corresponding output from the statemachine array. If the lever is falling and hydraulics not working it shows the gas pressure system.
The last function (or) State:

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.

PROGRAMMING C 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 pressurised 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);
}
OUTPUT:
Enter the status of pilot's lever:1
Current State:CheckBeforeLanding
Light is:Red
Landing Gear Hydraulic Control:Enabled
Enter the status of pilot's lever and hydraulic mechanism status:1 0
Current State:LoweringGear
Light is:Green
Status of Direction valve:Down
Enter the status of pilot's lever and limit switch:0 1
Current State:GearDown
Light is:Green
Landing Gear Hydraulic Control:Enabled
Enter the status of pilot's lever and limit switch: