Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Project 02: Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Solution:
Obejctive - The goal of the project is to use a finite state machine (FSM), a mathematical model of computation that defines a set of possible states and the transitions between those states in response to input events, to implement a control system for retracting and extending an airplane's landing gear.
The potential states of the landing gear and the events that can cause state transitions are defined by a mathematical calculation model used by the control system. The control system employs a C programme to implement the FSM and an infinite loop to wait for input events and update the state of the landing gear. The control system is intended to control the direction of a valve that regulates the hydraulic pressure used to raise and lower the landing gear. The programme also outputs to the user the status of the valve and controls the direction of the valve dependent on the condition of the landing gear. The programme guarantees that the landing gear is safely retracted and offers a user-friendly interface for manipulating the landing gear.
Goals for the project -
- Controlling the direction of Valve: The programme employs a Finite State Machine (FSM) to manage the extension and retraction of the aircraft's landing gear, which probably entails the usage of hydraulic valves to control the direction of hydraulic fluid flow. The direction of the valve can be controlled to extend or retract the landing gear using the FSM, which transitions between various states dependent on the input events that are received.
- Controlling the retraction and extension of Airplane’s landing gear: The program's FSM regulates how the landing gear on an aeroplane extends and retracts. The landing gear is retracted as the programme begins, and it waits for input events before moving on to the following state. The programme switches to the next state and the landing gear extends or retracts as needed as soon as the proper events are received. The programme can also determine whether the landing gear is fully extended or fully retracted, and it can print the necessary messages to show what position the landing gear is in at the time.
- A direction valve is controlled by the software to allow the hydraulic pressure to either raise or lower the landing gear: The direction valve for the landing gear system is controlled by the FSM in this C programme. The FSM transitions between various states based on the input events provided, and this programme uses the state transition table to determine the next state of the landing gear system based on the current state and received input event. The direction valve can be controlled and hydraulic pressure can be used to raise or lower the landing gear. In order to ensure accurate extension or retraction of the landing gear, this enables efficient and effective control of the direction valve.
C Program Used for the project is -
/*
* Project_02.c
*
* Created on: 20-Apr-2023
* Author: sagar
*/
// Include necessary libraries
#include
// Define possible states
enum {
RETRACTED_STATE,
EXTENDING_STATE,
EXTENDED_STATE,
RETRACTING_STATE,
NUM_STATES
};
// Define possible events
enum {
GEAR_UP_EVENT,
GEAR_DOWN_EVENT,
GEAR_LOCKED_EVENT,
NUM_EVENTS
};
// Define state transition table
int state_table[NUM_STATES][NUM_EVENTS] = {
{ RETRACTED_STATE, EXTENDING_STATE, RETRACTED_STATE },
{ EXTENDED_STATE, EXTENDING_STATE, EXTENDED_STATE },
{ EXTENDED_STATE, RETRACTING_STATE, EXTENDED_STATE },
{ RETRACTED_STATE, RETRACTING_STATE, RETRACTED_STATE }
};
int main() {
int current_state = 0; // Set the initial state to RETRACTED_STATE
int event = 0;
int valve_direction = 0; // Set the initial valve direction to closed and
// 0 - valve is closed, 1 - valve is open
printf("Landing gear is retracted\n");
while (1) {
// Wait for input event
printf("Waiting for input event...\n");
fflush(stdout); // Flush output buffer to ensure messages are printed immediately
scanf("%d", &event);
// Determine next state based on input event and current state
if (current_state == 0 && event == 1) { // If the current state is RETRACTED_STATE and the GEAR_UP_EVENT is received
current_state = 1; // Set the current state to EXTENDING_STATE
printf("Landing gear is transitioning up\n");
fflush(stdout);
valve_direction = 1; // Open valve to raise landing gear
} else if (current_state == 1 && event == 2) { // If the current state is EXTENDING_STATE and the GEAR_DOWN_EVENT is received
current_state = 2; // Set the current state to EXTENDED_STATE
printf("Landing gear is fully extended\n");
fflush(stdout);
valve_direction = 0; // Close valve to maintain pressure
} else if (current_state == 2 && event == 3) { // If the current state is EXTENDED_STATE and the GEAR_LOCKED_EVENT is received
current_state = 3; // Set the current state to RETRACTING_STATE
printf("Landing gear is transitioning down\n");
fflush(stdout);
valve_direction = 1; // Open valve to lower landing gear
} else if (current_state == 3 && event == 4) { // If the current state is RETRACTING_STATE and the GEAR_UP_EVENT is received
current_state = 0; // Set the current state to RETRACTED_STATE
printf("Landing gear is retracted\n");
fflush(stdout);
valve_direction = 0; // Close valve to maintain pressure
} else { // If an invalid event is received or the event does not cause a state change
printf("Invalid input event, landing gear remains in current state\n");
fflush(stdout);
}
// Control the direction of the valve based on current state
if (current_state == 0 || current_state == 2) { // If the current state is RETRACTED_STATE or EXTENDED_STATE
valve_direction = 0; // Close valve to maintain pressure
}
// Output valve direction
if (valve_direction == 0) {
printf("Valve is closed\n");
fflush(stdout);
} else if (valve_direction == 1) {
printf("Valve is open\n");
fflush(stdout);
}
}
return 0;
}



