Challenge

Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear

PROGRAM:

#include<stdio.h>

#include<string.h>

//limit switch and squat switch outputs are declared as enum datatype

enum limit_switch

{

Close=1,

Open=0

}squat_switch,ls;

//pilot lever inputs are declared as enum datatype

enum pilot_lever

{

Raising=1,

Falling=0

}pilot_lever;

//squat switch and pilot lever input are declared as global variables

char *squat_switch_temp,*pilot_lever_temp;

//finite states are declared in enum datatype

enum states

{

Gear_down,

Check_before_takeoff,

Raising_gear,

Gear_up,

Check_before_landing,

Lowering_gear

}current_state;

//output parameters in each state is declared in struct datatype

struct state_machine

{

char *state_name;

char *ng_light,*lg_light,*rg_light;

char *direction_valve;

char *landing_gear;

}states[6];

//the struct elements are declared for each finite states

struct state_machine states[6] =

{

{"Gear_down","Green","Green","Green","Down","Enabled"},

{"Check_before_takeoff","Green","Green","Green","Down","Enabled"},

{"Raising_gear","Red","Red","Red","Up","Enabled"},

{"Gear_up","OFF","OFF","OFF","Null","Disabled"},

{"Check_before_landing","Red","Red","Red","Down","Enabled"},

{"Lowering_gear","Red","Red","Red","Down","Enabled"}

};

//declaration of each state function prototype

void gear_down();

void check_before_takeoff();

void raising_gear();

void gear_up();

void check_before_landing();

void lowering_gear();

//Initializing finite state machine

void Init_State_Machine()

{

current_state = Gear_down;

printf("\n*** The Finite state machine is initialized ***\nThe current state is %s ",states[current_state].state_name);

printf("\nNose gear light %s\nLeft gear light %s\nRight gear light %s",states[current_state].ng_light,states[current_state].lg_light,states[current_state].rg_light);

}

//FSM in gear down state and computing the inputs to move to next state

void gear_down()

{

current_state = Gear_down;

printf("\nEnter pilot lever condition\n0 - Falling\t1 - Raising\n");

scanf("%d",&pilot_lever);

printf("\nEnter squat switch condition\n0 - Open\t1 - Close\n");

scanf("%d",&squat_switch);

if(pilot_lever&&squat_switch)

{

current_state = Check_before_takeoff;

printf("\nThe current state is %s ",states[current_state].state_name);

printf("\nNose gear light %s\nLeft gear light %s\nRight gear light %s",states[current_state].ng_light,states[current_state].lg_light,states[current_state].rg_light);

check_before_takeoff();

}

else

{

gear_down();

}

}

//FSM in checking before takeoff state and checking the inputs to move to next state

void check_before_takeoff()

{

current_state = Check_before_takeoff;

printf("\nEnter pilot lever condition\n0 - Falling\t1 - Raising\n");

scanf("%d",&pilot_lever);

printf("\nEnter squat switch condition\n0 - Open\t1 - Close\n");

scanf("%d",&squat_switch);

if(pilot_lever&&squat_switch)

{

current_state = Raising_gear;

printf("\nThe current state is %s ",states[current_state].state_name);

printf("\nNose gear light %s\nLeft gear light %s\nRight gear light %s",states[current_state].ng_light,states[current_state].lg_light,states[current_state].rg_light);

printf("\n Direction valve is %s",states[current_state].direction_valve);

raising_gear();

}

else

{

current_state = Gear_down;

printf("\nThe current state is %s ",states[current_state].state_name);

printf("\nNose gear light %s\nLeft gear light %s\nRight gear light %s",states[current_state].ng_light,states[current_state].lg_light,states[current_state].rg_light);

gear_down();

}

}

//FSM in raising_gear state and computing the inputs to move to next state

void raising_gear()

{

current_state = Raising_gear;

printf("\nEnter pilot lever condition\n0 - Falling\t1 - Raising\n");

scanf("%d",&pilot_lever);

printf("\nEnter limit switch condition\n0 - Open\t1 - Close\n");

scanf("%d",&ls);

if(pilot_lever==0)

{

current_state = Check_before_landing;

printf("\nThe current state is %s ",states[current_state].state_name);

printf("\nNose gear light %s\nLeft gear light %s\nRight gear light %s",states[current_state].ng_light,states[current_state].lg_light,states[current_state].rg_light);

check_before_landing();

}

else if(ls)

{

current_state = Gear_up;

printf("\nThe current state is %s ",states[current_state].state_name);

printf("\nNose gear light %s\nLeft gear light %s\nRight gear light %s",states[current_state].ng_light,states[current_state].lg_light,states[current_state].rg_light);

printf("\n Landing gear is %s",states[current_state].landing_gear);

gear_up();

}

else

{

raising_gear();

}

}

//FSM in gear up state and checking the inputs to move to next state

void gear_up()

{

current_state = Gear_up;

printf("\nEnter pilot lever condition\n0 - Falling\t1 - Raising\n");

scanf("%d",&pilot_lever);

if(pilot_lever==0)

{

current_state = Check_before_landing;

printf("\nThe current state is %s ",states[current_state].state_name);

printf("\nNose gear light %s\nLeft gear light %s\nRight gear light %s",states[current_state].ng_light,states[current_state].lg_light,states[current_state].rg_light);

check_before_landing();

}

else

gear_up();

}

//FSM in checking before landing state and computing the inputs to move to next state

void check_before_landing()

{

current_state = Check_before_landing;

printf("\nEnter pilot lever condition\n0 - Falling\t1 - Raising\n");

scanf("%d",&pilot_lever);

if(pilot_lever==0)

{

current_state = Lowering_gear;

printf("\nThe current state is %s ",states[current_state].state_name);

printf("\nNose gear light %s\nLeft gear light %s\nRight gear light %s",states[current_state].ng_light,states[current_state].lg_light,states[current_state].rg_light);

printf("\n Direction valve is %s",states[current_state].direction_valve);

lowering_gear();

}

else

check_before_landing();

}

//FSM in lowering gear state and computing the inputs to move to next state

void lowering_gear()

{

current_state = Lowering_gear;

printf("\nEnter pilot lever condition\n0 - Falling\t1 - Raising\n");

scanf("%d",&pilot_lever);

printf("\nEnter limit switch condition\n0 - Open\t1 - Close\n");

scanf("%d",&ls);

if(pilot_lever)

{

current_state = Check_before_takeoff;

printf("\nThe current state is %s ",states[current_state].state_name);

printf("\nNose gear light %s\nLeft gear light %s\nRight gear light %s",states[current_state].ng_light,states[current_state].lg_light,states[current_state].rg_light);

check_before_takeoff();

}

else if(ls==0)

{

current_state = Gear_down;

printf("\nThe current state is %s ",states[current_state].state_name);

printf("\nNose gear light %s\nLeft gear light %s\nRight gear light %s",states[current_state].ng_light,states[current_state].lg_light,states[current_state].rg_light);

printf("\n Direction valve is %s",states[current_state].direction_valve);

gear_down();

}

}

int main()

{

Init_State_Machine();//Initializing lift and its parameter

gear_down(); //calling the default state of FSM

}

OUTPUT:

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.