Challenge

Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear

This project explains the extraction and retraction of landing gear. Extraction of landing gear takes place while landing while retraction of landing gear takes place after takeoff. Here FSM(finite state machine)is used to check the different states.

CODE:

#include<stdio.h>

/* Functions*/
void Initial_state_machine(); //function declaration
void Gear_down();
void Checking_before_takeoff();
void Raising_gear();
void Gear_up();
void Checking_before_landing();
void Lowering_gear();


typedef enum states

{
//ENUM IS USED TO GIVING SUBSEQUENT VALUE FOR EACH STATES
GEAR_DOWN, //0
CHECK_BEFORE_TAKEOFF,//1
RAISING_GEAR,//2
GEAR_UP,//3
CHECK_BEFORE_LANDING,//4
LOWERING_GEAR//5
}state_type;

typedef enum Switch
{
open, //open=0;
close //close=1;plane is in ground
}Switch_status;

typedef enum pilot_lever
{
raising, //by using enum rising =0
falling,//falling=1
up,//up=2
down//down=3
}pilot_lever_status;

typedef enum hydraulic_mechanism
{
working, //0 for working
malfunctioning// 1 for malfunctioning
}hydraulic_mechanism_status;

/* input used in order to change the state */
static volatile Switch_status squat_switch;//..safety switch..open...extraction..landing..closed..retraction..takeoff
static volatile Switch_status limit_switch;//..gear up or down
static volatile pilot_lever_status pilot_lever;//..direction valve up or down also indicates the light status
static volatile pilot_lever_status hydraulic_mechanism;//holds gear in up for retraction

state_type current_state;

typedef struct // structure is used to have different types of data type
{
//output which we want to visualize are given under structure as structure member.
char* current_state_status;//current state of gear
char* light_status;//green or red..indicate the position of landing gear
char* direction_value_status;//up,down..control the hyd pressure for raising or lowering the landing gear
char* hydraulic_pump_ctrl_status;//enabled,disabled..emergency landing
}state_table;

/* State table of the require system*/

static state_table state_machine[6] = {
{"gear down","green","down","enables"},
{"checking before take off","green","down","enabled"},
{"raising gear","red","up","enabled"},
{"gear up","off","NULL","disabled"},
{"checking before landing","red","down","enabled"},
{"lowering gear","green","down","enabled"}//values of various output variables in the state table as per different state trasitions
};

static void(*State_Table[])(void)={Gear_down,Checking_before_takeoff,Raising_gear,
Gear_up,Checking_before_landing,Lowering_gear};//syntax for state table

/*initial state machine*/
void Initial_state_machine()
{
current_state = GEAR_DOWN; //during initialization, system is at gear down...always initally system must be in gear down position
printf("\n state machine is initials and at first program is in gear down and its light status is:%s\n",state_machine[current_state].light_status);//green
}

/*Gear down*/
void Gear_down(void)
{
current_state =GEAR_DOWN;
printf("\n Enter the pilot's lever position and squat switch status:\n");// getting pilot lever and squat switch value from input
fflush(stdout);
scanf("%d %d",&pilot_lever,&squat_switch);
if(pilot_lever == raising && squat_switch == open) // if pilot lever raising(0) and squat switch is open(0)..cond for chk.bfr..tkff
{
current_state = CHECK_BEFORE_TAKEOFF;
printf("\n The current state of the machine is: %s\n",state_machine[current_state].current_state_status);// current status value is printed from the state table..chk..bfr..tkff
printf("\n Light status of the current machine state is:%s\n",state_machine[current_state].light_status);// light status value is taken from state table current state..green
}
else//other than 00 stay in gear down
{
Gear_down(); //gear down function is called
}
}

/*checking before takeoff*/
void Checking_before_takeoff()
{
current_state= CHECK_BEFORE_TAKEOFF; //current state is check before takeoff
printf("\n Enter the pilot lever position, squat switch and hydraulic mechancism:\n");// getting pilot liver, squat switch and hydraulic mechanism as input
fflush(stdout);
scanf("%d %d %d",&pilot_lever, &squat_switch,&hydraulic_mechanism);
printf("\n just wait 2secs\n");
if (pilot_lever == up && squat_switch== close) //pilot lever is up and squat switch is closed it get into the if loop..2..1
{
Gear_down(); //gear down function is called
}
else if (pilot_lever== up && squat_switch == open && hydraulic_mechanism == working)//2..0..1
{
current_state= RAISING_GEAR;
printf("\n Current state: %s\n",state_machine[current_state].current_state_status);//rsing gear
printf("light status: %s\n", state_machine[current_state].light_status);//red
printf("\n direction status:%s\n\n",state_machine[current_state].direction_value_status);//up
}
else if (pilot_lever== up && squat_switch == open && hydraulic_mechanism == malfunctioning)//..2..0..0
{
current_state= RAISING_GEAR;
printf("\n Current state: %s\n", state_machine[current_state].current_state_status);//rsing gear
printf("\nlight status: %s\n", state_machine[current_state].light_status);//red
printf("\n direction status:%s\n\n",state_machine[current_state].direction_value_status);//up
}
else//pilot lever ..rising..0..falling..1..down..3..same state of chk bfr tkff..fun is called and run
{
Checking_before_takeoff();
}
}

/*raising gear*/
void Raising_gear()
{
current_state = RAISING_GEAR;
printf("\n Enter the switch status and pilot lever status:\n");
fflush(stdout);
scanf("%d %d",&limit_switch,&pilot_lever);
printf("\n wait for 1 sec\n");
if(pilot_lever == falling && limit_switch == open)//..1..0
{
current_state = CHECK_BEFORE_LANDING;
printf("\n current state is:%s\n",state_machine[current_state].current_state_status);//check before landing
printf("\n Light status:%s\n",state_machine[current_state].light_status);//red
printf("\nHydraulic condition for landing gear status is %s\n",state_machine[current_state].hydraulic_pump_ctrl_status);//enabled
}
else if(limit_switch == close && pilot_lever == up)//..1..2
{
current_state= GEAR_UP;
printf("\n current state is:%s\n",state_machine[current_state].current_state_status);//gear up
printf("\n Light status:%s\n",state_machine[current_state].light_status);//off
printf("\nHydraulic condition for landing gear status is %s\n",state_machine[current_state].hydraulic_pump_ctrl_status);//disabled
}
else//pilot lever..3..down..0..rising..stays in raising gear..function is called and run
{
Raising_gear();
}
}

/*gear up*/
void Gear_up()
{
current_state= GEAR_UP;
printf("\n Enter the pilot lever position :\n");
fflush(stdout);
scanf("%d",&pilot_lever);
if (pilot_lever == falling )//..1
{
current_state= CHECK_BEFORE_LANDING;
printf("\n Current state: %s\n", state_machine[current_state].current_state_status);//check before landing
printf("\nlight status: %s\n", state_machine[current_state].light_status);//red
printf("\nHydraulic condition for landing gear status is %s\n",state_machine[current_state].hydraulic_pump_ctrl_status);//enabled
}
else//pilot lever 0..rising..2..up..3..down then goes to gear up.. function is called and run
{
Gear_up();
}
}

/*checking before landing*/
void Checking_before_landing()
{
current_state= CHECK_BEFORE_LANDING;
printf("\n Enter the pilot lever position, status of hydraulic mechanism :\n");
fflush(stdout);
scanf("%d %d",&pilot_lever, &hydraulic_mechanism);
if(pilot_lever == down && hydraulic_mechanism == working)//..3..0
{
current_state= LOWERING_GEAR;
printf("\n Current state: %s\n", state_machine[current_state].current_state_status);//lowring gear
printf("\nlight status: %s\n", state_machine[current_state].light_status);//green
printf("\n direction status:%s\n\n",state_machine[current_state].direction_value_status);//down
}
else if (pilot_lever== down && hydraulic_mechanism == malfunctioning)//3..1
{
current_state= LOWERING_GEAR;
printf("\n Current state: %s\n", state_machine[current_state].current_state_status);//lwring gear
printf("light status: %s\n", state_machine[current_state].light_status);//green
printf("\n direction status:%s\n\n",state_machine[current_state].direction_value_status);//down
}
if(pilot_lever== (up || raising || falling))//2..0..1..chk bfr lnd fun is called and run
{
Checking_before_landing();
}
}

/*lowering gear*/
void Lowering_gear()
{
current_state= LOWERING_GEAR;
printf("\n Enter the pilot lever position, limit switch :");
fflush(stdout);
scanf("%d %d",&pilot_lever, &limit_switch);
if (pilot_lever == raising && limit_switch==close)//..0..1
{
current_state= CHECK_BEFORE_TAKEOFF;
printf("\n Current state: %s\n", state_machine[current_state].current_state_status);//chk bfr tkff
printf("\nlight status: %s\n", state_machine[current_state].light_status);//green
}
if (pilot_lever== down && limit_switch == open)//3..0
{
current_state= GEAR_DOWN;
printf("\n Current state: %s\n", state_machine[current_state].current_state_status);//gear dwn
printf("\n light status: %s\n", state_machine[current_state].light_status);//green
printf("\n Hydraulic condition for landing gear status is %s\n",state_machine[current_state].hydraulic_pump_ctrl_status);//enables
}
else//pilot lever..falling..1..up..2
{
Lowering_gear();
}
}

/*main function*/
int main()
{
Initial_state_machine();
while(1)
{
State_Table[current_state]();
}
}

OUTPUT:

state machine is initials and at first program is in gear down and its light status is:green

Enter the pilot's lever position and squat switch status:
0 0

The current state of the machine is: checking before take off

Light status of the current machine state is:green

Enter the pilot lever position, squat switch and hydraulic mechancism:
2 0 0

just wait 2secs

Current state: raising gear
light status: red

direction status:up


Enter the switch status and pilot lever status:
1 2

wait for 1 sec

current state is:gear up

Light status:off

Hydraulic condition for landing gear status is disabled

Enter the pilot lever position :
1

Current state: checking before landing

light status: red

Hydraulic condition for landing gear status is enabled

Enter the pilot lever position, status of hydraulic mechanism :
3 0

Current state: lowering gear

light status: green

direction status:down


Enter the pilot lever position, limit switch :3 0

Current state: gear down

light status: green

Hydraulic condition for landing gear status is enables

Enter the pilot's lever position and squat switch status:

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.