Challenge

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

#include<stdio.h> //including the libraries used
#include<stdlib.h>
//prototypes of the functions used
void Initiate_State_Machine();
void GearDown();
void CheckingBeforeTakeOFF();
void RaisingGear();
void GearUp();
void CheckBeforeLanding();
void LoweringGear();
//Array of functions
static void(*statetable[])(void)={GearDown, CheckingBeforeTakeOFF, RaisingGear, GearUp, CheckBeforeLanding, LoweringGear};
typedef enum State //the six states of airplane's landing gear stated in enum
{
GEAR_DOWN,
CHECKING_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR,
}State_Type;
typedef enum Switch //the two switch states stated in enum
{
open,
close//plane is on ground
}Switch_status;
typedef enum pilot_lever //the four pilot lever states stated in enum
{
raising,
falling,
up,
down
}Pilot_Lever;
typedef enum hydraulic_mechanism //the two working states of hydraulic mechanism stated in enum
{
working,
not_working
}Hydraulic_Mechanism;
static volatile Switch_status squat_switch; //variable for giving position of squat switch
static volatile Switch_status limit_switch; //variable for giving position of limit switch
static volatile Pilot_Lever pilot_lever; //variable for giving position of pilot lever
static volatile Hydraulic_Mechanism hydraulic_mechanism; //variable for giving position of hydraulic mechanism
State_Type current_state; //variable for giving position of gear
typedef struct //defining a structure data type to give airplane's modes
{
char* current_state_indication;
char* light;
char* direction_valve_status;
char* landing_gear_hydraulic_control;
char* Gas_Pressurized_Spring_system_status[2];
}State_Table;
//creating the array of structure and saving information
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","Red","Down","Enabled",{"Disabled","Enabled"}}
};
//GearDown function from this function we can jump to checkingBeforeTakeOFF function depending upon the input provided
void GearDown(void)
{
current_state = GEAR_DOWN;
printf("\nCurrent State : %s\n",State_Machine[current_state].current_state_indication);
printf("Light is : %s\n", State_Machine[current_state].light);
printf("\nEnter the status of pilot's lever and squat switch"); //asking the user to give input
printf("\n Pilot Lever :\n 0.raising\n 1.falling\n 2.up\n 3.down\n");
printf("\n Squat Switch :\n 0.open\n 1.close\n");
fflush(stdout);
scanf("%d %d",&pilot_lever,&squat_switch);
if(pilot_lever == raising && squat_switch == open) //checking the condition
{
current_state = CHECKING_BEFORE_TAKEOFF;
printf("\nCurrent State : %s\n",State_Machine[current_state].current_state_indication);
printf("Light is : %s\n", State_Machine[current_state].light);
}
else
{
GearDown();
}
}
//checkingBeforeTakeOFF function from this function we can jump to RaisingGear function or GearDown function depending upon the input provided
void CheckingBeforeTakeOFF(void)
{
current_state = CHECKING_BEFORE_TAKEOFF;
printf("\nEnter the status of pilot's lever and squat switch and hydraulic mechanism:"); //asking for the input and jumping to other states
printf("\n Pilot Lever :\n 0.raising\n 1.falling\n 2.up\n 3.down\n");
printf("\n Squat Switch :\n 0.open\n 1.close\n");
printf("\n Hydraulic Mechanism :\n 0.working 1.not_working\n");
fflush(stdout);
scanf("%d %d %d",&pilot_lever,&squat_switch,&hydraulic_mechanism);
if((pilot_lever == falling && squat_switch == close)
{
//GearDown();
current_state = GEAR_DOWN;
}
else if(pilot_lever == raising && squat_switch == open && hydraulic_mechanism == working)
{
current_state = RAISING_GEAR;
printf("\nCurrent 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(pilot_lever == raising && squat_switch == open && hydraulic_mechanism == 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);
}
else
{
CheckingBeforeTakeOFF();
}
}
//RaisingGear function from this function we can jump to checkBeforeLanding function or GearUp function depending upon the input provided
void RaisingGear(void)
{
current_state = RAISING_GEAR;
printf("\nEnter the status of pilot's lever and limit switch:"); //asking for the input and jumping to other states
printf("\n Pilot Lever :\n 0.raising\n 1.falling\n 2.up\n 3.down\n");
printf("\n Limit Switch :\n 0.open\n 1.close\n");
fflush(stdout);
scanf("%d %d",&pilot_lever,&limit_switch);
if(pilot_lever == falling && limit_switch == open)
{
current_state = CHECK_BEFORE_LANDING;
printf("\nCurrent 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(pilot_lever == raising && limit_switch == close)
{
current_state = GEAR_UP;
printf("\nCurrent 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();
}
}
//GeaUpr function from this function we can jump to checkBeforeLanding function depending upon the input provided
void GearUp(void)
{
current_state = GEAR_UP;
printf("\nEnter the status of pilot's lever : "); //asking for the input and jumping to other states
printf("\n Pilot Lever :\n 0.raising\n 1.falling\n 2.up\n 3.down\n");
fflush(stdout);
scanf("%d",&pilot_lever);
if(pilot_lever == falling)
{
current_state = CHECK_BEFORE_LANDING;
printf("\nCurrent 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 function from this function we can jump to loweringGear function depending upon the input provided
void CheckBeforeLanding(void)
{
current_state = CHECK_BEFORE_LANDING;
printf("\nEnter the status of pilot's lever : "); //asking for the input and jumping to other states
printf("\n Pilot Lever :\n 0.raising\n 1.falling\n 2.up\n 3.down\n");
printf("\n Hydraulic Mechanism :\n 0.working 1.not_working\n");
fflush(stdout);
scanf("%d %d",&pilot_lever,&hydraulic_mechanism);
if(pilot_lever == falling && hydraulic_mechanism == 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(pilot_lever == falling && hydraulic_mechanism == 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(pilot_lever == raising)
{
CheckBeforeLanding();
}
}
//loweringGear function from this function we can jump to GearDown function or checkBeforeTakeOFF function depending upon the input provided
void LoweringGear(void)
{
current_state = LOWERING_GEAR;
printf("\nEnter the status of pilot's lever and limit switch:"); //asking for the input and jumping to other states
printf("\n Pilot Lever :\n 0.raising\n 1.falling\n 2.up\n 3.down\n");
printf("\n Limit Switch :\n 0.open\n 1.close\n");
fflush(stdout);
scanf("%d %d",&pilot_lever,&limit_switch);
if(pilot_lever == raising && limit_switch == close)
{
current_state = CHECKING_BEFORE_TAKEOFF;
printf("\nCurrent State:%s\n",State_Machine[current_state].current_state_indication);
printf("Light is:%s\n",State_Machine[current_state].light);
}
else if(pilot_lever == falling && limit_switch == open)
{
current_state = GEAR_DOWN;
printf("\nCurrent 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
{
LoweringGear();
}
}
//Initiate_State_Machine and setting geadDown as initial state
void Initiate_State_Machine()
{
current_state == GEAR_DOWN;
printf("The Finite State Machine is Intialized and current the program is in GearDown State & Light is:%s\n\n",State_Machine[current_state].light);
}
int main()
{
Initiate_State_Machine(); //calling initiating state machine
while(1)
{
statetable[current_state](); //infinite to jump between states
}
}
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.