Challenge

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

#include <stdio.h>

#include <stdbool.h>

// Define states

typedef enum {

IDLE,

EXTENDING_GEAR,

RETRACTING_GEAR,

GEAR_EXTENDED,

GEAR_RETRACTED

} State;

// Function prototypes

void extend_gear();

void retract_gear();

bool is_extend_command_received();

bool is_retract_command_received();

bool is_gear_extended();

bool is_gear_retracted();

// Current state of the FSM

State current_state = IDLE;

// Function to handle state transitions

void handle_state_transition() {

switch (current_state) {

case IDLE:

if (is_extend_command_received()) {

current_state = EXTENDING_GEAR;

extend_gear();

} else if (is_retract_command_received()) {

current_state = RETRACTING_GEAR;

retract_gear();

}

break;

case EXTENDING_GEAR:

if (is_gear_extended()) {

current_state = GEAR_EXTENDED;

}

break;

case RETRACTING_GEAR:

if (is_gear_retracted()) {

current_state = GEAR_RETRACTED;

}

break;

case GEAR_EXTENDED:

if (is_retract_command_received()) {

current_state = RETRACTING_GEAR;

retract_gear();

}

break;

case GEAR_RETRACTED:

if (is_extend_command_received()) {

current_state = EXTENDING_GEAR;

extend_gear();

}

break;

}

}

// Function to extend gear

void extend_gear() {

// Code to control the direction valve to extend the gear

printf("Extending gear...\n");

}

// Function to retract gear

void retract_gear() {

// Code to control the direction valve to retract the gear

printf("Retracting gear...\n");

}

// Placeholder function to simulate receiving extend command

bool is_extend_command_received() {

// This should be replaced with actual code to check for an extend command

return false; // Placeholder

}

// Placeholder function to simulate receiving retract command

bool is_retract_command_received() {

// This should be replaced with actual code to check for a retract command

return false; // Placeholder

}

// Placeholder function to simulate checking if gear is fully extended

bool is_gear_extended() {

// This should be replaced with actual code to check if gear is extended

return true; // Placeholder

}

// Placeholder function to simulate checking if gear is fully retracted

bool is_gear_retracted() {

// This should be replaced with actual code to check if gear is retracted

return true; // Placeholder

}

int main() {

// Simulating the FSM in a loop

while (1) {

handle_state_transition();

// Add delay or wait for an event in a real system

}

return 0;

}

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.