Challenge

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

//header file
#include <stdio.h>

//function prototypes
void Initiate_State_Machine();
void GearDown();
void CheckingBeforeTakeOFF();
void RaisingGear();
void GearUp();
void CheckBeforeLanding();
void LoweringGear();

//enum function to describe the various states og landing gear
typedef enum State
{
GEAR_DOWN,
CHECKING_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECKING_BEFORE_LANDING,
LOWERING_GEAR
}State_type;

//enum function to describe the status of switches-squat switch and limit switch
typedef enum Switch{
off,
on
}Switch_status;

//enum function to describe the status of pilot lever
typedef enum Pilot_lever
{
falling,
raising
}pilot_lever_status;

//enum function to describe hydraulic mechanism
typedef enum Hydraulic_mechanism
{
not_working,
working
}hydraulic_mechanism;

//variable declaration of enum functions
static volatile Switch_status squat_switch;
static volatile Switch_status limit_switch;
static volatile pilot_lever_status pl;
static volatile hydraulic_mechanism hm;
State_type current_state;

//structure declaration to indicate the various status
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;

//array of structure definition
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"}}
};

//array of functions
static void(*statetable[])(void)={GearDown, CheckingBeforeTakeOFF, RaisingGear, GearUp, CheckBeforeLanding, LoweringGear};

//Initiate_State_Machine
void Initiate_State_Machine()
{
current_state=GEAR_DOWN;
printf("\n\nThe Finite State Machine is Intialized.");
}

//Gear down
void GearDown(void)
{
current_state=GEAR_DOWN;
printf("\n*****CURRENT STATUS*****\n");
printf("Current State: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n", State_Machine[current_state].light);
printf("\n\nEnter the status of pilot's lever and squat switch as per the directions given below: ");
printf("\nPilot lever status and the corresponding choice to be entered: Falling - 0, Raising - 1.");
printf("\nSquat switch status and the corresponding choice to be entered: Off - 0, On - 1.");
printf("\nEnter your choice: ");
fflush(stdout);
scanf("%d %d",&pl,&squat_switch);
if(pl==raising && squat_switch==on)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("\n*****CURRENT STATUS*****\n");
printf("current state: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n", State_Machine[current_state].light);
}
else
{

GearDown();
}
}

//CheckingBeforeTakeOFF
void CheckingBeforeTakeOFF(void)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("\n\nEnter the status of pilot's lever and squat switch and hydraulic mechanism as per the directions given below:");
printf("\nPilot lever status and the corresponding choice to be entered: Falling - 0, Raising - 1.");
printf("\nSquat switch status and the corresponding choice to be entered: Off - 0, On - 1.");
printf("\nHydraulic mechanism and the corresponding choice to be entered: Not working -0, working -1.");
printf("\nEnter your choice: ");
fflush(stdout);
scanf("%d %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("\n*****CURRENT STATUS*****\n");
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("\n*****CURRENT STATUS*****\n");
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);
}
}
//RaisingGear
void RaisingGear(void)
{
current_state=RAISING_GEAR;
printf("\n\nEnter the status of pilot's lever and limit switch as per the directions given below:");
printf("\nPilot lever status and the corresponding choice to be entered: Falling - 0, Raising - 1.");
printf("\nSquat limit status and the corresponding choice to be entered: Off - 0, On - 1.");
printf("\nEnter your choice: ");
fflush(stdout);
scanf("%d %d",&pl,&limit_switch);
if(pl==falling && limit_switch==on)
{
current_state=CHECKING_BEFORE_LANDING;
printf("\n*****CURRENT STATUS*****\n");
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("\n*****CURRENT STATUS*****\n");
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();
}
}
//GearUp
void GearUp(void)
{
current_state=GEAR_UP;
printf("\n\nEnter the status of pilot's lever as per the following: falling-0 raising -1 ");
fflush(stdout);
scanf("%d",&pl);
if(pl==falling)
{
current_state=CHECKING_BEFORE_LANDING;
printf("\n*****CURRENT STATUS*****\n");
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();
}

}
//CheckBeforeLanding
void CheckBeforeLanding(void)
{
current_state=CHECKING_BEFORE_LANDING;
printf("\n\nEnter the status of pilot's lever and hydraulic mechanism status as per the following instructions:");
printf("\nPilot lever status and the corresponding choice to be entered: Falling - 0, Raising - 1.");
printf("\nHydraulic mechanism and the corresponding choice to be entered: Not working -0, working -1.");
printf("\nEnter your choice: ");
fflush(stdout);
scanf("%d %d",&pl,&hm);
if(pl==falling && hm==working)
{
current_state=LOWERING_GEAR;
printf("\n*****CURRENT STATUS*****\n");
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("\n*****CURRENT STATUS*****\n");
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();
}
}
//LoweringGear.
void LoweringGear(void)
{
current_state=LOWERING_GEAR;
printf("\n\nEnter the status of pilot's lever and limit switch as per the following instructions:");
printf("\nPilot lever status and the corresponding choice to be entered: Falling - 0, Raising - 1.");
printf("\nLimit switch status and the corresponding choice to be entered: Off - 0, On - 1.");
printf("\nEnter your choice: ");
fflush(stdout);
scanf("%d %d",&pl,&limit_switch);
if(pl==falling && limit_switch==on)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("\n*****CURRENT STATUS*****\n");
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("\n*****CURRENT STATUS*****\n");
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();
}
}

//main function
int main()
{
Initiate_State_Machine(); //to initiate the state
while(1)
{
statetable[current_state]();
}
return 0;
}

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.