Challenge

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

#include<stdio.h>

typedef enum
{
geardown,
checkingbeforetakeoff,
risinggear,
gearup,
checkingbeforelanding,
loweringgear
}state;
typedef enum
{
open,
close
}Switch;
typedef enum
{
raising,
falling,
up,
down
}pilot;
void Geardown();
void Checkingbeforetakeoff();
void Raisinggear();
void Loweringgear();
void Checkingbeforelanding();
void Gearup();
static volatile Switch squatswitch;
static volatile Switch limitswitch;
static volatile pilot lever;
static state currentstate;
typedef struct plane
{
char* currentstatus;
char* lightstatus;
char* directionvalvestatus;
};

static struct plane state_machine[6]=
{
{"geardown","green","down"},
{"checkingbeforetakeoff","green","down"},
{"raisinggear","red","up"},
{"gearup","off","null"},
{"checkingbeforelanding","red","down"},
{"loweringgear","red","down"}
};
static void (*state_table[])(void)={Geardown,Checkingbeforetakeoff,Raisinggear,Gearup,Checkingbeforelanding,Loweringgear};

void init()
{
currentstate=geardown;
printf("the initial state is geardown \n");
}
void Geardown()
{
printf("enter the pilotlever and squat switch");
fflush(stdout);
scanf("%d %d", &lever,&squatswitch);

if(lever==up && squatswitch==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("enter the pilot lever and squat switch");
fflush(stdout);
scanf("%d %d", &lever,&squatswitch);

if(lever==up)
{
currentstate=risinggear;
printf("%s\n",state_machine[currentstate].currentstatus);
printf("%s\n",state_machine[currentstate].lightstatus);
printf("%s\n",state_machine[currentstate].directionvalvestatus);

}
else if(lever==falling && squatswitch==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 Raisinggear()
{
printf("enter the pilot lever and limit switch");
fflush(stdout);
scanf("%d %d", &lever,&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(lever==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
{
Raisinggear();
}
}
void Checkingbeforelanding()
{
printf("enter the pilot lever ");
fflush(stdout);
scanf("%d", &lever);

if(lever==down)
{
currentstate=loweringgear;
printf("%s\n",state_machine[currentstate].currentstatus);
printf("%s\n",state_machine[currentstate].lightstatus);
printf("%s\n",state_machine[currentstate].directionvalvestatus);

}
else
{
Checkingbeforelanding();
}

}
void Loweringgear()
{

printf("enter the pilot lever and limit switch");
fflush(stdout);
scanf("%d %d", &lever,&limitswitch);

if(limitswitch==open)
{
currentstate=geardown;
printf("%s\n",state_machine[currentstate].currentstatus);
printf("%s\n",state_machine[currentstate].lightstatus);
printf("%s\n",state_machine[currentstate].directionvalvestatus);
}
else if(lever==raising)
{
currentstate=checkingbeforetakeoff;
printf("%s\n",state_machine[currentstate].currentstatus);
printf("%s\n",state_machine[currentstate].lightstatus);
printf("%s\n",state_machine[currentstate].directionvalvestatus);

}
else
{
Loweringgear();
}
}
void Gearup()
{
printf("enter the pilot lever");
fflush(stdout);
scanf("%d", &lever);

if(lever==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();
}
}
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.