Challenge

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

Answer:

The project is an implementation of retraction and extension of an airplane's landing gear using finite state machine. An airplane has three landing gears, nose gear, left gear and right gear. All three landing gears are controlled by the pilot in the cockpit using a lever which moves up or down. There are limit switches for all three gears which indicate whether the gear is fully up or not. If the limit switch of each gear is closed, it means that the gear is fully retracted. Apart from this there is a squat switch in the nose gear which senses whether the aircraft is airborne or on solid ground. If the squat switch is open, it means that the aircraft is airborne and if the squat switch is closed, it means that the aircraft is on solid ground. The operation of retraction and extension of the landing gear is done by a hydraulic system which is controlled by a direction valve. If the direction valve is up, it exerts upward pressure on the gear system to retract it. If the direction valve is down then the weight of the aircraft along with luggage and passengers exerts downward pressure on the gear systems to extend them.

When the gear is down, the lights are GREEN. When the gear is up, the lights are OFF and at all other times the lights are RED. When the gear is up, the control of landing gear is disabled. At all other times, it is enabled.

Based on this operation of the landing gear, I summarized that there are three input signals and three output signals for the entire operation of retraction and extension of the landing gear.

The three input signals are the pilot's lever, squat switch and limit switch.

The three output signals are the lights, the direction valve and control of landing gear.

Based on the given state diagram, I prepared the state transition table which is as follows:

Sr. No Present State Inputs Next State Outputs
1) Gear DOWN

Pilot's lever rising (1)

Squat switch open (0)

Limit switch open (0)

Checking before take-off

Lights: GREEN

Direction Valve: DOWN

Control for landing gear: Enabled

2) Checking before take-off

Pilot's lever down (0)

Squat switch closed (1)

Limit switch open (0)

Gear DOWN

Lights: GREEN

Direction Valve: DOWN

Control for landing gear: Enabled

3) Checking before take-off

Pilot's lever up (1)

Squat switch open (0)

Limit switch open (0)

Raising gear

Lights: RED

Direction Valve: UP

Control for landing gear: Enabled

4) Raising gear

Pilot's lever falling (0)

Squat switch open (0)

Limit switch open (0)

Checking before landing

Lights: RED

Direction Valve: UP

Control for landing gear: Enabled

5) Raising gear

Pilot's lever up (1)

Squat switch open (0)

Limit switch closed (1)

Gear UP

Lights: OFF

Direction Valve: UP

Control for landing gear: Disabled

6) Gear UP

Pilot's lever falling (0)

Squat switch open (0)

Limit switch closed (1)

Checking before landing

Lights: RED

Direction Valve: UP

Control for landing gear: Enabled

7) Checking before landing

Pilot's lever down (0)

Squat switch open (0)

Limit switch open (0)

Lowering gear

Lights: RED

Direction Valve: DOWN

Control for landing gear: Enabled

8) Lowering gear

Pilot's lever down (0)

Squat switch open (0)

Limit switch open (0)

Gear DOWN

Lights: GREEN

Direction Valve: DOWN

Control for landing gear: Enabled

9) Lowering gear

Pilot's lever rising (1)

Squat switch open (0)

Limit switch closed (1)

Checking before take-off

Lights: RED

Direction Valve: DOWN

Control for landing gear: Enabled

Based on the above state transition table, I have written the C code to implement the operation of retraction and extension of the landing gears of an aircraft.

In the program, I have first defined the six states using enum to assign a numerical value to each state. The six states are as follows:

'Gear_DOWN', 'Checking_before_take_off', 'Raising_gear', 'Gear_UP' and 'Checking_before_landing'.

Then I have defined a structure which consists of array of pointers to character variable to display the status of the Lights, Direction Valve and Control for Landing Gear. I have also defined the state type in the structure in order to advance to the next state depending on the input signal. I have also defined an array of pointer to a character in order to display the current state and an integer array to display the time delay if any.

I have defined a variable named 'current_state' of type enum.

In the main function, I have first declared integer variables to store the user input for choosing to operate the landing gears, the pilot's lever, the squat switch and the limit switch.

If the squat switch is closed (1), then the current state is invariably Gear DOWN. Similarly, if the limit switch is closed (1), then the current state is invariably Gear UP. For all other possibilities, I have printed the status of lights, direction valve, control for landing gear, current state indication and time delay in the console. The current state is advanced to the next state depending on the input conditions.

I will type the program below so that it can be tested on an online compiler. I will also attach a text document containing the output and the source file for reference:

---------------------------------------------------------------------------------------------------------------------------

PROGRAM:

/*
* Project_(2).c
*
* Created on: Jul 4, 2022
* Author: Admin
*/
#include <stdio.h>
#include <stdlib.h>

/*Enumerating the state types*/
typedef enum
{
Gear_DOWN = 0,
Checking_before_take_off = 1,
Raising_gear = 2,
Gear_UP = 3,
Checking_before_landing = 4,
Lowering_gear = 5,
} State_Type;

