Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
1.
AIM:
To control 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.
CODE:
#include <stdio.h>
// Functions Declaration
void Start_State_Machine();
void GearDown();
void CheckingBeforeTakeOFF();
void RaisingGear();
void GearUp();
void CheckBeforeLanding();
void LoweringGear();
//Pointer to array function
// Static -- Makes the function accesible throughout the program
// Void --- Holds address of any type and can be typecasted to any type.
static void(*statetable[])(void) = {GearDown, CheckingBeforeTakeOFF, RaisingGear, GearUp,
CheckBeforeLanding, LoweringGear};
typedef enum State{
Gear_Down,
Checking_Before_Takeoff,
Raising_Gear,
Gear_Up,
Check_Before_Landing,
Lowering_Gear
}State_Type;
typedef enum Switch{
on,
off
}Switch_status;
typedef enum pilot_lever{
Raising,
Falling
}pilot_lever;
typedef enum hydraulic_mechanism{
working,
not_working
}hydraulic_mechanism;
// User-defined data-types declaration
// Violatile -- Tells the compiler that these variables can change their value anytime in the prog.
static volatile Switch_status squat_switch;
static volatile Switch_status limit_switch;
static volatile pilot_lever p;
static volatile hydraulic_mechanism h;
State_Type current_state;
typedef struct{
char* current_state_indication;
char* light;
char* direction_valve_status;
char* landing_gear_hydraulic_control;
char* GPSS_status[2];
}State_Table;
static State_Table State_Machine[6]={
{"GearDown","Yellow","Down","Enabled",{NULL,NULL}},
{"CheckingBeforeTakeOFF","Yellow","Down","Enabled",{NULL,NULL}},
{"RaisingGear","Red","Up","Enabled",{NULL,NULL}},
{"GearUp","Off","NULL","Disabled",{NULL,NULL}},
{"CheckBeforeLanding","Red","Down","Enabled",{NULL,NULL}},
{"LoweringGear","Yellow","Down","Enabled",{"Disabled","Enabled"}}
};
// Gear down mechanism
void GearDown(void){
current_state = Gear_Down;
printf("\nEnter the status of pilot's lever & squat switch : ");
// To clear the buffer
fflush(stdout);
scanf("%d %d",&p,&squat_switch);
if(p==Raising && squat_switch==on){
current_state=Checking_Before_Takeoff;
printf("Current state: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is :%s\n", State_Machine[current_state].light);
}
else{
GearDown();
}
}
//CheckingBeforeTakeOFF
void CheckingBeforeTakeOFF(void){
current_state = Checking_Before_Takeoff;
printf("\nEnter the status of pilot's lever and squat switch and hydraulic mechanism : ");
fflush(stdout);
scanf("%d %d",&p,&squat_switch,&h);
if(p==Falling && squat_switch==off){
GearDown();
}
else if(p ==Raising && squat_switch==on && h==working){
current_state=Raising_Gear;
printf("Current State: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
printf("Status of Direction valve: %s\n",State_Machine[current_state].direction_valve_status);
}
else if(p == Raising && squat_switch==on && h ==not_working){
current_state=Raising_Gear;
printf("Current State: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
printf("Status of Direction valve: %s\n",State_Machine[current_state].direction_valve_status);
printf("Status of gas pressurised Spring System: %s\n",State_Machine[current_state].GPSS_status);
}
}
//Raising Gear mechanism
void RaisingGear(void){
current_state=Raising_Gear ;
printf("\nEnter the status of pilot's lever and limit switch : ");
fflush(stdout);
scanf("%d %d",&p,&limit_switch);
if(p==Falling && limit_switch==on){
current_state=Check_Before_Landing ;
printf("Current State: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control: %s\n",State_Machine[current_state].landing_gear_hydraulic_control);
}
else if(p ==Raising && limit_switch==off){
current_state=Gear_Up ;
printf("Current State: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control: %s\n",State_Machine[current_state].landing_gear_hydraulic_control);
}
else{
RaisingGear();
}
}
//Gear Up mechanism
void GearUp(void){
current_state=Gear_Up;
printf("\nEnter the status of pilot's lever : ");
fflush(stdout);
scanf("%d",&p);
if(p ==Falling){
current_state = Check_Before_Landing;
printf("Current State: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control: %s\n",State_Machine[current_state].landing_gear_hydraulic_control);
}
else{
GearUp();
}
}
//CheckBeforeLanding
void CheckBeforeLanding(void)
{
current_state=Check_Before_Landing;
printf("\nEnter the status of pilot's lever and hydraulic mechanism status :");
fflush(stdout);
scanf("%d %d",&p,&h);
if(p ==Falling && h ==working){
current_state = Lowering_Gear;
printf("Current State: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
printf("Status of Direction valve: %s\n",State_Machine[current_state].direction_valve_status);
}
else if(p ==Falling && h ==not_working){
current_state = Lowering_Gear;
printf("Current State: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
printf("Status of Direction valve: %s\n",State_Machine[current_state].direction_valve_status);
printf("Status of gas pressurised Spring System: %s\n",State_Machine[current_state].GPSS_status);
}
else if(p ==(Raising)){
CheckBeforeLanding();
}
}
//Lowering Gear mechanism.
void LoweringGear(void){
current_state=Lowering_Gear;
printf("\nEnter the status of pilot's lever and limit switch : ");
fflush(stdout);
scanf("%d %d",&p,&limit_switch);
if(p == Falling &&limit_switch==on){
current_state=Checking_Before_Takeoff;
printf("\nCurrent State: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
}
else if(p ==Raising && limit_switch==off){
current_state=Gear_Down;
printf("Current State: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control: %s\n",State_Machine[current_state].landing_gear_hydraulic_control);
}
if(limit_switch==off){
LoweringGear();
}
}
//Initiate_State_Machine
void Start_State_Machine(){
current_state = Gear_Down;
printf("\nAeroplane's Landing Gear is Intialized and current the program is in GearDown State & Light is: %s\n",State_Machine[current_state].light);
}
// Driver Code
int main(){
Start_State_Machine();
while(1){
statetable[current_state]();
}
return 0 ;
}