Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Overview of the Project:
In this project, controlling the retraction and extension of the Airplane’s landing gear can be implemented using a Finite State Machine. The project describes the code for the landing of an airplane. we are going to write the code for different states before takeoff and time of landing. There are different switch states for every state. Using the finite state machine we are going to write the code.
Goals
- Using finite state machine to write a code.
- the understanding of the sequence of the code.
Key Highlights of the Project:
- Using the static declarations,
- use of enum functions
- calling functions.
Deliverables of this project:
- understanding the sequence of the code.
- understanding enum function.
- understanding calling function.
- usage of pointers
Program

#include<stdio.h>
#include"states.h"
static void(*state_table[])(void)={Gear_Down,Checking_Before_Takeoff,Raising_Gear,Gear_Up,Checking_Before_Landing,Lowering_Gear};//static fumction//
int main()//main function//
{
init_State_Machine();
while(1)//intlizing the state//
{
state_table[current_state]();//calling function//
}
}

#ifndef STATES_H_
#define STATES_H_
//fuctioms//
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,
LWRING_GR
}state_Type;
typedef enum Switch
{
open,
close//plane is grounded//
}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*hydrulic_pump_control_status;
char*gas_pressurized_spring_status[2];
}State_table;
static State_table State_Machine[6]=
{
{"Geardown","Green","Down","Enabled",0,0},
{"Checking Before Take Off","Green","Down","Enabled",0,0},
{"Raising Gear","Red","Up","Enbabled",{"Disabled","Enabled"}},
{"Gear Up","Null","Disabled",0,0},
{"Checking Before landing","Green","Down","Enabled",0,0},
{"Lowering Gear","Green","Down","enbaled",{"Disabled","Enabled"}}
};
#endif /* STATES_H_ */

#include"states.h"
#include<stdio.h>
void Lowering_Gear()
{
current_state = LWRING_GR;
printf("\nEnter the limit switch status,pilot Lever status::");//printing//
fflush(stdout);//clearing the cache memory//
scanf("%d %d",&limit_switch,&pilot_lever);//storing the data//
if(limit_switch== close && pilot_lever==rising )//checking the condition//
{
current_state=CHK_BFR_TKFF;//calling the state//
printf("\n Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("\n light status : %s\n",State_Machine[current_state].light_status);
}
if(limit_switch == open && pilot_lever== Down)//checking the condition//
{
current_state=GR_DWN;
printf("\n Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("\n light status : %s\n",State_Machine[current_state].light_status);
printf("\n Hydraulic control for landing gear status : %s\n",State_Machine[current_state].hydrulic_pump_control_status);
}
else
{
Lowering_Gear();
}
}

#include"states.h"
#include<stdio.h>
void Gear_Up()
{
current_state=GR_UP;
printf("\nEnter the pilot Lever status::");//printing//
fflush(stdout);//clearing the cache menory//
scanf("%d",&pilot_lever);//storing the data//
if(pilot_lever== Falling)//checking the condition//
{
current_state=CHK_BFR_LND;
printf("\n Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("\n light status : %s\n",State_Machine[current_state].light_status);
printf("\n Hydraulic control for landing gear status : %s\n",State_Machine[current_state].hydrulic_pump_control_status);
}
else
{
Gear_Up();
}
}

#include"states.h"
#include<stdio.h>
void init_State_Machine()
{
current_state = GR_DWN;
printf("\n state machine is initialized & program is in Gear_Down state & light status:%s\n",State_Machine[current_state].light_status);
}

#include"states.h"
#include<stdio.h>
void Gear_Down()
{
current_state=GR_DWN;
printf("\nEnter the pilot Lever position and squat switch status:");
fflush(stdout);
scanf("%d%d",&pilot_lever,&squat_switch);
if(pilot_lever== rising && squat_switch== open)
{
current_state=CHK_BFR_TKFF;
printf("\n Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("\n light status : %s\n",State_Machine[current_state].light_status);
}
else
{
Gear_Down();
}
}

#include"states.h"
#include<stdio.h>
void Raising_Gear()
{
current_state=RSING_GR;
printf("\nEnter the limit switch status,pilot Lever status:");
fflush(stdout);
scanf("%d %d",&limit_switch,&pilot_lever);
printf("\n wait for a sec");
if(limit_switch== open && pilot_lever==Falling )
{
current_state=CHK_BFR_LND;
printf("\n Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("\n light status : %s\n",State_Machine[current_state].light_status);
printf("\n Hydraulic control for landing gear status : %s\n",State_Machine[current_state].hydrulic_pump_control_status);
}
if(limit_switch == close && pilot_lever== UP)
{
current_state=GR_UP;
printf("\n Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("\n light status : %s\n",State_Machine[current_state].light_status);
printf("\n Hydraulic control for landing gear status : %s\n",State_Machine[current_state].hydrulic_pump_control_status);
}
else
{
Raising_Gear();
}
}

#include"states.h"
#include<stdio.h>
void Checking_Before_Landing()
{
current_state=CHK_BFR_LND;
printf("\nEnter the pilot Lever position and hydraulic Mechanism:");
fflush(stdout);
scanf("%d %d",&pilot_lever,&hydraulic_mechanism);
if(hydraulic_mechanism== Working && pilot_lever==Down)
{
current_state=LWRING_GR;
printf("\n Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("\n light status : %s\n",State_Machine[current_state].light_status);
printf("\n direction valve status : %s\n",State_Machine[current_state].direction_valve_status);
}
else if(hydraulic_mechanism== Malfunctioning && pilot_lever== Down)
{
current_state=LWRING_GR;
printf("\n Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("\n light status : %s\n",State_Machine[current_state].light_status);
printf("\n Hydraulic control for landing gear status : %s\n",State_Machine[current_state].hydrulic_pump_control_status);
printf("\n Gas pressure spring system status : %s\n",State_Machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]);
}
if(pilot_lever==(UP||rising||Falling))
{
Checking_Before_Landing();
}
}

#include"states.h"
#include<stdio.h>
void Checking_Before_Takeoff()
{
current_state=CHK_BFR_TKFF;
printf("\nEnter the pilot Lever position ,SQUAT switch status and hydraulic Mechanism:");
fflush(stdout);
scanf("%d %d %d",&pilot_lever,&squat_switch,&hydraulic_mechanism);
printf("\nwait for 2 sec\n");
if(pilot_lever==Falling|| squat_switch== close)
{
Gear_Down();
}
else if(hydraulic_mechanism== Working && pilot_lever==UP && squat_switch==open)
{
current_state=RSING_GR;
printf("\n Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("\n light status : %s\n",State_Machine[current_state].light_status);
printf("\n direction valve status : %s\n",State_Machine[current_state].direction_valve_status);
}
else if(hydraulic_mechanism== Malfunctioning && pilot_lever== UP && squat_switch==open)
{
current_state=LWRING_GR;
printf("\n Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("\n light status : %s\n",State_Machine[current_state].light_status);
printf("\n Hydraulic control for landing gear status : %s\n",State_Machine[current_state].hydrulic_pump_control_status);
printf("\n Gas pressure spring system status : %s\n",State_Machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]);
}
}
OUTPUTs
Expected Outcomes:

