Challenge

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

#include <stdio.h>

void init_state_machine(); //FUNCTION DECLARING INITIAL STATE MACHINE
void gear_down(); //FUNCTION DECLARING GEAR DOWN
void checking_before_takeoff(); //FUNCTION DECLARING CHECKING BEFORE TAKE OFF
void raising_gear(); //FUNCTION DECLARING RISING GEAR
void gear_up(); //FUNCTION DECLARING GEAR UP
void checking_before_landing(); //FUNCTION DECLARING CHECKING BEFORE LANDING
void lowering_gear(); //FUNCTION DECLARING LOWERING GEAR


typedef enum state //TYPEDEF ENUM FUNCTION TO DEFINE STATE TYPE
{

gr_dwn,
chk_bfr_tkof,
rsing_gr,
gr_up,
chk_bfr_lnd,
lwrng_gr

}state_type;

typedef enum Switch //TYPEDEF ENUM FUNCTION TO DEFINE SWITCH
{

open,
close

}Switch_status;

typedef enum pilot_lever //TYPEDEF ENUM FUNCTION TO DEFINE PILOT LEVER STATUS
{

rising,
falling,
up,
down

}pilot_lever_status;

typedef enum hydraulic_mechanism //TYPEDEF ENUM FUNCTION TO DEFINE HYDRAULIC MECHANISM STATUS
{

working,
malfunctioning

}hydraulic_mechanism_status;


static volatile Switch_status squat_switch; //INPUT VARIABLE FOR CHANGING THE STATES
static volatile Switch_status limit_switch; //INPUT VARIABLE FOR CHANGING THE STATES
static volatile pilot_lever_status pilot_lever; //INPUT VARIABLE FOR CHANGING THE STATES
static volatile hydraulic_mechanism_status hydraulic_mechanism; //INPUT VARIABLE FOR CHANGING THE STATES


state_type current_state; //DEFINING CURRENT STATE AS STATE TYPE

typedef struct //DEFINING STATE TABLE VARIABLES AS TYPEDEF STRUCTURE WITH CHARACTER DATATYPE
{
char* current_state_indication;
char* light_status;
char* direction_valve_status;
char* hydraulic_pump_control_status;
char* gas_pressurized_spring_status[2];
}State_table;

static State_table state_machine[6] = {
{"geardown","green","down","enabled",0,0},
{"checking before take off","green","down","enabled",0,0},
{"raising gear","red","up","enabled",{"disabled","enabled"}},
{"gear up","off","null","disabled",0,0},
{"checking before landing","red","down","enabled",0,0},
{"lowering gear","green","down","enabled",{"disabled","enabled"}}
}; //STATE TABLE ARRAY DECLARATION

