Challenge

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

Project:2

#include<stdio.h>
typedef enum
{
GearDown,
Checkingbeforetakeoff,
RisingGear,
Gearup,
Checkingbeforelanding,
Loweringgear,
}StateTypes;
typedef enum
{
open,
close,
}Switch;
typedef enum
{
rising,
falling,
up,
down
}lever;
void geardown();
void checkingbeforetakeoff();
void risinggear();
void gearup();
void checkingbeforelanding();
void lowergear();
static volatile Switch squadswitch;
static volatile Switch limitswitch;
static volatile lever pilot;
static StateTypes currentstate;
typedef struct plane
{
char* currentstatus;
char* lightstatus;
char* directionvalvestatus;
};
static struct plane state_machine[6]={{"GearDown","green", "down"}, {"Checkingbeforetakeoff","green","down"},{"Risinggear","red","up"},{"Gearup","off","null"},{"Checkingbeforelanding","red","down"},{"lowergear","red","down"}};
static void (*state_table[])(void)={geardown, checkingbeforetakeoff, risinggear, gearup, checkingbeforelanding,lowergear};
void init()
{
currentstate=GearDown;
printf("the initial state is geardown\n");
}

void geardown()
{
printf("enter the pilot lever and squad switch");
fflush(stdout);
scanf("%d %d",&pilot,&squadswitch);
if(pilot==rising&&squadswitch==open)
{
currentstate=Checkingbeforetakeoff;
printf("%s\n", state_machine[currentstate].currentstatus);
printf("%s\n", state_machine[currentstate].lightstatus);
printf("%s\n", state_machine [currentstate].directionvalvestatus);
}
else
{
geardown();
}

}
void checkingbeforetakeoff()
{
printf("\n enter the pilot lever and squad switch");
fflush(stdout);
scanf("%d %d",&pilot,&squadswitch);
if (pilot==up)
{
currentstate=RisingGear;
printf("%s\n", state_machine[currentstate].directionvalvestatus);
printf("%s\n", state_machine[currentstate].currentstatus);
printf("%s\n", state_machine[currentstate].lightstatus);

}
else if(pilot==falling&&squadswitch==close)
{
currentstate=GearDown;
printf("%s\n", state_machine [currentstate].currentstatus);
printf("%s\n", state_machine [currentstate].lightstatus);
printf("%s\n", state_machine [currentstate].directionvalvestatus);
}
else
{
checkingbeforetakeoff();
}
}
void risinggear()
{
printf("\n enter the pilot lever limit switch");
fflush(stdout);
scanf("%d %d", &pilot,&limitswitch);
if(limitswitch==close)
{
currentstate=Gearup;
printf("%s\n", state_machine[currentstate].currentstatus);
printf("%s\n", state_machine[currentstate].lightstatus);
printf("%s\n", state_machine[currentstate].directionvalvestatus);
}
else if(pilot==falling)
{
currentstate=Checkingbeforelanding;
printf("%s\n", state_machine[currentstate].currentstatus);
printf("%s\n", state_machine[currentstate].lightstatus);
printf("%s\n", state_machine[currentstate].directionvalvestatus);
}
else
{
risinggear();
}
}
void gearup()
{
printf("\n enter the pilot lever");
fflush(stdout);
scanf("%d",&pilot);
if(pilot==falling)
{
currentstate=Checkingbeforelanding;
printf("%s\n", state_machine [currentstate].currentstatus);
printf("%s\n", state_machine [currentstate].lightstatus);
printf("%s\n", state_machine [currentstate].directionvalvestatus);
}
else
{
gearup();
}

}
void checkingbeforelanding()
{
printf("\n enter the pilot lever");
fflush(stdout);
scanf("%d", &pilot);
if(pilot==down)
{
currentstate=Loweringgear;
printf("%s", state_machine[currentstate].currentstatus);
printf("%s", state_machine[currentstate].lightstatus);
printf("%s", state_machine[currentstate].directionvalvestatus);
}
else
{
checkingbeforelanding();
}

}
void lowergear()
{
printf("enter the pilot lever limit switch");
fflush(stdout);
scanf("%d %d", &pilot,&limitswitch);
if(limitswitch==open)
{
currentstate=GearDown;
printf("%s", state_machine[currentstate].currentstatus);
printf("%s", state_machine[currentstate].lightstatus);
printf("%s", state_machine[currentstate].directionvalvestatus);
}
if(pilot==rising)
{
currentstate=Checkingbeforetakeoff;
printf("%s", state_machine[currentstate].currentstatus);
printf("%s", state_machine[currentstate].lightstatus);
printf("%s", state_machine [currentstate].directionvalvestatus);
}
else
{
lowergear();

}
}
int main()
{
init();
while(1)
{
state_table[currentstate]();
}
}

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.