Challenge

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

#include 
void Initiate_State_Machine();
void GearDown();
void CheckingBeforeTakeOFF();
void RaisingGear();
void GearUp();
void CheckBeforeLanding();
void LoweringGear();
static void(*statetable[])(void)={GearDown, CheckingBeforeTakeOFF, RaisingGear, GearUp, CheckBeforeLanding, LoweringGear};
typedef enum State
{
	GEAR_DOWN,
	CHECKING_BEFORE_TAKEOFF,
	RAISING_GEAR,
	GEAR_UP,                                 //creating state types
	CHECK_BEFORE_LANDING,
	LOWERING_GEAR,
}State_Type;
typedef enum Switch
{
	on,        //(0,1)
	off
}Switch_status;
typedef enum pilot_lever
{
	up, //(0,1)
	down
}pilot_lever;
typedef enum hydraulic_mechanism
{
	working,
	not_working //(0,1)
}hydraulic_mechanism;

static volatile Switch_status squat_switch;
static volatile Switch_status limit_switch;    //declaring names in main memory
static volatile  pilot_lever pl;
static volatile hydraulic_mechanism hm;

State_Type current_state;

typedef struct
{
	char* current_state_indication;
	char* light;
    char* landing_gear_hydraulic_control;                //creating state table with 4 structures
	char* Gas_Pressurized_Spring_system_status;

}State_Table;

static State_Table State_Machine[6]={{"GearDown","Green","Enabled","NULL"},
		                             {"CheckingBeforeTakeOFF","Green","Enabled","NULL"},
	                                 {"RaisingGear","Red","Enabled","NULL"},
	                                 {"GearUp","Off","Disabled","NULL"},
		                             {"CheckBeforeLanding","Red","Enabled","NULL"},
	                                 {"LoweringGear","Green","Enabled","enable"}
};
int main()
{
	Initiate_State_Machine();
	while(1)
	{
		statetable[current_state]();
	}
}

void GearDown(void)
{
	current_state=GEAR_DOWN;
	printf("Enter the status of pilot's lever and squat switch\n");
	fflush(stdout);
	scanf("%d %d",&pl,&squat_switch);    //checking squat switch and lever,if both are 0 0 then next function will be performed
	if(pl==up && squat_switch==on)
	{
		current_state=CHECKING_BEFORE_TAKEOFF;
		printf("pilot lever 'up',squat switch 'on'\n");
		printf("current state:%s\n",State_Machine[current_state].current_state_indication);
		printf("Light is :%s\n", State_Machine[current_state].light);
	}
	else
	{
		GearDown();
	}
}

//CheckingBeforeTakeOFF
void CheckingBeforeTakeOFF(void)
{
	current_state=CHECKING_BEFORE_TAKEOFF;
	printf("Enter the status of pilot's lever and squat switch and hydraulic mechanism:\n");
	fflush(stdout);
	scanf("%d %d",&pl,&squat_switch);
	if(pl==down&&squat_switch==off)
	{
		GearDown();
	}
	else if(pl==up && squat_switch==on )
	{
		current_state=RAISING_GEAR;
		printf("pilot lever 'up',squat switch 'on'\n");
		printf("Current State:%s\n",State_Machine[current_state].current_state_indication);
		printf("Light is:%s\n",State_Machine[current_state].light);

	}

}
//RaisingGear
void RaisingGear(void)
{
	current_state=RAISING_GEAR;
	printf("Enter the status of pilot's lever and limit switch:\n");
	fflush(stdout);
	scanf("%d %d",&pl,&limit_switch);

	if(pl==up && limit_switch==off)
	{
	  	current_state=GEAR_UP;
	  	printf("pilot lever 'up',limit switch off'\n");
		printf("Current State:%s\n",State_Machine[current_state].current_state_indication);
		printf("Light is:%s\n",State_Machine[current_state].light);
		printf("Landing Gear Hydraulic Control:%s\n",State_Machine[current_state].landing_gear_hydraulic_control);
	}
	else
	{
		RaisingGear();
	}
	}
//GearUp
void GearUp(void)
{
	current_state=GEAR_UP;
	printf("Enter the status of pilot's lever:\n");
	fflush(stdout);
	scanf("%d",&pl);
	if(pl==down)
	{
	current_state=CHECK_BEFORE_LANDING;
	printf("pilot lever 'down'\n");
	printf("Current State:%s\n",State_Machine[current_state].current_state_indication);
	printf("Light is:%s\n",State_Machine[current_state].light);
	printf("Landing Gear Hydraulic Control:%s\n",State_Machine[current_state].landing_gear_hydraulic_control);
}
	else
	{
		GearUp();
	}

}
//CheckBeforeLanding
void CheckBeforeLanding(void)
{
	current_state=CHECK_BEFORE_LANDING;
	printf("Enter the status of pilot's lever and hydraulic mechanism status:\n");
	fflush(stdout);
	scanf("%d %d",&pl,&hm);
	if(pl==down && hm==working)
	{
		current_state=LOWERING_GEAR;
		printf("pilot lever 'down',hydraulics 'working' \n");
		printf("Current State:%s\n",State_Machine[current_state].current_state_indication);
		printf("Light is:%s\n",State_Machine[current_state].light);
		printf("Status of gas pressurised Spring System:%s,\n",State_Machine[current_state].Gas_Pressurized_Spring_system_status);
	}

	else if(pl==(up))
	 {
		CheckBeforeLanding();
	 }
}
//LoweringGear.
void LoweringGear(void)
{
	current_state=LOWERING_GEAR;
		printf("Enter the status of pilot's lever and limit switch:\n");
		fflush(stdout);
		scanf("%d %d",&pl,&limit_switch);

		 if(pl==up && limit_switch==off)
		{
			current_state=GEAR_DOWN;
			printf("pilot lever 'up',limit switch 'off'\n");
			printf("Current State:%s\n",State_Machine[current_state].current_state_indication);
			printf("Light is:%s\n",State_Machine[current_state].light);
			printf("Landing Gear Hydraulic Control:%s\n",State_Machine[current_state].landing_gear_hydraulic_control);
		}
		if(limit_switch==off)
		{
			LoweringGear();
		}
}
void Initiate_State_Machine()
{
	current_state==GEAR_DOWN;
	printf(" Current state is in GearDown & Light is:%s\n",State_Machine[current_state].light);
}





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.