Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
typedef enum {
GEAR_DOWN,
CHECK_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR
} GearState;
void handleGearDownState();
void handleCheckBeforeTakeoffState();
void handleRaisingGearState();
void handleGearUpState();
void handleCheckBeforeLandingState();
void handleLoweringGearState();
GearState currentState = GEAR_DOWN;
bool pilotLeverUp = false;
bool pilotLeverDown = false;
bool squatSwitchClosed = false;
bool limitSwitchOpened = true;
bool limitSwitchClosed = false;
bool directionValveUp = false;
bool directionValveDown = false;
void updateStateMachine() {
switch (currentState) {
case GEAR_DOWN:
handleGearDownState();
break;
case CHECK_BEFORE_TAKEOFF:
handleCheckBeforeTakeoffState();
break;
case RAISING_GEAR:
handleRaisingGearState();
break;
case GEAR_UP:
handleGearUpState();
break;
case CHECK_BEFORE_LANDING:
handleCheckBeforeLandingState();
break;
case LOWERING_GEAR:
handleLoweringGearState();
break;
default:
printf("Invalid state\n");
break;
}
}
void handleGearDownState() {
if (pilotLeverUp && squatSwitchClosed) {
printf("Transitioning to CHECK_BEFORE_TAKEOFF state\n");
currentState = CHECK_BEFORE_TAKEOFF;
sleep(3);
} else {
printf("Landing gear is down. Lights: GREEN\n");
}
}
void handleCheckBeforeTakeoffState() {
if (pilotLeverUp) {
printf("Transitioning to RAISING_GEAR state\n");
currentState = RAISING_GEAR;
}
}
void handleRaisingGearState() {
if (limitSwitchClosed) {
printf("Landing gear raised. Transitioning to GEAR_UP state\n");
currentState = GEAR_UP;
} else {
printf("Raising landing gear. Lights: RED\n");
}
}
void handleGearUpState() {
if (pilotLeverDown) {
printf("Transitioning to CHECK_BEFORE_LANDING state\n");
currentState = CHECK_BEFORE_LANDING;
} else {
printf("Landing gear is up. Lights: OFF\n");
}
}
void handleCheckBeforeLandingState() {
if (pilotLeverDown) {
printf("Transitioning to LOWERING_GEAR state\n");
currentState = LOWERING_GEAR;
}
}
void handleLoweringGearState() {
if (limitSwitchOpened) {
printf("Landing gear lowered. Transitioning to GEAR_DOWN state\n");
currentState = GEAR_DOWN;
} else {
printf("Lowering landing gear. Lights: RED\n");
}
}
int main() {
pilotLeverUp = true; // Simulates the pilot pulling the lever to raise the gear
squatSwitchClosed = true; // Indicates the aircraft is on the ground
while (1) {
updateStateMachine();
sleep(1);
}
return 0;
}