Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include
void renningstatemachine();
void geardown();
void checktakeoffcondition();
void risinggear();
void gearup();
void checklandingcondition();
void loweringgear();
static void(*statelist[])(void) = {geardown, checktakeoffcondition, risinggear, gearup, checklandingcondition, loweringgear};
typedef enum state{
gear_down,
check_takeoff_condition,
rising_gear,
gear_up,
check_landing_condition,
lowering_gear,
}state_type;
typedef enum switch_s{
open,
closed,
}switch_s;
typedef enum pilot_lever{
rising,
falling,
up,
down,
}pilot_lever;
typedef enum hydrolic_micanism{
working,
not_working,
}hydraulic_micanism;
typedef enum spring_control{
high,
low,
}spring_control;
switch_s limiting_switch;
switch_s squat_switch;
pilot_lever pl;
hydraulic_micanism hm;
spring_control sc;
state_type current_state;
typedef struct{
char *current_stutus;
char *light;
char *value_direction;
char *hydraulic_control;
char *spring_control_condition;
}state_list;
static state_list state_machine[6] = {{"gear_down", "green", "down", "working", "low"},
{"check_takeoff_condition", "green", "down", "working", "low"},
{"rising_gear", "red","up", "working", "low"},
{"gear_up", "off", "null", "not_working", "high"},
{"checking_landing_condition", "red", "down", "working", "low"},
{"lowering_gear", "green", "down", "working", "low"},};
int main()
{
runningstatemachine();
while(1){
statelist[current_state]();
}
}
void runningstatemachine()
{
current_state = gear_down;
printf("Aircraft gear is down and light : %s\n", state_machine[current_state].light);
fflush(stdout);
}
void geardown()
{
current_state = gear_down;
printf("Enter the condition for pilot lever and squat switch");
fflush(stdout);
scanf("%d %d", &pl, &squat_switch);
if(pl == rising && squat_switch == open)
{
current_state = check_takeoff_condition;
printf("Current state : %s\n", state_machine[current_state].current_stutus);
fflush(stdout);
printf("Light : %s\n", state_machine[current_state].light);
fflush(stdout);
printf("Direction value : %s\n", state_machine[current_state].value_direction);
fflush(stdout);
}
else
{
current_state = gear_down;
printf("Current state : %s\n", state_machine[current_state].current_stutus);
fflush(stdout);
printf("Light : %s\n", state_machine[current_state].light);
fflush(stdout);
printf("Direction value : %s\n", state_machine[current_state].value_direction);
fflush(stdout);
}
}
void checktakeoffcondition()
{
current_state = check_takeoff_condition;
printf("Enter the pilot lever, squat switch and hydraulic_micanism condition");
fflush(stdout);
scanf("%d %d %d", &pl, &squat_switch, &hm);
if(pl == up && squat_switch == closed && hm == working)
{
current_state = gear_down;
printf("Current state : %s\n", state_machine[current_state].current_stutus);
fflush(stdout);
printf("Light : %s\n", state_machine[current_state].light);
fflush(stdout);
printf("Direction value : %s\n", state_machine[current_state].value_direction);
fflush(stdout);
}
else if(pl == up && squat_switch == open && hm == working)
{
current_state = rising_gear;
printf("Current state : %s\n", state_machine[current_state].current_stutus);
fflush(stdout);
printf("Light : %s\n", state_machine[current_state].light);
fflush(stdout);
printf("Direction value : %s\n", state_machine[current_state].value_direction);
fflush(stdout);
}
}
void risinggear()
{
current_state = rising_gear;
printf("Enter the pilot lever and limit switch condition");
fflush(stdout);
scanf("%d %d", &pl, &limiting_switch);
if(pl == falling && limiting_switch == open)
{
current_state = check_landing_condition;
printf("Current state : %s\n", state_machine[current_state].current_stutus);
fflush(stdout);
printf("Light : %s\n", state_machine[current_state].light);
fflush(stdout);
printf("Hydraulic control : %s\n", state_machine[current_state].hydraulic_control);
fflush(stdout);
}
else if(pl == rising && limiting_switch == closed)
{
current_state = gear_up;
printf("Current state : %s\n", state_machine[current_state].current_stutus);
fflush(stdout);
printf("Light : %s\n", state_machine[current_state].light);
fflush(stdout);
printf("Hydraulic control : %s\n", state_machine[current_state].hydraulic_control);
fflush(stdout);
}
}
void gearup()
{
current_state = gear_up;
printf("Enter the pilot lever and switch condition\n");
fflush(stdout);
scanf("%d %d", &limiting_switch, &pl);
if(limiting_switch == closed && pl == rising)
{
current_state = rising_gear;
printf("Current state : %s\n", state_machine[current_state].current_stutus);
fflush(stdout);
printf("Light : %s\n", state_machine[current_state].light);
fflush(stdout);
printf("Spring control condition : %s\n", state_machine[current_state].spring_control_condition);
fflush(stdout);
}
else if(limiting_switch == open && pl == falling)
{
current_state = check_landing_condition;
printf("Current state : %s\n", state_machine[current_state].current_stutus);
fflush(stdout);
printf("Light : %s\n", state_machine[current_state].light);
fflush(stdout);
printf("Spring control condition : %s\n", state_machine[current_state].spring_control_condition);
fflush(stdout);
}
}
void checklandingcondition()
{
current_state = check_landing_condition;
printf("Enter the spring control and pilot lever");
fflush(stdout);
scanf("%d %d", ⪼, &pl);
if(sc == high && pl == falling)
{
current_state = gear_up;
printf("Current state : %s\n", state_machine[current_state].current_stutus);
fflush(stdout);
printf("Light : %s\n", state_machine[current_state].light);
fflush(stdout);
printf("Direction value : %s\n", state_machine[current_state].value_direction);
fflush(stdout);
}
else if(sc == low && pl == down)
{
current_state = lowering_gear;
printf("Current state : %s\n", state_machine[current_state].current_stutus);
fflush(stdout);
printf("Light : %s\n", state_machine[current_state].light);
fflush(stdout);
printf("Direction value : %s\n", state_machine[current_state].value_direction);
fflush(stdout);
}
}
void loweringgear()
{
current_state = lowering_gear;
printf("Enter the spring control and pilot lever");
fflush(stdout);
scanf("%d %d", &pl, &limiting_switch);
if(pl == rising && limiting_switch == open)
{
current_state = check_landing_condition;
printf("Current state : %s\n", state_machine[current_state].current_stutus);
fflush(stdout);
printf("Light : %s\n", state_machine[current_state].light);
fflush(stdout);
printf("Direction value : %s\n", state_machine[current_state].value_direction);
fflush(stdout);
}
else if(pl == falling && limiting_switch == open)
{
current_state = gear_down;
printf("Current state : %s\n", state_machine[current_state].current_stutus);
fflush(stdout);
printf("Light : %s\n", state_machine[current_state].light);
fflush(stdout);
printf("Direction value : %s\n", state_machine[current_state].value_direction);
fflush(stdout);
}
else if(pl == rising && limiting_switch == open)
{
current_state = check_takeoff_condition;
printf("Current state : %s\n", state_machine[current_state].current_stutus);
fflush(stdout);
printf("Light : %s\n", state_machine[current_state].light);
fflush(stdout);
printf("Direction value : %s\n", state_machine[current_state].value_direction);
fflush(stdout);
}
}
Output:
Aircraft gear is down and light : green
Enter the condition for pilot lever and squat switch0 0
Current state : check_takeoff_condition
Light : green
Direction value : down
Enter the pilot lever, squat switch and hydraulic_micanism condition2 0 0
Current state : rising_gear
Light : red
Direction value : up
Enter the pilot lever and limit switch condition0 1
Current state : gear_up
Light : off
Hydraulic control : not_working
Enter the pilot lever and switch condition
0 1
Current state : checking_landing_condition
Light : red
Spring control condition : low
Enter the spring control and pilot lever1 4
Enter the spring control and pilot lever0 1
Current state : gear_up
Light : off
Direction value : null
Enter the pilot lever and switch condition
0 0
Enter the pilot lever and switch condition
0 1
Current state : checking_landing_condition
Light : red
Spring control condition : low
Enter the spring control and pilot lever




