Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Que:Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Ans:
Overview of the Project: In this project, a Finite State Machine is used to manage the retraction and extension of an airplane's landing gear (FSM). The most efficient algorithm is the FSM, which is a mathematical processing model.
Actually an airplane has 3 gears for landing: Nose gear, Left gear, Right gear.
There is an electrically powered hydraulic pump that applies pressure to the gears to bring it down or pull it up.
There is a limit switch on each leg of the gear. One extra squat switch is installed in the nose gear to assess whether the plane is on the ground or not.
Goals:
Valve's direction can be controlled.
Controlling the retraction and extension of the landing gear on an aeroplane.
Program:
#include<stdio.h>
#include<stdlib.h>
/*Functions used in project*/
void Intial_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
{
GEAR_DOWN, //ENUM IS GIVING SUBSEQUENT VALUE FOR EACH STATES
CHECK_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR
}state_type;
typedef enum Switch
{
open,//open=0,close=1
close,//plane is in ground
}Switch_status;
typedef enum pilot_lever
{
raising,// by using enum rising=0 falling =1 up= 3 and down =4 is assumed
falling,
up,
down
}pilot_lever_status;
typedef enum hydraulic_mechanism
{
working, //0 resembles working 1 is for malfunctioning
malfunctioning
}hydraulic_mechanism_status;
/*input used to change the status*/
static volatile Switch_status squat_switch;
static volatile Switch_status limit_switch;
static volatile pilot_lever_status pilot_lever;
static volatile pilot_lever_status hydraulic_mechanism;
state_type current_state;
typedef struct //structure is used to have diffrent types of data type
{
char* current_state_status;//out put which we want to realise is given under structure as structure member
char* light_status;
char* direction_value_status;
char* hydraulic_pump_ctrl_status;
char* gas_pressurized_spring_status[2];
}state_table;
/* state table of the required system*/
static state_table state_machine[6] = {
{"gear down","green","down","enables",{0,0}},
{"checking before take off","green","down","enabled",{0,0}},
{"raising gear","red","up","enabled",{"disabled","enabled"}},
{"gear up","off","NULL","disabled",{0,0}},
{"checking before landing","red","down","enabled",{0,0}},
{"lowering gear","gear","down","enabled",{"disabled","enabled"}}
};
static void(*State_Table[])(void)={Gear_down,Checking_before_takeoff,Raising_gear,
Gear_up,Checking_before_landing,Lowering_gear};
//Intial_state_machine
void Intial_state_machine()
{
current_state = GEAR_DOWN; //during intialization, system is at gear down.
printf("\n state machine is intials and at first program is in gear down and its light_status is: %s\n",state_machine[current_state].light_status);
}
//Gear down//
void Gear_down(void)
{
current_state =GEAR_DOWN;
printf("\n Enter pilot's lever position and squat switch status:\n");
fflush(stdout);
scanf("%d %d ",&pilot_lever,&squat_switch);
if(pilot_lever == raising && squat_switch == open)
{
current_state = CHECK_BEFORE_TAKEOFF;
printf("\n The Current state of the machine is : %s\n",state_machine[current_state].current_state_status);
printf("\n Light status of the current machine state is : %s \n",state_machine[current_state].light_status);
}
else
{
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 postion, squat switch and hydraulic mechanism :\n");
fflush(stdout);
scanf("%d %d %d",&pilot_lever,&squat_switch,(int*)&hydraulic_mechanism);
printf("\n just wait 2secs\n");
if (pilot_lever == up && squat_switch == close)//pilot_lever is up and squat_switch is closed to get into if loop
{
Gear_down(); //gear down function is called
}
else if (pilot_lever== up && squat_switch == open && hydraulic_mechanism == working)
{
current_state=RAISING_GEAR;
printf("\n Current state: %s\n",state_machine[current_state].current_state_status);
printf("light status: %s\n",state_machine[current_state].light_status);
printf("\n direction status :%s \n\n",state_machine[current_state].direction_value_status);
}
else if(pilot_lever== up && squat_switch == open && hydraulic_mechanism == malfunctioning)
{
current_state= RAISING_GEAR;
printf("\n Current state: %s \n",state_machine[current_state].current_state_status);
printf("\n light status : %s\n",state_machine[current_state].light_status);
printf("\n direction status :%s\n\n",state_machine[current_state].direction_value_status);
}
}
//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)
{
current_state = CHECK_BEFORE_LANDING;
printf("\n current_state is: %s \n",state_machine[current_state].current_state_status);
printf("\n Light status: %s\n",state_machine[current_state].light_status);
printf("\nHydraulic condition for landing gear status is %s\n",
state_machine[current_state].hydraulic_pump_ctrl_status);
}
else if(limit_switch == close && pilot_lever == up)
{
current_state=GEAR_UP;
printf("\n Current state is %s \n",state_machine[current_state].current_state_status);
printf("\n Light status: %s \n",state_machine[current_state].light_status);
printf("\n Hydraulic condtion for landing gear status is %s\n",
state_machine[current_state].hydraulic_pump_ctrl_status);
}
else
{
Raising_gear();
}
}
//gear up//
void Gear_up()
{
current_state= GEAR_UP;
printf("\n Enter the pilot lever postion : \n");
fflush(stdout);
scanf("%d",&pilot_lever);
if(pilot_lever == falling)
{
current_state=CHECK_BEFORE_LANDING;
printf("\n Current state : %s\n", state_machine[current_state].current_state_status);
printf("\n light status: %s \n", state_machine[current_state].light_status);
printf("\nHydraulic condtion for landing gear status is %s\n",
state_machine[current_state].hydraulic_pump_ctrl_status);
}
else
{
Gear_up();
}
}
//Checking before landing//
void Checking_before_landing()
{
current_state=CHECK_BEFORE_LANDING;
printf("\n Enter the pilot lever postion, status of hydraulic mechanism :\n");
fflush(stdout);
scanf("%d %d",&pilot_lever,&hydraulic_mechanism);
if(pilot_lever == down && hydraulic_mechanism == working)
{
current_state= LOWERING_GEAR;
printf("\n Current_state: %s \n",state_machine[current_state].current_state_status);
printf("\nlight status: %s \n",state_machine[current_state].light_status);
printf("\n direction status: %s \n\n",state_machine[current_state].direction_value_status);
}
else if (pilot_lever == down && hydraulic_mechanism == malfunctioning)
{
current_state = LOWERING_GEAR;
printf("\n Current state : %s \n",state_machine[current_state].current_state_status);
printf("light status: %s \n",state_machine[current_state].light_status);
printf("\n direction status : %s \n\n",state_machine[current_state].direction_value_status);
printf("\n gas pressurized spring status: %s \n",state_machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]);
}
if (pilot_lever ==(up || raising || falling))
{
Checking_before_landing();
}
}
//Lowering gear //
void Lowering_gear()
{
current_state=LOWERING_GEAR;
printf("\n Enter the pilot lever postion, limit switch :");
fflush(stdout);
scanf("%d %d ",&pilot_lever,&limit_switch);
if (pilot_lever == raising && limit_switch == close)
{
current_state=CHECK_BEFORE_TAKEOFF;
printf("\n Current state : %s \n",state_machine[current_state].current_state_status);
printf("\nlight status:%s \n",state_machine[current_state].light_status);
}
if (pilot_lever == down && limit_switch == open)
{
current_state=GEAR_DOWN;
printf("\n Current state: %s \n",state_machine[current_state].current_state_status);
printf("\n light_status: %s \n",state_machine[current_state].light_status);
printf("\n Hydraulic condition for landing gear status is %s \n",state_machine[current_state].hydraulic_pump_ctrl_status);
}
else
{
Lowering_gear();
}
}
//main function//
int main()
{
Intial_state_machine();
while(1)
{
State_Table[current_state]();
}
}
Output:

