Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Objective
Here we are going to control the airplanes landing gear can be implemented using finite state machine (FMS).
What is finite state machine
A finite state machine is a computation model that can be implemented with hardware or software and can be used to simulate sequential logic and some computer programs.
Here we are using the switch states, direction of valves with respect to gear.
|
Switch state |
State of gears |
|
Limit switch CLOSED |
Gear is UP |
|
Limit switch OPENED |
Gear is DOWN |
|
Switch state |
State of airplane |
|
Squat switch CLOSED |
Airplane is grounded and gears are locked |
|
Squat switch OPENED |
Airplane is taking off/landing |
|
State of direction valve |
State of landing gear |
|
Direction valve=down |
Gear down parked at land |
|
Direction valve=up |
Raising gear(takinf off) |
|
Control of hydraulic pump for the landing gear part is diabled |
Gear up(airbome) |
|
Direction valve =down |
Lowering gear(landing) |
|
Pilot’s lever stae |
Direction valve state |
|
lever up |
Direction valve=up |
|
Lever down |
Direction valve=down |
|
Pilot’s lever state |
Light status |
|
Lever rising edge |
red |
|
Lever falling edge |
red |
|
Landing gear state |
Light status |
|
Gear is up |
Off |
|
Gear is down |
Green |
|
Gear in transit (raising or lowering) |
red |
The function are

Then we are entering this function into the array of 6 element to acces the function based on requirement in the name of static table.

Creating enum type structure to creating the number to our function which is easy to carry out comparison and easy way giving inputs.

We are creating the object name for the 4 enum structures

We mentioned that as static type because we can use this any where in the program volatile means it values are changeable.
Creating the structure where we are giving the different condition where how to control the landing gears.

That means state table
Creating the object named as state machine which is an array in which it contain six different type of conditional outputs
Creating main function

Now argument as 0 which means current state is gear down

Getting the pilot inputs if the pilot give inputs as raising and switch on it means read to takes off and changing the state as checking before take -off state
And printing current indication and light
Enter to current state function which is checking before take off

Checking beforetakeoff and getting the input from pilot for liver, squat switch
Print the direction of valve, light, indication statemacine.
Risinggear

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.
Gear up

Check the current state and pilots lever is 0 move to CheckBeforeLanding function and prints current state, light, landing gear hydraulic control.
CheckBeforeLanding
In this function getting the 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 lever is the state changes to gear up
loweringGear
If we are getting inputs 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 as lever raising and limit switch is off the state is gear down
If limit switch is off then it goes to lowering gear function itself
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
}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}},
{"CheckingBefore TakeOFF", "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("\nEnter 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("\ncurrent state: %s", State_Machine[current_state].current_state_indication);
printf("\nLight is : %s", State_Machine[current_state].light);
}
else
{
GearDown();
}
}
//CheckingBefore TakeOFF
void CheckingBeforeTakeOFF(void)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("\nEnter 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("\nCurrent State: %s", State_Machine[current_state]. current_state_indication);
printf("\nLight is: %s", State_Machine[current_state].light);
printf("\nStatus of Direction valve: %s", State_Machine[current_state].direction_valve_status);
}
}
//RaisingGear
void RaisingGear(void)
{
current_state=RAISING_GEAR;
printf("\nEnter 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("\nCurrent State: %s", State_Machine[current_state].current_state_indication);
printf("\nLight is: %s", State_Machine[current_state].light);
printf("\nLanding Gear Hydraulic Control: %s", State_Machine[current_state].landing_gear_hydraulic_control);
}
else if(pl==Raising && limit_switch==off)
{
current_state=GEAR_UP;
printf("\nCurrent State: %s", State_Machine[current_state].current_state_indication);
printf("\nLight is: %s", State_Machine[current_state].light);
printf("\nLanding Gear Hydraulic Control: %s", State_Machine[current_state].landing_gear_hydraulic_control);
}
else
{
RaisingGear();
}
}
//GearUp
void GearUp(void)
{
current_state=GEAR_UP;
printf("\nEnter the status of pilot's lever: ");
fflush(stdout);
scanf("%d",&pl);
if(pl=falling)
{
current_state=CHECK_BEFORE_LANDING;
printf("\nCurrent State: %s", State_Machine[current_state].current_state_indication);
printf("\nLight is: %s", State_Machine[current_state].light);
printf("\nLanding Gear Hydraulic Control: %s", State_Machine[current_state].landing_gear_hydraulic_control);
}
else
{
GearUp();
}
}
//CheckBeforeLanding
void CheckBeforeLanding(void)
{
current_state=CHECK_BEFORE_LANDING;
printf("\nEnter 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("\nCurrent State: %s", State_Machine[current_state].current_state_indication);
printf("\nLight is: %s", State_Machine[current_state]. light);
printf("\nStatus of Direction valve: %s", State_Machine[current_state].direction_valve_status);
}
else if(pl==falling && hm==not_working)
{
current_state=LOWERING_GEAR;
printf("\nCurrent State: %s", State_Machine[current_state].current_state_indication);
printf("\nLight is: %s", State_Machine[current_state].light);
printf("\nStatus of Direction valve: %s", State_Machine[current_state].direction_valve_status);
printf("\nStatus of gas pressurised Spring System: %s",State_Machine[current_state].Gas_Pressurized_Spring_system_status);
}
else if(pl==(Raising))
{
CheckBeforeLanding();
}
}
// LoweringGear
void LoweringGear(void)
{
current_state=LOWERING_GEAR;
printf("\nEnter 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("\nCurrent State: %sn",State_Machine[current_state].current_state_indication);
printf("\nLight is: %s", State_Machine[current_state].light);
}
else if(pl==Raising && limit_switch==off)
{
current_state=GEAR_DOWN;
printf("\nCurrent State: %s",State_Machine[current_state].current_state_indication);
printf("\nLight is: %s", State_Machine[current_state].light);
printf("\nLanding Gear Hydraulic Control: %s", 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("\nThe Finite State Machine is Initialized and current the program is in GearDown State &Light is: %s",State_Machine[current_state].light);
}
output
