Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include<stdio.h>
#include"datas.h"
// Function for Initiate the Process
void Initiate_State_Machine()
{
current_state=GEAR_DOWN;
printf("The FSM program for Flight Landing gear is Initialized and currently the program in light is : %s\n",State_Machine[current_state].current_state_indication);
printf("light is : %s\n",State_Machine[current_state].light_status);
}
// Function for Gear Down Process
void Gear_Down(void)
{
current_state=GEAR_DOWN;
printf("Enter the input from pilot_lever and squat switch\n");
fflush(stdout);
scanf("%d %d" ,&Pilot_Lever , &Squat_Switch);
if(Pilot_Lever==RAISING && Squat_Switch==ON)//Checking the pilot lever and squat switch
{
current_state=CHECKING_BEFORE_TAKE_OFF;
printf("current state is: %s\n ", State_Machine[current_state].current_state_indication);
printf("light is : %s\n",State_Machine[current_state].light_status);
}
else
{
Gear_Down();
}
}
// Function for Checking before Take off
void Checking_Before_Take_Off(void)
{
current_state=CHECKING_BEFORE_TAKE_OFF;
printf("Enter the input from pilot_lever and squat switch and Hydraulic Mechanism");
fflush(stdout);
scanf("%d %d %d",&Pilot_Lever, &Squat_Switch, &hydraulic_mechanism);
if(Pilot_Lever==FALLING && Squat_Switch==OFF)//Checking the pilot lever and squat switch
{
Gear_Down();
}
else if(Pilot_Lever==RAISING && Squat_Switch==ON && hydraulic_mechanism==WORKING)//Checking the pilot lever and squat switch and and hydraulic mechanism
{
current_state=RAISING_GEAR;
printf("current state is: %s\n ", State_Machine[current_state].current_state_indication);
printf("light is : %s\n",State_Machine[current_state].light_status);
printf("Direction value status: %s\n",State_Machine[current_state].direction_valve_status);
}
else if(Pilot_Lever==RAISING && Squat_Switch==ON && hydraulic_mechanism==NOT_WORKING)
{
current_state=RAISING_GEAR;
printf("current state is: %s\n ", State_Machine[current_state].current_state_indication);
printf("light is : %s\n",State_Machine[current_state].light_status);
printf("Direction value status: %s\n",State_Machine[current_state].direction_valve_status);
printf("status of gas pressurized spring system: %c\n",State_Machine[current_state].gas_pressurized_spring_status);
}
}
// Function for Raising Gear
void Raising_Gear(void)
{
current_state=RAISING_GEAR;
printf("Enter the input from pilot_lever and limit switch");
fflush(stdout);
scanf("%d %d",&Pilot_Lever, &Limit_Switch);
if(Pilot_Lever==FALLING && Limit_Switch==ON)// Checking the pilot lever and limit switch
{
current_state=CHECK_BEFORE_LANDING;
printf("current state is: %s\n ", State_Machine[current_state].current_state_indication);
printf("light is : %s\n",State_Machine[current_state].light_status);
printf("Landing Gear Hydraulic control status: %s\n",State_Machine[current_state].hydraulic_pump_control_status);
}
else if(Pilot_Lever==RAISING && Limit_Switch==OFF)
{
current_state=GEAR_UP;
printf("current state is: %s\n ", State_Machine[current_state].current_state_indication);
printf("light is : %s\n",State_Machine[current_state].light_status);
printf("Landing Gear Hydraulic control status: %s\n",State_Machine[current_state].hydraulic_pump_control_status);
}
else
{
Raising_Gear();
}
}
// Function for Gear Up
void Gear_Up(void)
{
current_state=GEAR_UP;
printf("Enter the input from pilot_lever");
fflush(stdout);
scanf("%d ",&Pilot_Lever);
if(Pilot_Lever==FALLING)
{
current_state=CHECK_BEFORE_LANDING;
printf("current state is: %s\n ", State_Machine[current_state].current_state_indication);
printf("light is : %s\n",State_Machine[current_state].light_status);
printf("Landing Gear Hydraulic control status: %s\n",State_Machine[current_state].hydraulic_pump_control_status);
}
else
{
Gear_Up();
}
}
// Function for Check Before Landing
void Check_Before_Landing(void)
{
current_state=CHECKING_BEFORE_TAKE_OFF;
printf("Enter the input from pilot_lever and Hydraulic Mechanism");
fflush(stdout);
scanf("%d %d ",&Pilot_Lever, &hydraulic_mechanism);
if(Pilot_Lever==FALLING && Squat_Switch==OFF)
{
Gear_Down();
}
else if(Pilot_Lever==FALLING && hydraulic_mechanism==WORKING)
{
current_state=LOWERING_GEAR;
printf("current state is: %s\n ", State_Machine[current_state].current_state_indication);
printf("light is : %s\n",State_Machine[current_state].light_status);
printf("Direction value status: %s\n",State_Machine[current_state].direction_valve_status);
}
else if(Pilot_Lever==FALLING && hydraulic_mechanism==NOT_WORKING)
{
current_state=RAISING_GEAR;
printf("current state is: %s\n ", State_Machine[current_state].current_state_indication);
printf("light is : %s\n",State_Machine[current_state].light_status);
printf("Direction value status: %s\n",State_Machine[current_state].direction_valve_status);
}
if(Pilot_Lever==(RAISING || FALLING))
{
Check_Before_Landing();
}
}
// Function for Check Lowering Gear
void Lowering_Gear(void)
{
current_state=LOWERING_GEAR;
printf("Enter the input from pilot_lever and limit switch");
fflush(stdout);
scanf("%d %d",&Pilot_Lever, &Limit_Switch);
if(Pilot_Lever==RAISING && Limit_Switch==ON)
{
current_state=CHECKING_BEFORE_TAKE_OFF;
printf("current state is: %s\n ", State_Machine[current_state].current_state_indication);
printf("light is : %s\n",State_Machine[current_state].light_status);
}
if(Pilot_Lever==RAISING && Limit_Switch==OFF)
{
current_state=GEAR_DOWN;
printf("current state is: %s\n ", State_Machine[current_state].current_state_indication);
printf("light is : %s\n",State_Machine[current_state].light_status);
}
if(Limit_Switch==OFF)
{
Lowering_Gear();
}
}
static void(*state_table[])(void)={Gear_Down,Checking_Before_Take_Off,Raising_Gear,Check_Before_Landing,Lowering_Gear};
int main()
{
Initiate_State_Machine();
while(1)
{
state_table[current_state]();
}
}

#ifndef DATAS_H_
#define DATAS_H_
#include<stdio.h>
void Initiate_State_Machine();
void Gear_Down();
void Checking_Before_Take_Off();
void Raising_Gear();
void Check_Before_Landing();
void Lowering_Gear();
typedef enum State
{
GEAR_DOWN,
CHECKING_BEFORE_TAKE_OFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR
}state_type;
state_type current_state;
typedef enum Switch
{
ON,
OFF
}Switch_Status;
typedef enum pilotlever
{
RAISING,
FALLING
}pilot_lever;
typedef enum Hydraulic_Mechanism
{
WORKING,
NOT_WORKING
}Hydraulic_Mechanism;
typedef struct Stat
{
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",{0,0}},
{"Checking Before Take Off","Green","Down","Enabled",{0,0}},
{"Raising Gear","Red","Up","Enabled",{"Disabled","Enabled"}},
{"Gear Up","Off","Null","Disabled",{0,0}},
{"Checking Before Landing","Red","Down","Enabled",{0,0}},
{"Lowering Gear","Green","Down","Enabled",{"Disabled","Enabled"}}
};
/* -----input for changing the State----- */
static volatile Switch_Status Squat_Switch;
static volatile Switch_Status Limit_Switch;
static volatile pilot_lever Pilot_Lever;
static volatile Hydraulic_Mechanism hydraulic_mechanism;
#endif /* DATAS_H_ */
