Challenge

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

// Online C compiler to run C program online

#include <iostream>

// Define the states of the FSM

enum GearState {

RETRACTED,

EXTENDED,

RETRACTING,

EXTENDING

};

// Define the events that trigger state transitions

enum GearEvent {

RETRACT_COMMAND,

EXTEND_COMMAND,

RETRACT_COMPLETE,

EXTEND_COMPLETE

};

// Function prototypes

void handleEvent(GearEvent event);

void controlValve(GearState state);

// Current state of the FSM

GearState currentState = RETRACTED;

int main() {

// Simulate events for testing

handleEvent(EXTEND_COMMAND);

handleEvent(EXTEND_COMPLETE);

handleEvent(RETRACT_COMMAND);

handleEvent(RETRACT_COMPLETE);

return 0;

}

// Function to handle events and transition states

void handleEvent(GearEvent event) {

switch (currentState) {

case RETRACTED:

if (event == EXTEND_COMMAND) {

currentState = EXTENDING;

controlValve(currentState);

}

break;

case EXTENDED:

if (event == RETRACT_COMMAND) {

currentState = RETRACTING;

controlValve(currentState);

}

break;

case RETRACTING:

if (event == RETRACT_COMPLETE) {

currentState = RETRACTED;

controlValve(currentState);

}

break;

case EXTENDING:

if (event == EXTEND_COMPLETE) {

currentState = EXTENDED;

controlValve(currentState);

}

break;

}

}

// Function to control the valve based on the current state

void controlValve(GearState state) {

switch (state) {

case RETRACTED:

std::cout << "Landing gear is retracted. Valve closed." << std::endl;

break;

case EXTENDED:

std::cout << "Landing gear is extended. Valve closed." << std::endl;

break;

case RETRACTING:

std::cout << "Retracting landing gear. Valve open to retract." << std::endl;

break;

case EXTENDING:

std::cout << "Extending landing gear. Valve open to extend." << std::endl;

break;

}

}

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.