void init_state_machine() //FUNCTION INITIAL STATE MACHINE CALLED
{

current_state = gr_dwn; //ASSIGNING CURRENT STATE TO LOWERING GEAR
printf("\nstate machine is initialized & program is in gear down state & light status:%s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//

}

/**********************************************GEAR DOWN *********************************************************/

void gear_down(void) //FUNCTION GEAR DOWN CALLED
{

current_state=gr_dwn; //ASSIGNING CURRENT STATE TO GEAR DOWN
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE

printf("\nenter the pilot lever position: \n0.rising, \n1.falling, \n2.up, \n3.down \n\n squat switch status: \n0.open, \n1.close \n"); //PRINT THE MESSAGE
fflush(stdout); //CLEAR CACHE MEMORY
scanf("%d %d",&pilot_lever,&squat_switch); //READS AND STORES THE DATA

if(pilot_lever==rising && squat_switch==open) //IF CONDITION TO CHECK THE CONDITIONS
{

current_state = chk_bfr_tkof; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO CHECK BEFORE TAKEOFF
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE

}

else //IF CONDITIONS ARE NOT SATISFIED GO TO GEAR DOWN FUNCTION
{
gear_down();
}
}

/***************************** CHECKING BEFORE TAKEOFF **************************************/


void checking_before_takeoff() //FUNCTION CHECKING BEFORE LANDING CALLED//
{

current_state=chk_bfr_tkof; //ASSIGNING CURRENT STATE TO CHECK BEFORE TAKEOFF//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//

printf("\nenter the pilot lever position \n0.rising, \n1.falling, \n2.up, \n3.down \n"); //PRINT THE MESSAGE//
printf("\nsquat switch status \n0.open, \n1.close \n\n status of hydraulic mechanism: \n0.working \n1.malfunctioning\n"); //PRINT THE MESSAGE//
printf("\nStatus of hydraulic mechanism: \n0.working \n1.malfunctioning\n"); //PRINT THE MESSAGE//
fflush(stdout); //CLEAR CACHE MEMORY//
scanf("%d %d %d",&pilot_lever,&squat_switch,&hydraulic_mechanism); //READS AND STORES THE DATA//
printf("\nwait for 2 secs\n"); //PRINT THE MESSAGE//

if(pilot_lever==falling || squat_switch==close) //IF CONDITION TO CHECK THE CONDITIONS//
{
gear_down(); //IF CONDITION IS TRUE GO TO GEAR DOWN FUNCTION//
}

else if(pilot_lever==up && squat_switch==open && hydraulic_mechanism==working) //NESTED IF CONDITION TO CHECK OTHER CONDITION//
{

current_state = rsing_gr; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO RISING GEAR//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("direction valve status:%s\n",state_machine[current_state].direction_valve_status); //DISPLAY DIRECTION VALVE STATUS//

}

else if(pilot_lever==up && squat_switch==open && hydraulic_mechanism==malfunctioning) //NESTED IF CONDITION TO CHECK OTHER CONDITION//
{

printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("direction valve status:%s\n",state_machine[current_state].direction_valve_status); //DISPLAY DIRECTION VALVE STATUS//
printf("gas pressurized spring status:%s\n",state_machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]); //DISPLAY GAS PRESSURIZED SPRING STATUS//

}
}

/*************************************** RISING GEAR ******************************************/

void raising_gear() //FUNCTION RAISING GEAR CALLED//
{

current_state=rsing_gr; //ASSIGNING CURRENT STATE TO RISING GEAR//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//

printf("\nenter the limit switch status \n0.open, \n1.close \n"); //PRINT THE MESSAGE//
printf("\nenter the pilot lever position \n0.rising, \n1.falling, \n2.up, \n3.down \n"); //PRINT THE MESSAGE//
fflush(stdout); //CLEAR CACHE MEMORY//
scanf("%d %d",&pilot_lever,&limit_switch); //READS AND STORES THE DATA//
printf("\nwait for 1 sec\n"); //PRINT THE MESSAGE//

if(pilot_lever==falling && limit_switch==open) //IF CONDITION TO CHECK THE CONDITIONS//
{

current_state = chk_bfr_lnd; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO CHECK BEFORE LANDING//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("hydraulic control for landing gear status:%s\n",state_machine[current_state].hydraulic_pump_control_status); //DISPLAY HYDRAULIC PUMP CONTROL STATUS//

}

else if(pilot_lever==up && limit_switch==close) //NESTED IF CONDITION TO CHECK OTHER CONDITION//
{

current_state=gr_up; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO GEAR UP//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("hydraulic control for landing gear status:%s\n",state_machine[current_state].hydraulic_pump_control_status); //DISPLAY HYDRAULIC PUMP CONTROL STATUS//

}

else //IF CONDITIONS ARE NOT SATISFIED GO TO RAISING GEAR FUNCTION//
{
raising_gear(); //REPEAT//
}
}

/***************************************** GEAR UP ***********************************************/


