Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
To control the retraction and extension of the Airplanes landing gear can be implemented using finite state machine.
#include<stdio.h>
#include<stdbool.h>
#include<unistd.h> //for sleep function
typedef enum
{
GEAR_UP,
GEAR_DOWN,
GEAR_TRANSIT
}GearState; //enum for GearState
typedef enum
{
VALVE_UP,
VALVE_DOWN
}ValveDirection; //enum for ValveDirection
void controlLandingGear(ValveDirection valveDirection, bool squatSwitch, bool limitSwitch, bool leverState, const char *gearName);
void controlLights(GearState gearState, bool squatSwitch, bool leverState);
//function ptototypes
int main()
{
ValveDirection valveDirection;
int squatSwitchInt, limitSwitchInt, leverStateInt;
// Int datatype used for integer input from user
bool squatSwitch, limitSwitch, leverState;
//bool datatype used for Boolean input NO or YES from user
printf("Enter valve Direction(0 for UP, 1 for DOWN):");
fflush(stdout);
scanf("%u",(unsigned int*)&valveDirection);
printf("Is Airplane airborne? (0 for NO, 1 for YES):");
fflush(stdout);
scanf("%d",&squatSwitchInt);
squatSwitch = squatSwitchInt;
printf("Is Limit switch OPEN? (0 for NO, 1 for YES):");
fflush(stdout);
scanf("%d",&limitSwitchInt);
limitSwitch = limitSwitchInt;
printf("Is Lever state UP? (0 for NO, 1 for YES):");
fflush(stdout);
scanf("%d",&leverStateInt);
leverState = leverStateInt;
printf("\nDebug: valveDirection = %d, squatSwitch = %d, limitSwitch = %d, leverState = %d\n",valveDirection,squatSwitch, limitSwitch, leverState);
//to print the input values for degugging
printf("\nNose Gear\n");
controlLandingGear(valveDirection, squatSwitch, limitSwitch, leverState,"Nose Gear");
//simulated action foe Nose Gear
printf("\nLeft Gear\n");
controlLandingGear(valveDirection, squatSwitch, limitSwitch, leverState,"Left Gear");
//simulated action foe Left Gear
printf("\nRight Gear\n");
controlLandingGear(valveDirection, squatSwitch, limitSwitch, leverState,"Right Gear");
//simulated action foe Right Gear
return 0;
}
void controlLandingGear(ValveDirection valveDirection, bool squatSwitch, bool limitSwitch, bool leverState, const char *gearName)
//Function to control the landing gear based on the valve direction and the switches
{
sleep(1);
//sleep function to delay the operation of hydraulic pump
if(valveDirection == VALVE_UP)
{
printf("Direction valve: UP\n");
}
else
{
printf("Direction valve: DOWN\n");
}
if(squatSwitch)
{
printf("Squat Switch: OPENED (Airplane is airborne)\n");
}
else
{
printf("Squat Switch: CLOSED (Airplane is Grounded)\n");
}
if(limitSwitch)
{
printf("limit Switch: OPENED (Gear is DOWN)\n");
}
else
{
printf("limit Switch: CLOSED (Gear is UP)\n");
}
if(leverState && !limitSwitch)
{
printf("Take-Off delay: 3 seconds\n");
sleep(3);
//Take of delay of 3 seconds if lever is up and gear is not down
}
controlLights(limitSwitch ? GEAR_DOWN : GEAR_UP, squatSwitch, leverState);
//call control lights with appropriate parameters
}
void controlLights(GearState gearState, bool squatSwitch, bool leverState)
//function to control the lights based on the gear state and other conditions
{
if(squatSwitch && leverState)
{
printf("Lights: Green\n");
}
else if(gearState == GEAR_UP)
{
printf("Lights: OFF\n");
}
else
{
printf("Lights: RED\n");
}
}