Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Batch- Live Batch(Preeti)
Name- Madhu D
Aim: To write a program for controlling the retraction and extension of Airplane’s landing gear using Finite State Machine (FSM).
Theary: A finite state machine that can, at any point in time, be in a specific state from a finite set of possible states. It can move (transition) to another state by accepting an input. If the machine allows for outputs, it can produce an output.
State machine of Aeroplane landing gear:

Program:
#include
#include//header file
void m (); //function
typedef enum
{
gear_down,
checking_before_take_off,
raising_gear,
gear_up,
checking_before_landing,
lowering_gear
} gear; //Enumerated data type
typedef enum
{
off,
green,
red
} light; //Enumerated data type
typedef enum
{
valve_up,
valve_down
} valve; //Enumerated data type
typedef struct
{
gear output;
gear next_state[6];
int time;
light l[3];
char *a[10];
} State; //Enumerated data type
/* creating a structure array */
State state_[6] = {
{gear_down, {gear_down, checking_before_take_off}, 3, {off, green, red},
{"gear down", "checking before take off", "airplan is on ground", "green","red"}},
{checking_before_take_off, {gear_down, raising_gear}, 3, {off, green, red},
{"gear down", "checking before take off", "raising_gear", "red"}},
{raising_gear, {gear_up, checking_before_landing}, 3, {off, green, red},
{"checking before take off", "gear up", "off"}},
{gear_up, {checking_before_landing}, 3, {off, green, red},
{"checking before landing", "gear up", "red"}},
{checking_before_landing, {lowering_gear}, 3, {off, green, red},
{"checking before landing", "lowering_gear", "gear up", "red"}},
{lowering_gear, {gear_up, checking_before_take_off}, 3, {off, green, red},
{"gear up", "checking before take off", "lowering_gear", "red"}},
};
//FSM Finite state machine
int squat_switch, current_state, pilot_lever, pilot_lever_up, limit_switch,pilot_lever_falling, pilot_lever_down; //Global variable
int main ()
{
while (1)
{
printf("\nsquat switch=");
fflush(stdout);
scanf("%d", &squat_switch);
if (squat_switch == 0) //plane is on ground squat switch off
{
printf("light=(%d)%s\n", state_[0].l[1], state_[0].a[3]);
printf ("%s\n", state_[0].a[0]);
printf ("%s\n", state_[0].a[2]);
current_state = state_[0].next_state[0];
continue;
} //condition to be in ground till squat switch is not closed
else if (squat_switch == 1) //plane is taking off or landing or at airborn
{
printf("Pilot lever checking to take off or stay at ground=");
fflush(stdout);
scanf("%d", &pilot_lever);
if (pilot_lever == 1 && squat_switch == 1) //condition to change state from down to rising
{
{
printf("\n%s", state_[0].a[1]);
printf("\nlight=(%d)%s\n", state_[0].l[2], state_[0].a[4]);
printf("delay=%d\n", state_[0].time);
current_state = checking_before_take_off;
} //state has been changed
printf("\npilot lever condtion for rising gear or gear down status =");
fflush(stdout);
scanf ("%d", &pilot_lever);
if (pilot_lever == 2 && squat_switch == 1) //condition to change state 2 for lever up
{
printf("\n%s", state_[1].a[2]);
printf ("\nlight=(%d)%s", state_[1].l[2], state_[1].a[3]);
current_state = raising_gear;
printf("\nlimit_switch to go at gear up or gear down before landing=");
fflush(stdout);
scanf ("%d", &limit_switch);
if (limit_switch == 1) //condition to change state from rising to gear up
{
{
printf("\n%s", state_[2].a[1]);
printf("\nlight=(%d)%s\n", state_[2].l[0], state_[2].a[2]);
printf ("control for landing gear disabled\n\n");
current_state = gear_up;
//3 for falling
}
printf("\npilot lever before landing condition=");
fflush(stdout);
scanf ("%d", &pilot_lever);
if (pilot_lever == 3) //condition to change state from gear up to check before landing
{
printf("\n%s", state_[3].a[0]);
printf("\nlight=(%d)%s\n", state_[3].l[2],
state_[3].a[2]);
current_state = checking_before_landing;
// 4 for down
}
}
else if (limit_switch == 1 || limit_switch == 0) //condition to change state from rising to check before landing
{
printf("\n%s", state_[3].a[0]);
printf("\nlight=(%d)%s\n", state_[3].l[1],
state_[3].a[3]);
current_state = checking_before_landing;
// 4 for down
}
printf("\npilot_lever before lowering gear condition="); //4 for landing
fflush(stdout);
scanf("%d", &pilot_lever);
if (pilot_lever == 4) //condition to change state from check before landing to lowering gear
{
printf("\n%s\n", state_[4].a[1]);
printf("\nlight=(%d)%s\n", state_[4].l[1],
state_[4].a[3]);
current_state = lowering_gear;
//0 for open
}
printf("limit switch = ");
fflush(stdout);
scanf("%d", &limit_switch);
printf("pilot_lever =");
fflush(stdout);
scanf("%d", &pilot_lever);
if (limit_switch == 0 || pilot_lever == 0) //condition to change state from lowering gear to gear down state
{
printf("\nlight=(%d)%s\n", state_[0].l[1],
state_[0].a[3]);
printf("\n%s", state_[0].a[0]);
printf("\n%s\n", state_[0].a[2]);
current_state = state_[0].next_state[0];
continue;
}
if (pilot_lever == 1 || limit_switch == 1)
{
m (); //called function
continue;
}
}
}
}
}
}
void m () //called function
{
while(1)
{
squat_switch = 1;
scanf ("%d", &pilot_lever);
if (pilot_lever == 2 && squat_switch == 1) //condition to change state 2 for lever up
{
printf ("\nlight=(%d)%s", state_[1].l[2], state_[1].a[3]);
printf ("\n%s", state_[1].a[2]);
current_state = raising_gear;
printf ("\nlimit_switch to go at gear up or gear down before landing=");
fflush(stdout);
scanf ("%d", &limit_switch);
if (limit_switch == 1) //condition to change state from rising to gear up
{
{
printf ("\n%s", state_[2].a[1]);
printf ("\nlight=(%d)%s\n", state_[2].l[1], state_[2].a[3]);
printf ("control for landing gear disabled");
current_state = gear_up;
printf ("control for landing gear are disabled");;
//3 for falling
}
printf ("\npilot lever checking before landing condition=");
fflush(stdout);
scanf ("%d", &pilot_lever);
if (pilot_lever == 3) //condition to change state from gear up to check before landing
{
printf ("\n%s", state_[3].a[0]);
printf ("\nlight=(%d)%s\n", state_[3].l[1], state_[3].a[3]);
current_state = checking_before_landing;
// 4 for down
}
}
else if (limit_switch == 1 || limit_switch == 0) //condition to change state from rising to check before landing
{
printf ("\n%s", state_[3].a[0]);
printf ("\n light=(%d)%s\n", state_[3].l[1], state_[3].a[3]);
current_state = checking_before_landing;
// 4 for down
}
printf ("\npilot_lever before lowering gear condition="); //4 for landing
fflush(stdout);
scanf ("%d", &pilot_lever);
if (pilot_lever == 4) //condition to change state from check before landing to lowering gear
{
printf ("\n%s", state_[4].a[1]);
printf ("\n light=(%d)%s\n", state_[4].l[1], state_[4].a[3]);
current_state = lowering_gear;
//0 for open
}
printf ("limit switch =");
fflush(stdout);
scanf ("%d", &limit_switch);
printf("pilot lever =");
fflush(stdout);
scanf("%d",&pilot_lever);
if (limit_switch == 0 || pilot_lever == 0) //condition to change state from lowering gear to gear down state
{
printf ("\nlight=(%d)%s\n", state_[0].l[1], state_[0].a[3]);
printf ("\n%s", state_[0].a[0]);
printf ("\n%s\n", state_[0].a[2]);
current_state = state_[0].next_state[0];
break;
}
}
}
}
RESULT:
