Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
ANS:
//We have to include stdio.h (standard input output library for C language)
#include
//We have to include stdlib.h (for memory allocation)
#include
/* Functions used in the projects*/
void Initial_state_machine(); //function declaration for Initial_state_machine here,no return type,no argument
void Gear_down(); //function declaration for Gear_down here,no return type,no argument
void Checking_before_takeoff(); //function declaration for Checking_before_takeoff here,no return type,no argument
void Raising_gear(); //function declaration for Raising_gear here,no return type,no argument
void Gear_up(); //function declaration for Gear_up here,no return type,no argument
void Checking_before_landing(); //function declaration for Checking_before_landing here,no return type,no argument
void Lowering_gear(); //function declaration for Lowering_gear here,no return type,no argument
/*create a structure with typedef enum
enum is used t giving subsequent value for each states
*/
typedef enum states
{
//below six are the Declaration with members of enum
GEAR_DOWN,
CHECK_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR
//state_type is name of the structure
}state_type;
//create a structure(typedef) with enum data type for switch
typedef enum Switch
{
open, //open=0
close //close=1 plane is in ground
//switch_status is name of the structure
}Switch_status;
//create a structure(typedef) with enum data type for pilot_lever
typedef enum pilot_lever
{
raising, // rising = 0 is assumed
falling, //falling = 1
up, //up = 2
down //down = 3
//pilot_lever_status is name of the structure
}pilot_lever_status;
//create a structure(typedef) with enum data type for hydraulic_mechanism
typedef enum hydraulic_mechanism
{
working, //here 0 means for working
malfunctioning //1 means for malfunctioning
}hydraulic_mechanism_status;
/* input used in order to change the state */
/*in all cases static volatile is used */
static volatile Switch_status squat_switch; //Declaration for Switch_status of squat_switch
static volatile Switch_status limit_switch; //Declaration for Switch_status of limit_switch
static volatile pilot_lever_status pilot_lever; //Declaration for pilot_lever_status of pilot lever
static volatile pilot_lever_status hydraulic_mechanism; //Declaration for pilot_lever_status of hydraulic_mechanism
state_type current_state; //state type is current state
// structure is used to have different types of data type
typedef struct
{
//output which we want to visualize are given under structure as structure member.
char* current_state_status; //current_state_status is a parameter of character data type
char* light_status; //light status is a parameter of character data type
char* direction_value_status; //direction_value_status is a parameter of character data type
char* hydraulic_pump_ctrl_status; //hydraulic_pump_ctrl_status is a parameter of character data type
char* gas_pressurized_spring_status[2]; //gas_pressurized_spring_status is a parameter of character data type with having size 2
}state_table; //name of the structure is state table
/* State table of the require system*/
//create a array of structure, having six inputs of each state
static state_table state_machine[6] = {
/*here,gear down = current_state_status,green = light_status,down = direction,
enable = hydraulic_pump,(0,0) = gas_pressurized */
{"gear down","green","down","enables",{0,0}},
/*here,checking before take off = current_state_status,green = light_status,
down = direction,enable = hydraulic_pump,(0,0) = gas_pressurized*/
{"checking before take off","green","down","enabled",{0,0}},
/*here,raising gear = current_state_status,red = light_status,up = direction,
enable = hydraulic_pump,(disabled,enable) = gas_pressurized */
{"raising gear","red","up","enabled",{"disabled","enabled"}},
/*here,gear up = current_state_status,off = light_status,NULL = direction,
disabled = hydraulic_pump,(0,0) = gas_pressurized*/
{"gear up","off","NULL","disabled",{0,0}},
/*here,checking before landing = current_state_status,red = light_status,
down = direction,enabled = hydraulic_pump,(0,0) = gas_pressurized*/
{"checking before landing","red","down","enabled",{0,0}},
/*here,lowering gear = current_state_status,green = light_status,down = direction,
enabled = hydraulic_pump,(disabled,enable) = gas_pressurized*/
{"lowering gear","green","down","enabled",{"disabled","enabled"}}
};
//function declaration call inside the main function,definition
static void(*State_Table[])(void)={Gear_down,Checking_before_takeoff,Raising_gear,
Gear_up,Checking_before_landing,Lowering_gear};
//initial state machine//
void Initial_state_machine() //create a function for Initial_state_machine
{
current_state = GEAR_DOWN; //during initialization, system is at gear down.
//print the input state_machine[current_state].light_status from the state table
printf("state machine is initials and at first program is in gear down and its light status is:%s",state_machine[current_state].light_status);
}
//create a function for Gear down
void Gear_down(void)
{
current_state =GEAR_DOWN; //during initialization, system is at gear down.
printf("\nEnter the pilot's lever position and squat switch status:"); // getting pilot lever and squat switch value from input
fflush(stdout); //to read the input
scanf("%d %d",&pilot_lever,&squat_switch); //to read the input of pilot lever and squat switch
if(pilot_lever == raising && squat_switch == open) // if pilot lever raising(0) and squar switch is open(0),condition to be applied
{
current_state = CHECK_BEFORE_TAKEOFF; //here,current state is CHECK_BEFORE_TAKEOFF
printf(" The current state of the machine is: %s",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 //if condition not get satisfied,remain in the gear down
{
Gear_down(); //gear down function is called
}
}
//create a function for 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: "); //getting pilot liver, squat switch and hydraulic mechanism as input
fflush(stdout); //to read the input
scanf("%d %d %d",&pilot_lever, &squat_switch,&hydraulic_mechanism); //to read the input for pilot lever,squat switch,hydraulic mechanism
printf(" just wait 2secs"); //print just wait 2seconds
if (pilot_lever == up && squat_switch== close) //pilot lever is up and squat switch is closed it get into the if loop,condition t be applied
{
Gear_down(); //gear down function is called
}
else if (pilot_lever== up && squat_switch == open && hydraulic_mechanism == working) //if if condition not get satisfied,then pilot lever =up,squat_switch == open,hydraulic_mechanism == working
{
current_state= RAISING_GEAR; //the current state is raising gear
printf("\n Current state: %s",state_machine[current_state].current_state_status); //print the current state
printf("light status: %s", state_machine[current_state].light_status); //print light status
printf("\n direction status: %s",state_machine[current_state].direction_value_status); //print the direction status
}
else if (pilot_lever== up && squat_switch == open && hydraulic_mechanism == malfunctioning) //else if pilot_lever== up,squat_switch == open, hydraulic_mechanism == malfunction get true
{
current_state= RAISING_GEAR; //the current state is raising gear
printf("\n Current state: %s", state_machine[current_state].current_state_status); //print the current state
printf("\n light status: %s", state_machine[current_state].light_status); //print light status
printf("\n direction status: %s",state_machine[current_state].direction_value_status); //print the direction status
printf("\n Gas pressurised spring status: %s\n",state_machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]); //print the Gas pressurised spring status
}
}
//create a function for raising gear
void Raising_gear()
{
current_state = RAISING_GEAR; //in this the current state is raising gear
printf("\n Enter the switch status and pilot lever status: "); //getting pilot liver, squat switch and hydraulic mechanism as input
fflush(stdout);
scanf("%d %d",&limit_switch,&pilot_lever); //to read the limit switch and pilot lever
printf(" wait for 1 sec"); //print wait for 1second
if(pilot_lever == falling && limit_switch == open) //pilot_lever == falling,limit_switch == open condition to be satisfied
{
current_state = CHECK_BEFORE_LANDING; //current state is check before landing
printf("\n current state is: %s",state_machine[current_state].current_state_status); //print the current state
printf("\n Light status: %s",state_machine[current_state].light_status); //print the light status
printf("\n Hydraulic condition for landing gear status is: %s",state_machine[current_state].hydraulic_pump_ctrl_status); //print the Hydraulic condition for landing gear status
}
else if(limit_switch == close && pilot_lever == up) //if if condition not get satisfied, condition: limit_switch == close,pilot_lever == up
{
current_state= GEAR_UP; //current state is gear up
printf("\n current state is:%s",state_machine[current_state].current_state_status); //print the current state
printf("\n Light status:%s",state_machine[current_state].light_status); //print the light status
printf("\n Hydraulic condition for landing gear status is %s\n",state_machine[current_state].hydraulic_pump_ctrl_status); //print Hydraulic condition for landing gear status
}
else //else,it is in raising gear
{
Raising_gear();
}
}
//create a function for gear up
void Gear_up()
{
current_state= GEAR_UP; //the current state is gear up
printf("\n Enter the pilot lever position :"); //getting the pilot lever position
fflush(stdout);
scanf("%d",&pilot_lever); //to read the pilot lever
if (pilot_lever == falling ) //condition to satisfied: pilot_lever == falling
{
current_state= CHECK_BEFORE_LANDING; //current state is check before landing
printf(" Current state: %s", state_machine[current_state].current_state_status); //print current state
printf("\n light status: %s", state_machine[current_state].light_status); //print light status
printf("\n Hydraulic condition for landing gear status is: %s\n",state_machine[current_state].hydraulic_pump_ctrl_status); //print Hydraulic condition for landing gear status
}
else //else it is in gear up only
{
Gear_up();
}
}
//create a function for checking before landing
void Checking_before_landing()
{
current_state= CHECK_BEFORE_LANDING; //current state is check before landing
printf("\n Enter the pilot lever position, status of hydraulic mechanism :"); //getting the pilot lever position, status of hydraulic mechanism
fflush(stdout);
scanf("%d %d",&pilot_lever, &hydraulic_mechanism); //to read the pilot lever and hydraulic mechanism
if(pilot_lever == down && hydraulic_mechanism == working) //condition: pilot_lever == down,hydraulic_mechanism == working
{
current_state= LOWERING_GEAR; //current state is lowering gear
printf(" Current state: %s", state_machine[current_state].current_state_status); //print the current state
printf("\n light status: %s", state_machine[current_state].light_status); //print the light status
printf("\n direction status: %s",state_machine[current_state].direction_value_status); //print the direction
}
else if (pilot_lever== down && hydraulic_mechanism == malfunctioning) //else if pilot_lever== down,hydraulic_mechanism == malfunctioning
{
current_state= LOWERING_GEAR; //the current state is lowering gear
printf("\n Current state: %s", state_machine[current_state].current_state_status); //print current state
printf("\n light status: %s", state_machine[current_state].light_status); //print light status
printf("\n direction status: %s",state_machine[current_state].direction_value_status); //print direction
printf("\n Gas pressurised spring status: %s\n",state_machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]); //print Gas pressurised spring status
}
if(pilot_lever== (up || raising || falling)) //here,OR operator is used if pilot_lever==up || raising || falling
{
Checking_before_landing(); //it is in checking before landing
}
}
//create a function for lowering gear
void Lowering_gear()
{
current_state= LOWERING_GEAR; //current state is lowering gear
printf("\n Enter the pilot lever position, limit switch :");
fflush(stdout);
scanf("%d %d",&pilot_lever, &limit_switch); //to read pilot lever,limit switch
if (pilot_lever == raising && limit_switch==close) //condition to satisfied pilot_lever == raising,limit_switch==close
{
current_state= CHECK_BEFORE_TAKEOFF; //current state is check before take off
printf(" Current state: %s", state_machine[current_state].current_state_status); //print current state
printf("\n light status: %s", state_machine[current_state].light_status); //print light status
}
if (pilot_lever== down && limit_switch == open) //condition to satisfied pilot_lever== down,limit_switch == open
{
current_state= GEAR_DOWN; //current state is gear down
printf("\n Current state: %s", state_machine[current_state].current_state_status); //print the current state
printf("\n light status: %s", state_machine[current_state].light_status); //print the light status
printf("\n Hydraulic condition for landing gear status is: %s",state_machine[current_state].hydraulic_pump_ctrl_status); //print Hydraulic condition for landing gear status
}
else //else it is in lowering gear
{
Lowering_gear();
}
}
//main function or driving function
int main()
{
Initial_state_machine(); //calling function of initial state
while(1) //loop get run continuous till input 1
{
State_Table[current_state](); //calling function of state table
}
}
OUTPUT:

