Challenge

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

Objective: In this project, controlling the retraction and extension of Airplane’s landing gear can be implemented using Finite State Machine (FSM).

Enum table:

State Type

Switch Status

Pilot Lever Status

Hydraulic Mechanism Status

SQUAT SWITCH

LIMIT SWITCH

0 - GR_DWN

1 - CHK_BFR_TKFF

2 - RSING_GR

3 - GR_UP

4 - CHK_BFR_LND

5 - LWRNG_GR

0 - Open

1 - Close

0 - Open

1 - Close

0 - Rising

1 - Falling

2 - UP

3 - DOWN

0 - Working

1 - Malfunctioning

State Table:

Current state

Input

Next state

Output

Pilot Lever

Squat switch

Limit switch

Hydraulic mechanism

Light status

Direction valve status

Hydraulic pump status

Gear Down

0

0

Checking Before Takeoff

Green

Down

Enabled

Checking Before Takeoff

2

0

0

Rising Gear

Red

Up

Enabled

Checking Before Takeoff

1

1

Gear Down

Green

Down

Enabled

Checking Before Takeoff

2

0

1

Rising Gear

Red

Up

Enabled

Rising Gear

2

1

Gear up

Off

Null

Disabled

Rising Gear

1

0

Checking Before Landing

Red

Down

Enabled

Gear up

1

Checking Before Landing

Red

Down

Enabled

Checking Before Landing

3

0

Lowering Gear

Green

Down

Enabled

Checking Before Landing

3

1

Lowering Gear

Green

Down

Enabled

Lowering Gear

0

1

Checking Before Takeoff

Green

Down

Enabled

Lowering Gear

3

0

Gear Down

Green

Down

Enabled

Code:-

main.c:

#include <stdio.h>
#include "states.h"

static void (*state_table_n[])(void) = {Gear_Down, Checking_Before_Takeoff, Rising_Gear, Gear_Up, Checking_Before_Landing, Lowering_Gear};

int main()
{
	Init_State_Machine();
	while(1)
	{
		state_table_n[current_state]();
	}
}

states.h:

#ifndef STATES_H_
#define STATES_H_

void Init_State_Machine();
void Gear_Down();
void Checking_Before_Takeoff();
void Rising_Gear();
void Gear_Up();
void Checking_Before_Landing();
void Lowering_Gear();

typedef enum state {GR_DWN, CHK_BFR_TKFF, 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_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;
}state_table;

static state_table state_machine[6] = {
		{"Gear Down", "Green", "Down", "Enabled"},
		{"Checking before take OFF", "Green", "Down", "Enabled"},
		{"Rising Gear", "Red", "Up", "Enabled"},
		{"Gear Up", "Off", "Null", "Disabled"},
		{"Checking before landing", "Red", "Down", "Enabled"},
		{"Lowering Gear", "Green", "Down", "Enabled"}
};


#endif /* STATES_H_ */

Init_State_Machine.c:

#include <stdio.h>
#include "states.h"

void Init_State_Machine()
{
	//System initialized and it is in State A
	current_state = GR_DWN;
	printf("\n\n State machine is initialized and program is in Gear down state and light status: %s\n", state_machine[current_state].light_status);
}

Gear_Down.c:

#include <stdio.h>
#include "states.h"

void Gear_Down()
{
	current_state = GR_DWN;
	printf("\n\n Current state: %s", state_machine[current_state].current_state_indication);

	printf("\n 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_TKFF;
		printf("\n Current state: %s", state_machine[current_state].current_state_indication);
		printf("\n Light status: %s", state_machine[current_state].light_status);
		printf("\n Direction valve status: %s", state_machine[current_state].direction_valve_status);
		printf("\n Hydraulic pump status: %s", state_machine[current_state].hydraulic_pump_control_status);
	}
	else
		Gear_Down();
}

Checking_Before_Takeoff.c:

#include <stdio.h>
#include "states.h"

void Checking_Before_Takeoff()
{
	current_state = CHK_BFR_TKFF;
	printf("\n\n Current state: %s", state_machine[current_state].current_state_indication);

	printf("\n Pilot's lever position, squat switch status and Hydraulic mechanism status: ");
	fflush(stdout);
	scanf("%d %d %d", &pilot_lever, &squat_switch, &hydraulic_mechanism);
	printf("\n Wait for 2 secs");

	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("\n Current state: %s", state_machine[current_state].current_state_indication);
		printf("\n Light status: %s", state_machine[current_state].light_status);
		printf("\n Direction valve status: %s", state_machine[current_state].direction_valve_status);
		printf("\n Hydraulic pump status: %s", state_machine[current_state].hydraulic_pump_control_status);
	}
	else if(pilot_lever == up && squat_switch == open && hydraulic_mechanism == malfunctioning)
	{
		current_state = RSING_GR;
		printf("\n Current state: %s", state_machine[current_state].current_state_indication);
		printf("\n Light status: %s", state_machine[current_state].light_status);
		printf("\n Direction valve status: %s", state_machine[current_state].direction_valve_status);
		printf("\n Hydraulic pump status: %s", state_machine[current_state].hydraulic_pump_control_status);
	}
}

Rising_Gear.c:

#include <stdio.h>
#include "states.h"

