Challenge

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.

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

To carry out the problem we need the states with their respective finctions.

Here we are using the switch states,Direction of valves with respect to gear.

From the above input we are creating six differernt conditions and function.In this function we are determing or controlling the land gear in diffferent conditions.

This flow chart represent condition of the state and condition required to jump into next state.

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 function are:

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

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

We are creating four type 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 any where in the program volatile means it values are changable:

This step is very imporatant 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 statemachine which is an array in which it contain six different type of condtional output:

we can access the inside array one element using dot sign. 

We are creating the main function:

In main function we are calling the function name is Intiate 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 staemachine[0].light status which is in statetable.

It return output like above.

Next step:

It enter 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 geardown:

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.

Now current state changes to checking before takeoff in current state.

It enters to current state finction which is checking before take off

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.

THIS 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 shoen from state machine.

If limit switch is off the it goes to lowering gear function itself in.

code:

#include 
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 groung
}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","Disbled",{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:

 

 

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.