Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include<stdio.h> //Standard library function
#include<stdlib.h>//standard library Used for functions and type conversions
//Declaring the functions
void GearUp();
void GearDown();
void RaisingGear();
void LoweringGear();
void CheckBeforeLanding();
void CheckBeforeTakeoff();
//Creating a enum of required state
typedef enum State{
GEAR_DOWN,
CHECKING_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECKING_BEFORE_LANDING,
LOWERING_GEAR
}State_type;
//Creating a enum for switch
typedef enum Switch{
on,
off
}Switch_status;
//Creating a enum for pilot lever
typedef enum pilot_lever{
Raising,
Falling
}pilot_lever;
//Creating a enum for hydraulic mechanism
typedef enum hydraulic_mech{
working,
not_working
}hydraulic_mech;
//Creating object names for four enums
static volatile Switch_status squat_switch;
static volatile Switch_status limit_switch;
static volatile pilot_lever pl;
static volatile hydraulic_mech hm;
//State table
static void(*statetable[])(void)={GearDown,CheckBeforeTakeoff,RaisingGear,GearUp,CheckBeforeLanding,LoweringGear};
State_type current_state;
//Defining the structure to control gears
typedef struct
{
char *current_state_indication;
char *light;
char *direction_valve_status;
char *landing_gear_hydraulic_control;
char *gas_pressurized_spring_system[2];
}state_table;
//Creating object name for state_table which is state machine,contains six different outputs
static state_table state_machine[6]={{"GEARDOWN","GREEN","DOWN","ENABLED",{"NULL,NULL"}},
{"CHECKINGBEFORETAKEOFF","GREEN","DOWN","ENABLED",{"NULL","NULL"}},
{"RAISINGGEAR","RED","UP","ENABLED",{"NULL","NULL"}},
{"GEARUP","OFF","NULL","DISABLED",{"NULL","NULL"}},
{"CHECKBEFORELANDING","RED","DOWN","ENABLED",{"NULL","NULL"}},
{"LOWERINGGEAR","GREEN","DOWN","ENABLED",{"Disabled","Enabled"}}
};
//Main Function
int main()
{
current_state=GEAR_DOWN;//Current state is gear down
printf("The current state machine is Intialized and is in Gear down and the Light is :%s\n",state_machine[current_state].light);
while(1)
{
statetable[current_state]();
}
return 0;
}
//GEAR DOWN
void GearDown()
{
current_state= GEAR_DOWN;
printf("Enter the status of pilot's lever and squat switch : ");
fflush(stdout);
scanf("%d %d",&pl,&squat_switch);
if(pl==Raising && squat_switch==on)// INPUT 0,0
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("current_state : %s\n",state_machine[current_state].current_state_indication);
printf("Light is : %s\n",state_machine[current_state].light);
}
else
{
GearDown();
}
}
//CHECKING BEFORE TAKE OFF
void CheckBeforeTakeoff(){
current_state= CHECKING_BEFORE_TAKEOFF;
printf("Enter the status of pilot's lever and squat switch and hydraulic mechanism : ");
fflush(stdout);
scanf("%d %d",&pl,&squat_switch,&hm);
if(pl==Falling && squat_switch==off) //If lever is falling goes back to gear down
{
GearDown();
}
else if(pl == Raising && squat_switch == on && hm == working)//Input 0,0
{
current_state= RAISING_GEAR;
printf("current_state : %s\n",state_machine[current_state].current_state_indication);
printf("Light is : %s\n",state_machine[current_state].light);
printf("Status of direction : %s\n",state_machine[current_state].direction_valve_status);
}
else if(pl == Raising && squat_switch == on && hm == not_working)
{
current_state= RAISING_GEAR;
printf("current_state : %s\n",state_machine[current_state].current_state_indication);
printf("Light is : %s\n",state_machine[current_state].light);
printf("Status of Direction : %s\n",state_machine[current_state].direction_valve_status);
printf("Status of Gas Pressurizsed Spring System : %s\n",state_machine[current_state].gas_pressurized_spring_system);
}
}
//RAISING GEAR
void RaisingGear(){
current_state = RAISING_GEAR;
printf("Enter the status of pilot's lever and limit switch : ");
fflush(stdout);
scanf("%d %d",&pl,&limit_switch);
if(pl == Falling && limit_switch == on)//Check before landing
{
current_state= CHECKING_BEFORE_LANDING;
printf("current_state : %s\n",state_machine[current_state].current_state_indication);
printf("Light is : %s\n",state_machine[current_state].light);
printf("Landing Gear Hydraulic Control : %s\n",state_machine[current_state].landing_gear_hydraulic_control);
}
else if(pl == Raising && limit_switch == off )//Input 0,1
{
current_state= GEAR_UP;
printf("current_state : %s\n",state_machine[current_state].current_state_indication);
printf("Light is : %s\n",state_machine[current_state].light);
printf("Landing Gear Hydraulic Control : %s\n",state_machine[current_state].landing_gear_hydraulic_control);
}
else{
RaisingGear();
}
}
//GEAR UP
void GearUp()
{
current_state = GEAR_UP;
printf("Enter the status of pilot's lever : ");
fflush(stdout);
scanf("%d",&pl);
if(pl == Falling)//Input 1
{
current_state= CHECKING_BEFORE_LANDING;
printf("current_state : %s\n",state_machine[current_state].current_state_indication);
printf("Light is : %s\n",state_machine[current_state].light);
printf("Landing Gear Hydraulic Control : %s\n",state_machine[current_state].landing_gear_hydraulic_control);
}
else{
GearUp();
}
}
//CHECKING BEFORE LANDING
void CheckBeforeLanding()
{
current_state = CHECKING_BEFORE_LANDING;
printf("Enter the status of pilot's lever and Hydraulic Mechanism : ");
fflush(stdout);
scanf("%d %d",&pl,&hm);
if(pl == Falling && hm == working)//Input 1,0 hm working
{
current_state=LOWERING_GEAR;
printf("current_state : %s\n",state_machine[current_state].current_state_indication);
printf("Light is : %s\n",state_machine[current_state].light);
printf("Status of direction : %s\n",state_machine[current_state].direction_valve_status);
}
else if(pl == Falling && hm == not_working)//hm not working ,Input 1,1
{
current_state=LOWERING_GEAR;
printf("current_state : %s\n",state_machine[current_state].current_state_indication);
printf("Light is : %s\n",state_machine[current_state].light);
printf("Status of Direction : %s\n",state_machine[current_state].direction_valve_status);
printf("Status of Gas Pressurised Spring System : %s\n",state_machine[current_state].gas_pressurized_spring_system);
}
else if(pl == (Raising))//0
{
CheckBeforeLanding();
}
}
//LOWERING GEAR
void LoweringGear()
{
current_state=LOWERING_GEAR;
printf("Enter the status of pilot's lever and limit_switch : ");
fflush(stdout);
scanf("%d %d",&pl,&limit_switch);
if(pl == Falling && limit_switch == on)//1 0
{
current_state= CHECKING_BEFORE_TAKEOFF;
printf("current_state : %s\n",state_machine[current_state].current_state_indication);
printf("Light is : %s\n",state_machine[current_state].light);
}
else if(pl == Raising && limit_switch == off)//0 1
{
current_state = GEAR_DOWN;
printf("current_state : %s\n",state_machine[current_state].current_state_indication);
printf("Light is : %s\n",state_machine[current_state].light);
printf("Landing Gear Hydraulic Control : %s\n",state_machine[current_state].landing_gear_hydraulic_control);
}
else if(limit_switch == off)//If off lowering gear
{
LoweringGear();
}
}