Challenge

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

Project-2:

CODE:

#include<stdio.h>
#include<stdlib.h>

void Initial_status();
void Gear_Down();
void Gear_Up();
void Raise_Gear();
void Lower_Gear();
void Check_b4_land();
void Check_b4_takeOff();


static void (*state_table[])(void) = {Gear_Down,Check_b4_takeOff,Raise_Gear,Gear_Up,Check_b4_land,Lower_Gear};


typedef enum State
{
	GEAR_DOWN,  CHECK_BEFORE_TAKEOFF,	RAISE_GEAR, 	GEAR_UP, 	CHECK_BEFORE_LAND,	LOWER_GEAR

}State_Type;
State_Type current_state;


typedef enum Switch
{
	Open,	Close

}Switch_Status;
static Switch_Status squat_switch,limit_switch;


typedef enum Pilot_Lever
{
	Rising,  	Falling,	UP,  	DOWN

}Pilot_Lever_Status;
static Pilot_Lever_Status pilot_lever;


typedef enum Hydrulic_Mechanism
{
	Working,	Malfunctioning

}Hydrulic_Mechanism_Status;
static Hydrulic_Mechanism_Status hydraulic_mechanism;


typedef struct
{
	char* crrnt_stts;
	char* lt_stts;
	char* valv_dir;
	char* hyd_pmp_stts;
	char* Prss_sprng[2];

}State_Table;


static State_Table State_Machine[] =
{
		{"Gear_Down","Green","Down","Enabled",{NULL,NULL}},
		{"Checking before take off","Green","Down","Enabled",{NULL,NULL}},
		{"Raising Gear","Red","Down","Enabled",{"Disabled","Enabled"}},
		{"Gear_Up","Green","Down","Enabled",{NULL,NULL}},
		{"Checking before Landing","Green","Down","Enabled",{NULL,NULL}},
		{"Lowering Gear","Green","Down","Enabled",{"Disabled","Enabled"}}
};

//========================================================================================================================
//====================================MAIN FUNCTION ============================================================================
//==========================================================================================================================

int main()
{
	Initial_status();
	while(1)
	{
		state_table[current_state]();
	}
	return 0;
}


//==================================================================================================================
//==========================================================INITIAL STATUS==========================================
//==================================================================================================================
void Initial_status()
{
	current_state = GEAR_DOWN;
	printf("\nCurrent State: %s\n",State_Machine[current_state].crrnt_stts);
	printf("INITIAL STATE IS GEAR_DOWN\n\n");
}


//==================================================================================================================
//========================================================GEAR DOWN==============================================
//==================================================================================================================
void Gear_Down()
{
	current_state = GEAR_DOWN;
	printf("\nCurrent State: %s\n",State_Machine[current_state].crrnt_stts);
	printf("\nThe Pilot Lever Position: ");
	printf("\n0: Raising\n1:Falling\n2:UP\n3:DOWN\tEnter: ");
	//fflush(stdout);
	scanf("%d",&pilot_lever);
	printf("\nSquat switch Status: ");
	printf("\n0: Open\n1:Close\tEnter: ");
	//fflush(stdout);
	scanf("%d",&squat_switch);
	if((pilot_lever == Rising)&&(squat_switch == Open))
	{
		current_state = CHECK_BEFORE_TAKEOFF;
		printf("\nCurrent State: %s\n",State_Machine[current_state].crrnt_stts);
		printf("Light Status: %s\n",State_Machine[current_state].lt_stts);
	}
	else
	{
		Gear_Down();
	}

}

//==================================================================================================================
//===============================================GEAR UP=========================================
//==================================================================================================================
void Gear_Up()
{
	current_state = GEAR_UP;
	printf("\nCurrent State: %s\n",State_Machine[current_state].crrnt_stts);
	printf("\nThe Pilot Lever Position: ");
	printf("\n0: Raising\n1:Falling\n2:UP\n3:DOWN\tEnter: ");
	scanf("%d",&pilot_lever);
	if(pilot_lever == Falling)
	{
		current_state = CHECK_BEFORE_LAND;
		printf("\nCurrent State: %s\n",State_Machine[current_state].crrnt_stts);
		printf("Light Status: %s\n",State_Machine[current_state].lt_stts);
		printf("Valve Direction Status: %s\n",State_Machine[current_state].valv_dir);
	}
	else
	{
		Gear_Up();
	}
}


