Challenge

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

/*
* LandingGear.c
*
* Created on: 07-May-2023
* Author: Sunnys
*/

#include<stdio.h> //preprocessor library
#include<conio.h> //preprocessor library

enum GEAR_STATE //declaration of all the described states in the question
{
GEAR_DOWN,
CB_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CB_LANDING,
LOWERING_GEAR,
};

enum SWITCH_STATE //declaring switch status, can be used for both limit switch and squat switch
{
OPEN,
CLOSE
};

enum LEVER_STATE //declaring pilot hand lever states
{
RAISING_EDGE,
UP,
FALLING_EDGE,
DOWN
};

enum LIGHT_STATE // declaring indicating light states
{
OFF,
RED,
GREEN
};

enum DIRECTION_VALVE_STATE //declaring directional valve states
{
OPEN1,
CLOSE1,
DISABLED
};

/*enum AEROPLANE_STATE //declaring aeroplane valve states
{
GROUNDED,
OTHERS //takeoff, airbourne, landing etc
};*/

struct status //declaring a structure with required readible outputs
{
char current_state1;
char light_state;
char direction_valve_state;
};


void (*state_table[])() = {Gear_Down, Checking_Before_Takeoff,Raising_Gear,
Gear_Up, Checking_Before_Landing, Lowering_Gear};

void Gear_Down()
{
GEAR_POSITION = GEAR_DOWN;
printf("\n Enter the Pilot's Lever Position and Squat Switch Status:");
fflush(stdout);
scanf("%d %d",&LEVER,&SQUAT_SWITCH);
if(LEVER == RAISING_EDGE && SQUAT_SWITCH == OPEN)
{
GEAR_POSITION = CB_TAKEOFF;
printf("\nCurrent State: %s\n",State_Machine[GEAR_POSITION].current_state1);
printf("Light Status:%s\n", State_Machine[GEAR_POSITION].light_state);

}
else
{
Gear_Down();
}
}

void Checking_Before_Takeoff()
{
GEAR_POSITION = CB_TAKEOFF;
printf("\n Enter the Pilot's Lever Position and Squat Switch Status:");
fflush(stdout);
scanf("%d %d",&LEVER,&SQUAT_SWITCH);
printf("\nWait for 2 secs\n");
if(LEVER == FALLING_EDGE && SQUAT_SWITCH == CLOSE)
{
Gear_Down();
}
else if(LEVER == UP && SQUAT_SWITCH == OPEN)
{
GEAR_POSITION = RAISING_GEAR;
printf("\nCurrent State :%s\n",State_Machine[GEAR_POSITION].current_state1);
printf("Light Status: %s\n", State_Machine[GEAR_POSITION].light_state);
printf("Direction Valve Status:%s\n\n", State_Machine[GEAR_POSITION].direction_valve_state);
}
}

void Raising_Gear()
{
GEAR_POSITION = RAISING_GEAR;
printf("\nEnter the Limit Switch Status,Pilots Leaver Status:");
fflush(stdout);
scanf("%d %d",&LEVER,&LIMIT_SWITCH);
printf("\nWait for 1 sec\n");
if(LEVER == FALLING_EDGE && LIMIT_SWITCH == OPEN)
{
GEAR_POSITION = CB_LANDING ;
printf("\nCurrent State: %s\n",State_Machine[GEAR_POSITION].current_state1);
printf("Light Status:%s\n",State_Machine[GEAR_POSITION].light_state);

}
else if(LEVER == UP && LIMIT_SWITCH == CLOSE)
{
GEAR_POSITION = GEAR_UP ;
printf("\nCurrent State: %s\n",State_Machine[GEAR_POSITION].current_state1);
printf("Light Status: %s\n", State_Machine[GEAR_POSITION].light_state);

}
else
{
Raising_Gear();
}

}

void Gear_Up()
{
GEAR_POSITION = GEAR_UP;
printf("\nEnter the Pilot's Lever Position:");
fflush(stdout);
scanf("%d",&LEVER);
if(LEVER == FALLING_EDGE)
{
GEAR_POSITION = CB_LANDING;
printf("\nCurrent State: %s\n",State_Machine[GEAR_POSITION].current_state1);
printf("Light Status:%s\n", State_Machine[GEAR_POSITION].light_state);

}
else
{
Gear_Up();
}
}

void Checking_Before_Landing()
{
GEAR_POSITION = CB_LANDING;
printf("\nEnter the Pilot's Lever Position and Status of Hydraulic Mechanism:");
fflush(stdout);
scanf("%d",&LEVER);
if(LEVER == DOWN)
{
GEAR_POSITION = LOWERING_GEAR;
printf("\nCurrent State:%s\n", State_Machine[GEAR_POSITION].current_state1);
printf("Light Status:%s\n", State_Machine[GEAR_POSITION].light_state);
printf("Direction Valve Status:%s\n", State_Machine[GEAR_POSITION].direction_valve_state);
}

else if(LEVER == (UP || RAISING_EDGE || FALLING_EDGE))

{
Checking_Before_Landing();
}
}

void Lowering_Gear()
{
GEAR_POSITION = LOWERING_GEAR;
printf("\nEnter the Limit Switch Status,Pilots Leaver Status:");
fflush(stdout);
scanf("%d %d",&LIMIT_SWITCH,&LEVER);
if(LIMIT_SWITCH == CLOSE && LEVER == RAISING_EDGE )
{
GEAR_POSITION = CB_LANDING;
printf("\nCurrent State:%s\n",State_Machine[GEAR_POSITION].current_state1);
printf("Light Status:%s\n",State_Machine[GEAR_POSITION].light_state);

}
if(LIMIT_SWITCH == OPEN && LEVER == DOWN)
{
GEAR_POSITION = GEAR_DOWN;
printf("\nCurrent State:%s\n",State_Machine[GEAR_POSITION].current_state1);
printf("Light Status:%s\n",State_Machine[GEAR_POSITION].light_state);

}
else
{
Lowering_Gear();
}

}
enum GEAR_STATE GEAR_POSITION; //declaring variables for enumerated datatypes
enum SWITCH_STATE LIMIT_SWITCH; //declaring variables for enumerated datatypes
enum SWITCH_STATE SQUAT_SWITCH;//declaring variables for enumerated datatypes
enum LEVER_STATE LEVER; //declaring variables for enumerated datatypes
enum DIRECTION_VALVE_STATE VALVE; //declaring variables for enumerated datatypes
enum LIGHT_STATE LIGHT; //declaring variables for enumerated datatypes
enum AEROPLANE_STATE PLANE; //declaring variables for enumerated datatypes

//declaring an array of readable struct outputs for all the six states as per given flowchart
status state_machine[6] =
{
{"Geardown","green","down"},
{"checkbeforetakeoff","green","down"},
{"raisinggear","red","up"},
{"gearup","null","null"},
{"checkbeforelanding","null","down"},
{"loweringgear","red","down"},
};

int main()
{
//declaring variables with respect to created enumerated data types


GEAR_POSITION = GEAR_DOWN;
printf("\nState Machine is initialized & Program is in Gear_Down State & Light Status: %s\n",State_Machine[current_state].light_state);
while(1)
{
state_table[GEAR_POSITION]();
}


}

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.