Challenge

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

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

Overview of the Project: 

The overview of this project is as follows:

  1. The project implements a state machine for controlling the retraction and extension of an airplane's landing gear.
  2. It uses several enums to define different states and variables related to the gear control.
  3. The state machine consists of six states, each representing a specific operation related to the gear.
  4. The main function continuously executes the state transition based on user input.
  5. Each state is implemented as a separate function, which updates the current state based on user input and displays relevant information about the current state and associated variables.
  6. The state transitions follow a specific logic based on the user inputs and the current state.

Goals:

The goals for this project are as follows:

  1. The goal of this project is to implement a state machine for controlling the retraction and extension of an airplane's landing gear.
  2. The code simulates the interaction between a pilot and the landing gear system by prompting the user for input and transitioning between different states based on the input.

Key Highlights of the Project:

The key highlights of this project are as follows:

1. State Machine Implementation: The code demonstrates the implementation of a state machine using a static function array 'state_table' and a current state variable' current_state'

2. User Interaction: The code interacts with the user by prompting for input during runtime. The user is asked to provide input for various conditions, such as the position of the pilot lever, the state of the squat switch, the position of the direction valve, the state of the limit switch, and the control status. The user's input determines the transitions between states and the behavior of the system.

3. Enum Types: The code utilizes enum types to define different states and switches in the landing gear system. These enums provide a structured and readable way to represent and work with the different states and conditions of the system.

4. Structure and Array: The code defines a structure 'state' to hold information about the different states of the landing gear system, such as the air state, lights state, direction valve state, and hydraulic valve state. It also declares an array 'State_Machine' of 'state' structures to represent the various states of the system.

5. Recursive Function Calls: In the state functions 'gear_down', 'check_before_taking_off', 'raising_gear', and 'lowering_gear', recursive function calls are used to handle invalid input from the user. If the user enters incorrect input, the function recalls itself, prompting the user again until valid input is provided.

6. Printing Current State: Each state function prints the current state of the system, as well as the associated information such as the air state, lights state, direction valve state, and hydraulic valve state. This helps in visualizing the state transitions and the overall state of the landing gear system.

7. Infinite Loop: The 'main' function contains an infinite loop that continuously invokes the state functions based on the current state of the system. This loop allows the system to repeatedly prompt the user for input and perform state transitions until the program is terminated externally.

Deliverables of this project: 

The deliverables of this project are as follows:

1. Simulation of Airplane Landing Gear: The code provides a simulation of an airplane's landing gear system. It allows the user to control the retraction and extension of the landing gear by providing input during runtime. The code implements a state machine that represents the behavior of the landing gear system.

2. State Transitions and System Behavior: The code ensures that the landing gear system follows a predefined sequence of state transitions based on the user's input. It enforces the correct order of actions and conditions required for safe operation, such as checking before taking off or landing, raising or lowering the gear, and validating the state of various switches and valves.

3. User Interaction: The code interacts with the user by prompting for input during runtime. The user's input determines the transitions between states and the behavior of the landing gear system. The code provides clear instructions and asks for specific input related to the position of the pilot lever, state of the squat switch, position of the direction valve, state of the limit switch, and control status.

4. Current State Information: The code displays the current state of the landing gear system and provides information about the associated parameters, such as the air state, lights state, direction valve state, and hydraulic valve state. This helps in visualizing the current state of the system and understanding its behavior.

5. Error Handling: The code includes recursive function calls in some state functions to handle invalid input from the user. If the user enters incorrect input, the code prompts the user again until valid input is provided. This ensures that the landing gear system operates correctly and responds appropriately to user input.

6. Debugging and Testing: The code can be used for debugging and testing purposes. By executing the code and providing different input scenarios, it allows for observing the state transitions and validating the behavior of the landing gear system. It helps in identifying and resolving issues related to the control flow and logic of the system.

Expected Outcomes: 

The expected outcomes of this project are as follows:

1. Demonstration of Landing Gear Control: The code aims to provide a functional demonstration of how the retraction and extension of an airplane's landing gear can be controlled using a state machine. It showcases the sequence of actions and conditions required for safe operation.

2. Understanding State Machine Concepts: By studying and analyzing the code, developers can gain a better understanding of state machine concepts, such as states, state transitions, and state-dependent behavior. They can observe how the code represents different states and defines the logic for transitioning between them.

3. Learning Control Flow and Decision Making: The code helps developers grasp the concepts of control flow and decision-making in programming. It showcases how user input influences the flow of the program and how conditional statements and recursive function calls are used to handle different scenarios and ensure correct behavior.

4. Debugging and Issue Identification: The code can be used as a basis for testing and debugging. By running the code with various input scenarios, developers can observe the state transitions and verify if the landing gear system behaves as expected. If any issues or unexpected behavior arises, the code can be examined and modified to identify and resolve the problems.

5. Foundation for Real-Life Implementations: While this code serves as a simplified simulation, it can serve as a starting point for building more sophisticated and realistic landing gear control systems. Developers can expand upon this code by incorporating actual hardware interfaces, safety checks, and integration with other aircraft systems to create a functional implementation of landing gear control.

The code for the given problem is as follows:

/*

* fsm01.c

*

*  Created on: 27 Jun 2023

*      Author: chinmaykadam

*/

#include

#include

//Initializing integer datatype variables to take value from user for different states

static int pilotlever, squatswitch, dirvalve, limswitch, control, choice;

//Enum statetypes in which there are total 6 different states

typedef enum{

geardown,

checkbeforetakingoff,

raisinggear,

gearup,

checkbeforelanding,

loweringgear

}statetypes;

//Enum for Limit switch functionality

typedef enum{

open,

close

}Limit_Switch;

//Enum for Lights indication

typedef enum{

green,

red,

null

}Lights;

//Enum for squat switch functionality

typedef enum{

open1,

close1

}Squat_Switch;

//Enum for Pilot Lever operation

typedef enum{

raising,

falling

}Pilot_Lever;

//Enum for Direction Valve operation

typedef enum{

up,

down

}Direction_Valve;

//Enum for control status of direction valve

typedef enum{

enable,

disable

}Control;

//Declaring state functions

void gear_down();

void check_before_taking_off();

void raising_gear();

void gear_up();

void check_before_landing();

void lowering_gear();

//Creating structure array

typedef struct{

char *AirStates;

char *Lights;

char *DirectionValve;

char *HydraulicValve;

}state;

//Creating static function array

static void(*state_table[])(void) =

{

gear_down,

check_before_taking_off,

raising_gear,

gear_up,

check_before_landing,

lowering_gear

};

//State machine table for all 6 states

state State_Machine[6] = {

{"Gear Down", "Green", "Down", "Enabled"},

{"Checking Before Take-off", "Green", "Up", "Enabled"},

{"Raising Gear", "Red", "Up", "Enabled"},

{"Gear Up", "Off", "Control Disabled", "Disabled"},

{"Checking Before Landing", "Red", "Control Enabled", "Disabled"},

{"Lowering Gear", "Red", "Down", "Enabled"}

};

statetypes current_state = geardown;    //Initializing current state to 'geardown'

int main()

{

printf("Welcome to the Implementation of the Code for controlling the retraction and extension of Airplane’s landing gear \n");

//Infinite while loop

while(1)

{

state_table[current_state]();    //Calling static function array

printf("\nEnter the next operation pilot!");

}

return 0;

}

//Defining 6 state functions

void gear_down()

{

//Assigning current state to geardown state

current_state = geardown;

printf("\nCurrent state is %s", State_Machine[current_state].AirStates);

//Taking value from user

printf("\nIs pilot rising or falling the lever? For Rising = 0, Falling = 1");

fflush(stdout);

scanf("%d", &pilotlever);

printf("\nIs squat switch open or closed? For Open = 0, Close = 1");

fflush(stdout);

scanf("%d", &squatswitch);

//Condition to go to next state

if(pilotlever == 0 && squatswitch == 0)

{

current_state = checkbeforetakingoff;

printf("\nAir state is %s", State_Machine[current_state].AirStates);

printf("\nLights is %s", State_Machine[current_state].Lights);

printf("\nDirection Valve is %s", State_Machine[current_state].DirectionValve);

printf("\nHydraulic Valve is %s", State_Machine[current_state].HydraulicValve);

}

//If above condition(s) doesn't satisfy the function is called again (Recursive function)

else

{

gear_down();

}

}

void check_before_taking_off()

{

//Taking value from user

printf("\nIs pilot rising or falling the lever? For Rising = 0, Falling = 1");

fflush(stdout);

scanf("%d", &pilotlever);

if(pilotlever)

{

//Taking value from user

printf("\nIs squat switch open or closed? For Open = 0, Close = 1");

fflush(stdout);

scanf("%d", &squatswitch);

////Condition to go to emergency state

if(squatswitch)

{

printf("\nEmergency! Abort Take-Off!");

current_state = geardown;

printf("\nAir state is %s", State_Machine[current_state].AirStates);

printf("\nLights is %s", State_Machine[current_state].Lights);

printf("\nDirection Valve is %s", State_Machine[current_state].DirectionValve);

printf("\nHydraulic Valve is %s", State_Machine[current_state].HydraulicValve);

}

//If above condition(s) doesn't satisfy the function is called again (Recursive function)

else

{

check_before_taking_off();

}

}

else

{

//Taking value from user

printf("\nIs direction valve up or down? For Up = 0, Down = 1");

fflush(stdout);

scanf("%d", &dirvalve);

//Condition to go to next state

if(!dirvalve)

{

current_state = raisinggear;

printf("\nAir state is %s", State_Machine[current_state].AirStates);

printf("\nLights is %s", State_Machine[current_state].Lights);

printf("\nDirection Valve is %s", State_Machine[current_state].DirectionValve);

printf("\nHydraulic Valve is %s", State_Machine[current_state].HydraulicValve);

}

//If above condition(s) doesn't satisfy the function is called again (Recursive function)

else

{

check_before_taking_off();

}

}}

void raising_gear()

{

//Taking value from user for emergency landing

printf("\n Do you want to Land in Emergency? Yes-1, No-0");

fflush(stdout);

scanf("%d",&choice);

if(choice)

{

//Condition to go to emergency state

printf("\nEmergency Landing!!\nPilot Lever is Falling.");

current_state = checkbeforelanding;

printf("\nAir state is %s", State_Machine[current_state].AirStates);

printf("\nLights is %s", State_Machine[current_state].Lights);

printf("\nDirection Valve is %s", State_Machine[current_state].DirectionValve);

printf("\nHydraulic Valve is %s", State_Machine[current_state].HydraulicValve);

}

else

{

//Taking value from user

printf("\nIs Limit Switch open or closed? For Open = 0, Close = 1");

fflush(stdout);

scanf("%d", &limswitch);

//Taking value from user

printf("\nIs control for direction valve enabled or disabled? For enabled = 0, disabled = 1");

fflush(stdout);

scanf("%d", &control);

//Condition to go to next state

if(limswitch == 1 && control == 1)

{

current_state = gearup;

printf("\nAir state is %s", State_Machine[current_state].AirStates);

printf("\nLights is %s", State_Machine[current_state].Lights);

printf("\nDirection Valve is %s", State_Machine[current_state].DirectionValve);

printf("\nHydraulic Valve is %s", State_Machine[current_state].HydraulicValve);

}

//If above condition(s) doesn't satisfy the function is called again (Recursive function)

else

{

raising_gear();

}

}

}

void gear_up()

{

//Taking value from user

printf("\nIs pilot rising or falling the lever? For Rising = 0, Falling = 1");

fflush(stdout);

scanf("%d", &pilotlever);

//Taking value from user

printf("\nIs control for direction valve enabled or disabled? For enabled = 0, disabled = 1");

fflush(stdout);

scanf("%d", &control);

//Condition to go to next state

if(pilotlever == 1 && control == 0)

{

current_state = checkbeforelanding;

printf("\nAir state is %s", State_Machine[current_state].AirStates);

printf("\nLights is %s", State_Machine[current_state].Lights);

printf("\nDirection Valve is %s", State_Machine[current_state].DirectionValve);

printf("\nHydraulic Valve is %s", State_Machine[current_state].HydraulicValve);

}

//If above condition(s) doesn't satisfy the function is called again (Recursive function)

else

{

gear_up();

}

}

void check_before_landing()

{

//Taking value from user

printf("\nIs pilot rising or falling the lever? For Rising = 0, Falling = 1");

fflush(stdout);

scanf("%d", &pilotlever);

//Taking value from user

printf("\nIs direction valve up or down? For Up = 0, Down = 1");

fflush(stdout);

scanf("%d", &dirvalve);

if(pilotlever == 1 && dirvalve == 1)

{

//Condition to go to next state

current_state = loweringgear;

printf("\nAir state is %s", State_Machine[current_state].AirStates);

printf("\nLights is %s", State_Machine[current_state].Lights);

printf("\nDirection Valve is %s", State_Machine[current_state].DirectionValve);

printf("\nHydraulic Valve is %s", State_Machine[current_state].HydraulicValve);

}

//If above condition(s) doesn't satisfy the function is called again (Recursive function)

else

{

check_before_landing();

}

}

void lowering_gear()

{

//Taking value from user for emergency take-off

printf("\n Do you want to take-off in Emergency? Yes-1, No-0");

fflush(stdout);

scanf("%d",&choice);

if(choice)

{

//Condition to go to emergency state

printf("\nEmergency Take-Off!! Pilot lever raising.");

current_state = checkbeforetakingoff;

printf("\nAir state is %s", State_Machine[current_state].AirStates);

printf("\nLights is %s", State_Machine[current_state].Lights);

printf("\nDirection Valve is %s", State_Machine[current_state].DirectionValve);

printf("\nHydraulic Valve is %s", State_Machine[current_state].HydraulicValve);

}

else

{

//Taking value from user

printf("\nIs Limit Switch open or closed? For Open = 0, Close = 1");

fflush(stdout);

scanf("%d", &limswitch);

if(!limswitch)

{

//Condition to go to next/first state

current_state = geardown;

printf("\nAir state is %s", State_Machine[current_state].AirStates);

printf("\nLights is %s", State_Machine[current_state].Lights);

printf("\nDirection Valve is %s", State_Machine[current_state].DirectionValve);

printf("\nHydraulic Valve is %s", State_Machine[current_state].HydraulicValve);

}

//If above condition(s) doesn't satisfy the function is called again (Recursive function)

else

{

lowering_gear();

}

}

}

All the necessary screenshots of the output are attached herewith.

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.