Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Aim:
To write a C Program by using FSM method for controlling the Flight landing gear, with the reference of various switch inputs.
State Table:
|
Current State |
Squat switch |
Limit switch |
Pilot Lever
|
LED |
Direction valve |
Hydraulic pump |
Next state |
|
Gear Down |
1 |
0 |
1 |
Green |
Down |
Enable |
Checking before take off |
|
Checking before take off |
1 |
0 |
1 |
Green |
Down |
Enable |
Raising gear |
|
Raising gear |
0 |
0 |
1 |
Red |
Up |
Enable |
Gear Up |
|
Gear Up |
0 |
1 |
0 |
OFF |
OFF |
Disable |
Checking before landing |
|
Checking before landing |
0 |
1 |
0 |
Red |
Down |
Enable |
Lowering gear |
|
Lowering gear |
0 |
0 |
0 |
Red |
Down |
Enable |
Gear Down |
Program:
#include
#include
#include
enum limit_switch //Switch controls declared as Enumeration data type
{
Close=1,
Open=0
}ss,ls; // ss-Squad switch. ls-limit switch
enum pilot_lever //pilot lever inputs are declared
{
Raising=1,
Falling=0
}pilot_lever;
enum states //FSM states are declared in enum data type
{
Gear_down,
Check_before_takeoff,
Raising_gear,
Gear_up,
Check_before_landing,
Lowering_gear
}current_state;
struct state_machine //FSM state table outputs
{
char *state_name;
char *LED_light;
char *direction_valve;
char *Hyd_pump;
};
struct state_machine states[6] = //the struct elements are declared for each finite states
{
{"Gear_down","Green","Down","Enabled"},
{"Check_before_takeoff","Green","Down","Enabled"},
{"Raising_gear","Red","Up","Enabled"},
{"Gear_up","OFF","OFF","Disabled"},
{"Check_before_landing","Red","Down","Enabled"},
{"Lowering_gear","Red","Down","Enabled"}
};
//declaration of each state functions
void gear_down(); //Gear in down position flight is landing
void check_before_takeoff();//Checking for flight tack off delay
void raising_gear(); //Gear raising flight to be takeoff
void gear_up(); //Gear is up Flight is airbone
void check_before_landing();//Check for landing delay
void lowering_gear(); //lowering gear flight to be landing
int main()
{
Init_State_Machine(); //starting function of FSM
gear_down(); //calling the default state of FSM
}
void Init_State_Machine() //Initializing finite state machine
{
current_state = Gear_down; // printing the status of FSM
printf("\n***-----Welcome to this FLIGHT------ ***\n");
printf("\nThe current state is : %s ",states[current_state].state_name);
printf("\nLED light status : %s",states[current_state].LED_light);
printf("\nDirection valve position :%s",states[current_state].direction_valve);
printf("\nHydrolic pump condition :%s",states[current_state].Hyd_pump);
}
void gear_down() //1.Gear down state, checking the related outputs
{
current_state = Gear_down; //State 1
printf("\nEnter pilot lever Position\t0 - Falling\t1 - Raising :");
fflush(stdout);
scanf("%d",&pilot_lever); // Getting the input of pilot lever position
printf("\nEnter squat switch Position\t0 - Open\t1 - Close :");
fflush(stdout);
scanf("%d",&ss); // Getting the input of squat switch position
if(pilot_lever && ss) //Next state selection
{
current_state = Check_before_takeoff; //State 2 status printing
printf("\nThe current state is : %s ",states[current_state].state_name);
printf("\nLED light status : %s",states[current_state].LED_light);
printf("\nDirection valve position :%s",states[current_state].direction_valve);
printf("\nHydrolic pump condition :%s",states[current_state].Hyd_pump);
check_before_takeoff();
}
else
{
printf("***Process returned to starting state***");
gear_down();
}
}
//FSM in checking before takeoff state and checking the inputs to move to next state
void check_before_takeoff() //2nd state
{
current_state = Check_before_takeoff;
printf("\nEnter pilot lever position\t0 - Falling\t1 - Raising :");
fflush(stdout);
scanf("%d",&pilot_lever); // Getting the input of pilot lever position
printf("\nEnter squat switch position\t0 - Open\t1 - Close :");
fflush(stdout);
scanf("%d",&ss); // Getting the input of squat switch position
if(pilot_lever && ss) //Next state selection
{
current_state = Raising_gear; //State 4 status
printf("\nThe current state is :%s ",states[current_state].state_name);
printf("\nLED light status :%s",states[current_state].LED_light);
printf("\nDirection valve is :%s",states[current_state].direction_valve);
printf("\nHydrolic pump condition :%s",states[current_state].Hyd_pump);
raising_gear();
}
else
{
printf("***Process returned to previous state Check before take off***");
check_before_takeoff();
}
}
void raising_gear() //3rd Raising gear state data print and state change
{
current_state = Raising_gear;
printf("\nEnter pilot lever condition\t0 - Falling\t1 - Raising :");
fflush(stdout);
scanf("%d",&pilot_lever);// Getting the input of pilot lever position
printf("\nEnter limit switch condition\t0 - Open\t1 - Close :");
fflush(stdout);
scanf("%d",&ls); // Getting the input of limit switch position
if(pilot_lever==0)
{
current_state = Check_before_landing; //State 5
printf("\nThe current state is :%s ",states[current_state].state_name);
printf("\nLED light status : %s",states[current_state].LED_light);
printf("\nDirection valve is :%s",states[current_state].direction_valve);
printf("\nHydrolic pump condition :%s",states[current_state].Hyd_pump);
check_before_landing();
}
else if(ls==0)
{
current_state = Gear_up;
printf("\nThe current state is :%s ",states[current_state].state_name);
printf("\nLED light status :%s",states[current_state].LED_light);
printf("\nDirection valve is :%s",states[current_state].direction_valve);
printf("\nHyd_Pump status is : %s",states[current_state].Hyd_pump);
gear_up();
}
else
{
printf("***Process returned to previous state raising_gear***");
raising_gear();
}
}
void gear_up() //4th Gear up state output printing and next state change
{
current_state = Gear_up;
printf("\n***Now Take off and Gear up done Flight is on sky***");
printf("\nEnter pilot lever condition\t0 - Falling\t1 - Raising :");
fflush(stdout);
scanf("%d",&pilot_lever);// Getting the input of pilot lever position
if(pilot_lever==0)
{
current_state = Check_before_landing;
printf("\nThe current state is :%s ",states[current_state].state_name);
printf("\nLED light status : %s",states[current_state].LED_light);
printf("\nDirection valve is :%s",states[current_state].direction_valve);
printf("\nHyd_Pump status is : %s",states[current_state].Hyd_pump);
check_before_landing();
}
else
printf("***Process returned to previous state Gear_Up***");
gear_up();
}
void check_before_landing() //5th Checking for landing state printing and state change
{
current_state = Check_before_landing;
printf("\nEnter pilot lever condition\t0 - Falling\t1 - Raising :");
fflush(stdout);
scanf("%d",&pilot_lever); // Getting the input of pilot lever position
if(pilot_lever==0)
{
current_state = Lowering_gear;
printf("\nThe current state is :%s ",states[current_state].state_name);
printf("\nLED light status :%s",states[current_state].LED_light);
printf("\nDirection valve is :%s",states[current_state].direction_valve);
printf("\nHyd_Pump status is : %s",states[current_state].Hyd_pump);
lowering_gear();
}
else
printf("***Process returned to previous state check_before_landing***");
check_before_landing();
}
void lowering_gear() // 6th lowering gear state
{
current_state = Lowering_gear;
printf("\nEnter pilot lever condition\t0 - Falling\t1 - Raising :");
fflush(stdout);
scanf("%d",&pilot_lever); // Getting the input of pilot lever position
printf("\nEnter limit switch condition\t0 - Open\t1 - Close :");
fflush(stdout);
scanf("%d",&ls); // Getting the input of Limit switch position
if(pilot_lever && ls)
{
current_state = Check_before_takeoff;
printf("\nThe current state is :%s",states[current_state].state_name);
fflush(stdout);
printf("\nLED light status :%s",states[current_state].LED_light);
check_before_takeoff();
}
else
{
current_state = Gear_down;
printf("\nThe current state is :%s ",states[current_state].state_name);
printf("\nLED light status :%s",states[current_state].LED_light);
printf("\nDirection valve is :%s",states[current_state].direction_valve);
printf("\nHyd_Pump status is : %s",states[current_state].Hyd_pump);
gear_down();
}
}
Output:
***-----Welcome to this FLIGHT------ ***
The current state is : Gear_down
LED light status : Green
Direction valve position :Down
Hydrolic pump condition :Enabled
Enter pilot lever Position 0 - Falling 1 - Raising :1
Enter squat switch Position 0 - Open 1 - Close :1
The current state is : Check_before_takeoff
LED light status : Green
Direction valve position :Down
Hydrolic pump condition :Enabled
Enter pilot lever position 0 - Falling 1 - Raising :1
Enter squat switch position 0 - Open 1 - Close :1
The current state is :Raising_gear
LED light status :Red
Direction valve is :Up
Hydrolic pump condition :Enabled
Enter pilot lever condition 0 - Falling 1 - Raising :1
Enter limit switch condition 0 - Open 1 - Close :0
The current state is :Gear_up
LED light status :OFF
Direction valve is :OFF
Hyd_Pump status is : Disabled
***Now Take off and Gear up done Flight is on sky***
Enter pilot lever condition 0 - Falling 1 - Raising :0
The current state is :Check_before_landing
LED light status : Red
Direction valve is :Down
Hyd_Pump status is : Enabled
Enter pilot lever condition 0 - Falling 1 - Raising :0
The current state is :Lowering_gear
LED light status :Red
Direction valve is :Down
Hyd_Pump status is : Enabled
Enter pilot lever condition 0 - Falling 1 - Raising :0
Enter limit switch condition 0 - Open 1 - Close :0
The current state is :Gear_down
LED light status :Green
Direction valve is :Down
Hyd_Pump status is : Enabled
Enter pilot lever Position 0 - Falling 1 - Raising :