Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
In this project, controlling the retraction and extension of Airplane’s landing gear can be implemented using Finite State Machine (FSM). FSM is the most efficient algorithm which is mathematical model of computation.
This project using FSM machine state and represent in the airplane landing current state with given input string.
GOALS:
Controlling the direction of Valve.
Controlling the retraction and extension of Airplane’s landing gear.
indicating stages by different light signals.
proper checking of gears before landing and take off.
GEARS:
Usually the airplane consists of three gears for smooth landing and take off
1.Nose gear.
2.Left gear.
3.Right gear.
SWITCHES:
Each gear consists of seperate limit switches and additionally nose gear will have a squat switch to check whether is lane is in ground or not.
HYDRALIC PUMP:
It exerting pressure to the gears to bring it down or pull it up and that pump is electrically driven.
- Hydraulic Control can detect the rising edge and falling edge of pilot’s lever
- In case of Hydraulic system failure,there are two gas pressurized springs by which gears can be extended
- The case of failure of Squat Switch is ignored.


code -
The code created in saparate header file and set the state .
Use typedef to set the state condition,hydraulic condition,switch condition and pilot liver condition.
Create saparate main source file when calling header file and set the state tbale of main source file to shows the current state .
Create saparate source file the to diasplay the state of the machine .
Header file -
#ifndef SECOND_H_
#define SECOND_H_
#include <stdio.h>
void Init_State_machine();
void Gear_Down();
void Checking_Before_Takeoff();
void Raising_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 Hydraulic_Mechanism
{
Working=0,
Malfunctioning=1
}Hydraulic_Mechanism_Status;
typedef enum Switch
{
Open=0,
Close=1
}Switch_Status;
typedef enum Pilot_Lever
{
Rising=0,
Falling=1,
UP=2,
DOWN=3
}Pilot_Lever_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;
char* light_Status;
char* direction_value_status;
char* hydraulic_pump_control_status;
char* gas_pressurized_spring_status[2];
}State_Table;
static State_Table State_Machine[6] = {
{"GearDown","Green","Down","Enabled",0,0},
{"Checking Before Take Off ","Green","Down","Enabled",0,0},
{"Raising Gear","Red","Up","Enabled",{"Disabled","Enabled"}},
{"Gear Up","Off","Null","Disabled",0,0},
{"Checking Before Landing","Red","Down","Enabled",0,0},
{"Lowering Gear","Green","Down","Enabled",{"Disabled","Enabled"}}
};
#endif /* SECOND_H_ */

main source file -
#include <stdio.h>
#include "second.h"
static void (*state_table[])(void) = {Gear_Down,Checking_Before_Takeoff,Raising_Gear,Gear_Up,Checking_Before_Landing,Lowering_Gear};
int main()
{
Init_State_Machine();
while(1)
{
state_table[current_state]();
}
}

sub source file -
#include "second.h"
#include <stdio.h>
void Init_State_Machine()
{
printf("\n Light_Status : %s\n",State_Machine[current_state].light_Status);
printf("\n Current State is%s\n",State_Machine[current_state].current_state);
}
void Gear_Down(void)
{
current_state = GR_DWN;
printf("\n Enter the position of Pilot's Lever 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\n",State_Machine[current_state].current_state);
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
}
else
{
printf("\n returning to gear down state");
Gear_Down();
}
}
void Checking_Before_Takeoff()
{
current_state = CHK_BFR_TKFF;
printf("\n Enter the position of Pilot's Lever,Squat Switch Status, Hydraulic Mechanism Status :");
fflush(stdout);
scanf("%d %d %d",&pilot_lever,&squat_switch,&hydraulic_mechanism);
printf("\n Wait for 2 secs\n");
if (pilot_lever == UP && squat_switch == Open )
{
fflush(stdout);
if(hydraulic_mechanism == Working)
{
current_state = RSING_GR;
printf("\n Current State: %s\n",State_Machine[current_state].current_state);
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
printf("Direction Value Status:%s\n\n",State_Machine[current_state].direction_value_status);
}
else if ( hydraulic_mechanism == Malfunctioning)
{
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
printf("Direction Value Status:%s\n",State_Machine[current_state].direction_value_status);
printf("Gas Pressurized Spring System Status:%s\n",State_Machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]);
}
}
else if(pilot_lever == Falling || squat_switch == Close)
{
printf("\n returning to gear down");
Gear_Down();
}
}
void Raising_Gear()
{
current_state = RSING_GR;
printf("\n Enter the Limit Switch Status, Pilot's Lever Position :");
fflush(stdout);
scanf("%d %d",&limit_switch,&pilot_lever);
printf("\nWait for 1 sec\n");
if(pilot_lever == Falling && limit_switch == Open)
{
current_state = CHK_BFR_LND;
printf ("\n Current State: %s\n",State_Machine[current_state].current_state);
printf ("Light Status:%s\n",State_Machine[current_state].light_Status);
printf ("Hydraulic Control for Landing Gear Status:%s\n",State_Machine[current_state].hydraulic_pump_control_status);
}
else if (limit_switch == Close && pilot_lever == UP)
{
current_state = GR_UP;
printf ("\n Current State: %s\n",State_Machine[current_state].current_state);
printf ("Light Status:%s\n",State_Machine[current_state].light_Status);
printf ("Hydraulic Control for Landing Gear Status:%s\n",State_Machine[current_state].hydraulic_pump_control_status);
}
else
{
printf("\n returning to raising gear ");
Raising_Gear();
}
}
void Gear_Up()
{
current_state = GR_UP;
printf("\n Enter the Pilot's Lever Position:");
fflush(stdout);
scanf("%d",&pilot_lever);
if(pilot_lever ==Falling)
{
current_state = CHK_BFR_LND;
printf("\n Current State:%s\n",State_Machine[current_state].current_state);
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
printf("Hydraulic Control for Landing Gear Status:%s\n",State_Machine[current_state].hydraulic_pump_control_status);
}
else
{
printf("\n returning to gear up");
Gear_Up();
}
}
void Checking_Before_Landing()
{
current_state = CHK_BFR_LND;
printf("\nCurrent State : %s\n",State_Machine[current_state].current_state);
printf("\nEnter the Pilot's Lever Position and Status of Hydraulic Mechanism:");
fflush(stdout);
scanf("%d %d",&pilot_lever,&hydraulic_mechanism);
if(pilot_lever == DOWN && hydraulic_mechanism == Working)
{
current_state = LWRNG_GR;
printf("\nCurrent State : %s\n",State_Machine[current_state].current_state);
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
printf("Direction Valve Status:%s\n",State_Machine[current_state].direction_value_status);
}
else if(pilot_lever == DOWN && hydraulic_mechanism == Malfunctioning)
{
current_state = LWRNG_GR;
printf("\nCurrent State : %s\n",State_Machine[current_state].current_state);
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
printf("Direction Valve Status:%s\n",State_Machine[current_state].direction_value_status);
printf("Gas Pressurized Spring System Status:%s\n",State_Machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]);
}
else if(pilot_lever == (UP || Rising || Falling))
{
printf("\n returning to Checking_Before_Landing ");
Checking_Before_Landing();
}
}
void Lowering_Gear()
{
current_state = LWRNG_GR;
printf("\n Enter the Limit_Switch Status,Pilot_Lever Status ::");
fflush(stdout);
scanf("%d %d",&limit_switch,&pilot_lever);
if( limit_switch == Close && pilot_lever == Rising)
{
current_state = CHK_BFR_TKFF;
printf("\n Current State: %s\n",State_Machine[current_state].current_state);
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
}
else if(limit_switch == Open && pilot_lever == DOWN)
{
current_state = GR_DWN;
printf ("\n Current State: %s\n",State_Machine[current_state].current_state);
printf ("Light Status:%s\n",State_Machine[current_state].light_Status);
printf ("Hydraulic Control for Landing Gear Status:%s\n",State_Machine[current_state].hydraulic_pump_control_status);
}
else
{
printf("\n returning to lower gear");
Lowering_Gear();
}
}

output -

