Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Project 2
Objective:
- To control the retraction and extension of Airplane’s landing gear.
- To implement FSM to control the retraction i.e utilizes pressurized hydraulic fluid to actuate linkages to raise and lower the gear.
- To control the direction of the pressurized gas valve
State Diagram:

The conditions and functions are made as per the state diagram.
Program:
#include<stdio.h>
#include<stdlib.h>
void init_state_machine(void);
void Gear_down(void);
void Checking_before_take_off(void);
void Rising_gear(void);
void Check_before_landing(void);
void Lowering_gear(void);
typedef enum states
{
s1,
s2,
s3,
s4,
s5,
s6,
}STATES;
typedef enum Switch
{
ON,
OFF
}SWITCH;
typedef enum hydraullic_mechanism
{
WORKING,
NOT_WORKING
}HM;
typedef enum pilot_lever
{
RAISING,
FALLING,
}PL;
typedef struct
{
STATES current_state;
int light;
int direction_valve;
int hydraullic_control;
}State_Table;
State_Table state_machine[6]=
{
{"Gear down","Green","Down","Enabled"},
{"Checking before take off","Green","Down","Enabled"},
{"Rising gear","Red","Up","Enabled"},
{"Lowering gear","Off","Null","Disable"},
{"Checking before landing","Red","Down","Enabled"},
{"Lowering gear","Green","Down","Enable"}
};
static volatile SWITCH limit_switch;
static volatile SWITCH squat_switch;
int main()
{
init_state_machine();
while(1)
{
state_table[current_state]();
}
}
void init_state_machine(void)
{
current_state=s1;
printf("\n current state: %s",state_machine[current_state].current_state[STATES]);
printf("\n Light is : %s",state_machine[squat_switch].light[SWITCH]);
printf("\n hydraullic condition is : %s",state_machine[hydraullic_mechanism].direction_valve[HM]);
printf("\n Pilot lever: %s",state_machine[pilot_lever].hydraullic_control[PL]);
}
void Gear_down(void)
{
current_state=s1;
printf("\n enter value of pilot lever and squat switch");
fflush(stdout);
scanf("%d %d",&PL,&squat_switch);
if (PL==RISING && squat_switch==OPEN)
{
current_state=Checking_before_take_off();
printf("\n current state: %s",state_machine[current_state].current_state[STATES]);
printf("\n Light is : %s",state_machine[squat_switch].light[SWITCH]);
printf("\n hydraullic condition is : %s",state_machine[hydraullic_mechanism].direction_valve[HM]);
printf("\n Pilot lever: %s",state_machine[pilot_lever].hydraullic_control[PL]);
}
else
{
Gear_down();
}
}
void Checking_before_take_off(void)
{
current_state=s2;
printf("\nEnter the status of pilot lever:");
fflush(stdout);
scanf("%d",&PL);
if (PL==UP)
{
current_state=Rising_gear();
printf("\n current state: %s",state_machine[current_state].current_state[STATES]);
printf("\n Light is : %s",state_machine[squat_switch].light[SWITCH]);
printf("\n hydraullic condition is : %s",state_machine[hydraullic_mechanism].direction_valve[HM]);
printf("\n Pilot lever: %s",state_machine[pilot_lever].hydraullic_control[PL]);
}
else if (PL==FALLING && squat_switch==OFF)
{
current_state=Gear_down();
printf("\n current state: %s",state_machine[current_state].current_state[STATES]);
printf("\n Light is : %s",state_machine[squat_switch].light[SWITCH]);
printf("\n hydraullic condition is : %s",state_machine[hydraullic_mechanism].direction_valve[HM]);
printf("\n Pilot lever: %s",state_machine[pilot_lever].hydraullic_control[PL]);
}
else
{
Checking_before_take_off();
}
}
void Rising_gear(void)
{
current_state=s3;
printf("\n Enter the status of pilot lever:");
fflush(stdout);
scanf("%d",&PL);
if (PL==Falling)
{
current_state=Check_before_landing();
printf("\n current state: %s",state_machine[current_state].current_state[STATES]);
printf("\n Light is : %s",state_machine[squat_switch].light[SWITCH]);
printf("\n hydraullic condition is : %s",state_machine[hydraullic_mechanism].direction_valve[HM]);
printf("\n Pilot lever: %s",state_machine[pilot_lever].hydraullic_control[PL]);
}
else if (PL==RISING && limit_switch==OFF)
{
current_state=Gear_up();
printf("\n current state: %s",state_machine[current_state].current_state[STATES]);
printf("\n Light is : %s",state_machine[squat_switch].light[SWITCH]);
printf("\n hydraullic condition is : %s",state_machine[hydraullic_mechanism].direction_valve[HM]);
printf("\n Pilot lever: %s",state_machine[pilot_lever].hydraullic_control[PL]);
}
else
{
Rising_gear();
}
}
void Gear_up(void)
{
current_state=s4;
printf("\n Enter the status of pilot lever:");
fflush(stdout);
scanf("%d",&PL);
if (PL==FALLING)
{
current_state=Check_before_landing();
printf("\n current state: %s",state_machine[current_state].current_state[STATES]);
printf("\n Light is : %s",state_machine[squat_switch].light[SWITCH]);
printf("\n hydraullic condition is : %s",state_machine[hydraullic_mechanism].direction_valve[HM]);
printf("\n Pilot lever: %s",state_machine[pilot_lever].hydraullic_control[PL]);
}
else
{
Gear_up();
}
}
void Check_before_landing(void)
{
current_state=s5;
printf("\n Enter the status of pilot lever:");
fflush(stdout);
scanf("%d",&PL);
if (PL==DOWN)
{
current_state=Lowering_gear();
printf("\n current state: %s",state_machine[current_state].current_state[STATES]);
printf("\n Light is : %s",state_machine[squat_switch].light[SWITCH]);
printf("\n hydraullic condition is : %s",state_machine[hydraullic_mechanism].direction_valve[HM]);
printf("\n Pilot lever: %s",state_machine[pilot_lever].hydraullic_control[PL]); }
else
{
Gear_up();
}
}
void Lowering_gear(void)
{
current_state=s6;
printf("\n Enter the status of pilot lever and limit switch:");
fflush(stdout);
scanf("%d %d",&PL,&limit_switch);
if (PL==RISING)
{
current_state=Check_before_take_off();
printf("\n current state: %s",state_machine[current_state].current_state[STATES]);
printf("\n Light is : %s",state_machine[squat_switch].light[SWITCH]);
printf("\n hydraullic condition is : %s",state_machine[hydraullic_mechanism].direction_valve[HM]);
printf("\n Pilot lever: %s",state_machine[pilot_lever].hydraullic_control[PL]);
}
else if (limit_switch==ON)
{
current_state=Gear_down();
printf("\n current state: %s",state_machine[current_state].current_state[STATES]);
printf("\n Light is : %s",state_machine[squat_switch].light[SWITCH]);
printf("\n hydraullic condition is : %s",state_machine[hydraullic_mechanism].direction_valve[HM]);
printf("\n Pilot lever: %s",state_machine[pilot_lever].hydraullic_control[PL]);
}
else
{
Lowering_gear();
}
}