Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
<form>
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.
- Controlling the direction of Valve.
- Controlling the retraction and extension of Airplane’s landing gear.
ANSWER
/*
* controlling_the_retraction_and_extension.c
* Created on: 31-Aug-2023
* Author: SAVITA BHAGAT
*/
#include //We have to include stdio.h (standard input output library for C language).to use printf and scanf functions.
#include // We have to include stdlib.h for memory allocation,conversion, random number generation, and more.
/* Functions used in our projects*/
void Initial_state_machine(); //we have declared function.
void Gear_down();
void Checking_before_takeoff();
void Raising_gear();
void Gear_up();
void Checking_before_landing();
void Lowering_gear();
typedef enum states
{
GEAR_DOWN, //ENUM IS USED TO GIVING SUBSEQUENT VALUE FOR EACH STATES
CHECK_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR
}state_type;
typedef enum Switch
{
open, //open=0,close=1;
close //plane is in ground
}Switch_status;
typedef enum pilot_lever
{
raising, //by using enum rising =0,
falling, //falling=1,
up, //up=3and
down //down=4 is assumed
}pilot_lever_status;
typedef enum hydraulic_mechanism
{
working, //here 0 means for working and 1 means for malfunction
malfunctioning
}hydraulic_mechanism_status;
/* input used in order to change the state */
static volatile Switch_status squat_switch; // blue_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 // structure is used to have different types of data type
{
char* current_state_status; //output which we want to visualize are given under structure as structure member.
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 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("\n Enter the pilot's lever position and squat switch status:\n"); // getting pilot lever and squat switch value from input
//Lever:rising(0),falling(1),up(2),down(3)
fflush(stdout); //switch:open(0),close(1) Hydraulic:working(0),not-working(1)
scanf("%d %d", (int*)&pilot_lever, (int*)&squat_switch);
if(pilot_lever == raising && squat_switch == open) // if pilot lever raising(0) and squar switch is open(0)
{
current_state = CHECK_BEFORE_TAKEOFF;
printf("\n The current state of the machine is: %s\n",state_machine[current_state].current_state_status); // current status value is printed from the state table
printf("\n Light status of the current machine state is:%s\n",state_machine[current_state].light_status); // light status value is taken from state table current state
}
else
{
Gear_down(); //gear down function is called
}
}
//checking before takeoff//
void Checking_before_takeoff()
{
current_state= CHECK_BEFORE_TAKEOFF; //current state is check before takeoff
printf("\n Enter the pilot lever position, squat switch and hydraulic mechancism:\n"); // getting pilot liver, squat switch and hydraulic mechanism as input
//Lever:rising(0),falling(1),up(2),down(3)
fflush(stdout); //switch:open(0),close(1) Hydraulic:working(0),not-working(1)
scanf("%d %d %d", (int*)&pilot_lever, (int*)&squat_switch, (int*)&hydraulic_mechanism);
printf("\n just wait 2secs\n");
if (pilot_lever == up && squat_switch== close) //pilot lever is up and squat switch is closed it get into the if loop
{
Gear_down(); //gear down function is called
}
else if (pilot_lever == up && squat_switch == open && (int)hydraulic_mechanism == (int)working)//Lever:rising(0),falling(1),up(2),down(3),switch:open(0),close(1) Hydraulic:working(0),not-working(1)
{
current_state= RAISING_GEAR;
printf("\n Current state: %s\n",state_machine[current_state].current_state_status);
printf("light status: %s\n", state_machine[current_state].light_status);
printf("\n direction status:%s\n\n",state_machine[current_state].direction_value_status);
}
else if (pilot_lever== up && squat_switch == open && hydraulic_mechanism == (int)malfunctioning)
{
current_state= RAISING_GEAR;
printf("\n Current state: %s\n", state_machine[current_state].current_state_status);
printf("light status: %s\n", state_machine[current_state].light_status);
printf("\n direction status:%s\n\n",state_machine[current_state].direction_value_status);
printf("\n Gas pressurised spring status:%s\n",state_machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]);
}
}
void Raising_gear()
{
current_state= RAISING_GEAR;
printf("\n Enter the Switch_status and pilot_lever status:\n");
fflush(stdout); //Lever:rising(0),falling(1),up(2),down(3)
scanf("%d %d", (int*)&limit_switch, (int*)&pilot_lever); //switch:open(0),close(1) Hydraulic:workinh(0),not-working(1)
printf("\n wait for 1 sec\n");
if(pilot_lever == falling && limit_switch == open)
{
current_state=CHECK_BEFORE_LANDING;
printf("\n current state is:%s\n", state_machine[current_state].current_state_status);
printf("\n light status:%s",state_machine[current_state].light_status);
printf("\n Hyadraulic condition for landing gear status is%s\n",state_machine[current_state].hydraulic_pump_ctrl_status);
}
else if(limit_switch == close && pilot_lever == up)
{
current_state= GEAR_UP;
printf("current_state is : %s\n",state_machine[current_state].current_state_status);
printf("light status %s\n",state_machine[current_state].light_status);
printf("\n Hyadraulic condition for landing rear status:%s",state_machine[current_state].hydraulic_pump_ctrl_status);
}
else
{
Raising_gear();
}
}
void Gear_up() //gear up//
{
current_state= GEAR_UP;
printf("\n Enter the pilot lever position :\n");
fflush(stdout); //Lever:rising(0),falling(1),up(2),down(3)
scanf("%d", (int*)&pilot_lever); //switch:open(0),close(1) Hydraulic:workinh(0),not-working(1)
if (pilot_lever == falling)
{
current_state= CHECK_BEFORE_LANDING;
printf("\n current state is:%s\n", state_machine[current_state].current_state_status);
printf("\n light status:%s",state_machine[current_state].light_status);
printf("\n Hyadraulic condition for landing gear status is%s\n",state_machine[current_state].hydraulic_pump_ctrl_status);
}
else
{
Gear_up();
}
}
void Checking_before_landing() // Checking_before_landing//
{
current_state= CHECK_BEFORE_LANDING;
printf("\n Enter the pilot lever position, status of hydraulic_mechanism_status :\n");
fflush(stdout);
scanf("%d %d",(int*)&pilot_lever, (int*)&hydraulic_mechanism); //switch:open(0),close(1) Hydraulic:workinh(0),not-working(1)
if(pilot_lever == down && hydraulic_mechanism == (int) working)
{
current_state= LOWERING_GEAR;
printf("\n Current state: %s\n",state_machine[current_state].current_state_status);
printf("light status: %s\n", state_machine[current_state].light_status);
printf("\n direction status:%s\n\n",state_machine[current_state].direction_value_status);
}
else if ( pilot_lever == down && hydraulic_mechanism == (int)malfunctioning)
{
current_state= LOWERING_GEAR;
printf("\n Current state: %s\n",state_machine[current_state].current_state_status);
printf("light status: %s\n", state_machine[current_state].light_status);
printf("\n direction status:%s\n\n",state_machine[current_state].direction_value_status);
}
if(pilot_lever == (up || raising || falling))
{
Checking_before_landing();
}
}
void Lowering_gear() // Lowering_gear//
{
current_state= LOWERING_GEAR;
printf("\n Enter the limit_switch:\n");
fflush(stdout); //Lever:rising(0),falling(1),up(2),down(3)
scanf("%d", (int*)&limit_switch); //switch:open(0),close(1) Hydraulic:workinh(0),not-working(1)
if( limit_switch == close )
{
current_state= GEAR_DOWN;
printf("light status: %s\n", state_machine[current_state].light_status);
}
}
//main function//
int main()
{
Initial_state_machine();
while(1)
{
State_Table[current_state]();
}
}