Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include <stdio.h>//preprocessor
#include <stdlib.h>
//function prototypes
void Initiate_State_Machine();
void GearDown();
void CheckingBeforeTakeOFF();
void RaisingGear();
void GearUp();
void CheckBeforeLanding();
void LoweringGear();
static void(*statetable[])(void)= {GearDown, CheckingBeforeTakeOFF, RaisingGear, GearUp, CheckBeforeLanding, LoweringGear};
//creating enum type structure to creating the alise number to our function
typedef enum State
{
GEAR_DOWN,
CHECKING_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR,
} State_Type;
typedef enum Switch
{
ON,
off
} Switch_status;
typedef enum pilot_lever
{
Raising,
falling
} pilot_lever;
typedef enum hydraulic_mechanism
{
working,
not_working
} hydraulic_mechanism;
//object name for the enum structures
static volatile Switch_status squat_switch;
static volatile Switch_status limit_switch;
static volatile pilot_lever pl;
static volatile hydraulic_mechanism hm;
State_Type current_state;
typedef struct
{
char* current_state_indication;
char* light;
char* direction_valve_status;
char* landing_gear_hydraulic_control;
char* Gas_Pressurized_Spring_system_status[2];
} State_Table;
static State_Table State_Machine[6]= {{"GearDown","Green","Down","Enabled",{NULL,NULL}}, //object named as statemachine
{"CheckingBeforeTakeOFF","Green","Down","Enabled",{NULL,NULL}},
{"RaisingGear","Red","Up","Enabled",{NULL,NULL}},
{"GearUp","Off","NULL","Disbled",{NULL,NULL}},
{"CheckBeforeLanding","Red","Down","Enabled",{NULL,NULL}},
{"LoweringGear","Green","Down","Enabled",{"Disabled","Enabled"}}
};
int main() //main function
{
Initiate_State_Machine();
while(1)
{
statetable[current_state]();
}
}
void GearDown(void) //geardown function
{
current_state=GEAR_DOWN;
printf("Enter the status of pilots lever & squat switch =");
scanf("%d %d",&pl,&squat_switch);
if(pl==Raising && squat_switch==ON)
{
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();
}
}
void CheckingBeforeTakeOFF(void)//CheckingBeforeTakeOFF function
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("Enter the status of pilots lever & squat switch & hydraulic mechanism =");
scanf("%d %d",&pl,&squat_switch,&hm);
if(pl==falling&&squat_switch==off)
{
GearDown();
}
else if(pl==Raising && squat_switch==ON && hm==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 valve:%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 valve:%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_status);
}
}
void RaisingGear(void)//RaisingGear function
{
current_state=RAISING_GEAR;
printf("Enter the status of pilots lever & limit switch =");
scanf("%d %d",&pl,&limit_switch);
if(pl==falling && limit_switch==ON)
{
current_state=CHECK_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)
{
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();
}
}
void GearUp(void)//GearUp function
{
current_state=GEAR_UP;
printf("Enter the status of pilots lever:");
scanf("%d",&pl);
if(pl==falling)
{
current_state=CHECK_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();
}
}
void CheckBeforeLanding(void)//CheckBeforeLanding function
{
current_state=CHECK_BEFORE_LANDING;
printf("Enter the status of pilots lever and hydraulic mechanism status =");
scanf("%d %d",&pl,&hm);
if(pl==falling && 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 valve:%s\n",State_Machine[current_state].direction_valve_status);
}
else if(pl==falling && hm==not_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 valve:%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_status);
}
else if(pl==(Raising))
{
CheckBeforeLanding();
}
}
void LoweringGear(void)//LoweringGear function
{
current_state=LOWERING_GEAR;
printf("Enter the status of pilots lever and limit switch =");
scanf("%d %d",&pl,&limit_switch);
if(pl==falling && limit_switch==ON)
{
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)
{
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);
}
if(limit_switch==off)
{
LoweringGear();
}
}
void Initiate_State_Machine()//Initiate_State_Machine
{
current_state==GEAR_DOWN;
printf("The flight is in GearDown(parked) State & Light is %s\n",State_Machine[current_state].light);
}