Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Overview of the Project:
Using Finite State Machine (FSM), Controlling the retraction and extension of Airplane's landing gear.
Goals:
To Control the direction of valve.
To Control the retraction and extension of Airplane's landing gear.
Keyhighlights:
A direction valve is controlled by the software to allow the hydraulic pressure to either raise or lower the landing gear.
Deliverables of this project:
Controlling the retraction and extension of airplane's landing gear using finite state machine.
Expected Outcomes:
Getting knowledge of controlling the retraction and extension.
Understanding the use of Finite State Machine.
Understanding the airplane's landing gear.
Getting knowledge of switch and gear states of airplanes.
Program:
#include<stdio.h>
#include<stdlib.h>
//functions declaration
void Initial_state_machine();
void Gear_down();
void Checking_before_takeoff();
void Raising_gear();
void Gear_up();
void Checking_before_landing();
void Lowering_gear();
typedef enum states //enum is used to give the values to each declared variables
{
GEAR_DOWN,
CHECK_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR
}state_type;
typedef enum Switch
{
open, //open=0,close=1;
close
}Switch_status;
typedef enum pilot_lever
{
raising, //raising = 0, falling = 1, up = 3, down = 4
falling,
up,
down
}pilot_lever_status;
typedef enum hydraulic_mechanism
{
working, //working = 0, malfunctioning = 1
malfunctioning
}hydraulic_mechanism_status;
//input used in order to change the state
static volatile Switch_status squat_switch;
static volatile Switch_status limit_switch;
static volatile pilot_lever_status pilot_lever;
static volatile pilot_lever_status hydraulic_mechanism;
state_type current_state;
typedef struct //structures
{
char* current_state_status;
char* light_status;
char* direction_value_status;
char* hydraulic_pump_ctrl_status;
char* gas_pressurized_spring_status[2];
}state_table;
//state table of the require system
static state_table state_machine[6] = {
{"gear down","green","down","enables",{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"}}
};
static void(*State_Table[])(void)={Gear_down,Checking_before_takeoff,Raising_gear,Gear_up,Checking_before_landing,Lowering_gear};
//initial state state_machine
void Initial_state_machine()
{
current_state = GEAR_DOWN; //during initialization, system is at gear down.
printf("\n state machine is initials and at first program is in gear down and its light status is: %s\n", state_machine[current_state].light_status);
}
//Gear down
void Gear_down(void)
{
current_state = GEAR_DOWN;
printf("\nEnter the pilot's lever position and squat switch status:");
fflush(stdout);
scanf("%d %d", &pilot_lever , &squat_switch);
if(pilot_lever == raising && squat_switch == open) //if pilot lever raising(0) and squat swtich is open(0)
{
current_state = CHECK_BEFORE_TAKEOFF;
printf("\nThe current state of the machine is: %s\n", state_machine[current_state].current_state_status);
printf("Light Status of the current machine state is: %s\n", state_machine[current_state].light_status);
}
else
{
Gear_down(); //gear down function is called
}
}
//checking before CHECK_BEFORE_TAKEOFF
void Checking_before_takeoff()
{
current_state = CHECK_BEFORE_TAKEOFF;
printf("\nEnter the 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 just wait for 2 secs\n");
if(pilot_lever == up && squat_switch == close) //pilot lever is up(2) and squat swtich is closed(1) it get into the if loop
{
Gear_down(); //gear down function is called
}
else if(pilot_lever == up && squat_switch == open && hydraulic_mechanism == working) //pilot lever is up (2) , squat switch (0) and hydraulic mechanism working(0)
{
current_state = RAISING_GEAR;
printf("\nCurrent State : %s\n",state_machine[current_state].current_state_status);
printf("Light Status:%s\n", state_machine[current_state].light_status);
printf("Direction Valve Status:%s\n\n",state_machine[current_state].direction_value_status);
}
else if(pilot_lever == up && squat_switch == open && hydraulic_mechanism == malfunctioning) //pilot lever is up (2) , squat switch (0) and hydraulic mechanism working(1)
{
current_state = RAISING_GEAR;
printf("\nCurrent State : %s\n",state_machine[current_state].current_state_status);
printf("Light Status:%s\n", state_machine[current_state].light_status);
printf("Direction Valve Status:%s\n\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]);
}
}
//raising gear
void Raising_gear()
{
current_state = RAISING_GEAR;
printf("\nEnter the Limit Switch Status, Pilot Lever Status:");
fflush(stdout);
scanf("%d %d ",&limit_switch,&pilot_lever);
printf("\nWait for 1 secs\n");
if(pilot_lever == falling && limit_switch == open) //pilot lever is falling (1) and limit_switch is open(0)
{
current_state = CHECK_BEFORE_LANDING;
printf("\nCurrent State : %s\n",state_machine[current_state].current_state_status);
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_ctrl_status);
}
else if(limit_switch == close && pilot_lever == up) //limit switch is close (1) and pilot level is up(2)
{
current_state = GEAR_UP;
printf("\nCurrent State : %s\n",state_machine[current_state].current_state_status);
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_ctrl_status);
}
else
{
Raising_gear(); //Raising_gear function is called
}
}
//gear up
void Gear_up()
{
current_state = GEAR_UP;
printf("\nEnter the pilot's lever position:");
fflush(stdout);
scanf("%d", &pilot_lever);
if(pilot_lever == falling) //pilot lever is falling (1)
{
current_state = CHECK_BEFORE_LANDING;
printf("\nCurrent State : %s\n", state_machine[current_state].current_state_status);
printf("\nLight Status: %s\n", state_machine[current_state].light_status);
printf("Hydraulic Control for Landing Gear Status: %s\n",state_machine[current_state].hydraulic_pump_ctrl_status);
}
else
{
Gear_up(); //Gear_up function is called
}
}
//Checking_before_landing
void Checking_before_landing()
{
current_state = CHECK_BEFORE_LANDING;
printf("\nEnter the Pilot's Lever Position and Status of Hydraulic Mechanism Status:");
fflush(stdout);
scanf("%d %d ",&pilot_lever, &hydraulic_mechanism);
if(pilot_lever == down && hydraulic_mechanism == working) //pilot lever is down (3) and hydraulic mechanism is working(0)
{
current_state = LOWERING_GEAR;
printf("\nCurrent State : %s\n",state_machine[current_state].current_state_status);
printf("Light Status:%s\n", state_machine[current_state].light_status);
printf("Direction Valve Status:%s\n\n",state_machine[current_state].direction_value_status);
}
else if(pilot_lever == down && hydraulic_mechanism == malfunctioning) //pilot lever is down (3) and hydraulic mechanism is working(1)
{
current_state = LOWERING_GEAR;
printf("\nCurrent State : %s\n",state_machine[current_state].current_state_status);
printf("Light Status:%s\n", state_machine[current_state].light_status);
printf("Direction Valve Status:%s\n\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
{
Checking_before_landing(); //Checking_before_landing function is called
}
}
//Lowering gear
void Lowering_gear()
{
current_state = LOWERING_GEAR;
printf("\nEnter the Limit Switch Status, Pilot Lever Status:");
fflush(stdout);
scanf("%d %d ",&pilot_lever, &limit_switch);
if(pilot_lever == raising && limit_switch == close) //pilot lever raising (0) and limit switch is close(1)
{
current_state = CHECK_BEFORE_TAKEOFF;
printf("\nCurrent State : %s\n",state_machine[current_state].current_state_status);
printf("\nLight Status:%s\n", state_machine[current_state].light_status);
}
if(pilot_lever == down && limit_switch == open) //pilot lever is down (3) and limit switch is open(0)
{
current_state = GEAR_DOWN;
printf("\nCurrent State : %s\n",state_machine[current_state].current_state_status);
printf("\nLight Status:%s\n", state_machine[current_state].light_status);
printf("\nHydraulic Control for Landing Gear Status :%s\n",state_machine[current_state].hydraulic_pump_ctrl_status);
}
else
{
Lowering_gear(); //Lowering_gear function is called
}
}
//main funciton
int main()
{
Initial_state_machine();
while(1)
{
State_Table[current_state]();
}
}