Challenge

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

First create required functions to controll gear using switch and gear states of airplane.

then enter this function into the array of 6 element to access the function based on requirement.In the name of static table.

The create Enumerations to define states and statuses.

then create the object name for the enum structures:

Then create structure to perform conditions.

after that we initiate current state as gear down which is 0

then in main function call Initiate_State_Machine function we no arrgument and iterate while loop.

Next Each state function performs specific actions and transitions based on input conditions. For example, GearDown function checks the status of the pilot's lever and squat switch to transition to the next state or remain in the current state.

after user input pilot's lever 0 and squat switch as 0 next state is before take off.

In this state function pilot's lever, squat switch and hydraulic mechanism are check. if lever is falling it goes to gear down else it check hydraulic mechanism and next state is raising gear.

Raising gear function getting input from the pilot as lever is falling and switch is on it means it change state is check before landing and print the respectine value.If the lever is up then state changes to gear up.Then check before landing system

In this Pilot's lever is falling and hydraulinc mechanism is same then state changes to lowering gear and show corresponding output from the statemachine array.If the lever is falling and hydralics not working it shows the gas pressure system.

If pilot's lever is falling and limit switch is on then state is check before landing and it is output shown from state machine.If pilot's lever is raising and limit switch is off then state is gear down and it's output is shown from state machine.If limit switch is off the it goes to lowering gear itself.

Output:-

Program:-

#include

// Function prototypes

void Initiate_State_Machine();

void GearDown();

void CheckingBeforeTakeOFF();

void RaisingGear();

void GearUp();

void CheckBeforeLanding();

void LoweringGear();

// Array of function pointers representing the states of the Finite State Machine (FSM)

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

// Enumeration for different states of the FSM

typedef enum State {

GEAR_DOWN,

CHECKING_BEFORE_TAKEOFF,

RAISING_GEAR,

GEAR_UP,

CHECK_BEFORE_LANDING,

LOWERING_GEAR,

} State_Type;

// Enumeration for the status of switches and mechanisms

typedef enum Switch {

ON,

OFF

} Switch_status;

typedef enum pilot_lever {

Raising,

Falling

} pilot_lever;

typedef enum hydraulic_mechanism {

Working,

Not_working

} hydraulic_mechanism;

// Global variables to track current state and various switch and mechanism statuses

State_Type current_state;

static volatile Switch_status squat_switch;

static volatile Switch_status limit_switch;

static volatile pilot_lever pl;

static volatile hydraulic_mechanism hm;

// Structure to hold information about each state

typedef struct {

char* current_state_indication;

char* light;

char* direction_valve_status;

char* landing_gear_hydraulic_control;

char* gas_pressurized_spring_system_status[2];

} State_Table;

// Array of State_Table representing each state of the FSM

static State_Table State_Machine[6] = {

{"GearDown", "Green", "Down", "Enabled", {NULL, NULL}},

{"CheckingBeforeTakeOFF", "Green", "Down", "Enabled", {NULL, NULL}},

{"RaisingGear", "Red", "Up", "Enabled", {NULL, NULL}},

{"GearUp", "Off", "NULL", "Disabled", {NULL, NULL}},

{"CheckBeforeLanding", "Red", "Down", "Enabled", {NULL, NULL}},

{"LoweringGear", "Green", "Down", "Enabled", {"Disabled", "Enabled"}}

};

// Function to initialize the FSM

void Initiate_State_Machine() {

current_state = GEAR_DOWN;

printf("The Finite State Machine is initialized. Current state: %s, Light is: %s\n", State_Machine[current_state].current_state_indication, State_Machine[current_state].light);

}

// Main function

int main() {

Initiate_State_Machine();

// Main loop to continuously execute the FSM

while (1) {

statetable[current_state](); // Execute the current state function

}

return 0;

}

// GearDown state function

