Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Implementation of control for the retraction and extension of Airplane’s landing gear using Finite state machine:
#include<stdio.h> //Preprocessor
#include<string.h> //Preprocessor
typedef enum Airplane_States //Enumerated data type declaration and variables
{
GEAR_DOWN, CHECKING_BEFORE_TAKEOFF, RAISING_GEAR, GEAR_UP, CHECKING_BEFORE_LANDING, LOWERING_GEAR
} States; //Alias name to enum Airplane_States
typedef struct attributes //Structure Declaration
{
char* Current_state[2]; //member
char* direction_valve[2]; //member
char* gear_light[2]; //member
States updator[2]; //member
char* next_state[2]; //member
} Airplane; //Alias name for struct attributes
//Array of Structure for the 6 input states
Airplane State_table[6]={
{{"GEAR_DOWN","GEAR_DOWN"},{"Down","Up"},{"Green","Green"},
{GEAR_DOWN,CHECKING_BEFORE_TAKEOFF},{"GEAR_DOWN","CHECKING_BEFORE_TAKEOFF"}}, //FOR GEAR_DOWN STATE
{{"CHECKING_BEFORE_TAKEOFF","CHECKING_BEFORE_TAKEOFF"},{"Down","Up"},{"Red","Red"},
{GEAR_DOWN,RAISING_GEAR},{"GEAR_DOWN","RAISING_GEAR"}}, //FOR CHECKING_BEFORE_TAKEOFF STATE
{{"RAISING_GEAR","RAISING_GEAR"},{"Down","Control for Landing is Disabled"},{"Red","Off"},
{CHECKING_BEFORE_LANDING,GEAR_UP},{"CHECKING_BEFORE_LANDING","GEAR_UP"}}, //FOR RAISING_GEAR STATE
{{"GEAR_UP","GEAR_UP"},{"Control for Landing is disabled","Control for Landing is enabled"},{"Off","Red"},
{GEAR_UP,CHECKING_BEFORE_LANDING},{"GEAR_UP","CHECKING_BEFORE_LANDING"}}, //FOR GEAR_UP STATE
{{"CHECKING_BEFORE_LANDING","CHECKING_BEFORE_LANDING"},{"Control for Landing is enabled","Down"},
{"Red","Red"},{CHECKING_BEFORE_LANDING,LOWERING_GEAR},{"CHECKING_BEFORE_LANDING","LOWERING_GEAR"}}, //FOR CHECKING_BEFORE_LANDING STATE
{{"LOWERING_GEAR","LOWERING_GEAR"},{"Down","Up"},
{"Green","Red"},{GEAR_DOWN,CHECKING_BEFORE_TAKEOFF},{"GEAR_DOWN","CHECKING_BEFORE_TAKEOFF"}}
}; //For LOWERING_GEAR STATE
static volatile int current_state,control_input; //Global Variable Declaration
int main() //Main Function
{ //Open loop
char limit_switch[7],squat_switch[7],pilot_lever[7]; //Variable Declaration
printf("\t***Welcome to RAM AIRPLANES***\n"); //Prints output in console
printf("Initially Plane is in GEAR_DOWN state\n"); //Prints output in console
current_state = GEAR_DOWN; //Variable Initialization
while(1) //While loop to run continuously
{
switch(current_state) //Switch case
{
case 0: //FOR GEAR_DOWN STATE
{
printf("Enter the input of Limit-Switch: select from OPEN or CLOSE\n"); //Prints output in console
fflush(stdout); //Clears buffer
scanf("%s",limit_switch); //Reads input from console
printf("Enter the input of Squat-Switch: select from OPEN or CLOSE\n"); //Prints output in console
fflush(stdout); //Clears buffer
scanf("%s",squat_switch); //Reads input from console
printf("Enter the input of Pilot_Lever: select from RISING OR FALLING\n"); //Prints output in console
fflush(stdout); //Clears buffer
scanf("%s",pilot_lever); //Reads input from console
if(((!strcmp(limit_switch,"CLOSE")) && (!strcmp("OPEN",squat_switch)) && (!strcmp("RISING",pilot_lever))))
control_input=1;
else
control_input=0;
break;
}
case 1: //FOR CHECKING_BEFORE_TAKEOFF STATE
{
printf("Limit-Switch is in CLOSE Position and if you want to change it, choose from STAY or OPEN\n"); //Prints output in console
fflush(stdout); //Clears buffer
scanf("%s",limit_switch); //Reads input from console
printf("Squat-Switch is in OPEN Position and if you want to change it, choose from STAY OR CLOSE\n"); //Prints output in console
fflush(stdout); //Clears buffer
scanf("%s",squat_switch); //Reads input from console
printf("Pilot-Lever is in RISING Position and if you want change it, choose from FALLING or UP\n"); //Prints output in console
fflush(stdout); //Clears buffer
scanf("%s",pilot_lever); //Reads input from console
if((!strcmp("STAY",limit_switch)) && (!strcmp("STAY",squat_switch)) && (!strcmp("UP",pilot_lever)))
control_input=1;
else
control_input=0;
break;
}
case 2: //FOR RAISING_GEAR STATE
{
printf("Limit-switch is in CLOSE position"); //Prints output in console
printf("\nSquat-switch is in OPEN position"); //Prints output in console
printf("\nPilot-Lever is in UP Position and if you want change it, choose from FALLING or CONTINUE\n"); //Prints output in console
fflush(stdout); //Clears buffer
scanf("%s",pilot_lever); //Reads input from console
if(!strcmp("CONTINUE",pilot_lever))
control_input=1;
else
control_input=0;
break;
}
case 3: //FOR GEAR_UP STATE
{
printf("Limit-switch is in CLOSE position"); //Prints output in console
printf("\nSquat-switch is in OPEN position\n"); //Prints output in console
printf("Now the Control for LANDING Gear is Disabled\n"); //Prints output in console
printf("PLANE is in Air Completely"); //Prints output in console
printf("\nPilot-Lever is in UP Position and if you want change it, choose from FALLING or CONTINUE\n"); //Prints output in console
fflush(stdout); //Clears buffer
scanf("%s",pilot_lever); //Reads input from console
if(!strcmp(pilot_lever,"FALLING"))
control_input=1;
else
{
printf("PLANE is Still in Air Completely\n"); //Prints output in console
control_input=0;
}
break;
}
case 4: //FOR CHECKING_BEFORE_LANDING STATE
{
printf("Limit-switch is in CLOSE position"); //Prints output in console
printf("\nSquat-switch is in OPEN position\n"); //Prints output in console
printf("Now the Control for LANDING Gear is Enabled\n"); //Prints output in console
printf("Pilot-Lever is in FALLING Position and if you want change it, choose from DOWN OR NOT\n"); //Prints output in console
fflush(stdout); //Clears buffer
scanf("%s",pilot_lever); //Reads input from console
if(!strcmp("DOWN",pilot_lever))
control_input=1;
else
control_input=0;
break;
}
case 5: //For LOWERING_GEAR STATE
{
printf("Limit-Switch is in CLOSE Position and if you want to change it, choose from STAY or OPEN\n"); //Prints output in console
fflush(stdout); //Clears buffer
scanf("%s",limit_switch); //Reads input from console
printf("Squat-Switchis in OPEN Position and if you want to change it, choose from STAY OR CLOSE\n"); //Prints output in console
fflush(stdout); //Clears buffer
scanf("%s",squat_switch); //Reads input from console
printf("Pilot-Lever is in FALLING Position and if you want to change it, choose from RISING OR DOWN\n"); //Prints output in console
fflush(stdout); //Clears buffer
scanf("%s",pilot_lever); //Reads input from console
if(((!strcmp(limit_switch,"STAY")) && (!strcmp(squat_switch,"STAY")) && (!strcmp(pilot_lever,"RISING"))))
control_input=1;
else
{
control_input=0;
printf("\t-->Plane Landed Successfully<--\n"); //Prints output in console
}
break;
}
}
printf("Current State is %s\n",State_table[current_state].Current_state[control_input]); //Prints output in console
printf("Status of Direction Valve: %s\n",State_table[current_state].direction_valve[control_input]); //Prints output in console
printf("Status of Gear Light: %s\n",State_table[current_state].gear_light[control_input]); //Prints output in console
printf("State to be entered next is %s\n",State_table[current_state].next_state[control_input]); //Prints output in console
current_state=State_table[current_state].updator[control_input]; //Updates Current state value
printf("\t\t**Moved to next state**\n"); //Prints output in console
fflush(stdout); //Clears buffer
}
} //Close loop