Logical Explaination -
This code uses a finite state machine to implement the control for retracting and extending an airplane's landing gear. RETRACTED_STATE, EXTENDING_STATE, EXTENDED_STATE, and RETRACTING_STATE are the four potential states of the state machine. The three events GEAR_UP_EVENT, GEAR_DOWN_EVENT, and GEAR_LOCKED_EVENT are all capable of causing a state change.
Based on the current state and the input event, the state transition table specifies which state the state machine will transition to. The state machine will change to the EXTENDING_STATE, for instance, if the current state is RETRACTED_STATE and the input event is GEAR_UP_EVENT.
In the main function, the valve_direction is initialised to 0, indicating that the valve is closed, and the current state is initialised to RETRACTED_STATE. Following this, the programme starts an infinite loop where it waits for an input event, predicts the following state using the current state and the input event, and regulates the direction of the valve using the current state.
The programme switches to the EXTENDING_STATE, opens the valve to raise the landing gear, and produces a message indicating that the landing gear is transitioning up if the current state is RETRACTED_STATE and the input event is GEAR_UP_EVENT.
The programme switches to the extended state and closes the valve to preserve pressure if the current state is EXTENDING_STATE and the input event is GEAR_DOWN_EVENT and outputs a message indicating that the landing gear is fully extended.
The programme switches to the RETRACTING_STATE, opens the valve to lower the landing gear, and produces a message indicating that the landing gear is transitioning down if the current state is EXTENDED_STATE and the input event is GEAR_LOCKED_EVENT.
The programme switches to the RETRACTED_STATE, closes the valve to maintain pressure, and outputs a message indicating that the landing gear is retracted if the current state is RETRACTING_STATE and the input event is GEAR_UP_EVENT.
The programme displays a message stating that the landing gear is still in its current state if the input event is invalid.
Based on the valve's present condition, the programme then generates a message saying whether the valve is open or closed.
Output form the Program -

The status of the landing gear and the valve's direction are the program's outputs. The programme prints "Landing gear is retracted" and initially sets the current state to "RETRACTED_STATE". Then, it moves into a while loop to wait for input events.
The programme switches to the "EXTENDING_STATE," prints "Landing gear is transitioning up," and sets the valve direction to open when the user enters the "GEAR_UP_EVENT" (value 1). The programme switches to the "EXTENDED_STATE," prints "Landing gear is fully extended," and sets the valve direction to closed (value 0) when the user enters the "GEAR_DOWN_EVENT" (value 2).
The programme switches to the "RETRACTING_STATE," prints "Landing gear is transitioning down," and sets the valve direction to open (value 1) when the user enters the "GEAR_LOCKED_EVENT" (value 3). The programme switches back to the "RETRACTED_STATE" and prints "Landing gear is retracted" when the user enters the "GEAR_UP_EVENT" command once more (value 1). It also adjusts the valve direction to closed (value 0) and returns to the "RETRACTED_STATE" state.
The programme prints "Invalid input event, landing gear remains in current state" if the user submits an invalid input event.
In accordance with the current state, the programme also controls the valve's direction. The valve direction is set to closed (value 0) when the current state is either "RETRACTED_STATE" or "EXTENDED_STATE". The valve direction is set to open when the current state is either "EXTENDING_STATE" or "RETRACTING_STATE" (value 1).
Depending on the value of the valve direction variable, the console displays "Valve is closed" or "Valve is open" as the valve's direction.
Outcome/Learnings from the project -
The programme alternates between several states based on the input events received from the switches and controls the landing gear mechanism using an FSM. The programme is made to be aware of different switch and gear states, including the current position of the landing gear (retracted or extended) and the switch position (up or down) that controls the gear.
Additionally, based on the present state and the input event received, the programme employs a state transition table to determine the subsequent state of the landing gear system. With this strategy, the FSM may be used to efficiently and effectively operate the landing gear system.
The C programme therefore fulfils the requirement of providing control for the retraction and extension of Airplane's landing gear using an FSM with knowledge of various states of switches and gear.