void GearDown(void) {

// Prompt user input for pilot's lever and squat switch status

printf("Enter the status of pilot's lever and squat switch: ");

fflush(stdout);

scanf("%d %d", &pl, &squat_switch);

// Transition logic based on input conditions

if (pl == Raising && squat_switch == ON) {

current_state = CHECKING_BEFORE_TAKEOFF;

printf("Current state: %s\n", State_Machine[current_state].current_state_indication);

printf("Light is: %s\n", State_Machine[current_state].light);

} else {

GearDown(); // Repeat GearDown state if conditions not met

}

}

// Similar functions for other states (CheckingBeforeTakeOFF, RaisingGear, GearUp, CheckBeforeLanding, LoweringGear)...

// CheckingBeforeTakeOFF state function

void CheckingBeforeTakeOFF(void) {

// Prompt user input for pilot's lever, squat switch, and hydraulic mechanism status

printf("Enter the status of pilot's lever, squat switch, and hydraulic mechanism: ");

fflush(stdout);

scanf("%d %d %d", &pl, &squat_switch, &hm);

// Transition logic based on input conditions

if (pl == Falling && squat_switch == OFF) {

GearDown(); // Return to GearDown state

} else if (pl == Raising && squat_switch == ON && hm == Working) {

current_state = 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) {

current_state = 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);

printf("Status of gas pressurised Spring System: %s\n", State_Machine[current_state].gas_pressurized_spring_system_status[1]);

}

}

// RaisingGear state function

void RaisingGear(void) {

// Prompt user input for pilot's lever and limit switch status

printf("Enter the status of pilot's lever and limit switch: ");

fflush(stdout);

scanf("%d %d", &pl, &limit_switch);

// Transition logic based on input conditions

if (pl == Falling && limit_switch == ON) {

current_state = CHECK_BEFORE_LANDING;

printf("Current State: %s\n", State_Machine[current_state].current_state_indication);

printf("Light is: %s\n", State_Machine[current_state].light);

printf("Landing Gear Hydraulic Control: %s\n", State_Machine[current_state].landing_gear_hydraulic_control);

} else if (pl == Raising && limit_switch == OFF) {

current_state = 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("Landing Gear Hydraulic Control: %s\n", State_Machine[current_state].landing_gear_hydraulic_control);

} else {

RaisingGear(); // Repeat RaisingGear state if conditions not met

}

}

// GearUp state function

void GearUp(void) {

// Prompt user input for pilot's lever status

printf("Enter the status of pilot's lever: ");

fflush(stdout);

scanf("%d", &pl);

// Transition logic based on input condition

if (pl == Falling) {

current_state = CHECK_BEFORE_LANDING;

printf("Current State: %s\n", State_Machine[current_state].current_state_indication);

printf("Light is: %s\n", State_Machine[current_state].light);

printf("Landing Gear Hydraulic Control: %s\n", State_Machine[current_state].landing_gear_hydraulic_control);

} else {

GearUp(); // Repeat GearUp state if condition not met

}

}

// CheckBeforeLanding state function

void CheckBeforeLanding(void) {

// Prompt user input for pilot's lever and hydraulic mechanism status

printf("Enter the status of pilot's lever and hydraulic mechanism status: ");

fflush(stdout);

scanf("%d %d", &pl, &hm);

// Transition logic based on input conditions

if (pl == Falling && hm == Working) {

current_state = LOWERING_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 == Falling && hm == Not_working) {

current_state = LOWERING_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);

printf("Status of gas pressurised Spring System: %s\n", State_Machine[current_state].gas_pressurized_spring_system_status[1]);

} else if (pl == Raising) {

CheckBeforeLanding(); // Repeat CheckBeforeLanding state if condition not met

}

}

// LoweringGear state function

void LoweringGear(void) {

// Prompt user input for pilot's lever and limit switch status

printf("Enter the status of pilot's lever and limit switch: ");

fflush(stdout);

scanf("%d %d", &pl, &limit_switch);

// Transition logic based on input conditions

if (pl == Falling && limit_switch == ON) {

current_state = CHECKING_BEFORE_TAKEOFF;

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 && limit_switch == OFF) {

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("Landing Gear Hydraulic Control: %s\n", State_Machine[current_state].landing_gear_hydraulic_control);

}

if (limit_switch == OFF) {

LoweringGear(); // Repeat LoweringGear state if limit switch is 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.