Challenge

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

#include<stdio.h>


void initiate_state_machine();
void geardown();
void checkingbeforetakeoff();
void raisinggear();
void gearup();
void checkbeforelanding();
void loweringgear();

typedef enum state
{
gear_down,
checking_before_takeoff,
raising_gear,
gear_up,
check_before_landing,
lowering_gear,
}state_type;

typedef enum Switch
{
on,
off//plane is on ground
}switch_status;

typedef enum pilot_lever
{
raising,
falling
}pilot_lever_status;
typedef enum hydraulic_mechanism
{
working,
notworking
}hydraulic_mechanism_status;
static volatile switch_status squat_switch;
static volatile switch_status limit_switch;
static volatile pilot_lever_status pilot_lever;
static volatile hydraulic_mechanism_status hydraulic_mechanism;

state_type current_state;

typedef struct
{
char* current_state_indication;
char* light;
char* direction_valve_status;
char* gas_pressurized_spring_system_status;
char* landing_gear_hydraulic_control[2];
}state_table;

static state_table state_machine[6]=
{
{"geardown","green","down","enabled",{"NULL","NULL"}},
{"checkingbeforetakeoff"," green "," down"," enabled",{ "NULL","NULL"}},
{"raisinggear", " red " , "up" ,"enabled",{" disabled"," enabled"}},
{"gearup"," off"," null"," disabled",{ "NULL","NULL"}},
{"checkbeforelanding"," red ","down ","enabled",{ "NULL","NULL"}},
{"loweringgear"," green"," down"," enabled",{" disabled","enabled"}}
};

static void(*statetable[])(void)={geardown,checkingbeforetakeoff,raisinggear,gearup,
checkbeforelanding,loweringgear};
int main()
{initiate_state_machine();
while(1)
{
statetable[current_state]();
}
}

//geardown
void geardown(void)
{
current_state=gear_down;
printf("enter the status of pilot's lever and squat switch:");
fflush(stdout);
scanf( "%d %d",&pilot_lever,&squat_switch);
if(pilot_lever==raising && squat_switch==on)
{ current_state=checking_before_takeoff;
printf("current state:%s\n",state_machine[current_state].current_state_indication);
printf("light is :%s\n",state_machine[current_state].light);

}
else
{geardown();
}
}

//checkingbeforetakeoff
void checkingbeforetakeoff(void)
{
current_state=checking_before_takeoff;
printf("enter the status of poilet's lever and squat switch and hydraulic mechanism:");
fflush(stdout);
scanf("%d %d %d",&pilot_lever,&squat_switch,&hydraulic_mechanism);
if(pilot_lever==falling && squat_switch==off)
{ geardown();

}
else if(pilot_lever==raising && squat_switch==on && hydraulic_mechanism==working)
{ current_state=raising_gear;
printf("current state:%s\n",state_machine[current_state].current_state_indication);
printf("light is :%s\n",state_machine[current_state].light);
printf("status of direction valve:%s\n",state_machine[current_state].direction_valve_status);
printf("status of gas pressurized spring system :%s\n",
state_machine[current_state].gas_pressurized_spring_system_status);
}
}
//raisinggear
void raisinggear(void)
{
current_state=raising_gear;
printf("enter the status of pilot's lever and limit switch:");
fflush(stdout);
scanf("%d %d" ,&pilot_lever,&limit_switch);
if(pilot_lever==falling && limit_switch==on)
{
current_state=check_before_landing;
printf("current state:%s\n",state_machine[current_state].current_state_indication);
printf("light is :%s\n",state_machine[current_state].light);
printf("landing gear hydraulic control:%s\n",state_machine[current_state].landing_gear_hydraulic_control);
}
else
{raisinggear();
}

}
//gearup
void gearup(void)
{
current_state=gear_up;
printf("enter the status of pilot's lever:");
fflush(stdout);
scanf("%d",&pilot_lever);
if(pilot_lever==falling)
{
current_state=check_before_landing;
printf("current state:%s\n",state_machine[current_state].current_state_indication);
printf("light is :%s\n",state_machine[current_state].light);
printf("landing gear hydraulic control:%s\n",
state_machine[current_state].landing_gear_hydraulic_control);

}
else{
gearup();
}
}

void checkbeforelanding(void)
{
current_state=check_before_landing;
printf("enter the status of poilet's lever and hydraulic mechanism status:");
fflush(stdout);
scanf("%d %d",&pilot_lever,&hydraulic_mechanism);
if(pilot_lever==falling && hydraulic_mechanism==working)
{
current_state=lowering_gear;
printf("current state:%s\n",state_machine[current_state].current_state_indication);
printf("light is :%s\n",state_machine[current_state].light);
printf("status of direction valve:%s\n",
state_machine[current_state].direction_valve_status);
}

else if(pilot_lever == falling && squat_switch == on && hydraulic_mechanism == notworking)
{
current_state=lowering_gear;
printf("current state:%s\n",state_machine[current_state].current_state_indication);
printf("light is :%s\n",state_machine[current_state].light);
printf("status of direction valve:%s\n",state_machine[current_state].direction_valve_status);
printf("status of gas pressurized spring system :%s\n",
state_machine[current_state].gas_pressurized_spring_system_status);
}
if(pilot_lever==(raising || falling))
{
checkbeforelanding();
}
}
//loweringgear
void loweringgear(void)
{
current_state=lowering_gear;
printf("enter the status of piolet's lever and limit switch:");
fflush(stdout);
scanf("%d %d ", &pilot_lever, &limit_switch);
if(pilot_lever==raising && limit_switch==on)
{
current_state=checking_before_takeoff;
printf("current sate :%s\n",state_machine[current_state].current_state_indication);
printf("light is :%s\n",state_machine[current_state].light);
}
if(pilot_lever==raising && limit_switch==off)
{
current_state=gear_down;
printf("current state:%s\n",state_machine[current_state].current_state_indication);
printf("light is :%s\n",state_machine[current_state].light);
printf("landing gear hydraulic control:%s\n",
state_machine[current_state].landing_gear_hydraulic_control);


}
if(limit_switch==off)
{
loweringgear();
}

}
void initiate_state_machine()
{
current_state=gear_down;
printf("the finite state machine is initialized and current the program is in"
"geardown state & light is :%s\n", state_machine[current_state].light);
}

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.