Challenge

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

#include<stdio.h> //preprocessor

typedef enum st //typedef enum types for State_types in airplane Gear
{
GEAR_DOWN,
INSPECT_GEAR_BEFORE_TAKEOFF,
GEAR_RISING,
GEAR_UP,
INSPECT_GEAR_BEFORE_LAND,
LOWERING_GEAR
}State_Type; //enum name
typedef enum p //typedef enum types for State_types in airplane Gear
{
Rising,
Falling
}Lever; //enum name
typedef enum s //typedef enum types for State_types in airplane Gear
{
on,
off
}Switch; //enum name
typedef enum pr //typedef enum types for State_types in airplane Gear
{
Working,
Not_Working
}Pressure_System; //enum name

static volatile Lever Gear_Lever; //declaring volatile varible from typedef enum Lever
static volatile Switch Gear_Switch; //declaring volatile varible from typedef enum Switch
static volatile Pressure_System System_Status; //declaring volatile varible from typedef enum Pressure System

State_Type current;

typedef struct //structure definition as typedef
{
char *Current_State; //structure members
char *light;
char *Gear_Valve_Direction;
char *Pressure_Sys;
char *H_Status;

}State; //struct variable

State State_Table[6]= //declaring array of structure from above variable state
{
{"GEAR_DOWN", "GREEN", "DOWN", "DISABLED", "NOT_WORKING"},
{"INSPECTING_BEFORE_TAKEOFF", "GREEN", "DOWN", "ENABLED", "WORKING"},
{"GEAR_RISING", "RED", "UP", "ENABLED", "WORKING"},
{"GEAR_UP", "OFF", "NULL", "DISABLED", "NULL"},
{"INSPECTING_BEFORE_LANDING", "RED", "DOWN", "ENABLED", "WORKING"},
{"LOWERING_GEAR", "RED", "NULL", "NULL", "NULL"}

};

