Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include<stdio.h> //header file declaration
void GearDown(); //function declaration
void CheckingBeforeTakeoff();
void RaisingGear();
void GearUp();
void CheckingBeforeLanding();
void LoweringGear();
typedef enum {
GEAR_DOWN,
CHECKING_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECKING_BEFORE_LANDING,
LOWERING_GEAR
} State_Types; //different state types of FSM
typedef enum {
Open,
Close
} Switch; //user input for status of switch
typedef enum {
Rising,
Falling,
Up,
Down
} Pilot_lever; //user input for position of pilot lever
typedef enum {
working,
not_working
} hydraulic_Mechanism; //user input for status of hydraulic mechanism
static volatile int squat_switch; //variable declarations
static volatile int limit_switch;
static volatile int pilot; //pilot lever
static volatile int hydraulic_mechanism;
State_Types Currentstate;
typedef struct {
char* current_state; //current state of gear
char* light; //light color
char* direction_valve; //status of direction valve
char* hydraulic_control; //status of hydraulic valve
}state; //structure declaration named as state for outputs
static void(*state_table[])(void)={GearDown,
CheckingBeforeTakeoff,
RaisingGear,
GearUp,
CheckingBeforeLanding,
LoweringGear}; //functions are stored in an array of function pointer
static state state_machine[6]={
{"GearDown", "Green","Down","Enabled"},
{"CheckingBeforeTakeoff", "Green","Down", "Enabled"},
{"RaisingGear", "Red", "Up", "Enabled"},
{"GearUp", "off", "Null", "Disabled"},
{"CheckingBeforeLanding","Red","Down","Enabled"},
{"LoweringGear", "Green", "Down", "Enabled"}
}; //array of structure
int main() //initialization
{
Currentstate=GEAR_DOWN;
while(1)
{
state_table[Currentstate]();
}
}
//GearDown function
void GearDown()
{
Currentstate=GEAR_DOWN;
printf("\nEnter the value of pilot lever and squat switch");
fflush(stdout);
scanf("%d %d",&pilot,&squat_switch);
if(pilot==Rising&&squat_switch==Close)//possible values 0 1
{
Currentstate=CHECKING_BEFORE_TAKEOFF;
printf("\nCurrent state is %s\n",state_machine[Currentstate].current_state);
printf("\nLight indication is %s\n",state_machine[Currentstate].light);
printf("\nPosition of direction valve is %s\n",state_machine[Currentstate].direction_valve);
printf("\nHydraulic control is %s\n",state_machine[Currentstate].hydraulic_control);
}
else
{
GearDown();
}
}
//Checking Before Landing function
void CheckingBeforeTakeoff()
{
Currentstate=CHECKING_BEFORE_TAKEOFF;
printf("\nEnter the value of pilot lever,squat switch and hydraulic control ");
fflush(stdout);
scanf("%d %d %d",&pilot,&squat_switch,&hydraulic_mechanism);
if(pilot==Falling&&squat_switch==Close)//possible values 1 1
{
Currentstate=GEAR_DOWN;
printf("\nCurrent state is %s\n",state_machine[Currentstate].current_state);
printf("\nLight indication is %s\n",state_machine[Currentstate].light);
printf("\nPosition of direction valve is %s\n",state_machine[Currentstate].direction_valve);
printf("\nHydraulic control is %s\n",state_machine[Currentstate].hydraulic_control);
}
if(pilot==Up&&hydraulic_mechanism==working)//possible values 2 0
{
Currentstate=RAISING_GEAR;
printf("\nCurrent state is %s\n",state_machine[Currentstate].current_state);
printf("\nLight indication is %s\n",state_machine[Currentstate].light);
printf("\nPosition of direction valve is %s\n",state_machine[Currentstate].direction_valve);
printf("\nHydraulic control is %s\n",state_machine[Currentstate].hydraulic_control);
}
else
{
CheckingBeforeTakeoff();
}
}
//Raising Gear function
void RaisingGear()
{
Currentstate=RAISING_GEAR;
printf("\nEnter the value of pilot lever,limit switch ");
fflush(stdout);
scanf("%d %d",&pilot,&limit_switch);
if(pilot==Falling)//possible values 0
{
Currentstate=CHECKING_BEFORE_LANDING;
printf("\nCurrent state is %s\n",state_machine[Currentstate].current_state);
printf("\nLight indication is %s\n",state_machine[Currentstate].light);
printf("\nPosition of direction valve is %s\n",state_machine[Currentstate].direction_valve);
printf("\nHydraulic control is %s\n",state_machine[Currentstate].hydraulic_control);
}
if(limit_switch==Close)//possible values 1
{
Currentstate=GEAR_UP;
printf("\nCurrent state is %s\n",state_machine[Currentstate].current_state);
printf("\nLight indication is %s\n",state_machine[Currentstate].light);
printf("\nPosition of direction valve is %s\n",state_machine[Currentstate].direction_valve);
printf("\nHydraulic control is %s\n",state_machine[Currentstate].hydraulic_control);
}
}
//Gear Up function
void GearUp()
{
Currentstate=GEAR_UP;
printf("\nEnter the value of pilot lever ");
fflush(stdout);
scanf("%d",&pilot);
if(pilot==Falling)//possible values 1
{
Currentstate=CHECKING_BEFORE_LANDING;
printf("\nCurrent state is %s\n",state_machine[Currentstate].current_state);
printf("\nLight indication is %s\n",state_machine[Currentstate].light);
printf("\nPosition of direction valve is %s\n",state_machine[Currentstate].direction_valve);
printf("\nHydraulic control is %s\n",state_machine[Currentstate].hydraulic_control);
}
else
{
GearUp();
}
}
//Checking Before landing function
void CheckingBeforeLanding()
{
Currentstate=CHECKING_BEFORE_LANDING;
printf("\nEnter the value of pilot lever and hydraulic control ");
fflush(stdout);
scanf("%d %d",&pilot,&hydraulic_mechanism);
if(pilot==Down||hydraulic_mechanism==not_working)//possible values 3 1
{
Currentstate=LOWERING_GEAR;
printf("\nCurrent state is %s\n",state_machine[Currentstate].current_state);
printf("\nLight indication is %s\n",state_machine[Currentstate].light);
printf("\nPosition of direction valve is %s\n",state_machine[Currentstate].direction_valve);
printf("\nHydraulic control is %s\n",state_machine[Currentstate].hydraulic_control);
}
else
{
CheckingBeforeLanding();
}
}
//LoweringGear function
void LoweringGear(void)
{
Currentstate=LOWERING_GEAR;
printf("\nEnter the value of pilot lever and limit switch ");
fflush(stdout);
scanf("%d %d",&pilot,&limit_switch);
if(limit_switch==Open)//possible value 0
{
Currentstate=GEAR_DOWN;
printf("\nCurrent state is %s\n",state_machine[Currentstate].current_state);
printf("\nLight indication is %s\n",state_machine[Currentstate].light);
printf("\nPosition of direction valve is %s\n",state_machine[Currentstate].direction_valve);
printf("\nHydraulic control is %s\n",state_machine[Currentstate].hydraulic_control);
}
if(pilot==Rising)//possible values 0
{
Currentstate=CHECKING_BEFORE_TAKEOFF;
printf("\nCurrent state is %s\n",state_machine[Currentstate].current_state);
printf("\nLight indication is %s\n",state_machine[Currentstate].light);
printf("\nPosition of direction valve is %s\n",state_machine[Currentstate].direction_valve);
printf("\nHydraulic control is %s\n",state_machine[Currentstate].hydraulic_control);
}
else
{
LoweringGear();
}
}