Challenge

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

Aim: To implement the Code for controlling the retraction and extension of the Airplane’s landing gear using FSM.

In this project, controlling the retraction and extension of the Airplane’s landing gear is implemented using Finite State Machine (FSM). FSM is the most efficient algorithm which is a mathematical model of computation. A direction valve is controlled by the software to allow the hydraulic pressure to either raise or lower the landing gear.

Here we will achieve our 2 goals i.e.

1. Controlling the direction of the Valve.
2. Controlling the retraction and extension of the Airplane’s landing gear.

PROJECT CODE:

/* Implement the Code for controlling the retraction and extension of Airplane’s landing gear.
 * Created on: 01-Feb-2023	Author: RC*/

#include<stdio.h>

//Function Prototype Declaration
void Init_State_Machine();
void GearDown();
void CheckingBeforeTakeOff();
void RaisingGear();
void GearUp();
void CheckingBeforeLanding();
void LoweringGear();

//system state declaration
typedef enum{
	Gear_Down,
	Checking_Before_Take_Off,
	Raising_Gear,
	Gear_Up,
	Lowering_Gear,
	Checking_Before_Landing
}state_type;

//limit and squat switch state declaration ... open = 0 & close = 1
typedef enum{
	open,
	close
}switch_state;

//pilot's lever state declaration ... rising = 0; falling = 1 ; up = 2; down = 3
typedef enum{
	rising,
	falling,
	up,
	down
}lever_state;

//hydraulic mechanism state declaration ... on = 0 and off = 1
typedef enum{
	on,
	off
}hydraulic_state;

//inputs for changing state are declared here
static volatile lever_state lever_status;
static volatile switch_state squat_switch_status;
static volatile switch_state limit_switch_status;
static volatile hydraulic_state hydraulic_status;

state_type current_state;

//state table attribute declaration
typedef struct{
	state_type state_gear_signal;
	char *light;
	char *direction_valve;
	int delay;
	char *gas_pressurized_springs[2];
	char *hydraulic_control;
	char *current_state_indication;
}STATE_TABLE;

//state table formation
static STATE_TABLE state_machine[6]= {
		{Gear_Down,"Green","Down",0,{"NULL","NULL"},"Enabled","GEAR DOWN"},
		{Checking_Before_Take_Off,"Green","Down",3,{"NULL","NULL"},"Enabled","CHECKING BEFORE TAKE OFF"},
		{Raising_Gear,"RED","Up",0,{"Disabled","Enabled"},"Enabled","RAISING GEAR"},
		{Gear_Up,"off","NULL",0,{"NULL","NULL"},"Disabled","GEAR UP"},
		{Lowering_Gear,"Green","Down",0,{"Disabled","Enabled"},"Enabled","LOWERING GEAR"},
		{Checking_Before_Landing,"RED","Down",0,{"NULL","NULL"},"Enabled","CHECKING BEFORE LANDING"}

};


static void(*state_table[])(void)={GearDown,CheckingBeforeTakeOff,RaisingGear,GearUp,LoweringGear,CheckingBeforeLanding};

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


void Init_State_Machine()
{
	current_state = Gear_Down;
	printf("State Machine is initialized & Program is in Gear_Down State and Light is %s.\n",state_machine[current_state].light);
}

//Gear_Down State Function
void GearDown()
{
	current_state = Gear_Down; //initially the plane was either parked at bay or was landed.
	printf("\nEnter the status of Pilot Lever and Squat Switch: ");
	fflush(stdout);
	scanf("%d %d",&lever_status, &squat_switch_status);

	if(lever_status == rising && squat_switch_status == open) //Gear Raised ..checking for take off;
	{
		current_state = Checking_Before_Take_Off;
		printf("\nCurrent State : %s\n",state_machine[current_state].current_state_indication);
		printf("Light : %s\n",state_machine[current_state].light);
	}
	else
	{
		GearDown();
	}
}

//Checking_Before_Take_Off State Function
void CheckingBeforeTakeOff()
{
	current_state = Checking_Before_Take_Off;
	printf("\nEnter the status of Pilot Lever, Squat Switch and Hydraulic mechanism : ");
	fflush(stdout);
	scanf("%d %d %d",&lever_status, &squat_switch_status, &hydraulic_status);
	printf("\nDelay of %d seconds.\n",state_machine[current_state].delay);

	if(lever_status == falling || squat_switch_status == close) // Gear to be locked when Squat switch is close
	{
		 GearDown();
	}

	else if(lever_status == up && squat_switch_status == open && hydraulic_status == on)
	{
		current_state = Raising_Gear;
		printf("\nCurrent State : %s\n",state_machine[current_state].current_state_indication);
		printf("Light : %s\n",state_machine[current_state].light);
		printf("Direction Valve : %s\n",state_machine[current_state].direction_valve);
	}

	else if(lever_status == up && squat_switch_status == open && hydraulic_status == off)
	{
		current_state = Raising_Gear;
		printf("\nCurrent State : %s\n",state_machine[current_state].current_state_indication);
		printf("Light : %s\n",state_machine[current_state].light);
		printf("Direction Valve : %s\n",state_machine[current_state].direction_valve);
		printf("Gas_pressurized_springs Status : %s\n",state_machine[current_state].gas_pressurized_springs[hydraulic_status]);
	}
	else
	{
		CheckingBeforeTakeOff();
	}
}

//Raising_Gear state Function
void RaisingGear()
{
	current_state = Raising_Gear;
	printf("\nEnter the status of Pilot Lever,Limit Switch : ");
	fflush(stdout);
	scanf("%d %d",&lever_status, &limit_switch_status);

	if(lever_status == falling  && limit_switch_status == open) // Gear down ..i.e. landing could take place..so shift to check for landing
	{
		current_state = Checking_Before_Landing;
		printf("\nCurrent State : %s\n",state_machine[current_state].current_state_indication);
		printf("Light : %s\n",state_machine[current_state].light);
		printf("Control for Landing Gear : %s\n",state_machine[current_state].hydraulic_control);
	}

	else if(lever_status == up && limit_switch_status == close ) // Gear Up
	{
		current_state = Gear_Up;
		printf("\nCurrent State : %s\n",state_machine[current_state].current_state_indication);
		printf("Light : %s\n",state_machine[current_state].light);
		printf("Control for Landing Gear : %s\n",state_machine[current_state].hydraulic_control);
	}
	else
	{
		RaisingGear();
	}
}

//Gear_Up state Function
void GearUp()
{
	current_state = Gear_Up;
	printf("\nEnter the status of Pilot Lever: ");
	fflush(stdout);
	scanf("%d",&lever_status);
	if(lever_status == falling )
	{
		current_state = Checking_Before_Landing;
		printf("\nCurrent State : %s\n",state_machine[current_state].current_state_indication);
		printf("Light : %s\n",state_machine[current_state].light);
		printf("Control for Landing Gear : %s\n",state_machine[current_state].hydraulic_control);
	}
	else
	{
		GearUp();
	}
}

//Checking_Before_Landing state Function
void CheckingBeforeLanding()
{
	current_state = Checking_Before_Landing;
	printf("\nEnter the status of Pilot Lever and Hydraulic mechanism : ");
	fflush(stdout);
	scanf("%d %d",&lever_status,&hydraulic_status);
	if(lever_status == down && hydraulic_status == on)
	{
		current_state = Lowering_Gear;
		printf("\nCurrent State : %s\n",state_machine[current_state].current_state_indication);
		printf("Light : %s\n",state_machine[current_state].light);
		printf("Direction Valve : %s\n",state_machine[current_state].direction_valve);
	}
	else if (lever_status == down  && hydraulic_status == off)
	{
		current_state = Lowering_Gear;
		printf("\nCurrent State : %s\n",state_machine[current_state].current_state_indication);
		printf("Light : %s\n",state_machine[current_state].light);
		printf("Direction Valve : %s\n",state_machine[current_state].direction_valve);
		printf("Gas_pressurized_springs Status : %s\n",state_machine[current_state].gas_pressurized_springs[hydraulic_status]);
	}
	if(lever_status == (up || rising || falling))
	{
		CheckingBeforeLanding();
	}
}

//Lowering_Gear state Function
void LoweringGear()
{
	current_state = Lowering_Gear;
	printf("\nEnter the status of Pilot Lever and Limit Switch : ");
	fflush(stdout);
	scanf("%d %d",&lever_status, &limit_switch_status);
	if(lever_status == rising && limit_switch_status == close)
	{
		current_state = Checking_Before_Take_Off;
		printf("\nCurrent State : %s\n",state_machine[current_state].current_state_indication);
		printf("Light : %s\n",state_machine[current_state].light);
	}
	else if(limit_switch_status == open && lever_status == down)
	{
		current_state = Gear_Down;
		printf("\nCurrent State : %s\n",state_machine[current_state].current_state_indication);
		printf("Light : %s\n",state_machine[current_state].light);
		printf("Control for Landing Gear : %s\n",state_machine[current_state].hydraulic_control);
	}
	else
	{
		LoweringGear();
	}
}

RESULT-

The Finite State Machine algorithm is used to achieve control over the retraction and extension of the Airplane’s landing gear using the switch and gear states of the Airplane.

State Machine is initialized & Program is in Gear_Down State and Light is Green.

Enter the status of Pilot Lever and Squat Switch: 0 0

Current State : CHECKING BEFORE TAKE OFF
Light : Green

Enter the status of Pilot Lever, Squat Switch and Hydraulic mechanism : 1 0 1

Delay of 3 seconds.

Enter the status of Pilot Lever and Squat Switch: 0 0

Current State : CHECKING BEFORE TAKE OFF
Light : Green

Enter the status of Pilot Lever, Squat Switch and Hydraulic mechanism : 2 0 1

Delay of 3 seconds.

Current State : RAISING GEAR
Light : RED
Direction Valve : Up
Gas_pressurized_springs Status : Enabled

Enter the status of Pilot Lever,Limit Switch : 2 1

Current State : GEAR UP
Light : off
Control for Landing Gear : Disabled

Enter the status of Pilot Lever: 1

Current State : CHECKING BEFORE LANDING
Light : RED
Control for Landing Gear : Enabled

Enter the status of Pilot Lever and Hydraulic mechanism : 3 1

Current State : LOWERING GEAR
Light : Green
Direction Valve : Down
Gas_pressurized_springs Status : Enabled

Enter the status of Pilot Lever and Limit Switch : 

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.