Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
/*
* project2.c
*
* Created on: 06-Aug-2023
* Author: RAJASEKAR R
* Description: 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.*/
STATE TABLE:
| valve_direction | gear_state | light_status | hydrallic_status | squat_status | |
| Gear_down | down | down | green | enable | close |
| check_before_take_off | down | down | green | enable | close |
| raising_gear | up | raising | red | enable |
open |
| gear_up | Null | up | off | disable | open |
| check_before_landing | down | lowering | red | enable | close |
| lowering_gear | down | down | green | enable | close |
SOURCE CODE:
// preprocesor defined
#include
// function prototype
void Gear_down(void);
void Checking_before_takeoff(void);
void Raising_gear(void);
void Gear_up(void);
void Checking_before_landing(void);
void Lowering_gear(void);
//state transition defined
typedef enum
{
Gr_dwn,
Chk_bfe_tkoff,
Rsg_gr,
Gr_up,
Chk_bfe_land,
Lwrg_gr
}state_type;
//limit switch states declared
typedef enum
{
Open = 0,
Close = 1
}limit_switch_status;
// squat switch states declared
typedef enum
{
Open_ = 0,
Close_ = 1
}squat_switch_status;
//pilotlever state transition defined
typedef enum
{
Raising = 0,
Falling = 1,
Up = 2,
Down = 3
}pilot_lever;
// hydrallic control declared
typedef enum
{
Working = 0,
Not_working = 1
}hydrallic_state;
// state table attributes
typedef struct
{
char *currentstate;
char *light_status;
char *direction_valve_status;
char *hydrallic_status;
}state;
// global variable declared values are not fixec(constant)
static volatile hydrallic_state hs;
static volatile pilot_lever pl;
static volatile squat_switch_status sss;
static volatile limit_switch_status lss;
static volatile current_state;
// array of function pointer to function ,Gear_down, Checking_before_takeoff, Raising_gear, Gear_up, Checking_before_landing, Lowering_gear
static void( *state_table [])(void) = { Gear_down, Checking_before_takeoff, Raising_gear, Gear_up, Checking_before_landing, Lowering_gear };
// state table formation
static state state_machine[6]={
{"Gear_down","Green","Down","Enabled"},
{"Checking_before_takeoff","Green","Down","Enabled"},
{"Raising_gear","Red","Up","Enabled"},
{"Gear_up","off","Null","Disabled"},
{"Checking_before_landing","Red","Down","Enabled"},
{"Lowering_gear","Green","Down","Enabled"}
};
int main()
{
// For reference the following values are given by the user for control process
printf("\n Pilot_lever_values"
"\n Raising = 0"
"\n Falling = 1"
"\n Up = 2"
"\n Down = 3");
printf("\n");
printf("\n limit_switch,squat_switch values"
"\n open = 0"
"\n close = 1");
printf("\n");
printf("\n hydrallic_state values"
"\n working = 0"
"\n not_working = 1");
printf("\n");
current_state = Gr_dwn ;
while(1) // while loop it run the pointer function.
{
state_table[current_state](); // function call
}
return 0;
}
Gear_down() // function for the gear down operation
{
// function body
// initial stage
current_state = Gr_dwn;
// values are get by the user (control values).
printf("\nEnter the Value of Pilot lever and Squat_Switch:");
// console buffer
setbuf(stdout,NULL);
// values are stored in the global variable
scanf("%d %d",&pl,&sss);
if((pl == Raising)&&(sss == Close_)) // condition check pilot lever and squat switch both result are true the following function execute or move to else part
{
current_state = Chk_bfe_tkoff ; // move to next stage
// display the output
printf("\n check the all feedback before take off");
printf("\nCurrent state: %s",state_machine[current_state].currentstate);
printf("\nDirection_valve Status: %s",state_machine[current_state].direction_valve_status);
printf("\nHydraulic_control Status: %s",state_machine[current_state].hydrallic_status);
printf("\nLight Status: %s",state_machine[current_state].light_status);
printf("\n");
}
else
{
Gear_down(); // function call
}
}
// function for checking before take off
Checking_before_takeoff()
{
current_state = Chk_bfe_tkoff;
// values are get by user for control
printf("\nEnter the Pilot lever, Squat_Switch and Hydraulic_mechanism Values:");
// console buffer clear
setbuf(stdout,NULL);
// values are stored in the global variable
scanf("%d %d %d",&pl,&sss,&hs);
if((pl==Falling)&&(sss==Open_))// condition check pilot lever and squat switch both result are true the following function execute
{
current_state = Gr_dwn;
// display the output
printf("\n check the all feedback before take off");
printf("\nCurrent state: %s",state_machine[current_state].currentstate);
printf("\nDirection_valve Status: %s",state_machine[current_state].direction_valve_status);
printf("\nHydraulic_control Status: %s",state_machine[current_state].hydrallic_status);
printf("\nLight Status: %s",state_machine[current_state].light_status);
printf("\n");
}
else if((pl==Up)&&(hs==Working)) // if part condition fails else if part condition check pilot lever and hydrallic status both true the condition executes
{
current_state = Rsg_gr;
// display the output
printf("\n check the all feedback before take off");
printf("\nCurrent state: %s",state_machine[current_state].currentstate);
printf("\nDirection_valve Status: %s",state_machine[current_state].direction_valve_status);
printf("\nHydraulic_control Status: %s",state_machine[current_state].hydrallic_status);
printf("\nLight Status: %s",state_machine[current_state].light_status);
printf("\n");
}
else // both if and else if fails else executes
{
Checking_before_takeoff(); // function call
}
}
//function for raising gear control
Raising_gear()
{
current_state = Rsg_gr;
// the values are het by user
printf("\nEnter the Pilot lever,Limit_Switch Values:");
setbuf(stdout,NULL); // console buffer clear
scanf("%d %d",&pl,&lss);// values stored in the global variable
if(pl==Falling) //condition check if its true the condition executes
{
current_state = Chk_bfe_land;
// display
printf("\n check the all feedback plane is check before landing");
printf("\nCurrent state: %s",state_machine[current_state].currentstate);
printf("\nDirection_valve Status: %s",state_machine[current_state].direction_valve_status);
printf("\nHydraulic_control Status: %s",state_machine[current_state].hydrallic_status);
printf("\nLight Status: %s",state_machine[current_state].light_status);
printf("\n");
}
else if(lss==Open) // condition check if condition fails else if condition check if its true the condition executes
{
current_state = Gr_up;
// display
printf("\n check the all feedback plane is taking off");
printf("\nCurrent state: %s",state_machine[current_state].currentstate);
printf("\nDirection_valve Status: %s",state_machine[current_state].direction_valve_status);
printf("\nHydraulic_control Status: %s",state_machine[current_state].hydrallic_status);
printf("\nLight Status: %s",state_machine[current_state].light_status);
printf("\n");
}
else // both if and else if fails else executes
{
Raising_gear(); // function call
}
}
// function for gear upo control
Gear_up()
{
current_state = Gr_up;
// values are get by the user
printf("\nEnter the Pilot lever Values:");
fflush(stdout); // console buffer clear
scanf("%d",&pl); // global variable stores the values
if(pl==Falling) // condition check if condition fails else if condition check if its true the condition executes or move else part
{
current_state = Chk_bfe_land;
// display
printf("\n check the all feedback before landing");
printf("\nCurrent state: %s",state_machine[current_state].currentstate);
printf("\nDirection_valve Status: %s",state_machine[current_state].direction_valve_status);
printf("\nHydraulic_control Status: %s",state_machine[current_state].hydrallic_status);
printf("\nLight Status: %s",state_machine[current_state].light_status);
printf("\n");
}
else
{
Gear_up(); // function call
}
}
// function for checking before landing control
Checking_before_landing()
{
current_state = Chk_bfe_land;
// control values are get by the user
printf("\nEnter the Pilot lever and Hydraulic_mechanism Values:");
fflush(stdout);// console buffer clear
scanf("%d%d",&pl,&hs); // value stored in global variable
// condition check if its true the condition executes or move else part
if((pl==Down)||(hs == Not_working))
{
current_state = Lwrg_gr;
printf("\n check the all feedback before landing");
printf("\nCurrent state: %s",state_machine[current_state].currentstate);
printf("\nDirection_valve Status: %s",state_machine[current_state].direction_valve_status);
printf("\nHydraulic_control Status: %s",state_machine[current_state].hydrallic_status);
printf("\nLight Status: %s",state_machine[current_state].light_status);
printf("\n");
}
else
{
Checking_before_landing(); // function call
}
}
// function for lowering gear
Lowering_gear()
{
current_state = Lwrg_gr;
// values get by the user
printf("\nEnter the Pilot lever and Limit Switch Values:");
fflush(stdout);// console buffer clear
scanf("%d%d",&pl,&lss); // value stores inthe global variable
// condition check if its true the condition executes or move elseif part
if((lss == Close))
{
current_state = Gr_dwn;
// display
printf("\n check the all feedback before plan landing");
printf("\nCurrent state: %s",state_machine[current_state].currentstate);
printf("\nDirection_valve Status: %s",state_machine[current_state].direction_valve_status);
printf("\nHydraulic_control Status: %s",state_machine[current_state].hydrallic_status);
printf("\nLight Status: %s",state_machine[current_state].light_status);
printf("\n");
}
// condition check if its true the condition executes or move else part
else if(pl==Raising)
{
current_state = Chk_bfe_tkoff;
printf("\ncheck the all feedback before take off");
printf("\nCurrent state: %s",state_machine[current_state].currentstate);
printf("\nDirection_valve Status: %s",state_machine[current_state].direction_valve_status);
printf("\nHydraulic_control Status: %s",state_machine[current_state].hydrallic_status);
printf("\nLight Status: %s",state_machine[current_state].light_status);
printf("\n");
}
else
{
Lowering_gear(); // function call
}
}
OUTPUT:

