Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include<stdio.h>
#include<stdlib.h>
void Initiate_state_Machine();
typedef enum state
{
Gear_down,
Checking_before_takeoff,
Raising_Gear,
Gear_up,
Checking_before_landing,
Lowering_Gear,
}state_type;
typedef enum Switch
{
open,
close
}switch_status;
typedef enum Pilot_lever
{
raising,
falling,
up,
down
}Pilot_lever_status;
typedef enum hydraulic_mechanism
{
working,
malfunction
}hydraulic_mechanism_status;
static volatile switch_status squat_switch;
static volatile switch_status limit_switch;
static volatile Pilot_lever_status Pilot_lever;
static volatile hydraulic_mechanism_status hydraulic_mechanism;
state_type current_state;
typedef struct
{
char* current_state_indication;
char* light;
char* time;
char* direction_valve_state;
char* landing_Gear_state;
char* spring[2];
}state_Table;
static state_Table state_Machine[6]=
{
{"Gear_down","Green","three","down","Disabled",{NULL,NULL}},
{"Checking_before_takeoff","green","three","up","Enabled",{NULL,NULL}},
{"Raising_Gear","red","three","up","Enabled",{"Disabled","Enabled"}},
{"Gear_up","off","three","null","Disabled",{NULL,NULL}},
{"Checking_before_landing","red","three","down","Enabled",{NULL,NULL}},
{"Lowering_Gear","green","three","down","Enabled",{"Disabled","Enabled"}}
};
void Geardown(void)
{
current_state=Gear_down;
printf("\n Enter the status of pilots lever AND squat switch:");
fflush(stdout);
scanf("%d %d", &Pilot_lever ,&squat_switch);
if(Pilot_lever==raising && squat_switch==open)
{
current_state=Checking_before_takeoff;
printf("\n current state:% s",state_Machine[current_state].current_state_indication);
printf("\n light is:",state_Machine[current_state].light);
}
}
void Checkingbeforetakeoff(void)
{
current_state=Checking_before_takeoff;
printf("\n Enter the status of pilots lever OR squat switch:");
fflush(stdout);
scanf("%d %d", &Pilot_lever ,&squat_switch);
if(Pilot_lever==raising || squat_switch==open)
{
current_state=Raising_Gear;
printf("\n current state:% s",state_Machine[current_state].current_state_indication);
printf("\n light is:",state_Machine[current_state].light);
}
}
void RaisingGear(void)
{
current_state=Raising_Gear;
printf(" \n Enter the status of Pilots lever OR limit switch:");
fflush(stdout);
scanf("%d %d", &Pilot_lever ,&squat_switch);
if(Pilot_lever==falling || limit_switch==close)
{
current_state=Gear_up;
printf("\n current state:% s",state_Machine[current_state].current_state_indication);
printf("\n light is:",state_Machine[current_state].light);
}
}
void Gearup (void)
{
current_state=Gear_up;
printf("\n Enter the status of Pilots lever OR limit switch:");
fflush(stdout);
scanf("%d %d", &Pilot_lever ,&squat_switch);
if(Pilot_lever==falling || limit_switch==close)
{
current_state=Checking_before_landing;
printf("\n current state:% s",state_Machine[current_state].current_state_indication);
printf("\n light is:",state_Machine[current_state].light);
}
}
void Checkingbeforelanding(void)
{
current_state=Checking_before_landing;
printf("\n Enter the status of Pilots lever OR limit switch:");
fflush(stdout);
scanf("%d %d", &Pilot_lever ,&squat_switch);
if(Pilot_lever==falling || limit_switch==close)
{
current_state=Lowering_Gear;
printf("\n current state:% s",state_Machine[current_state].current_state_indication);
printf("\n light is:",state_Machine[current_state].light);
}
}
void LoweringGear(void)
{
current_state=Lowering_Gear;
printf("\n Enter the status of Pilots lever OR limit switch:");
fflush(stdout);
scanf("%d %d", &Pilot_lever ,&squat_switch);
if(Pilot_lever==falling || limit_switch==open)
{
current_state=Gear_down;
printf("\n current state:% s",state_Machine[current_state].current_state_indication);
printf("\n light is:",state_Machine[current_state].light);
}
}
void Initiate_state_Machine(void)
{
current_state= Gear_down;
printf("The finite state machine is Initialized and the program is"
"Geardown state and light is:%s", state_Machine[current_state].light);
}
static void (*stateTable[])(void)={Geardown,Checkingbeforetakeoff,RaisingGear,Gearup,Checkingbeforelanding,LoweringGear};
int main()
{
Initiate_state_Machine();
while(1)
{
stateTable[current_state]();
}
}