Challenge

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

Code:

#include

#include

#include

// Enum for the pilot's lever states

enum pilot {

    rising,

    falling,

    up,

    down

};

// Struct for representing the landing gear states

struct airplane {

    const char *squatswitch;

    const char *light;

};

// Define the landing gear states in the struct

struct airplane table[] = {

    {"open", "green"}, // Gear down, squat switch open

    {"close", "red"},  // Gear down, squat switch close

    {"close", "red"},  // Gear up

};

// Define the enum for the state machine

enum state {

    geardown,

    checktakeoffcondition,

    gearup,

    checklandingcondition,

    loweringgear

};

// Function prototypes

void gear_down();

void check_takeoff_condition();

void gear_up();

void check_landing_condition();

void lowering_gear();

// Function pointers to handle state transitions

void (*function[])(void) = {gear_down, check_takeoff_condition, gear_up, check_landing_condition, lowering_gear};

// Current state

enum state currentstate = geardown;

// Current pilot's lever state

enum pilot pilotlever;

// Simulated control flag for hydraulic control

bool hydraulicControlEnabled = true;

// Simulated limit switch state

bool limitSwitchOpened = false;

int main() {

    while (1) {

        function[currentstate]();

    }

    return 0;

}

void gear_down() {

    printf("\nThe current state is geardown");

    printf("\nEnter 0.rising, 1.falling, 2.up, 3.down: ");

    scanf("%d",(int *) &pilotlever);

    if (pilotlever == rising && table[currentstate].squatswitch[0] == 'o') {

        // Pilot's Lever Rising && Squat Switch Open

        printf("\nLights = %s", table[currentstate].light);

        currentstate = gearup;

        hydraulicControlEnabled = false;

        printf("\nControl for landing gear disabled");

    } else if (pilotlever == falling || table[currentstate].squatswitch[0] == 'c') {

        // Squat Switch Close or Pilot's Lever Falling

        printf("\nLights = %s", table[currentstate].light);

    } else if (pilotlever == up) {

        // Pilot's Lever UP

        currentstate = checktakeoffcondition;

    } else if (pilotlever == down) {

        // Pilot's Lever Down

        currentstate = loweringgear;

        printf("\nLights = RED");

    }

}

void check_takeoff_condition() {

    printf("\nThe current state is checktakeoffcondition");

    printf("\nEnter 0.rising, 1.falling, 2.up, 3.down: ");

    scanf("%d",(int *)&pilotlever);

    if (pilotlever == rising && table[currentstate].squatswitch[0] == 'o') {

        // Pilot's Lever Rising && Squat Switch Open

        printf("\nLights = %s", table[currentstate].light);

    } else if (pilotlever == falling || table[currentstate].squatswitch[0] == 'c') {

        // Squat Switch Close or Pilot's Lever Falling

        printf("\nLights = %s", table[currentstate].light);

        currentstate = geardown;

    } else if (pilotlever == up) {

        // Pilot's Lever UP

        currentstate = gearup;

    }

}

void gear_up() {

    printf("\nThe current state is gearup");

    printf("\nEnter 0.rising, 1.falling, 2.up, 3.down: ");

    scanf("%d",(int *)&pilotlever);

    if (pilotlever == up) {

        // Pilot's Lever UP

        currentstate = checklandingcondition;

        printf("\nLights = %s", table[currentstate].light);

    } else if (pilotlever == falling) {

        // Pilot's Lever Falling

        hydraulicControlEnabled = true;

        printf("\nControl enabled");

    }

}

void check_landing_condition() {

    printf("\nThe current state is checklandingcondition");

    printf("\nEnter 0.rising, 1.falling, 2.up, 3.down: ");

    scanf("%d",(int *)&pilotlever);

    if (pilotlever == rising && table[currentstate].squatswitch[0] == 'o') {

        // Pilot's Lever Rising && Squat Switch Open

        printf("\nLights = %s", table[currentstate].light);

    } else if (pilotlever == falling || table[currentstate].squatswitch[0] == 'c') {

        // Squat Switch Close or Pilot's Lever Falling

        printf("\nLights = %s", table[currentstate].light);

    } else if (pilotlever == down) {

        // Pilot's Lever Down

        currentstate = geardown;

        hydraulicControlEnabled = false;

        printf("\nDirection Valve = DOWN");

    }

}

void lowering_gear() {

    printf("\nThe current state is loweringgear");

    printf("\nEnter 0.rising, 1.falling, 2.up, 3.down: ");

    scanf("%d",(int *)&pilotlever);

    if (pilotlever == rising) {

        // Pilot's Lever Rising

        printf("\nLights = RED");

    } else if (pilotlever == down) {

        // Pilot's Lever Down

        printf("\nDirection Valve = DOWN");

        if (limitSwitchOpened) {

            printf("\nLights = Green");

        }

        currentstate = geardown;

    }

}

Output:

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.