Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear by Nigesh M
Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear using FSM:
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.
#include<stdio.h>
//functions declared for each states to perform operations using void because function does not return a value.
void geardown();
void checkbfrtakeoff();
void raisinggear();
void gearup();
void checkbfrlanding();
void loweringgear();
//declaring the states in elevator operation using typedef
typedef enum //enum provide an alias name for state type
{
gear_down,
gear_chk_bfr_takeoff,
gear_raising,
gear_up,
gear_chk_bfr_landing,
gear_lowering,
}state_type;
typedef enum
{
open,
close
}Switch;
typedef enum
{
Geardown,
Raisegear,
Gearup,
Lowergear,
}pilot_lever;
typedef enum
{
working,
malfunction
}hydraulics_mechanism;
state_type current_state;
//define the current state of the elevator using user defined data type named state type
typedef struct
//Each element represents a state of a state machine, with various properties associated with that state.
{
char* current_state_indication;
char* LED_Indication;
char* direction_valve;
char* hydraulics_pumb_status;
char* gas_pressured_spring_status[2];
}state_table;
//structures is initialized with values for each of its properties, using a combination of string literals and array literals
state_table state_machine[6]=
{
{"gear_down", "Green", "down","enabled",{0,0}},
{"check_before_takeoff", "Green", "down","enabled",{0,0}},
{"raising_gear","Red","up", "enabled",{"enabled","displayed"}},
{"gear_up","off","Null", "displayed",{0,0}},
{"check_before_landing", "Red", "down","enabled",{0,0}},
{"lowering_gear", "Green", "down", "enabled",{"enabled","displayed"}}
};
//initializing table function to call different functions
void (*table[])()={geardown,checkbfrtakeoff,raisinggear,gearup,checkbfrlanding,loweringgear};
//variable declaration for enum type
pilot_lever lever; //pilot lever position
Switch ls; //limit switch
Switch ss; //squat switch
hydraulics_mechanism hydraulic; //hydraulic control
int main() // the current state has finished executing, the state machine transitions to a new state based on the system inputs
{
current_state=gear_down; //represents the initial state of the system being modeled by the state machine.
while(1) // infinite loop,the program will continue to run indefinitely.
{
table[current_state](); //calling the table function
}
}
void geardown()
{
current_state=gear_down;
printf("\nEnter pilot lever position & squat switch position");
fflush(stdout);
scanf("%u %u",&lever,&ss);
if((lever==Raisegear)&&(ss==open))
{
current_state=gear_chk_bfr_takeoff;
printf("\n current state is :%s",state_machine[current_state].current_state_indication);
printf("\n light status is :%s",state_machine[current_state].LED_Indication);
}
else
{
geardown();
}
}
void checkbfrtakeoff()
{
current_state=gear_chk_bfr_takeoff;
printf("\nEnter pilot lever position , squat switch & hydraulic mechanism position");
fflush(stdout);
scanf("%u %u %u",&lever,&ss,&hydraulic);
printf("\n Delay for 4 seconds");
if((lever==Lowergear)||(ss==close))
{
current_state=gear_down;
printf("\n Current state is %s",state_machine[current_state].current_state_indication);
printf("\n Light status is :%s",state_machine[current_state].LED_Indication);
printf("\n Directional valve status is:%s",state_machine[current_state].direction_valve);
}
else if((lever==Gearup)&&(ss==open)&&(hydraulic==working))
{
current_state=gear_raising;
printf("\n Current state is %s",state_machine[current_state].current_state_indication);
printf("\n Light status is :%s",state_machine[current_state].LED_Indication);
printf("\n Directional valve status is:%s",state_machine[current_state].direction_valve);
}
else if((lever==Gearup)&&(ss==open)&&(hydraulic==malfunction))
{
current_state=gear_raising;
printf("\n Current state is %s",state_machine[current_state].current_state_indication);
printf("\n Light status is :%s",state_machine[current_state].LED_Indication);
printf("\n Directional valve status is:%s",state_machine[current_state].direction_valve);
}
}
void raisinggear()
{
current_state=gear_raising;
printf("\nEnter pilot lever position & limit switch position");
fflush(stdout);
scanf("%u %u",&lever,&ls);
if((lever==Gearup)&&(ls==close))
{
current_state=gear_up;
printf("\n Current state is %s",state_machine[current_state].current_state_indication);
printf("\n Light status is :%s",state_machine[current_state].LED_Indication);
printf("\n Directional valve status is:%s",state_machine[current_state].direction_valve);
}
else if((lever==Lowergear)&&(ls==open))
{
current_state=gear_chk_bfr_landing;
printf("\n Current state is %s",state_machine[current_state].current_state_indication);
printf("\n Light status is :%s",state_machine[current_state].LED_Indication);
printf("\n Directional valve status is:%s",state_machine[current_state].direction_valve);
}
else
{
raisinggear();
}
}
void gearup()
{
current_state=gear_up;
printf("\nEnter pilot lever position ");
fflush(stdout);
scanf("%u",&lever);
if(lever==Lowergear)
{
current_state=gear_chk_bfr_landing;
printf("\n Current state is %s",state_machine[current_state].current_state_indication);
printf("\n Light status is :%s",state_machine[current_state].LED_Indication);
printf("\n Directional valve status is:%s",state_machine[current_state].direction_valve);
}
else
{
gearup();
}
}
void checkbfrlanding()
{
{
current_state=gear_chk_bfr_landing;
printf("\nEnter pilot lever position , hydraulic mechanism position");
fflush(stdout);
scanf("%u %u",&lever,&hydraulic);
printf("delay for 4 seconds");
if((lever== Geardown)&&(hydraulic==working))
{
current_state=gear_lowering;
printf("\n Current state is %s",state_machine[current_state].current_state_indication);
printf("\n Light status is :%s",state_machine[current_state].LED_Indication);
printf("\n Directional valve status is:%s",state_machine[current_state].direction_valve);
}
else if((lever==Geardown)&&(hydraulic==malfunction))
{
current_state=gear_lowering;
printf("\n Current state is %s",state_machine[current_state].current_state_indication);
printf("\n Light status is :%s",state_machine[current_state].LED_Indication);
printf("\n Directional valve status is:%s",state_machine[current_state].direction_valve);
printf("\n Gas pressure valve status is:%s",state_machine[current_state].gas_pressured_spring_status[hydraulic]);
}
else if((lever==Gearup||lever==Raisegear)&&(lever==Lowergear))
{
void checkbfrlanding();
}
}
}
void loweringgear()
{
current_state=gear_lowering;
printf("\nEnter pilot lever position & limit switch position");
fflush(stdout);
scanf("%u %u",&lever,&ls);
if((lever== Raisegear)&&(ls==close))
{
current_state=gear_chk_bfr_takeoff;
printf("\n Current state is %s",state_machine[current_state].current_state_indication);
printf("\n Light status is :%s",state_machine[current_state].LED_Indication);
printf("\n Directional valve status is:%s",state_machine[current_state].direction_valve);
}
else if((lever==Geardown)&&(ls==open))
{
current_state=gear_down;
printf("\n Current state is %s",state_machine[current_state].current_state_indication);
printf("\n Light status is :%s",state_machine[current_state].LED_Indication);
printf("\n Directional valve status is:%s",state_machine[current_state].direction_valve);
}
else
{
loweringgear();
}
}
OUTPUT:

Contine...