/*Declaration of a structure named state_machine to declare the state transition table*/
typedef struct
{
char *Lights[2];
char *Direction_valve[2];
char *Control_for_landing_gear[2];
State_Type gear_status[2];
char *current_state_display[2];
int delay[2];
} state_machine;

/*State transition table for nose gear*/
state_machine nose_gear[6] =
{
{{"GREEN","GREEN"},{"DOWN","DOWN"},{"Enabled","Enabled"},{Gear_DOWN,Checking_before_take_off},{"Gear Down","Checking before take-off"},{0,3}},
{{"RED","RED"},{"DOWN","UP"},{"Enabled","Enabled"},{Gear_DOWN,Raising_gear},{"Gear Down","Raising Gear"},{0,0}},
{{"RED","OFF"},{"UP","UP"},{"Enabled","Disabled"},{Checking_before_landing,Gear_UP},{"Checking before landing","Gear up"},{3,0}},
{{"RED","RED"},{"UP","UP"},{"Enabled","Disabled"},{Checking_before_landing,Gear_UP},{"Checking before landing","Gear up"},{3,0}},
{{"RED","RED"},{"DOWN","UP"},{"Enabled","Enabled"},{Lowering_gear,Raising_gear},{"Lowering gear","Raising gear"},{0,0}},
{{"GREEN","RED"},{"DOWN","DOWN"},{"Enabled","Enabled"},{Gear_DOWN,Checking_before_take_off},{"Gear Down","Checking before take off"},{0,3}}
};

/*State transition table for left gear*/
state_machine left_gear[6] =
{
{{"GREEN","GREEN"},{"DOWN","DOWN"},{"Enabled","Enabled"},{Gear_DOWN,Checking_before_take_off},{"Gear Down","Checking before take-off"},{0,3}},
{{"RED","RED"},{"DOWN","UP"},{"Enabled","Enabled"},{Gear_DOWN,Raising_gear},{"Gear Down","Raising Gear"},{0,0}},
{{"RED","OFF"},{"UP","UP"},{"Enabled","Disabled"},{Checking_before_landing,Gear_UP},{"Checking before landing","Gear up"},{3,0}},
{{"RED","RED"},{"UP","UP"},{"Enabled","Disabled"},{Checking_before_landing,Gear_UP},{"Checking before landing","Gear up"},{3,0}},
{{"RED","RED"},{"DOWN","UP"},{"Enabled","Enabled"},{Lowering_gear,Raising_gear},{"Lowering gear","Raising gear"},{0,0}},
{{"GREEN","RED"},{"DOWN","DOWN"},{"Enabled","Enabled"},{Gear_DOWN,Checking_before_take_off},{"Gear Down","Checking before take off"},{0,3}}
};

/*State transition table for right gear*/
state_machine right_gear[6] =
{
{{"GREEN","GREEN"},{"DOWN","DOWN"},{"Enabled","Enabled"},{Gear_DOWN,Checking_before_take_off},{"Gear Down","Checking before take-off"},{0,3}},
{{"RED","RED"},{"DOWN","UP"},{"Enabled","Enabled"},{Gear_DOWN,Raising_gear},{"Gear Down","Raising Gear"},{0,0}},
{{"RED","OFF"},{"UP","UP"},{"Enabled","Disabled"},{Checking_before_landing,Gear_UP},{"Checking before landing","Gear up"},{3,0}},
{{"RED","RED"},{"UP","UP"},{"Enabled","Disabled"},{Checking_before_landing,Gear_UP},{"Checking before landing","Gear up"},{3,0}},
{{"RED","RED"},{"UP","UP"},{"Enabled","Enabled"},{Lowering_gear,Raising_gear},{"Lowering gear","Raising gear"},{0,0}},
{{"GREEN","RED"},{"DOWN","DOWN"},{"Enabled","Enabled"},{Gear_DOWN,Checking_before_take_off},{"Gear Down","Checking before take off"},{0,0}}
};

State_Type current_state;

int main()
{
/*Declaration of integer variables to store the input commands*/
int pilot_lever;
int squat_switch;
int limit_switch;
int sel;

current_state = Gear_DOWN;

while(1)
{
printf("\n ********************************************* \n");
printf("\n Do you wish to operate the landing gear?");
printf("\n Enter '1' for 'YES' and '0' for 'NO': ");
fflush(stdout);
scanf(" %d",&sel);

if (sel == 1)
{
printf("\n Enter the status of pilot's lever: ");
printf("\n Enter '0' for 'DOWN' and '1' for 'UP': ");
fflush(stdout);
scanf(" %d",&pilot_lever); //Acquiring the status of the pilot's lever
printf("\n Enter the status of squat switch: ");
printf("\n Enter '0' for 'OPEN' and '1' for 'CLOSED': ");
fflush(stdout);
scanf(" %d",&squat_switch); //Acquiring the status of the squat switch for all three gears
printf("\n Enter the status of limit switch: ");
printf("\n Enter '0' for 'OPEN' and '1' for 'CLOSED'");
fflush(stdout);
scanf(" %d",&limit_switch); //Acquiring the status of the limit switch for all three gears

if (squat_switch == 1) //Whenever the squat switch is closed, the aircraft is on solid ground, so the current state is invariably, 'GEAR DOWN'
{
current_state = Gear_DOWN;
printf("\n *******Status of Nose Gear: *******");
printf("\n Current state: %s", nose_gear[current_state].current_state_display[0]);
printf("\n Lights: %s", nose_gear[current_state].Lights[0]);
printf("\n Direction Valve: %s", nose_gear[current_state].Direction_valve[0]);
printf("\n Control for landing gear is %s", nose_gear[current_state].Control_for_landing_gear[0]);
printf("\n Delay time: %d sec",nose_gear[current_state].delay[0]);

printf("\n *******Status of Left Gear: *******");
printf("\n Current state: %s", left_gear[current_state].current_state_display[0]);
printf("\n Lights: %s", left_gear[current_state].Lights[0]);
printf("\n Direction Valve: %s", left_gear[current_state].Direction_valve[0]);
printf("\n Control for landing gear is %s", left_gear[current_state].Control_for_landing_gear[0]);
printf("\n Delay time: %d sec",left_gear[current_state].delay[0]);

printf("\n *******Status of Right Gear: *******");
printf("\n Current state: %s", right_gear[current_state].current_state_display[0]);
printf("\n Lights: %s", right_gear[current_state].Lights[0]);
printf("\n Direction Valve: %s", right_gear[current_state].Direction_valve[0]);
printf("\n Control for landing gear is %s", right_gear[current_state].Control_for_landing_gear[0]);
printf("\n Delay time: %d sec",right_gear[current_state].delay[0]);
}

if (limit_switch == 1) //Whenever the limit switch is closed, the aircraft is completely airborne, so the current state is invariably 'GEAR UP'
{
current_state = Gear_UP;
printf("\n *******Status of Nose Gear: *******");
printf("\n Current state: %s", nose_gear[current_state].current_state_display[1]);
printf("\n Lights: %s", nose_gear[current_state].Lights[1]);
printf("\n Direction Valve: %s", nose_gear[current_state].Direction_valve[1]);
printf("\n Control for landing gear is %s", nose_gear[current_state].Control_for_landing_gear[1]);
printf("\n Delay time: %d sec",nose_gear[current_state].delay[1]);

printf("\n *******Status of Left Gear: *******");
printf("\n Current state: %s", left_gear[current_state].current_state_display[1]);
printf("\n Lights: %s", left_gear[current_state].Lights[1]);
printf("\n Direction Valve: %s", left_gear[current_state].Direction_valve[1]);
printf("\n Control for landing gear is %s", left_gear[current_state].Control_for_landing_gear[1]);
printf("\n Delay time: %d sec",left_gear[current_state].delay[1]);

printf("\n *******Status of Right Gear: *******");
printf("\n Current state: %s", right_gear[current_state].current_state_display[1]);
printf("\n Lights: %s", right_gear[current_state].Lights[1]);
printf("\n Direction Valve: %s", right_gear[current_state].Direction_valve[1]);
printf("\n Control for landing gear is %s", right_gear[current_state].Control_for_landing_gear[1]);
printf("\n Delay time: %d sec",right_gear[current_state].delay[1]);
}
if (squat_switch == 0 && limit_switch == 0) //When squat switch and limit switch are not closed, the normal control of gears is defined below
{
printf("\n *******Status of Nose Gear: *******");
printf("\n Current state: %s", nose_gear[current_state].current_state_display[pilot_lever]);
printf("\n Lights: %s", nose_gear[current_state].Lights[pilot_lever]);
printf("\n Direction Valve: %s", nose_gear[current_state].Direction_valve[pilot_lever]);
printf("\n Control for landing gear is %s", nose_gear[current_state].Control_for_landing_gear[pilot_lever]);
printf("\n Delay time: %d sec",nose_gear[current_state].delay[pilot_lever]);

printf("\n *******Status of Left Gear: *******");
printf("\n Current state: %s", left_gear[current_state].current_state_display[pilot_lever]);
printf("\n Lights: %s", left_gear[current_state].Lights[pilot_lever]);
printf("\n Direction Valve: %s", left_gear[current_state].Direction_valve[pilot_lever]);
printf("\n Control for landing gear is %s", left_gear[current_state].Control_for_landing_gear[pilot_lever]);
printf("\n Delay time: %d sec",left_gear[current_state].delay[pilot_lever]);

printf("\n *******Status of Right Gear: *******");
printf("\n Current state: %s", right_gear[current_state].current_state_display[pilot_lever]);
printf("\n Lights: %s", right_gear[current_state].Lights[pilot_lever]);
printf("\n Direction Valve: %s", right_gear[current_state].Direction_valve[pilot_lever]);
printf("\n Control for landing gear is %s", right_gear[current_state].Control_for_landing_gear[pilot_lever]);
printf("\n Delay time: %d sec",right_gear[current_state].delay[pilot_lever]);

current_state = nose_gear[current_state].gear_status[pilot_lever]; //The current state is advanced to the next state depending on the pilot's lever
}
}
else if (sel == 0)
{
printf("\n You have chosen to exit the program.");
break;
}
}

}

=================================END================================

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.