Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include <stdio.h>
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_TKEF,
RSING_GR,
GR_UP,
CHK_BFR_LND,
LWRNG_GR
}State_Type;
typedef enum Switch
{
open,
close
}Switch_Status;
typedef enum Pilot_Lever
{
Rising,
falling,
up,
Down
}Pilot_Lever_Status;
typedef enum Hydraulic_Mechanisum
{
Working,
Malfunctioning
}Hydraulic_Mechanisum_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_Mechanisum_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_pressurised_spring_status[2];
}State_Table;
static State_Table State_Machine[6] = {
{"gear down","green","down","enables",{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"}}
};
static void (*state_table[])(void) = {Gear_Down,Checking_Before_Takeoff,Raising_Gear,Gear_Up,Checking_Before_Landing,Lowering_Gear};
int main()
{
Init_state_Machine();
while(1)
{
state_table[Current_State]();
}
}
void Checking_Before_Takeoff()
{
Current_State = CHK_BFR_TKEF;
printf("\n Enter the pilot's lever position & squat switch status status hydraulic machanism:");
fflush(stdout);
scanf("%d %d %d",&pilot_lever,&squat_switch,& hydraulic_mechanism);
if(pilot_lever == falling && squat_switch == close)
{
Gear_Down();
}
else if(pilot_lever ==up && squat_switch == open && hydraulic_mechanism == Working)
{
Current_State = RSING_GR;
printf("\ncurrent status :%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 ==up && squat_switch == open && hydraulic_mechanism == Malfunctioning)
{
Current_State = RSING_GR;
printf("\ncurrent status :%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_pressurised_spring_status[hydraulic_mechanism]);
}
}
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);
}
void Gear_Down()
{
Current_State = GR_DWN;
printf("\n Enter 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)
{
Current_State = CHK_BFR_TKEF;
printf("\n current state : %s\n",State_Machine[Current_State].current_state_indication);
printf("Light status : %s\n",State_Machine[Current_State].light_status);
}
else
{
Gear_Down();
}
}
void Raising_Gear()
{
Current_State = RSING_GR;
printf("\n Enter the limit switch status,pilot's lever status:");
fflush(stdout);
scanf("%d %d",&limit_switch,&pilot_lever);
printf("\n Wait for 2 sec\n");
if(pilot_lever == falling && limit_switch == open)
{
Current_State= CHK_BFR_LND;
printf("\ncurrent status :%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 status :%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();
}
}
void Gear_Up()
{
Current_State = GR_UP;
printf("\n Enter the pilot's lever position:");
fflush(stdout);
scanf("%d",&pilot_lever);
if(pilot_lever == falling)
{
Current_State = CHK_BFR_LND;
printf("\ncurrent status :%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();
}
}
void Checking_Before_Landing()
{
Current_State= CHK_BFR_LND;
printf("\n Enter the pilot's lever position & status hydraulic machanism:");
fflush(stdout);
scanf("%d %d",&pilot_lever,&hydraulic_mechanism);
if(pilot_lever == Down && hydraulic_mechanism == Working)
{
Current_State = LWRNG_GR;
printf("\ncurrent status :%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 status :%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_pressurised_spring_status[hydraulic_mechanism]);
}
if(pilot_lever ==(up || Rising || falling))
{
Checking_Before_Landing();
}
}
void Lowering_Gear()
{
Current_State = LWRNG_GR;
printf("\n Enter the pilot's lever position & status hydraulic machanism:");
fflush(stdout);
scanf("%d %d",&limit_switch,&pilot_lever);
if(limit_switch == close && pilot_lever == Rising)
{
Current_State = CHK_BFR_TKEF;
printf("\ncurrent status :%s\n", State_Machine[Current_State].current_state_indication);
printf("Light status :%s\n", State_Machine[Current_State].light_status);
}
if(limit_switch == open && pilot_lever == Down)
{
Current_State = GR_DWN;
printf("\ncurrent status :%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
{
Lowering_Gear();
}
}