//==================================================================================================================
//==================================================RAISE GEAR======================================================
//==================================================================================================================
void Raise_Gear()
{
	current_state = RAISE_GEAR;
	printf("\nCurrent State: %s\n",State_Machine[current_state].crrnt_stts);
	printf("\nThe Pilot Lever Position: ");
	printf("\n0: Raising\n1:Falling\n2:UP\n3:DOWN\tEnter: ");
	scanf("%d",&pilot_lever);
	printf("\nLimit Switch Status: ");
	printf("\n0: Open\n1:Close\tEnter: ");
	scanf("%d",&limit_switch);
	if((pilot_lever == Falling)&&(limit_switch == Open))
	{
		current_state = CHECK_BEFORE_LAND;
		printf("\nCurrent State: %s\n",State_Machine[current_state].crrnt_stts);
		printf("Light Status: %s\n",State_Machine[current_state].lt_stts);
		printf("Hydraulic Control Status: %s\n",State_Machine[current_state].hyd_pmp_stts);
	}
	else if((pilot_lever == UP)&&(limit_switch == Close))
	{
		current_state = GEAR_UP;
		printf("\nCurrent State: %s\n",State_Machine[current_state].crrnt_stts);
		printf("Light Status: %s\n",State_Machine[current_state].lt_stts);
		printf("Hydraulic Control Status: %s\n",State_Machine[current_state].hyd_pmp_stts);
	}
	else
	{
		Raise_Gear();
	}
}


//==================================================================================================================
//===================================================LOWE%R GEAR=========================================================
//==================================================================================================================
void Lower_Gear()
{
	current_state = LOWER_GEAR;
	printf("\nCurrent State: %s\n",State_Machine[current_state].crrnt_stts);
	printf("\nThe Pilot Lever Position: ");
	printf("\n0: Raising\n1:Falling\n2:UP\n3:DOWN\tEnter: ");
	scanf("%d",&pilot_lever);
	printf("\nLimit Switch Status: ");
	printf("\n0: Open\n1:Close\tEnter: ");
    scanf("%d",&limit_switch);
	if(pilot_lever == Rising)
	{
		current_state = CHECK_BEFORE_LAND;
		printf("\nCurrent State: %s\n",State_Machine[current_state].crrnt_stts);
		printf("Light Status: %s\n",State_Machine[current_state].lt_stts);
	}
	else if(limit_switch == Open)
	{
		current_state = GEAR_DOWN;
		printf("\nCurrent State: %s\n",State_Machine[current_state].crrnt_stts);
		printf("Light Status: %s\n",State_Machine[current_state].lt_stts);
		printf("Hydraulic Control Status: %s\n",State_Machine[current_state].hyd_pmp_stts);
	}

	else
	{
		Lower_Gear();
	}
}


//==================================================================================================================
//===========-==============================CHECKING B4 LANDING========================================================
//==================================================================================================================
void Check_b4_land()
{
	current_state = CHECK_BEFORE_LAND;
	printf("\nCurrent State: %s\n",State_Machine[current_state].crrnt_stts);
	printf("\nThe Pilot Lever Position: ");
				printf("\n0: Raising\n1:Falling\n2:UP\n3:DOWN\tEnter: ");
	//fflush(stdout);
	scanf("%d",&pilot_lever);
	printf("\nHydraulic Mechanism Status: ");
	printf("\n0: Working\n1:Malfunctioning\tEnter: ");
	//fflush(stdout);
	scanf("%d",&hydraulic_mechanism);
	if((pilot_lever == DOWN))
	{
		current_state = LOWER_GEAR;
		printf("\nCurrent State: %s\n",State_Machine[current_state].crrnt_stts);
		printf("Light Status: %s\n",State_Machine[current_state].lt_stts);

	}
	else
	{
		Check_b4_land();
	}
	if(hydraulic_mechanism==Malfunctioning)
	{
		printf("Pressurised Spring System Status: %s\n",State_Machine[current_state].Prss_sprng[hydraulic_mechanism]);
	}
}
//========================================================================================================
//================================================CHECKING B4 TAKEOFF===========================================
//==================================================================================================================
void Check_b4_takeOff()
{
	current_state = CHECK_BEFORE_TAKEOFF;
	printf("\nCurrent State: %s\n",State_Machine[current_state].crrnt_stts);
	printf("\nThe Pilot Lever Position: ");
	printf("\n0: Raising\n1:Falling\n2:UP\n3:DOWN\tEnter: ");
	//fflush(stdout);
	scanf("%d",&pilot_lever);
	printf("\nHydraulic Mechanism Status: ");
	printf("\n0: Working\n1:Malfunctioning\tEnter: ");
	//fflush(stdout);
	scanf("%d",&hydraulic_mechanism);
	printf("\nSquat switch Status: ");
	printf("\n0: Open\n1:Close\tEnter: ");
	//fflush(stdout);
	scanf("%d",&squat_switch);
	if((pilot_lever == Falling )||(squat_switch == Close))
	{
	    Gear_Down();
	}
	else if((pilot_lever == UP)&&(hydraulic_mechanism == Working)&&(squat_switch == Open))
	{
	    current_state = RAISE_GEAR;
	    printf("\nCurrent State: %s\n",State_Machine[current_state].crrnt_stts);
	    printf("Light Status: %s\n",State_Machine[current_state].lt_stts);
	    printf("Valve Direction Status: %s\n",State_Machine[current_state].valv_dir);
	    printf("Pressurised Spring System Status: %s\n",State_Machine[current_state].Prss_sprng[hydraulic_mechanism]);
	}
}

OUTPUT:

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.