void Init_Machine(void) { //called function for starting machine
current=GEAR_DOWN; //initializing current State
printf("**FSM BASED MACHINE IS INITIALIZED AND STARTED**\n"); //o/p Statement
printf("\n");//o/p Statement for new line
printf("The Initial State is %s\n", State_Table[current].Current_State); //o/p Statement for Current State
printf("------------------------------------------------\n");//o/p Statement
}
void Geardown(void)//called function for Geardown State
{
current=GEAR_DOWN; //initializing current State
printf("The Current State is %s\n", State_Table[current].Current_State);//o/p Statement for Current State
printf("Enter the Values of Lever and Switch: "); //o/p Statement
scanf("%d%d", &Gear_Lever, &Gear_Switch);//i/p Statement
printf("\n");//o/p Statement
if(Gear_Lever==Falling && Gear_Switch==off) //checking if input is 1 & 1
{
current=INSPECT_GEAR_BEFORE_TAKEOFF; //initializing current State after if statemetnt
printf("Wait....\n");
sleep(1);
printf("The State is Changing.....\n");
sleep(2);
printf("The Current state is %s\n", State_Table[current].Current_State);//o/p Statement for Current State
printf("The Light is %s\n", State_Table[current].light);//o/p Statement for light State
printf("The Direction of the Gear Valve is %s\n", State_Table[current].Gear_Valve_Direction);//o/p Statement for Direction of Gear
printf("**Going to Next States**\n");//o/p Statement
printf("------------------------------------------------\n");//o/p Statement
} else {
printf("Back To->Geardown()_State\n");//o/p Statement
Geardown();
}
}
void CheckTakeoff(void)//called function for checkingbefore Takeoff
{
current=INSPECT_GEAR_BEFORE_TAKEOFF; //initializing current State
printf("The Current State is %s\n", State_Table[current].Current_State);//o/p Statement for Current State
printf("Enter the Values of Lever and Switch: ");//o/p Statement
scanf("%d%d", &Gear_Lever, &Gear_Switch);//i/p Statement
if(Gear_Lever==Falling && Gear_Switch==off) //checking if input is 1 & 1
{
printf("Back to->Geardown()_State\n");
Geardown();
}
else if(Gear_Lever==Falling && Gear_Switch==on) //checking if input is 1 & 0
{
current=GEAR_RISING;//initializing current State after if statemetnt
printf("Wait....\n");
sleep(1);
printf("The State is Changing.....\n");
sleep(2);
printf("The Current State is %s\n", State_Table[current].Current_State);//o/p Statement for Current State
printf("The Light is %s\n", State_Table[current].light);//o/p Statement for light State
printf("The Direction of the Valve is %s\n", State_Table[current].Gear_Valve_Direction);//o/p Statement for Direction of Gear
printf("The Pressure System is %s and %s\n", State_Table[current].Pressure_Sys, State_Table[current].H_Status);//o/p Statement for Pressure System State
printf("**Going to Next States**\n");//o/p Statement
printf("------------------------------------------------\n");//o/p Statement
}
else {
printf("------------------------------------------------\n");//o/p Statement
printf("Values are Invalid\n");//o/p Statement
CheckTakeoff();
}
}
void Gearrising(void)//called function for Rising Gear
{
current=GEAR_RISING; //initializing current State
printf("The Current State is %s\n", State_Table[current].Current_State);//o/p Statement for Current State
printf("Enter the Values of Lever and Switch: ");//o/p Statement
scanf("%d%d", &Gear_Lever, &System_Status);//i/p Statement
if(Gear_Lever==Falling){//checking if input is 1
printf("Back to->CheckTakeoff()_State\n");//o/p Statement
CheckTakeoff();
}else if(Gear_Lever==Rising && System_Status==Not_Working){//checking if input is 0 & 1
current=GEAR_UP;//initializing current State after if statemetnt
printf("Wait....\n");
sleep(1);
printf("The State is Changing.....\n");
sleep(2);
printf("The Current State is %s\n", State_Table[current].Current_State);//o/p Statement for Current State
printf("The Light is %s\n", State_Table[current].light);//o/p Statement for light State
printf("The Direction of the Gear is %s\n", State_Table[current].Gear_Valve_Direction);//o/p Statement for Direction of Gear
printf("The Pressure System is %s\n", State_Table[current].Pressure_Sys);//o/p Statement for Pressure System State
printf("**Going to Next States**\n");//o/p Statement
printf("------------------------------------------------\n");//o/p Statement
} else
{
printf("------------------------------------------------\n");//o/p Statement
printf("Values are Invalid\n");//o/p Statement
Gearrising();
}
}
void Gearup(void)//called function for GearUp
{
current=GEAR_UP; //initializing current State
printf("The Currrent State is %s\n", State_Table[current].Current_State);//o/p Statement for Current State
printf("Enter the Values of Lever and Pressure System: ");//o/p Statement
scanf("%d%d", &Gear_Lever, &System_Status);//i/p Statement
if(Gear_Lever==Falling)//checking if input is 1
{
printf("Back to->Checkland()_State\n");
Gearrising();
}
else if(Gear_Lever==Rising && System_Status==Working) //checking if input is 0 & 1
{
current=INSPECT_GEAR_BEFORE_LAND;//initializing current State after if statemetnt
printf("Wait....\n");
sleep(1);
printf("The State is Changing.....\n");
sleep(2);
printf("The Current State is %s\n", State_Table[current].Current_State);//o/p Statement for Current State
printf("The Light is %s\n", State_Table[current].light);//o/p Statement for light State
printf("The Direction of the Gear is %s\n", State_Table[current].Gear_Valve_Direction);//o/p Statement for Direction of Gear
printf("The Pressure System is %s and %s\n", State_Table[current].Pressure_Sys, State_Table[current].H_Status);//o/p Statement for Pressure System State
printf("**Going to Next States**\n");//o/p Statement
printf("------------------------------------------------\n");//o/p Statement

}
else
{
printf("------------------------------------------------\n");//o/p Statement
printf("Values are Invalid\n");//o/p Statement
Gearup();
}
}
void Checkland(void)//called function for Checking Before Landing
{
current=INSPECT_GEAR_BEFORE_LAND; //initializing current State
printf("The Current State is %s\n", State_Table[current].Current_State);//o/p Statement for Current State
printf("Enter the Values of Lever and Pressure System: ");//o/p Statement
scanf("%d%d", &Gear_Lever, &System_Status);//i/p Statement
if(Gear_Lever==Rising && System_Status==Working) //checking if input is 0 & 0
{
printf("Back to->Gearup()_State\n");//o/p Statement
Gearup();
}
else if(Gear_Lever==Falling && System_Status==Working) //checking if input is 1 & 1
{
current=LOWERING_GEAR;//initializing current State after if statemetnt
printf("Wait....\n");
sleep(1);
printf("The State is Changing.....\n");
sleep(2);
printf("The Current State is %s\n", State_Table[current].Current_State);//o/p Statement for Current State
printf("The Light is %s\n", State_Table[current].light);//o/p Statement for light State
printf("The Direction of the Valve is %s\n", State_Table[current].Gear_Valve_Direction);//o/p Statement for Direction of Gear
printf("The Pressure System is %s\n", State_Table[current].H_Status);
printf("**Going to Next States**\n");//o/p Statement
printf("------------------------------------------------\n");//o/p Statement
}
else
{
printf("------------------------------------------------\n");//o/p Statement
printf("Values are Invalid\n");//o/p Statement
Checkland();
}
}
void Gearlowering(void)//called function for Lowering Gear
{
current=LOWERING_GEAR; //initializing current State
printf("The Current State is %s\n", State_Table[current].Current_State);//o/p Statement for Current State
printf("Enter the Values of Lever and Switch: ");//o/p Statement
scanf("%d%d", &Gear_Lever, &Gear_Switch);//i/p Statement
if(Gear_Lever==Falling && Gear_Switch==off) //checking if input is 1 & 1
{
printf("Wait....\n");
sleep(1);
printf("The State is Changing.....\n");
sleep(2);
printf("Back to the First State i.e Gear_Down()_State\n"); //o/p Statement
printf("The Current state is %s\n", State_Table[current].Current_State);//o/p Statement for Current State
printf("The Light is %s\n", State_Table[current].light);//o/p Statement for light State
printf("The Direction of the Gear Valve is %s\n", State_Table[current].Gear_Valve_Direction);//o/p Statement for Direction of Gear
printf("------------------------------------------------\n");
printf("***System is Restarting To Function***\n");//o/p Statement
printf("------------------------------------------------\n");
Init_Machine();
Geardown();
} else {
printf("Back to->Checkland()_State \n");
Checkland();
}
printf("***System is Restarting To Function***\n");//o/p Statement
sleep(3);
Init_Machine();
}

int main() //main function
{
Init_Machine(); //calling function
static (*state_table[6])(void) = {Geardown, CheckTakeoff, Gearrising, Gearup, Checkland, Gearlowering}; //array of function pointers
while(1) //iterating while (1)
{
state_table[current]();//calling array of functions
}
return 0; //returning zero to the return type 'int'
}

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.