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

/*
* states.h
*
* Created on: 18-Aug-2023
* Author: Naveen kumar
*/
#ifndef STATES_H_
#define STATES_H_
#include
/* -----Functions----- */
void Init_State_Machine();
void Gear_Down();
void Checking_Before_Takeoff();
void Raising_Gear();
void Gear_Up();
void Checking_Before_Landing();
void Lowering_Gear();
typedef enum State
{
GR_DWN, CHK_BFR_TKFF, RSING_GR, GR_UP, CHK_BFR_LND, LWRNG_GR
}State_Type;
typedef enum Switch
{
Open, Close //Plane is in ground
}Switch_Status;
typedef enum Pilot_Lever
{
Rising, Falling, UP, DOWN
}Pilot_Lever_Status;
typedef enum Hydraulic_Mechanism
{
Working, Malfunctioning
}Hydraulic_Mechanism_Status;
/* -----input for changing the State----- */
static volatile Switch_Status squat_switch;
static volatile Switch_Status limit_switch;
static volatile Pilot_Lever_Status pilot_lever;
static volatile Hydraulic_Mechanism_Status hydraulic_mechanism;
State_Type current_state;
typedef struct
{
char *current_state_indication;
char *light_status;
char *direction_valve_status;
char *hydraulic_pump_control_status;
char *gas_pressurized_spring_status[2];
}State_Table;
static State_Table State_Machine[6] = { { "Gear Down", "Green", "Down",
"Enabled", { NULL, NULL } }, { "Checking Before Take Off", "Green",
"Down", "Enabled", { NULL, NULL } }, { "Raising Gear", "Red", "Up",
"Enabled", { "Disabled", "Enabled" } }, { "Gear Up", "Off", "Null",
"Disabled", { NULL, NULL } }, { "Checking Before Landing", "Red",
"Down", "Enabled", { NULL, NULL } }, { "Lowering Gear", "Green", "Down",
"Enabled", { "Disabled", "Enabled" } } };
#endif /* STATES_H_ */
//STATE_MACHINE
#include "States.h" // include the user defined header file
void Init_State_Machine()
{
//System Initialized and its in State_A
current_state = GR_DWN;
printf(
"\nState Machine is initialized & Program is in Gear_Down State & Light Status : %s\n",
State_Machine[current_state].light_status);
}
//MAIN
#include "States.h" //include the user defined header file
static void (*state_table[])(void) = {Gear_Down, Checking_Before_Takeoff, Raising_Gear,Gear_Up, Checking_Before_Landing, Lowering_Gear};
int main()
{
Init_State_Machine(); //calling function
while (1) // loop
{
state_table[current_state]();
}
}
//GEAR DOWN
void Gear_Down(void)
{
current_state = GR_DWN;
printf("\nEnter the Pilot's Lever Position and Squat Switch Status:");
fflush(stdout);
scanf("%d %d", &pilot_lever, &squat_switch);
if (pilot_lever == Rising && squat_switch == Open) //check the condition
{
current_state = CHK_BFR_TKFF;
printf("\nCurrent State : %s\n",
State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n", State_Machine[current_state].light_status);
} else {
Gear_Down(); //calling function
}
}
//CHECKING BEFORE TAKEOF
#include "States.h" // include the user defined header file
void Checking_Before_Takeoff()
{
current_state = CHK_BFR_TKFF;
printf(
"\nEnter the Pilot's Lever Position,Squat Switch Status and Hydraulic Mechanism Status:");
fflush(stdout);
scanf("%d %d %d", &pilot_lever, &squat_switch, &hydraulic_mechanism); // get the input from user
printf("\nWait for 2 secs\n");
if (pilot_lever == Falling || squat_switch == Close) {
Gear_Down(); // calling function
}
else if (pilot_lever == UP && squat_switch == Open
&& hydraulic_mechanism == Working) {
current_state = RSING_GR;
printf("\nCurrent State : %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\n",
State_Machine[current_state].direction_valve_status);
}
else if (pilot_lever == UP && squat_switch == Open
&& hydraulic_mechanism == Malfunctioning) {
current_state = RSING_GR;
printf("\nCurrent State : %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 System Status:%s\n",
State_Machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]);
}
}
//RISING GEAR
#include "States.h" // include the user defined header file
void Raising_Gear()
{
current_state = RSING_GR;
printf("\nEnter the Limit Switch Status,Pilots Leaver Status:");
fflush(stdout);
scanf("%d %d", &limit_switch, &pilot_lever); //getting input from user
printf("\nWait for 1 sec\n");
if (pilot_lever == Falling && limit_switch == Open) //check the condition
{
current_state = CHK_BFR_LND;
printf("\nCurrent State : %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].hydraulic_pump_control_status);
} else if (limit_switch == Close && pilot_lever == UP) {
current_state = GR_UP;
printf("\nCurrent State : %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].hydraulic_pump_control_status);
}
else {
Raising_Gear(); //calling function
}
}
//GEAR UP
#include "States.h" // include the user defined header file
void Gear_Up()
{
current_state = GR_UP;
printf("\nEnter the Pilot's Lever Position:");
fflush(stdout);
scanf("%d", &pilot_lever); // get the input from user
if (pilot_lever == Falling) //check the condition
{
current_state = CHK_BFR_LND;
printf("\nCurrent State : %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].hydraulic_pump_control_status);
} else {
Gear_Up(); //calling function
}
}
//CHECKING BEFORE LANDING
#include "States.h" //include the user defined header files
void Checking_Before_Landing()
{
current_state = CHK_BFR_LND;
printf(
"\nEnter the Pilot's Lever Position and Status of Hydraulic Mechanism:");
fflush(stdout);
scanf("%d %d", &pilot_lever, &hydraulic_mechanism); // get the user input
if (pilot_lever == DOWN && hydraulic_mechanism == Working) // checking condition
{
current_state = LWRNG_GR;
printf("\nCurrent State : %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 && hydraulic_mechanism == Malfunctioning) {
current_state = LWRNG_GR;
printf("\nCurrent State : %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 System Status:%s\n",
State_Machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]);
}
if (pilot_lever == (UP || Rising || Falling)) {
Checking_Before_Landing(); //calling function
}
}
//LOWERING GEAR
#include "States.h" // include the user defined header file
void Lowering_Gear()
{
current_state = LWRNG_GR;
printf("\nEnter the Limit Switch Status,Pilots Leaver Status::");
fflush(stdout);
scanf("%d %d", &limit_switch, &pilot_lever); //getting input from user
if (pilot_lever == Rising) //Checking the condition
{
current_state = CHK_BFR_TKFF;
printf("\nCurrent State : %s\n",
State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n", State_Machine[current_state].light_status);
}
if (limit_switch == Open) {
current_state = GR_DWN;
printf("\nCurrent State : %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].hydraulic_pump_control_status);
}
if (limit_switch == Close) {
Lowering_Gear(); //calling the function
}
}