Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
//Instructor:abinaya inbamani (programme for an FSM)
#include<stdio.h>//header file for input and output
#include<stdlib.h>//header files for mathematical operations
void Initial_state_machine(); //function declaration
void Gear_down();//function declaration
void Checking_before_takeoff();//function declaration
void Raising_gear();//function declaration
void Gear_up();//function declaration
void Checking_before_landing();//function declaration
void Lowering_gear();//function declaration
typedef enum states//assigning variable a constant value
{
GEAR_DOWN,//GEAR_DOWN=0,CHECK_BEFORE_TAKEOFF=1, AND SO ON
CHECK_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR
}state_type;
typedef enum Switch //similarly assigning constant value to variable
{
open,
close
}Switch_status;
typedef enum pilot_lever//similarly assigning constant value to variable
{
raising, //by using enum rising =0,falling=1,up=3and down=4 is assumed
falling,
up,
down
}pilot_lever_status;
typedef enum hydraulic_mechanism//ENUM TO PROVIDE CONSTANT VALUE TO A VARIABLE
{
working, //here 0 means for working and 1 means for malfunction
malfunctioning
}hydraulic_mechanism_status;
static volatile Switch_status squat_switch;//CHANGING THE STATE INPUTS
static volatile Switch_status limit_switch;//CHANGING THE STATES INPUTS
static volatile pilot_lever_status pilot_lever;//CHANGING THE STATES INPUTS
static volatile pilot_lever_status hydraulic_mechanism;//CHANGING THE STATES INPUTS
state_type current_state;//CURRENT STATE DECLARATION TO KEEP A TRACK OF THE CURRENT STATE
typedef struct // structure is used to have different types of data type
{
char* current_state_status; //output which we want to visualize are 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 require system*/
static state_table state_machine[6] = {//TABLE CREATED WITH DIFFERENT STRINGS AND VALUE
{"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","green","down","enabled",{"disabled","enabled"}}
};
static void(*State_Table[])(void)={Gear_down,Checking_before_takeoff,Raising_gear,
Gear_up,Checking_before_landing,Lowering_gear};//INITIALISING THE STATE TABLE
//initial state machine//
void Initial_state_machine()//FUNCTION DEFINITION
{
current_state = GEAR_DOWN; //during initialization, system is at gear down.
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);
}
//Gear down//
void Gear_down(void)//FUNCTION DEFINITION
{
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);//CLEARING CACHE
scanf("%d %d",&pilot_lever,&squat_switch);
if(pilot_lever == raising && squat_switch == open) // if pilot lever raising(0) and squar switch is open(0)
{
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
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
}
else
{
Gear_down(); //gear down function is called
}
}
//checking before takeoff//
void Checking_before_takeoff()//FUNCTION DECLARATION
{
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);//CACHE MEMORY CLEANING
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
{
Gear_down(); //gear down function is called
}
else if (pilot_lever== up && squat_switch == open && hydraulic_mechanism == working)//CONDITION CHECK
{
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)//CONDITION CHECK
{
current_state= RAISING_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);
printf("\n Gas pressurised spring status:%s\n",state_machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]);
}
}
//raising gear//
void Raising_gear()//FUNCTION DEFINITION
{
current_state = RAISING_GEAR;
printf("\n Enter the switch status and pilot lever status:\n");
fflush(stdout);//CACHE MEMORY CLEANING
scanf("%d %d",&limit_switch,&pilot_lever);
printf("\n wait for 1 sec\n");
if(pilot_lever == falling && limit_switch == open)//CONDITION CHECK
{
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)//CONDITION CHECK
{
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("\nHydraulic condition for landing gear status is %s\n",state_machine[current_state].hydraulic_pump_ctrl_status);
}
else
{
Raising_gear();//FUNCTION CALLING
}
}
//gear up//
void Gear_up()//FUNCTION DEFINITION
{
current_state= GEAR_UP;
printf("\n Enter the pilot lever position :\n");
fflush(stdout);
scanf("%d",&pilot_lever);
if (pilot_lever == falling )//CONDITION CHECK
{
current_state= CHECK_BEFORE_LANDING;
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("\nHydraulic condition for landing gear status is %s\n",state_machine[current_state].hydraulic_pump_ctrl_status);
}
else
{
Gear_up();//FUNCTION CALLING
}
}
//checking before landing//
void Checking_before_landing()//FUNCTION DEFINITION
{
current_state= CHECK_BEFORE_LANDING;
printf("\n Enter the pilot lever position, status of hydraulic mechanism :\n");
fflush(stdout);//CLEANING THE CACHE
scanf("%d %d",&pilot_lever, &hydraulic_mechanism);
if(pilot_lever == down && hydraulic_mechanism == working)//CONDITION CHECK
{
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)//CONDITION CHECK
{
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 pressurised spring status:%s\n",state_machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]);
}
if(pilot_lever== (up || raising || falling))//CONDITION CHECK
{
Checking_before_landing();//FUNCTION CALLED
}
}
//lowering gear//
void Lowering_gear()//FUNCTION DEFINITION
{
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)
{
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)//CONDITION CHECK
{
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();//FUNCTION CALLED
}
}
//main function//
int main()//MAIN FUNCTION
{
Initial_state_machine();//FUNCTION CALLED
while(1)//UNTIL THE CONDITIONS BECOMES FALSE LOOPING IS DONE
{
State_Table[current_state]();//FUNCTION CALLED
}
}