Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
/*
============================================================================
Name : Project_2.c
Author : Om Deore
Date : 09/08/2022
Copyright : Your copyright notice
Description : controlling the retraction and extension of Airplane’s landing gear
can be implemented using Finite State Machine (FSM). FSM is the most
efficient algorithm which is mathematical model of computation.
============================================================================
*/
#include "States.h" // Including the States.h header file
// Creating a void pointer array and updating all functions name inside it
static void(*state_table[])(void) ={ Gear_Down, Checking_Before_Takeoff, Raising_Gear,
Gear_Up, Checking_Before_Landing, Lowering_Gear};
int main() // Main program
{
init_state_machine(); // Calling the init_state_machine function
while(1) // Using while loop to continuously run the loop
{
state_table[current_state](); // Calling the required function
}
return 0;
}
/*
* States.h
*
* Created on: Aug 9, 2022
* Author: OM
* Description: This header file contains declaration of all functions, including all the libraries required
* The required enum for state,switch,pilot_lever,hydraulic mechanism status is created
* Declaration of required variables, structure is created to create the state table representation
*/
#ifndef STATES_H_ // Un-defining the header file with same name
#define STATES_H_ // Defining the header file again
#include<stdio.h> // Including the stdio.h header file to access functions
void init_state_machine(); // Declaring the init_state_machine function with no return,no arguments
void Gear_Down(); // Declaring the Gear_Down function with no return,no arguments
void Checking_Before_Takeoff(); // Declaring the Checking_Before_Takeoff function with no return,no arguments
void Raising_Gear(); // Declaring the Raising_Gear function with no return,no arguments
void Gear_Up(); // Declaring the Gear_Up function with no return,no arguments
void Checking_Before_Landing(); // Declaring the Checking_Before_Landing function with no return,no arguments
void Lowering_Gear(); // Declaring the Lowering_Gear function with no return,no arguments
typedef enum State // Creating the enum State for various states
{
GR_DWN,
CHK_BFR_TKFF,
RSING_GR,
GR_UP,
CHK_BFR_LND,
LWRNG_GR
}state_type;
typedef enum Switch // Creating the enum Switch for various positions
{
open,
close
}switch_status;
typedef enum Pilot_Lever // Creating the enum Pilot_Lever for various conditions
{
Rising,
Falling,
UP,
DOWN
}pilot_lever_status;
typedef enum Hyrdraulic_Mechanism // Creating the enum Hyrdraulic_Mechanism for various conditions
{
working,
malfunctioning
}Hydraulic_mechanism_status;
static volatile switch_status limit_switch; // Declaring the limit_switch variable of static volatile enum switch type
static volatile switch_status squat_switch; // Declaring the squat_switch variable of static volatile enum switch type
static volatile pilot_lever_status pilot_lever; // Declaring the pilot_lever variable of static volatile enum Pilot_Lever type
static volatile Hydraulic_mechanism_status Hydrualic_status; // Declaring the Hydrualic_status variable of static volatile enum Hyrdraulic_Mechanism type
state_type current_state; // Declaring the current_state of enum State type
typedef struct // Creating the structure for State Table
{
char *current_state_indication; // Pointer for current_state_indication of char type
char *light_status; // Pointer for light_status of char type
char *direction_valve_status; // Pointer for current_state_indication of char type
char *hydrualic_pump_control_status; // Pointer for hydrualic_pump_control_status of char type
char *gas_pressurized_spring_status[2]; // Pointer array for gas_pressurized_spring_status[] of char type
}State_table;
static State_table State_Machine[6] = // Creating State_Machine Variable array of state_table type
{ {"Gear Down","Green","Down","Enabled",{0,0}}, // State_table values for Gear Down
{"Check before take-off","Green","Down","Enabled",{0,0}}, // State_table values for Check before take-off
{"Raising Gear","Red","Up","Enabled",{"Disabled","Enabled"}}, // State_table values for Raising Gear
{"Gear Up","Off","Null","Disabled",{0,0}}, // State_table values for Gear Up
{"Check before landing","Red","Down","Enabled",{0,0}}, // State_table values for Check before landing
{"Lowering Gear","Green","Down","Enabled",{"Disabled","Enabled"}} // State_table values for Lowering Gear
};
#endif /* STATES_H_ */ // End of States.h
/*
* init_state_machine.c
*
* Created on: Aug 9, 2022
* Author: OM
* Description: This code will initialize current_state to gear down and will print light status
*/
#include "States.h" // Including the States.h header file
void init_state_machine() // Defining the init_state_machine function
{
current_state = GR_DWN; // Updating the current_state to gear down
// printing the current state and light state
printf("The state machine is initialized and is in Gear Down state and Light State is %s\n",
State_Machine[current_state].light_status);
}
/*
* Gear_Down.c
*
* Created on: Aug 9, 2022
* Author: OM
* Description: This code will initialize current_state to gear down
* Then depending upon the Pilot_lever position and Squat Switch status
* The current state will get updated to check before take-off state
*/
#include "States.h" // Including the States.h header file
void Gear_Down(void) // Defining the Gear_Down function
{
current_state = GR_DWN; // Updating the current_state to gear down
printf("\nEnter the Pilot's Lever Position and Squat Switch Status:"); // Asking the user for Pilot lever position and squat switch status
fflush(stdout); // Clearing the Output Stream
scanf("%d %d",&pilot_lever,&squat_switch); // Storing the value
if(pilot_lever == Rising && squat_switch == open) // If loop is used to check the conditions
{
//If true then the state is changed to check before take-off state
current_state = CHK_BFR_TKFF; // Updating the current state
printf("\nThe current state is: %s\n",State_Machine[current_state].current_state_indication); // printing the current state and light status
printf("Light Status: %s\n",State_Machine[current_state].light_status);
}
else // If condition is not satisfied again gear_down function is called
{
Gear_Down(); // Calling Gear_Down function
}
}
/*
* Checking_Before_Takeoff.c
*
* Created on: Aug 9, 2022
* Author: OM
* Description: This code will initialize current_state to check before take-off
* Then depending upon the Pilot_lever position and Squat Switch status and Hydraulic mechanism status
* The current state will get updated to Rising gear state or gear down state
*/
#include "States.h" // Including the States.h header file
void Checking_Before_Takeoff() // Defining the Checking_Before_Takeoff function
{
current_state = CHK_BFR_TKFF; // Updating the current_state to Checking_Before_Takeoff
// Asking the user for Pilot lever position and squat switch status and Hydraulic Mechanism Status
printf("\nEnter the Pilot's Lever Position and Squat Switch Status and Hydraulic Mechanism Status:");
fflush(stdout); // Clearing the Output Stream
scanf("%d %d %d",&pilot_lever,&squat_switch,&Hydrualic_status); // Storing the value
printf("\nWait for 2 seconds\n"); // Waiting for 2 seconds
if(pilot_lever == Falling || squat_switch == close) // If loop is used to check the conditions
{
Gear_Down(); //If true then Gear_Down function is called
}
else if(pilot_lever == UP && squat_switch == open && Hydrualic_status == working) // Else-If loop is used to check the conditions
{
current_state = RSING_GR; // Updating the current state to raising gear
// Printing the current state and light status and direction valve status
printf("\nThe current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light Status: %s\n",State_Machine[current_state].light_status);
printf("Hydraulic Mechanism Status: %s\n",State_Machine[current_state].direction_valve_status);
}
else if(pilot_lever == UP && squat_switch == open && Hydrualic_status == malfunctioning) // Else-If loop is used to check the conditions
{
current_state = RSING_GR; // Updating the current state to raising gear
// Printing the current state and light status and direction valve status along with the gas pressurized spring status
printf("\nThe current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light Status: %s\n",State_Machine[current_state].light_status);
printf("Hydraulic Mechanism Status: %s\n",State_Machine[current_state].direction_valve_status);
printf("Gas Pressurized Spring Status: %s\n",State_Machine[current_state].gas_pressurized_spring_status[Hydrualic_status]);
}
}
/*
* Raising_Gear.c
*
* Created on: Aug 10, 2022
* Author: OM
* Description: This code will initialize current_state to Raising Gear
* Then depending upon the Pilot_lever position and Limit Switch status
* The current state will get updated to Check before landing or gear up or raising gear is called again
*/
#include"States.h" // Including the States.h header file
void Raising_Gear() // Defining the Raising_Gear function
{
current_state = RSING_GR; // Updating the current_state to Raising_Gear
printf("\nEnter the Pilot's Lever Position and Limit Switch Status:"); // Asking the user for Pilot lever position and Limit switch status
fflush(stdout); // Clearing the Output Stream
scanf("%d %d",&pilot_lever,&limit_switch); // Storing the value
printf("\nWait for 1 second\n"); // Waiting for 2 seconds
if(pilot_lever == Falling && limit_switch == open) // If loop is used to check the conditions
{
current_state = CHK_BFR_LND; // Updating the current state to check before landing
// Printing the current state and light status and Hydraulic Control for Landing Gear Status
printf("\nThe current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light Status: %s\n",State_Machine[current_state].light_status);
printf("Hydraulic Control for Landing Gear Status: %s\n",State_Machine[current_state].hydrualic_pump_control_status);
}
else if(pilot_lever == UP && limit_switch == close) // Else-If loop is used to check the conditions
{
current_state = GR_UP; // Updating the current state to gear up
// Printing the current state and light status and Hydraulic Control for Landing Gear Status
printf("\nThe current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light Status: %s\n",State_Machine[current_state].light_status);
printf("Hydraulic Control for Landing Gear Status: %s\n",State_Machine[current_state].hydrualic_pump_control_status);
}
else // If both the conditions are not satisfied again Raising_Gear function is called
{
Raising_Gear(); // Calling Raising_Gear function
}
}
/*
* Gear_Up.c
*
* Created on: Aug 10, 2022
* Author: OM
* Description: This code will initialize current_state to Gear Up
* Then depending upon the Pilot_lever position,
* The current state will get updated to Check before landing or gear up function is called again
*/
#include "States.h" // Including the States.h header file
void Gear_Up() // Defining the Gear_Up function
{
current_state = GR_UP; // Updating the current_state to Gear up
printf("\nEnter the Pilot's Lever position:"); // Asking the user for Pilot lever position
fflush(stdout); // Clearing the Output Stream
scanf("%d",&pilot_lever); // Storing the value
if(pilot_lever == Falling) // If loop is used to check the conditions
{
current_state = CHK_BFR_LND; // Updating the current state to check before landing
// Printing the current state and light status and Hydraulic Control for Landing Gear Status
printf("\nThe current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light Status: %s\n",State_Machine[current_state].light_status);
printf("Hydraulic Control for Landing Gear Status: %s\n",State_Machine[current_state].hydrualic_pump_control_status);
}
else // If condition is not satisfied again gear_down function is called
{
Gear_Up(); // Calling Gear_Up function
}
}
/*
* Checking_Before_Landing.c
*
* Created on: Aug 10, 2022
* Author: OM
* Description: This code will initialize current_state to Check before landing
* Then depending upon the Pilot_lever position and Hydraulic Mechanism Status,
* The current state will get updated to Lowering gear or Checking_Before_Landing function is called again
*/
#include "States.h" // Including the States.h header file
void Checking_Before_Landing() // Defining the Checking_Before_Landing function
{
current_state = CHK_BFR_LND; // Updating the current_state to Check Before landing
// Asking the user for Pilot lever position and Hydraulic Mechanism Status
printf("\n Enter the Pilot's Lever Position and Hydraulic Mechanism Status:");
fflush(stdout); // Clearing the Output Stream
scanf("%d %d",&pilot_lever,&Hydrualic_status); // Storing the value
if(pilot_lever == DOWN && Hydrualic_status == working) // If loop is used to check the conditions
{
current_state = LWRNG_GR; // Updating the current state to Lowering Gear
// Printing the current state and light status and Direction Valve Status
printf("\nThe current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light Status: %s\n",State_Machine[current_state].light_status);
printf("Direction Valve Status: %s\n",State_Machine[current_state].direction_valve_status);
}
else if(pilot_lever == DOWN && Hydrualic_status == malfunctioning) // Else-If loop is used to check the conditions
{
current_state = LWRNG_GR; // Updating the current state to Lowering Gear
// Printing the current state and light status and Direction Valve Status and Gas Pressurized Spring Status
printf("\nThe current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light Status: %s\n",State_Machine[current_state].light_status);
printf("Direction Valve Status: %s\n",State_Machine[current_state].direction_valve_status);
printf("Gas Pressurized Spring Status: %s\n",State_Machine[current_state].gas_pressurized_spring_status[Hydrualic_status]);
}
else if(pilot_lever == (UP || Rising || Falling)) // Else-If loop is used to check the conditions
{
Checking_Before_Landing(); // Calling Checking_Before_Landing function
}
}
/*
* Lowering_Gear.c
*
* Created on: Aug 10, 2022
* Author: OM
* Description: This code will initialize current_state to Lowering_Gear
* Then depending upon the Pilot_lever position and Limit Switch Status,
* The current state will get updated to Check before takeoff or Gear Down or Lowering_Gear function is called again
*/
#include "States.h" // Including the States.h header file
void Lowering_Gear() // Defining the Lowering_Gear function
{
current_state = LWRNG_GR; // Updating the current_state to Lowering Gear
printf("\nEnter the Pilot's Lever Position and Limit Switch Status: "); // Asking the user for Pilot lever position and Limit Switch Status
fflush(stdout); // Clearing the output stream
scanf("%d %d",&pilot_lever,&limit_switch); // Storing the value
if(pilot_lever == Rising && limit_switch == close) // If loop is used to check the conditions
{
current_state = CHK_BFR_TKFF; // Updating the current state to Check before take-off
// Printing the current state and light status
printf("\nThe current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light Status: %s\n",State_Machine[current_state].light_status);
}
else if(pilot_lever == DOWN && limit_switch == open) // Else-If loop is used to check the conditions
{
current_state = GR_DWN; // Updating the current state to Gear Down
// Printing the current state and light status and Hydraulic Control for Landing Gear Status
printf("\nThe current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light Status: %s\n",State_Machine[current_state].light_status);
printf("Hydraulic Control for Landing Gear Status: %s\n",State_Machine[current_state].hydrualic_pump_control_status);
}
else // If both the conditions are not satisfied again Lowering_Gear function is called
{
Lowering_Gear(); // Calling Checking_Before_Landing function
}
}
Normal Flow of States Output

2. Other states of Gear Down

3. Other states of Checking before Takeoff

4. Other states of Raising Gear

5. Other states of Check Before Landing

6. Other states of Lowering Gear
