Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Offline Batch
Code:
#include <stdio.h>
typedef enum State
{
GEAR_DWN,
CHK_BFR_TKOF,
RISING_GR,
GEAR_UP,
CHK_BFR_LND,
LOWRNG_GR
}State_Type;
typedef enum Switch
{
Open,
Close
}Switch_Status;
typedef enum Pilot_Lever
{
Rising,
Falling,
Up,
Down
}Pilot_Lever_Status;
typedef enum Hydraulic_Mechanism
{
Working,
Malfunctioning
}Hydraulic_Mechanism_Status;
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;
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"}}
};
void Lowering_Gear();
void Checking_Before_Takeoff();
void Checking_Before_Landing();
void Raising_Gear();
void Raising_Gear();
void Gear_Down(void);
void Init_State_Machine();
void Gear_Up();
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]();
}
}
//function to check the lowering gear state
void Lowering_Gear()
{
Current_State = LOWRNG_GR;
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Indication);
printf("\nEnter the Limit Switch Status, Pilot's Lever Position");
fflush(stdout);
scanf("%d%d",&Pilot_Lever, &Limit_Switch);
if(Limit_Switch == Close && Pilot_Lever == Rising )
{
Current_State=CHK_BFR_TKOF;
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Indication);
printf("\nLight Status: %s",State_Machine[Current_State].light_status);
}
if(Limit_Switch == Open && Pilot_Lever ==Down)
{
Current_State = GEAR_DWN;
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Indication);
printf("\nLight Status: %s",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();
}
}
//function to check before take off
void Checking_Before_Takeoff()
{
Current_State = CHK_BFR_TKOF;
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Indication);
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);
if(Pilot_Lever == Falling ||Squat_Switch == Close)
{
Gear_Down();
}
else if(Pilot_Lever==Up && Squat_Switch==Open && Hydraulic_Mechanism==Working)
{
Current_State=RISING_GR;
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Indication);
printf("\nLight Status: %s",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)
{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]);
}
}
//function to check the state before landing
void Checking_Before_Landing()
{
Current_State = CHK_BFR_LND;
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Indication);
printf("\nEnter the Pilot's Lever Position and HydraulicMechanism Status:");
fflush(stdout);
scanf("%d %d",&Pilot_Lever, &Hydraulic_Mechanism);
if(Pilot_Lever == Down && Hydraulic_Mechanism == Working)
{
Current_State = LOWRNG_GR;
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Indication);
printf("\nLight State: %s",State_Machine[Current_State].light_status);
printf("Direction Valve Status:%s\n\n",State_Machine[Current_State].direction_valve_status);
}
else if(Pilot_Lever== Down && Hydraulic_Mechanism == Malfunctioning)
{
Current_State = LOWRNG_GR;
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Indication);
printf("\nLight Status: %s",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();
}
}
//function to check the raising gear state
void Raising_Gear()
{
Current_State = RISING_GR;
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Indication);
printf("\nEnter the Limit Switch Status,Pilots Lever Status:");
fflush(stdout);
scanf("%d %d",&Limit_Switch,&Pilot_Lever);
if(Pilot_Lever == Falling && Limit_Switch == Open)
{
Current_State=CHK_BFR_LND;
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Indication);
printf("\nLight State: %s",State_Machine[Current_State].light_status);
printf("Hydraulic Control for Landing Gear State:%s\n\n",State_Machine[Current_State].hydraulic_pump_control_status);
}
else if(Limit_Switch == Close && Pilot_Lever == Up)
{
Current_State=GEAR_UP;
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Indication);
printf("\nLight State: %s",State_Machine[Current_State].light_status);
printf("\nHydraulic Control for landing Gear Status:%s\n",State_Machine[Current_State].hydraulic_pump_control_status);
}
else
{
Raising_Gear();
}
}
//function to check the gear down state
void Gear_Down(void)
{
Current_State = GEAR_DWN;
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Indication);
printf("\nEnter the Pilot's Lever Position, Squat Switch Status ");
fflush(stdout);
scanf("%d %d",&Pilot_Lever, &Squat_Switch);
if(Pilot_Lever == Rising && Squat_Switch == Open)
{
Current_State=CHK_BFR_TKOF;
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Indication);
printf("\nLight State: %s",State_Machine[Current_State].light_status);
}
else
{
Gear_Down();
}
}
void Init_State_Machine()
{
Current_State = GEAR_DWN;
printf("\nState Machine is initialized & program is in Gear_Down_State & Light_Status:%s\n",State_Machine[Current_State].light_status);
}
//function to check the gear up state
void Gear_Up()
{
Current_State = GEAR_UP;
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Indication);
printf("\nEnter the Pilot's lever Position ");
fflush(stdout);
scanf("%d",&Pilot_Lever);
if(Pilot_Lever == Falling)
{
Current_State=CHK_BFR_LND;
printf("\nCurrent State:%s\n",State_Machine[Current_State].Current_State_Indication);
printf("\nLight Status: %s",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();
}
}
Output:
