Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Based on the detailed study of the project following components are considered as inputs from the pilot
- Pilot lever handle input
- Limit switch input
- Squat switch input
Based on the inputs given Next states are derived and respective outputs are displayed in the console
Outputs displayed on the console
- statues of the current state
- statues of the value direction
- statues of the lights
- statues of the gear
Following table describes the inputs should be given for respective next state
|
Present state |
Pilot lever handle input |
limit switch input |
squat switch input |
Next state |
|
GEAR_DOWN |
0 |
0 |
0 |
CHECKING_BEFORE_TAKE_OFF |
|
CHECKING_BEFORE_TAKE_OFF |
1 |
0 |
0 |
RAISING_GEAR |
|
2 |
0 |
1 |
GEAR_DOWN |
|
|
RAISING_GEAR |
1 |
1 |
0 |
GEAR_UP |
|
2 |
1 |
0 |
CHECKING_BEFORE_LANDING |
|
|
GEAR_UP |
2 |
1 |
0 |
CHECKING_BEFORE_LANDING |
|
CHECKING_BEFORE_LANDING |
3 |
1 |
0 |
LOWERING_GEAR |
|
LOWERING_GEAR |
0 |
0 |
0 |
CHECKING_BEFORE_TAKE_OFF |
|
0 |
0 |
1 |
GEAR_DOWN |
Code for the given project
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//enums are defined for different states//
typedef enum{
GearDown,
Checkingbeforetakeoff,
RaisingGear,
GearUp,
CheckingbeforeLanding,
LoweringGear,
}State_types;
//enums are defined for lever state//
typedef enum{
Lever_Rising,
Lever_UP,
Lever_Falling,
Lever_DOWN,
}Pilot_Lever_States;
//enums for different light states//
typedef enum{
Green,
Red,
OFF,
}Light_Status;
//enums for gear state//
typedef enum{
Gear_Down,
Raising_Gear,
Gear_Up,
Lowering_Gear,
}Landing_Gear_States;
//enum for limit switch state//
typedef enum{
Opened,
Closed,
}Limit_Switch_State;
//enum for squat switch state//
typedef enum{
SS_Opened,
SS_Closed,
}Squat_Switch_State;
//structure declaration//
typedef struct{
State_types Landing_gear_state[6];
Pilot_Lever_States Lever_state[4];
Limit_Switch_State Limit_switch[2];
Squat_Switch_State Squat_switch[2];
char* Value_state[4];
char* output[4];
char* Gear_state[4];
char* Landing_gear_status[6];
}State;
//finite state model to define the states//
State State_Machine[6]={
{{Checkingbeforetakeoff},{Lever_Rising},{Opened},{SS_Opened},{"DOWN"},{"GREEN"},{"GEAR_DOWN"},{"CHECKING_BEFORE_TAKE_OFF"}},
{{GearDown,RaisingGear},{Lever_UP,Lever_Rising},{Opened,Opened},{SS_Opened,SS_Opened},{"ERROR","UP"},{"ERROR","RED"},{"ERROR","TAKING_OFF"},{"ERROR","RAISING_GEAR"}},
{{CheckingbeforeLanding,GearUp},{Lever_Falling,Lever_UP},{Closed,Closed},{SS_Opened,SS_Opened},{"ERROR","DISABLED"},{"ERROR","OFF"},{"ERROR","GEAR_UP and AIRBORNE"},{"ERROR","GEAR_UP"}},
{{GearUp,LoweringGear,CheckingbeforeLanding},{Lever_UP,Lever_Falling},{Closed,Closed},{SS_Opened,SS_Opened},{"ERROR","ERROR","ENABLED"},{"ERROR","ERROR","RED"},{"ERROR","ERROR","GEAR_ENABLED"},{"ERROR","ERROR","CHECKING_BEFORE_LANDING"}},
{{GearUp,RaisingGear,CheckingbeforeLanding,LoweringGear},{Lever_UP,Lever_Falling,Lever_DOWN},{Closed,Closed},{SS_Opened,SS_Opened},{"ERROR","ERROR","ERROR","DOWN"},{"ERROR","ERROR","ERROR","RED"},{"ERROR","ERROR","ERROR","GEAR_DOWN"},{"ERROR","ERROR","ERROR","LOWERING_GEAR"}},
{{Checkingbeforetakeoff},{Lever_UP},{Opened,Opened},{SS_Opened,SS_Opened},{"UP"},{"RED"},{"RASING"},{"CHECKING_BEFORE_TAKE_OFF"}},
};
//initializing inputs//
static Pilot_Lever_States Pilot_Lever_state_input;
static Limit_Switch_State Limit_switch_input;
static Squat_Switch_State Squat_switch_input;
int main()
{
//defining the present state of the flight//
int current_state = GearDown;
Pilot_Lever_state_input = Lever_Rising;
Limit_switch_input = Opened;
Squat_switch_input = SS_Opened;
//declaration of inputs//
int handle_lever_state_input,handle_limit_switch_input,handle_squat_switch_input;
printf("Finite state machine to implement controlling the retraction and extension of Airplan's landing gear\n");
printf("----------------------------------------------------------------------------------------------------\n");
printf("present state = GEAR_DOWN\n");
printf("\n");
while(1)
{
printf("enter pilot lever handle input");
fflush(stdout);
scanf("%d",&handle_lever_state_input);
printf("enter limit switch input");
fflush(stdout);
scanf("%d",&handle_limit_switch_input);
printf("enter squat switch input");
fflush(stdout);
scanf("%d",&handle_squat_switch_input);
printf("\n");
//if loop to run in regular cycle//
if ((handle_lever_state_input == Pilot_Lever_state_input)&&(handle_limit_switch_input == Limit_switch_input)&&(handle_squat_switch_input == Squat_switch_input))
{
printf("present state = %s\n",State_Machine[current_state].Landing_gear_status[Pilot_Lever_state_input]);
printf("statues of the direction value = %s\n",State_Machine[current_state].Value_state[Pilot_Lever_state_input]);
printf("statues of light = %s\n",State_Machine[current_state].output[Pilot_Lever_state_input]);
printf("statues of gear = %s\n",State_Machine[current_state].Gear_state[Pilot_Lever_state_input]);
printf("\n");
//Defining next state
current_state=State_Machine[current_state].Landing_gear_state[Pilot_Lever_state_input];
Pilot_Lever_state_input=State_Machine[current_state].Lever_state[Pilot_Lever_state_input];
Limit_switch_input=State_Machine[current_state].Limit_switch[Limit_switch_input];
Squat_switch_input=State_Machine[current_state].Squat_switch[Squat_switch_input];
/*printf("%d\n",current_state);
printf("%d\n",Pilot_Lever_state_input);
printf("%d\n",Limit_switch_input);
printf("%d\n",Squat_switch_input);*/
}
//if loop to change state form CHECKING_BEFORE_TAKE_OFF state to GEAR_DOWN state
else if (((handle_lever_state_input == 2)||(handle_squat_switch_input == 0))&&(handle_limit_switch_input == 0))
{
current_state = GearDown;
Pilot_Lever_state_input = 0;
Limit_switch_input = 0;
Squat_switch_input = 0;
printf("present state = GEAR_DOWN\n");
printf("statues of the direction value = %s\n",State_Machine[current_state].Value_state[Pilot_Lever_state_input]);
printf("statues of light = RED\n");
printf("statues of gear = %s\n",State_Machine[current_state].Gear_state[Pilot_Lever_state_input]);
printf("\n");
//Defining next state
Pilot_Lever_state_input=State_Machine[current_state].Lever_state[Pilot_Lever_state_input];
Limit_switch_input=State_Machine[current_state].Limit_switch[Limit_switch_input];
Squat_switch_input=State_Machine[current_state].Squat_switch[Squat_switch_input];
/*printf("%d\n",current_state);
printf("%d\n",Pilot_Lever_state_input);
printf("%d\n",Limit_switch_input);
printf("%d\n",Squat_switch_input);*/
}
//if loop to change state from RAISING_GEAR state to CHECKING_BEFORE_LANDING state//
else if ((handle_lever_state_input == 2)&&(handle_limit_switch_input == 1)&&(handle_squat_switch_input == 0))
{
current_state = GearUp;
Pilot_Lever_state_input = 2;
Limit_switch_input = 1;
Squat_switch_input = 0;
printf("present state = %s\n",State_Machine[current_state].Landing_gear_status[Pilot_Lever_state_input]);
printf("statues of the direction value = %s\n",State_Machine[current_state].Value_state[Pilot_Lever_state_input]);
printf("statues of light = %s\n",State_Machine[current_state].output[Pilot_Lever_state_input]);
printf("statues of gear = %s\n",State_Machine[current_state].Gear_state[Pilot_Lever_state_input]);
printf("\n");
//Defining next state
current_state=State_Machine[current_state].Landing_gear_state[Pilot_Lever_state_input];
Pilot_Lever_state_input=State_Machine[current_state].Lever_state[Pilot_Lever_state_input];
Limit_switch_input=State_Machine[current_state].Limit_switch[Limit_switch_input];
Squat_switch_input=State_Machine[current_state].Squat_switch[Squat_switch_input];
/*printf("%d\n",current_state);
printf("%d\n",Pilot_Lever_state_input);
printf("%d\n",Limit_switch_input);
printf("%d\n",Squat_switch_input);*/
}
//if loop to change LOWERING_GEAR state to GEAR_DOWN state//
else if ((handle_lever_state_input == 0)&&(handle_limit_switch_input == 0)&&(handle_squat_switch_input == 1))
{
current_state = LoweringGear;
Pilot_Lever_state_input = 0;
Limit_switch_input = 0;
Squat_switch_input = 0;
printf("present state = GEAR_DOWN\n");
printf("statues of the direction value = DOWN\n");
printf("statues of light = GREEN\n");
printf("statues of gear = DOWN and Locked\n");
printf("parked at bay or landed\n");
/*current_state=State_Machine[current_state].Landing_gear_state[Pilot_Lever_state_input];
Pilot_Lever_state_input=State_Machine[current_state].Lever_state[Pilot_Lever_state_input];
Limit_switch_input=State_Machine[current_state].Limit_switch[Limit_switch_input];
Squat_switch_input=State_Machine[current_state].Squat_switch[Squat_switch_input];*/
break;
}
}
return 0;
}