Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include <stdio.h>
void Init_State_Machine();
void Gear_Down();
void Checking_Before_Takeoff();
void Raising_Gear();
void Gear_Up();
void Checking_Before_Landing();
void Lowering_Gear();
static void(*State_func[])(void)={Gear_Down, Checking_Before_Takeoff, Raising_Gear, Gear_Up, Checking_Before_Landing,Lowering_Gear};
typedef enum States
{
GEAR_DOWN,
CHECKING_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR,
}State_Name;
typedef enum Limit_Switches
{
L_CLOSED,
L_OPEN
} Limit_Switch_status;
typedef enum
{
Close,
Open
}squat_switch_status;
typedef enum Lights
{
OFF,
GREEN,
RED
} Light_status;
typedef enum
{
Rising =0,
Falling=1,
UP=2,
DOWN=3
}pilot_lever;
typedef enum Control
{
ENABLED,
DISABLE
} Landing_Gear_Control;
static volatile squat_switch_status sws;
static volatile Light_status light;
static volatile pilot_lever pl;
static volatile Landing_Gear_Control LGC;
static volatile Limit_Switch_status LS;
typedef struct
{
char* state;
char* light;
int time_delay;
char* direction_valve_status;
char* landing_gear_status;
}State_Table;
static State_Table FSM[6]=
{
{"Gear Down","Green",0,"Down",NULL },
{"Checking before take-off","Green",3,"Down",NULL},
{"Raising Gear","RED",0,"Up",NULL},
{"Gear Up","Off",0,NULL, "Disabled"},
{"Checking before Landing","Red",3,"Falling","Enabled"},
{"Lowering Gear","Red",0,"Down",NULL}
};
int current_state;
int main()
{
Init_State_Machine();
while(1)
{
State_func[current_state]();
}
return 0;
}
void Init_State_Machine(void)
{
current_state==GEAR_DOWN;
printf("The Finite State Machine is Intialized and current the program is in 'Gear Down' State.\n");
}
void Gear_Down(void)
{
current_state=GEAR_DOWN;
printf("Enter the Inputs: Pilot_Lever_Status (options: A. Rising:0, B. Falling:1, C. UP:2, D. DOWN: 3) Squat Switch Status (Options: A. CLOSE:0, B. OPEN:1 )\n");
fflush(stdout);
scanf("%d %d",&pl,&sws);
if(pl==Rising && sws==Open)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("current state:%s\n",FSM[current_state].state);
printf("Light is :%s\n", FSM[current_state].light);
printf("Wait for %d minutes\n",FSM[current_state].time_delay);
printf("\n");
}
else
{
Gear_Down();
}
}
void Checking_Before_Takeoff(void)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("Enter the Inputs: Pilot_Lever_Status (options: A. Rising:0, B. Falling:1, C. UP:2, D. DOWN: 3) Squat Switch Status (Options: A. CLOSE:0, B. OPEN:1)\n");
fflush(stdout);
scanf("%d %d",&pl,&sws);
if(pl==UP && sws == Open)
{
current_state = RAISING_GEAR;
printf("Current State:%s\n",FSM[current_state].state);
printf("Light is:%s\n",FSM[current_state].light);
printf("Status of Direction valve:%s\n",FSM[current_state].direction_valve_status);
printf("\n");
}
else if(pl== Falling || sws == Close)
{
current_state=GEAR_DOWN;
printf("Current State:%s\n",FSM[current_state].state);
printf("Light is:%s\n",FSM[current_state].light);
printf("Status of Direction valve:%s\n",FSM[current_state].direction_valve_status);
printf("\n");
}
else
{
Checking_Before_Takeoff();
}
}
void Raising_Gear(void)
{
current_state= RAISING_GEAR;
printf("Enter the input:Limit Switch Status (Options: A. CLOSE:0, B. OPEN:1) \n");
fflush(stdout);
scanf("%d",&LS);
if(LS == Close)
{
current_state = GEAR_UP;
printf("Current State:%s\n",FSM[current_state].state);
printf("Light is:%s\n",FSM[current_state].light);
printf("Status of Landing Gear Control:%s\n",FSM[current_state].landing_gear_status);
printf("\n");
}
else
{
printf("Enter the Inputs: Pilot_Lever_Status (options: A. Rising:0, B. Falling:1, C. UP:2, D. DOWN: 3)\n");
scanf("%d",&pl);
if(pl == Falling)
{
current_state = CHECK_BEFORE_LANDING;
printf("Current State:%s\n",FSM[current_state].state);
printf("Light is:%s\n",FSM[current_state].light);
printf("\n");
}
else
{
Raising_Gear();
}
}
}
void Gear_Up(void)
{
current_state = GEAR_UP;
printf("Enter the Inputs: Pilot_Lever_Status (options: A. Rising:0, B. Falling:1, C. UP:2, D. DOWN: 3)\n");
fflush(stdout);
scanf("%d",&pl);
if(pl == Falling)
{
current_state = CHECK_BEFORE_LANDING;
printf("Current State:%s\n",FSM[current_state].state);
printf("Status of Landing Gear Control:%s\n",FSM[current_state].landing_gear_status);
printf("Light is:%s\n",FSM[current_state].light);
printf("\n");
}
else
{
Gear_Up();
}
}
void Checking_Before_Landing(void)
{
current_state = CHECK_BEFORE_LANDING;
printf("Enter the Inputs: Pilot_Lever_Status (options: A. Rising:0, B. Falling:1, C. UP:2, D. DOWN: 3)\n");
fflush(stdout);
scanf("%d",&pl);
if(pl == DOWN)
{
current_state = LOWERING_GEAR;
printf("Current State:%s\n",FSM[current_state].state);
printf("Status of Direction valve:%s\n",FSM[current_state].direction_valve_status);
printf("\n");
}
else
{
Checking_Before_Landing();
}
}
void Lowering_Gear(void)
{
current_state = LOWERING_GEAR;
printf("Enter the input:Limit Switch Status (Options: A. CLOSE:0, B. OPEN:1) \n");
fflush(stdout);
scanf("%d",&LS);
if(LS == Open)
{
current_state = GEAR_DOWN;
printf("Current State:%s\n",FSM[current_state].state);
printf("Light is:%s\n",FSM[current_state].light);
printf("Status of Landing Gear:%s\n",FSM[current_state].landing_gear_status);
printf("\n");
}
else
{
printf("Enter the Inputs: Pilot_Lever_Status (options: A. Rising:0, B. Falling:1, C. UP:2, D. DOWN: 3)\n");
scanf("%d",&pl);
if(pl == Rising)
{
current_state = CHECKING_BEFORE_TAKEOFF;
printf("Current State:%s\n",FSM[current_state].state);
printf("Light is:%s\n","Red");
printf("\n");
}
else
{
Lowering_Gear();
}
}
}