void Rising_Gear()
{
	current_state = RSING_GR;
	printf("\n\n Current state: %s", state_machine[current_state].current_state_indication);

	printf("\n Pilot's lever position and limit switch status: ");
	fflush(stdout);
	scanf("%d %d", &pilot_lever, &limit_switch);
	printf("\n Wait for 2 secs");

	if(pilot_lever == falling && limit_switch == open)
	{
		current_state = CHK_BFR_LND;
		printf("\n Current state: %s", state_machine[current_state].current_state_indication);
		printf("\n Light status: %s", state_machine[current_state].light_status);
		printf("\n Direction valve status: %s", state_machine[current_state].direction_valve_status);
		printf("\n Hydraulic pump status: %s", state_machine[current_state].hydraulic_pump_control_status);
	}

	else if(pilot_lever == up && limit_switch == close)
	{
		current_state = GR_UP;
		printf("\n Current state: %s", state_machine[current_state].current_state_indication);
		printf("\n Light status: %s", state_machine[current_state].light_status);
		printf("\n Direction valve status: %s", state_machine[current_state].direction_valve_status);
		printf("\n Hydraulic pump status: %s", state_machine[current_state].hydraulic_pump_control_status);
	}

	else
		Rising_Gear();
}

Gear_Up.c:

#include <stdio.h>
#include "states.h"

void Gear_Up()
{
	current_state = GR_UP;
	printf("\n\n Current state: %s", state_machine[current_state].current_state_indication);

	printf("\n Pilot's lever position: ");
	fflush(stdout);
	scanf("%d", &pilot_lever);

	if(pilot_lever == falling)
	{
		current_state = CHK_BFR_LND;
		printf("\n Current state: %s", state_machine[current_state].current_state_indication);
		printf("\n Light status: %s", state_machine[current_state].light_status);
		printf("\n Direction valve status: %s", state_machine[current_state].direction_valve_status);
		printf("\n Hydraulic pump status: %s", state_machine[current_state].hydraulic_pump_control_status);
	}

	else
		Rising_Gear();
}

Checking_Before_Landing.c:

#include <stdio.h>
#include "states.h"

void Checking_Before_Landing()
{
	current_state = CHK_BFR_LND;
	printf("\n\n Current state: %s", state_machine[current_state].current_state_indication);

	printf("\n Pilot's lever position and Hydraulic mechanism status: ");
	fflush(stdout);
	scanf("%d %d", &pilot_lever, &hydraulic_mechanism);

	if(pilot_lever == down && hydraulic_mechanism == working)
	{
		current_state = LWRNG_GR;
		printf("\n Current state: %s", state_machine[current_state].current_state_indication);
		printf("\n Light status: %s", state_machine[current_state].light_status);
		printf("\n Direction valve status: %s", state_machine[current_state].direction_valve_status);
		printf("\n Hydraulic pump status: %s", state_machine[current_state].hydraulic_pump_control_status);
	}

	else if(pilot_lever == down && hydraulic_mechanism == malfunctioning)
	{
		current_state = LWRNG_GR;
		printf("\n Current state: %s", state_machine[current_state].current_state_indication);
		printf("\n Light status: %s", state_machine[current_state].light_status);
		printf("\n Direction valve status: %s", state_machine[current_state].direction_valve_status);
		printf("\n Hydraulic pump status: %s", state_machine[current_state].hydraulic_pump_control_status);
	}

	if(pilot_lever == (up || rising || falling))
		Checking_Before_Landing();
}

Lowering_Gear.c:

#include <stdio.h>
#include "states.h"

void Lowering_Gear()
{
	current_state = LWRNG_GR;
	printf("\n\n Current state: %s", state_machine[current_state].current_state_indication);

	printf("\n Pilot's lever position and Limit switch status: ");
	fflush(stdout);
	scanf("%d %d", &pilot_lever, &limit_switch);

	if(pilot_lever == rising && limit_switch == close)
	{
		current_state = CHK_BFR_TKFF;
		printf("\n Current state: %s", state_machine[current_state].current_state_indication);
		printf("\n Light status: %s", state_machine[current_state].light_status);
		printf("\n Direction valve status: %s", state_machine[current_state].direction_valve_status);
		printf("\n Hydraulic pump status: %s", state_machine[current_state].hydraulic_pump_control_status);
	}

	else if(pilot_lever == down && limit_switch == open)
	{
		current_state = GR_DWN;
		printf("\n Current state: %s", state_machine[current_state].current_state_indication);
		printf("\n Light status: %s", state_machine[current_state].light_status);
		printf("\n Direction valve status: %s", state_machine[current_state].direction_valve_status);
		printf("\n Hydraulic pump status: %s", state_machine[current_state].hydraulic_pump_control_status);
	}

	else
		Lowering_Gear();
}

Output:


State machine is initialized and program is in Gear down state and light status: Green

Current state: Gear Down
Pilot's lever position and squat switch status: 0 0

Current state: Checking before take OFF
Light status: Green
Direction valve status: Down
Hydraulic pump status: Enabled

Current state: Checking before take OFF
Pilot's lever position, squat switch status and Hydraulic mechanism status: 2 0 0

Wait for 2 secs
Current state: Rising Gear
Light status: Red
Direction valve status: Up
Hydraulic pump status: Enabled

Current state: Rising Gear
Pilot's lever position and limit switch status: 2 1

Wait for 2 secs
Current state: Gear Up
Light status: Off
Direction valve status: Null
Hydraulic pump status: Disabled

Current state: Gear Up
Pilot's lever position: 1

Current state: Checking before landing
Light status: Red
Direction valve status: Down
Hydraulic pump status: Enabled

Current state: Checking before landing
Pilot's lever position and Hydraulic mechanism status: 3 0

Current state: Lowering Gear
Light status: Green
Direction valve status: Down
Hydraulic pump status: Enabled

Current state: Lowering Gear
Pilot's lever position and Limit switch status: 3 0

Current state: Gear Down
Light status: Green
Direction valve status: Down
Hydraulic pump status: Enabled
Current state: Gear Down
Pilot's lever position and squat switch status: 0 0

Current state: Checking before take OFF
Light status: Green
Direction valve status: Down
Hydraulic pump status: Enabled

Current state: Checking before take OFF
Pilot's lever position, squat switch status and Hydraulic mechanism status:

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.