Challenge

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

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

Overview:

Actually an airplane has 3 gears for landing like nose gear, left gear, right gear.

There is an electrically powered hydraulic pump that applies pressure to the gears to bring it down or pull it up.

there is a limit switch on each leg of the gear to assess whether the plane is on the ground or not.

When the direction of the valve is down then it is landed or gear down position. similarly when the direction of the valve is up then it is in rising gear or taking off. When gear is up then light status is off and Gear is down then the light status is green. Gear is raising or lowering then the colour of light is red.

Code:

Header file (state.h): Static table functions are defined in this header file of the states.It consists of the various states of retraction and extension of airplanes landing gear modes.

#ifndef STATE_H_
#define STATE_H_
#include<stdio.h>
void Initiate_State_Machine();
void GearDown();
void CheckBeforeTakeoff();
void RaisingGear();
void GearUp();
void CheckBeforeLanding();
void LoweringGear();
typedef enum State
{
GEAR_DOWN,
CHECK_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR,
}State_Type;
typedef enum Switch
{
on,
off //plane is on ground
}Switch_status;
typedef enum pilot_lever
{
Raising,
falling
}Pilot_lever;
typedef enum hydraulic_mechanism
{
working,
notworking
}Hydraulic_mechanism;
static volatile Switch_status squat_switch;
static volatile Switch_status limit_switch;
static volatile Pilot_lever pilot_lever;
static volatile Hydraulic_mechanism hydraulic_mechanism;
State_Type Current_State;
typedef struct
{
char* current_state_indication;
char* light;
char* direction_valve_status;
char* landing_gear_hydraulic_control;
char* Gas_pressarized_system_status[2];
}State_Table;
static State_Table
State_Machine[6] =
{
{"GearDown", "Green", "Down", "Enabled", {NULL,NULL}},
{"CheckBeforeTakeoff", "Green", "Down", "Enabled", {NULL,NULL}},
{"RaisingGear", "Red", "Up", "Enabled", {"Disabled","Enabled"}},
{"GearUp", "Off", "NULL", "Disabled", {NULL,NULL}},
{"CheckBeforeLanding", "Red", "Down", "Enabled", {NULL,NULL}},
{"LoweringGear", "Green","Down", "Enabled", {"Disabled","Enabled"}}
};
#endif /* STATE_H_ */

Source file

#include<stdio.h>
#include"state.h" // From header file 'state.h'
static void(*StateTable[])(void)={GearDown, CheckBeforeTakeoff, RaisingGear,
GearUp,CheckBeforeLanding, LoweringGear};
int main()
{
Initiate_State_Machine();
while(1)
{
StateTable[Current_State]();
}
}
void Initiate_State_Machine()
{
Current_State=GEAR_DOWN;
printf("The Finite State machine is Initialized and current program is in "
"GearDown state & light is: %s\n",State_Machine[Current_State].light);
} // After initializing the code, current state is in Gear Down with green light.
//Gear Down State
void GearDown(void)
{
Current_State=GEAR_DOWN;
printf("Enter the Pilot's lever Position and squat Switch:");
fflush(stdout);
scanf("%d %d", &pilot_lever, &squat_switch);
if(pilot_lever==Raising && squat_switch==on)
{
Current_State=CHECK_BEFORE_TAKEOFF;
printf("Current State: %s\n", State_Machine[Current_State].current_state_indication);
printf("Light is: %s\n", State_Machine[Current_State].light);
}
else
{
GearDown();
}
}
//Check Before Takeoff
void CheckBeforeTakeoff(void)
{
Current_State=CHECK_BEFORE_TAKEOFF;
printf("Enter the Pilot's lever Position and squat Switch and hydraulic mechanism:");
fflush(stdout);
scanf("%d %d", &pilot_lever, &squat_switch, &hydraulic_mechanism);
if(pilot_lever==falling && squat_switch==off)
{
GearDown();
}
else if(pilot_lever==Raising && squat_switch==on && hydraulic_mechanism==working)
{
Current_State=RAISING_GEAR;
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 Direction Valve: %s\n", State_Machine[Current_State].direction_valve_status);
}
else if(pilot_lever==Raising && squat_switch==on && hydraulic_mechanism==notworking)
{
Current_State=RAISING_GEAR;
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 Direction Valve: %s\n", State_Machine[Current_State].direction_valve_status);
printf("status of Gas Pressurized system: %s\n", State_Machine[Current_State].Gas_pressarized_system_status);
}
}
//Raising Gear
void RaisingGear(void)
{
Current_State=RAISING_GEAR;
printf("Enter the status of pilot's lever and limit switch:");
fflush(stdout);
scanf("%d %d", &pilot_lever, &limit_switch);
if(pilot_lever==falling && limit_switch==on)
{
Current_State=CHECK_BEFORE_LANDING;
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 if(pilot_lever==Raising && limit_switch==off)
{
Current_State=GEAR_UP;
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();
}
}
//Gear Up
void GearUp(void)
{
Current_State=GEAR_UP;
printf("Enter the status of pilot's lever:");
fflush(stdout);
scanf("%d", &pilot_lever);
if(pilot_lever==falling)
{
Current_State=CHECK_BEFORE_LANDING;
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();
}
}
//Checking before landing
void CheckBeforeLanding(void)
{
Current_State=CHECK_BEFORE_LANDING;
printf("Enter the status of pilot's level and hydraulic Mechanism status:");
fflush(stdout);
scanf("%d %d", &pilot_lever, &hydraulic_mechanism);
if(pilot_lever==falling && hydraulic_mechanism==working)
{
Current_State=LOWERING_GEAR;
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 Direction Valve: %s\n", State_Machine[Current_State].direction_valve_status);
}
else if(pilot_lever==falling && squat_switch==on && hydraulic_mechanism==notworking)
{
Current_State=LOWERING_GEAR;
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 Direction Valve: %s\n", State_Machine[Current_State].direction_valve_status);
printf("Status of Gas Pressurized system: %s\n",State_Machine[Current_State].Gas_pressarized_system_status);
}
if(pilot_lever==(Raising || falling))
{
CheckBeforeLanding();
}
}
//Lowering Gear
void LoweringGear(void)
{
Current_State=LOWERING_GEAR;
printf("Enter the status of pilot's level and limit switch:");
fflush(stdout);
scanf("%d %d", &pilot_lever, &limit_switch);
if(pilot_lever==Raising && limit_switch==on)
{
Current_State=CHECK_BEFORE_TAKEOFF;
printf("Current State: %s\n", State_Machine[Current_State].current_state_indication);
printf("Light is: %s\n", State_Machine[Current_State].light);
}
if(pilot_lever==Raising && limit_switch==off)
{
Current_State=GEAR_DOWN;
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();
}
}

Output:

In header file(state.h) static table functions are defined. it consists of various states of retraction and extension of airplanes landing gear modes.

States:

  • GEAR_DOWN
  • CHECK_BEFORE_TAKEOFF
  • RAISING_GEAR
  • GEAR_UP
  • CHECK_BEFORE_LANDING
  • LOWERING_GEAR

Switch state:

  • on
  • off

Pilot lever state:

  • raising
  • falling

Hydraulic mechanism:

  • working
  • notworking

In source file state table conditions are declared.

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.