void gear_up() //FUNCTION GEAR UP CALLED//
{

current_state=gr_up; //ASSIGNING CURRENT STATE TO GEAR UP//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//

printf("\nenter the pilot lever position: \n0.rising, \n1.falling, \n2.up, \n3.down \n"); //PRINT THE MESSAGE//
fflush(stdout); //CLEAR CACHE MEMORY//
scanf("%d",&pilot_lever); //READS AND STORES THE DATA//

if(pilot_lever==falling) //IF CONDITION TO CHECK THE CONDITIONS//
{

current_state = chk_bfr_lnd; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO CHECK BEFORE LANDING//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("hydraulic control for landing gear status:%s\n",state_machine[current_state].hydraulic_pump_control_status); //DISPLAY HYDRAULIC PUMP CONTROL STATUS//

}

else //IF CONDITIONS ARE NOT SATISFIED GO TO GEAR UP FUNCTION//
{
gear_up(); //REPEAT//
}
}

/**************************************** CHECKING BEFORE LANDING **************************************************/


void checking_before_landing() //FUNCTION CHECKING BEFORE LANDING CALLED//
{

current_state=chk_bfr_lnd; //ASSIGNING CURRENT STATE TO CHECK BEFORE LANDING//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//

printf("\nenter the pilot lever position: \n0.rising, \n1.falling, \n2.up, \n3.down \n"); //PRINT THE MESSAGE//
printf("\nStatus of hydraulic mechanism: \n0.working \n1.malfunctioning\n"); //PRINT THE MESSAGE//

fflush(stdout); //CLEAR CACHE MEMORY//
scanf("%d %d",&pilot_lever,&hydraulic_mechanism); //READS AND STORES THE DATA//

if(pilot_lever==down && hydraulic_mechanism==working) //IF CONDITION TO CHECK THE CONDITIONS//
{

current_state = lwrng_gr; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO LOWERING GEAR//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("direction valve status:%s\n",state_machine[current_state].direction_valve_status); //DISPLAY DIRECTION VALVE STATUS//

}

else if(pilot_lever==down && hydraulic_mechanism==malfunctioning) //NESTED IF CONDITION TO CHECK OTHER CONDITION//
{

current_state = lwrng_gr; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO LOWERING GEAR//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("direction valve status:%s\n",state_machine[current_state].direction_valve_status); //DISPLAY DIRECTION VALVE STATUS//
printf("gas pressurized spring status:%s\n",state_machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]); //DISPLAY GAS PRESSURIZED SPRING STATUS//

}

else if (pilot_lever == (up || rising || falling)) //NESTED IF CONDITION TO CHECK OTHER CONDITION//
{
checking_before_landing(); //REPEAT//
}
}

/********************************************** LOWERING GEAR ************************************************/


void lowering_gear() //FUNCTION LOWERING GEAR CALLED//
{

current_state=lwrng_gr; //ASSIGNING CURRENT STATE TO LOWERING GEAR//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//

printf("\nenter the limit switch status: \n0.open, \n1.close \n"); //PRINT THE MESSAGE//
printf("\nenter the pilot lever position: \n0.rising, \n1.falling, \n2.up, \n3.down \n"); //PRINT THE MESSAGE//

fflush(stdout); //CLEAR CACHE MEMORY//
scanf("%d %d",&limit_switch,&pilot_lever); //READS AND STORES THE DATA//

if (limit_switch == close && pilot_lever==rising) //IF CONDITION TO CHECK THE CONDITIONS//
{
current_state = chk_bfr_tkof; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO CHECK BEFORE TAKEOFF//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight state: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
}

else if(limit_switch==open && pilot_lever==down) //NESTED IF CONDITION TO CHECK OTHER CONDITION//
{
current_state = gr_dwn; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO GEAR DOWN//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("hydraulic control for landing gear status:%s\n",state_machine[current_state].hydraulic_pump_control_status); //DISPLAY HYDRAULIC PUMP CONTROL STATUS//
}

else //IF CONDITIONS ARE NOT SATISFIED GO TO LOWERING GEAR FUNCTION//
{
lowering_gear(); //REPEAT//
}
}
/*************************************************************************************************************************************/


//DECLARING VARIABLES TO STATE TABLE//
static void (*state_table[])(void) = {gear_down, checking_before_takeoff, raising_gear,
gear_up, checking_before_landing, lowering_gear};

int main() //MAIN FUNCTION//
{
init_state_machine(); //INITIALIZE STATE MACHINE//
while(1) //WHILE LOOP//
{
state_table[current_state](); //CALLING STATE TABLE//
}
}
/*********************************Output***************************/

