Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Overview of the Project:
This project is a simulation of a landing gear system for an aircraft. The program is designed to run on a state machine(FSM) that controls the various components of the landing gear system, such as the pilot's lever, hydraulic mechanism, limit switches, and squat switches. The system goes through different states of operation such as gear down, checking before takeoff, raising gear, gear up, checking before landing, and lowering gear. The program prompts the user for inputs to simulate the different conditions required for the state transitions, and the system responds by changing the state and updating the status of the various components. The program uses enums, structs, and functions to organize the code and make it easier to read and maintain.

#include <stdio.h> // stdio.h for standard input and output operations
#include <stdlib.h> // stdlib.h for general purpose functions, such as program termination
#include <stdbool.h> // bool.h for boolean data type and logical operations
#include <time.h> // time.h for time and date operations
// Define constants using enum
typedef enum {
GEAR_DOWN,
CHECKING_BEFORE_TAKE_OFF,
RAISING_GEAR,
GEAR_UP,
CHECKING_BEFORE_LANDING,
LOWERING_GEAR
} state_type;
typedef enum {
RISING,
UP,
FALLING,
DOWN
} pilot_lever_status;
typedef enum {
OPEN,
CLOSE
} switch_state;
typedef enum {
DISABLED,
ENABLED
} hydraulic_mechanism;
// Declare global variables for state and lever status
state_type current_state = GEAR_DOWN;
pilot_lever_status pilot_lever;
switch_state limit_switch, squat_switch;
hydraulic_mechanism landing_control;
// Define a custom data type 'state_t' using struct to store states' information
typedef struct {
const char* present_state; // state name or description
const char* light_status; // status of the light for the state
const char* direction_valve_status; // direction valve status
const char* hydraulics_status; // hydraulics status
const char* spring_status; // spring status
} state_t;
// Use an array of 'state_t' structures to store the state transition table
const state_t STATE_TABLE[] = {
{"GEAR DOWN", "GREEN", "DOWN", "ENABLED", "DISABLED"}, // state 0
{"CHECK BEFORE TAKE OFF", "GREEN", "UP", "ENABLED", "DISABLED"}, // state 1
{"RAISING GEAR", "RED", "UP", "ENABLED", "DISABLED"}, // state 2
{"GEAR UP", "OFF", "UP", "DISABLED", "DISABLED"}, // state 3
{"CHECK BEFORE LANDING", "RED", "DOWN", "ENABLED", "DISABLED"}, // state 4
{"LOWERING GEAR", "GREEN", "DOWN", "ENABLED", "ENABLED"} // state 5
};
// Use function prototypes
void delay(int seconds);
void init_state_machine(void);
void gear_down(void);
void check_before_takeoff(void);
void raising_gear(void);
void gear_up(void);
void check_before_landing(void);
void lowering_gear(void);
// The main function simply initializes the state machine and starts the state loop
int main(void) {
init_state_machine();
while (true) {
switch (current_state) {
case GEAR_DOWN:
gear_down();
break;
case CHECKING_BEFORE_TAKE_OFF:
check_before_takeoff();
break;
case RAISING_GEAR:
raising_gear();
break;
case GEAR_UP:
gear_up();
break;
case CHECKING_BEFORE_LANDING:
check_before_landing();
break;
case LOWERING_GEAR:
lowering_gear();
break;
}
}
return 0;
}
// Add a delay function for readability
void delay(int seconds) {
clock_t start_time = clock(); // Get the current system time in clock ticks
int milliseconds = 1000 * seconds; // Convert the delay time from seconds to milliseconds
while (clock() < start_time + milliseconds);// Wait until the current system time in clock ticks has exceeded the start time plus the delay time
}
// Use a function to initialize the state machine
void init_state_machine(void) {
printf("State Machine is initialized\nProgram is in %s State\nLight Status: %s\n\n",
STATE_TABLE[current_state].present_state, // Displaying initial state name state 0
STATE_TABLE[current_state].light_status); // Displaying initial light status state 0
}
// This function checks the conditions of gear down
void gear_down(void) {
printf("Enter the Pilot's Lever Position and Squat Switch Status: ");
printf("\nExpected input 0 0: ");
fflush(stdout);
scanf("%u %u", &pilot_lever, &squat_switch);
if (pilot_lever == RISING && squat_switch == OPEN) {
current_state = CHECKING_BEFORE_TAKE_OFF; // Update the current state
printf("\nCurrent State: %s\nLight Status: %s\n\n", // Print the current state and light status
STATE_TABLE[current_state].present_state, //print present state of state 1
STATE_TABLE[current_state].light_status); //print light status of state 1
delay(1); // Wait for 1 second
}
}
// This function checks the conditions before taking off
void check_before_takeoff(void) {
printf("Enter the Pilot's Lever Position, Squat Switch Status: ");
printf("\nExpected input 1 0: ");
fflush(stdout);
scanf("%u %u", &pilot_lever, &squat_switch);
if (pilot_lever == UP && squat_switch == OPEN) {
current_state = RAISING_GEAR;
printf("\nWait for 3 seconds...\n");
for (int i = 3; i > 0; i--) {
printf("%d\n", i);
fflush(stdout);
delay(1);
}
printf("\nCurrent State: %s\nDirection Valve Status: %s\nLight Status: %s\n\n",
STATE_TABLE[current_state].present_state,
STATE_TABLE[current_state].direction_valve_status,
STATE_TABLE[current_state].light_status);
delay(1);
} else if(pilot_lever == FALLING && squat_switch == CLOSE){
current_state = GEAR_DOWN;
printf("\nCurrent State: %s\nLight Status: %s\n\n",
STATE_TABLE[current_state].present_state, //print present state of state 2
STATE_TABLE[current_state].light_status); //print light status of state 2
}
}
// This function checks the conditions of RAISING GEAR
void raising_gear(void) {
printf("Enter the Limit Switch Status, Pilot's Lever Position: ");
printf("\nExpected input 1 1: ");
fflush(stdout);
scanf("%u %u", &limit_switch, &pilot_lever);
if (limit_switch == CLOSE && pilot_lever == UP) {
current_state = GEAR_UP;
printf("\nCurrent State: %s\nLight Status: %s\nHydraulic Control for Landing Gear Status: %s\n\n",
STATE_TABLE[current_state].present_state, //print present state of state 3
STATE_TABLE[current_state].light_status, //print Light Status of state 3
STATE_TABLE[current_state].hydraulics_status); //print hydraulics status of state 3
delay(1);
}
else if(limit_switch == OPEN && pilot_lever == FALLING) {
current_state = CHECKING_BEFORE_LANDING;
printf("\nCurrent State: %s\nLight Status: %s\nHydraulic Control for Landing Gear Status: %s\n\n",
STATE_TABLE[current_state].present_state, //print present state of state 4
STATE_TABLE[current_state].light_status, //print Light Status of state 4
STATE_TABLE[current_state].hydraulics_status);//print hydraulics status of state 4
}
}
// This function checks the conditions of GEAR UP
void gear_up(void) {
printf("Enter the Pilot's Lever Position: ");
printf("\nExpected input 2: ");
fflush(stdout);
scanf("%u", &pilot_lever);
if (pilot_lever == FALLING) {
current_state = CHECKING_BEFORE_LANDING;
printf("\nCurrent State: %s\nLight Status: %s\nHydraulic Control for Landing Gear Status: %s\n\n",
STATE_TABLE[current_state].present_state, //print present state of state 4
STATE_TABLE[current_state].light_status, //print Light Status of state 4
STATE_TABLE[current_state].hydraulics_status); //print Hydraulics Status of state 4
delay(1);
}
}
// This function checks the conditions of CHECK BEFORE LANDING
void check_before_landing(void) {
printf("Enter the Pilot's Lever Position: ");
printf("\nExpected input 3: ");
fflush(stdout);
scanf("%u", &pilot_lever);
if (pilot_lever == DOWN) {
current_state = LOWERING_GEAR;
printf("\nWait for 3 seconds...\n");
for (int i = 3; i > 0; i--) {
printf("%d\n", i);
fflush(stdout);
delay(1);
}
printf("\nCurrent State: %s\nDirection Valve Status: %s\n\n",
STATE_TABLE[current_state].present_state, //print present state of state 5
STATE_TABLE[current_state].direction_valve_status); //print Direction Valve Status of state 5
delay(1);
}
}
// This function checks the conditions of LOWERING GEAR
void lowering_gear(void) {
printf("Enter the Limit_Switch Status, Pilot_Lever Status: ");
printf("\nExpected input 0 3: ");
fflush(stdout);
scanf("%u %u", &limit_switch, &pilot_lever);
if(limit_switch == OPEN && pilot_lever == DOWN) {
current_state = GEAR_DOWN;
printf("\nCurrent State: %s\nLight Status: %s\nFlight Status: LANDED AND PARKED...\n\n",
STATE_TABLE[current_state].present_state, //print present state of state 0
STATE_TABLE[current_state].light_status); //print Light Status of state 0
delay(1);
exit(0); //EXIT THE CODE
}
else if (limit_switch == CLOSE && pilot_lever == RISING) {
current_state = CHECKING_BEFORE_TAKE_OFF;
printf("\nCurrent State: %s\nLight Status: %s\n\n",
STATE_TABLE[current_state].present_state, //print present state of state 1
STATE_TABLE[current_state].light_status); //print Light Status of state 1
}
}
OUTPUT:

