Challenge

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

Solution:

//Preprocessor section/Header files
#include //Standard input/output library
#include //Standard library
#include //Standard string library

//Function prototype
void Initiate_State_Machine(); //Initialize the state machine
void GearDown(); //Shift the gear to the down position
void CheckingBeforeTakeOFF(); //Checking before take off
void RaisingGear(); // Raise the landing gear
void GearUp(); //Shift the gear to the up position
void CheckBeforeLanding(); //Checking before landing
void LoweringGear(); //Lower the landing gear


static void (*statetable[])(void) = {GearDown, CheckingBeforeTakeOFF, RaisingGear, GearUp, CheckBeforeLanding, LoweringGear};

//Define enumerations for different states and statuses
typedef enum State
{
    GEAR_DOWN, //Gear is in the down position
    CHECKING_BEFORE_TAKEOFF, //State for checking before take-off
    RAISING_GEAR, //State for raising the gear
    GEAR_UP, //Gear is in the up position
    CHECK_BEFORE_LANDING, //State for check before landing
    LOWERING_GEAR, //State for lowering the gear
} State_Type;

//Define enumeration for representing 'switch'
typedef enum Switch
{
    on, //Switch is in the 'on' position
    off //Switch is in the 'off' position
} Switch_status;

//Define an enumeration type 'pilot_lever'
typedef enum pilot_lever
{
    Raising,  //Lever is in the "raising" position
    Falling   //Lever is in the "falling" position
} pilot_lever;

//Define enumeration type the 'hydraulic mechanism'
typedef enum hydraulic_mechanism
{
    working, //Hydraulic mechanism is in working
    not_working //Hydraulic mechanism is not working
} hydraulic_mechanism;

// Static volatile variables to store status
static volatile Switch_status squat_switch;
static volatile Switch_status limit_switch;
static volatile pilot_lever pl;
static volatile hydraulic_mechanism hm;

//Variable to keep track of the current state
State_Type current_state;

//Define a Structure to represent the state table
typedef struct
{
    char *current_state_indication; //Pointer to a string indicating the current state of the system
    char *light; //Pointer to a string indicating the state of the light
    char *direction_valve_status; //Pointer to a string indicating the status of the direction valve
    char *landing_gear_hydraulic_control; //Pointer to a string indicating the control of the landing gear hydraulic system
} State_Table;

//Define a static array of State_Table structures
static State_Table State_Machine[6] =
{
    {"GearDown", "Green", "Down", "Enabled"}, //Define the first state: "GearDown"
    {"CheckingBeforeTakeOFF", "Green", "Up", "Enabled"}, //Define the second state: "CheckingBeforeTakeOFF"
    {"RaisingGear", "Red", "Up", "Enabled"}, //Define the third state: "RaisingGear"
    {"GearUp", "Off", "Up", "Disabled"}, //Define the fourth state: "GearUp"
    {"CheckBeforeLanding", "Red", "Down", "Enabled"}, //Define the fifth state: "CheckBeforeLanding"
    {"LoweringGear", "Red", "Down", "Enabled"} //Define the sixth state: "LoweringGear"
};

//Main function
int main()
{
    Initiate_State_Machine();

    while (1)
    {
        statetable[current_state]();
    }
    return 0;
}

//function Definition
void GearDown(void) //Implementation for GEAR_DOWN state
{
    current_state = GEAR_DOWN; //Set the current state to GEAR_DOWN
    printf("Enter the status of pilot's lever and squat switch (0 for Raising, 1 for Falling, 0 for On, 1 for Off): ");
    fflush(stdout);
    scanf("%d %d", (int *)&pl, (int *)&squat_switch);

    if (pl == Raising && squat_switch == on) //Check if pilot lever is Raising and squat switch is On
    {
        current_state = CHECKING_BEFORE_TAKEOFF; //Update the current state to CHECKING_BEFORE_TAKEOFF
        printf("Current state: %s\n", State_Machine[current_state].current_state_indication); //Print current state indication
        printf("Light is: %s\n", State_Machine[current_state].light); //Print the light status
        printf("Status of Direction valve: %s\n", State_Machine[current_state].direction_valve_status); //Print direction valve status
    }
    else
    {
        GearDown();
    }
}

void CheckingBeforeTakeOFF(void) //Implementation for CHECKING_BEFORE_TAKEOFF state
{
    current_state = CHECKING_BEFORE_TAKEOFF; //Set the current state to CHECKING_BEFORE_TAKEOFF
    printf("Enter the status of pilot's lever and squat switch (0 for Raising, 1 for Falling, , 0 for On, 1 for Off): ");
    fflush(stdout);  //
    scanf("%d %d %d", (int *)&pl, (int *)&squat_switch, (int *)&hm);

    if (pl == Falling && squat_switch == off) //Check if pilot lever is Falling and squat switch is off
    {
        GearDown();
    }
    else if (pl == Raising && squat_switch == on && hm == working) //Check if pilot lever is Raising, squat switch is on and hydraulic mechanism is working
    {
        current_state = RAISING_GEAR;  //Update the current state to 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);
    }
    else if (pl == Raising && squat_switch == on && hm == not_working) //Check if pilot lever is Raising, squat switch is on and hydraulic mechanism is not working
    {
        current_state = RAISING_GEAR;  //Update the current state to 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);
    }
}

void RaisingGear(void) //Implementation for RAISING_GEAR state
{
    current_state = RAISING_GEAR;  //Set the current state to RAISING_GEAR
    printf("Enter the status of pilot's lever and squat switch (0 for Raising, 1 for Falling, , 0 for On, 1 for Off): ");
    fflush(stdout);
    scanf("%d %d %d", (int *)&pl, (int *)&squat_switch, (int *)&hm);

    if (pl == Falling || squat_switch == off) //Check if the pilot lever is in the falling position or squat switch is off
    {
        GearDown();
    }
    else if (pl == Raising && squat_switch == on && hm == working) //Check if the pilot lever is in the raising position, squat switch is on, and hydraulic mechanism is working

    {
        current_state = GEAR_UP; //Set the current state to GEAR_UP
        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 (pl == Raising && squat_switch == on && hm == not_working) //Check if the pilot's lever is in the raising position, squat switch is on,and hydraulic mechanism is not working
    {
        current_state = RAISING_GEAR; //Set the current state back to 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);
    }
}

void GearUp(void) //Implementation for GEAR_UP state
{
    current_state = GEAR_UP; //Set the current state to GEAR_UP
    printf("Enter the status of pilot's lever and squat switch (0 for Raising, 1 for Falling, , 0 for On, 1 for Off): ");
    fflush(stdout);
    scanf("%d %d %d", (int *)&pl, (int *)&squat_switch, (int *)&hm);

    if (pl == Raising && squat_switch == on && hm == working) //Check if the pilot lever is in the raising position, squat switch is on, and hydraulic mechanism is working
    {
        current_state = CHECK_BEFORE_LANDING; //Update the current state and provide information about it
        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
    {
        GearUp();
    }
}

void CheckBeforeLanding(void) //Implementation for CHECK_BEFORE_LANDING state
{
    current_state = CHECK_BEFORE_LANDING;  //Set the current state to CHECK_BEFORE_LANDING
    printf("Enter the status of pilot's lever and squat switch (0 for Raising, 1 for Falling, , 0 for On, 1 for Off): ");
    fflush(stdout);
    scanf("%d %d %d", (int *)&pl, (int *)&squat_switch, (int *)&hm);

    if (pl == Raising && squat_switch == on && hm == working) //Check if the pilot lever is in the raising position, squat switch is on, and hydraulic mechanism is working
    {
        current_state = GEAR_UP;  //Update to GEAR_UP state if conditions are met
        printf("Current State: %s\n", State_Machine[current_state].current_state_indication);
        printf("Light is: %s\n", State_Machine[current_state].light);
    }
    else if (pl == Raising && squat_switch == on && hm == not_working)
    {
        current_state = GEAR_UP; //Update to GEAR_UP state if conditions are met
        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
    {
        CheckBeforeLanding();
    }
}

void LoweringGear(void) //Implementation for LOWERING_GEAR state
{
    current_state = LOWERING_GEAR; //Set the current state to LOWERING_GEAR
    printf("Enter the status of pilot's lever and squat switch (0 for Raising, 1 for Falling, , 0 for On, 1 for Off): ");
    fflush(stdout);
    scanf("%d", &squat_switch);

    if (squat_switch == on) //Check if the squat_switch is in the 'on' position
    {
        current_state = GEAR_DOWN;
        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
    {
        LoweringGear();
    }
}


void Initiate_State_Machine() //Function to initialize the state machine
{
    current_state = GEAR_DOWN; //Set the current state to GEAR_DOWN
    printf("The Finite State Machine is Initialized, and the program is in GearDown State. Light is: %s\n", State_Machine[current_state].light);
}

Output :

The Finite State Machine is Initialized, and the program is in GearDown State. Light is: Green
Enter the status of pilot's lever and squat switch (0 for Raising, 1 for Falling, 0 for On, 1 for Off): 0 0 0
Current state: CheckingBeforeTakeOFF
Light is: Green
Status of Direction valve: Up
Enter the status of pilot's lever and squat switch (0 for Raising, 1 for Falling, , 0 for On, 1 for Off): 0 0 0
Current State: RaisingGear
Light is: Red
Status of Direction valve: Up
Enter the status of pilot's lever and squat switch (0 for Raising, 1 for Falling, , 0 for On, 1 for Off): 0 0 0
Current State: GearUp
Light is: Off
Status of Direction valve: Up
Enter the status of pilot's lever and squat switch (0 for Raising, 1 for Falling, , 0 for On, 1 for Off): 0 0 0
Current State: CheckBeforeLanding
Light is: Red
Status of Direction valve: Down
Enter the status of pilot's lever and squat switch (0 for Raising, 1 for Falling, , 0 for On, 1 for Off): 0 0 0
Current State: GearUp
Light is: Off
Enter the status of pilot's lever and squat switch (0 for Raising, 1 for Falling, , 0 for On, 1 for Off): 

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.