PS D:\E\C Codes\VT_Practics\Project2> gcc planeFsm.c
PS D:\E\C Codes\VT_Practics\Project2> .\a.exe

state machine is initialized & program is in gear down state & light status:green

current state: geardown

enter the pilot lever position:
0.rising,
1.falling,
2.up,
3.down

squat switch status:
0.open,
1.close
3 1

current state: geardown

enter the pilot lever position:
0.rising,
1.falling,
2.up,
3.down

squat switch status:
0.open,
1.close
3 0

current state: geardown

enter the pilot lever position:
0.rising,
1.falling,
2.up,
3.down

squat switch status:
0.open,
1.close
1 0

current state: geardown

enter the pilot lever position:
0.rising,
1.falling,
2.up,
3.down

squat switch status:
0.open,
1.close
0 0

current state: checking before take off

light status: green

current state: checking before take off

enter the pilot lever position
0.rising,
1.falling,
2.up,
3.down

squat switch status
0.open,
1.close

status of hydraulic mechanism:
0.working
1.malfunctioning

Status of hydraulic mechanism:
0.working
1.malfunctioning
2 0 0

wait for 2 secs

current state: raising gear

light status: red
direction valve status:up

current state: raising gear

enter the limit switch status
0.open,
1.close

enter the pilot lever position
0.rising,
1.falling,
2.up,
3.down
0 0

wait for 1 sec

current state: raising gear

enter the limit switch status
0.open,
1.close

enter the pilot lever position
0.rising,
1.falling,
2.up,
3.down
1 0

wait for 1 sec

current state: checking before landing

light status: red
hydraulic control for landing gear status:enabled

current state: checking before landing

enter the pilot lever position:
0.rising,
1.falling,
2.up,
3.down

Status of hydraulic mechanism:
0.working
1.malfunctioning
1 0

current state: checking before landing

enter the pilot lever position:
0.rising,
1.falling,
2.up,
3.down

Status of hydraulic mechanism:
0.working
1.malfunctioning
3 0

current state: lowering gear

light status: green
direction valve status:down

current state: lowering gear

enter the limit switch status:
0.open,
1.close

enter the pilot lever position:
0.rising,
1.falling,
2.up,
3.down
1 1

current state: lowering gear

enter the limit switch status:
0.open,
1.close

enter the pilot lever position:
0.rising,
1.falling,
2.up,
3.down
1 0

current state: checking before take off

light state: green

current state: checking before take off

enter the pilot lever position
0.rising,
1.falling,
2.up,
3.down

squat switch status
0.open,
1.close

status of hydraulic mechanism:
0.working
1.malfunctioning

Status of hydraulic mechanism:
0.working
1.malfunctioning
2 0 0

wait for 2 secs

current state: raising gear

light status: red
direction valve status:up

current state: raising gear

enter the limit switch status
0.open,
1.close

enter the pilot lever position
0.rising,
1.falling,
2.up,
3.down
0 1

wait for 1 sec

current state: raising gear

enter the limit switch status
0.open,
1.close

enter the pilot lever position
0.rising,
1.falling,
2.up,
3.down
1 0

wait for 1 sec

current state: checking before landing

light status: red
hydraulic control for landing gear status:enabled

current state: checking before landing

enter the pilot lever position:
0.rising,
1.falling,
2.up,
3.down

Status of hydraulic mechanism:
0.working
1.malfunctioning
3 0

current state: lowering gear

light status: green
direction valve status:down

current state: lowering gear

enter the limit switch status:
0.open,
1.close

enter the pilot lever position:
0.rising,
1.falling,
2.up,
3.down
03
3

current state: lowering gear

enter the limit switch status:
0.open,
1.close

enter the pilot lever position:
0.rising,
1.falling,
2.up,
3.down
0 3

current state: geardown

light status: green
hydraulic control for landing gear status:enabled

current state: geardown

enter the pilot lever position:
0.rising,
1.falling,
2.up,
3.down

squat switch status:
0.open,
1.close

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.