Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
1.
#include<stdio.h>
typedef enum //defining enumerated data
{
GearDown,
Checkingbeforetakeoff,
RisingGear,
Gearup,
Checkingbeforelanding,
loweringgear
}StateTypes;
typedef enum
{
open,
close
}Switch;
typedef enum
{
rising,
falling,
up,
down
}lever;
void geardown(); //function prototype
void checkingbeforetakeoff();
void risinggear();
void gearup();
void checkingbeforelanding();
void lowergear();
static volatile Switch squadswitch; //giving variable
static volatile Switch limitswitch;
static volatile lever pilot;
static StateTypes currentstate;
typedef struct plane //creating structure
{
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"},{"loweringgear","red","down"}};//giving it as string
static void (*state_table[])(void)={geardown,checkingbeforetakeoff,risinggear,gearup,checkingbeforelanding,lowergear};
void init()
{
currentstate=GearDown; //initial stage of the plane
printf("the initial state is geardown\n");
}
void geardown()
{
printf("enter the pilot lever and squad switch"); //asking to enter the lever and squadswitch value
fflush(stdout);
scanf("%d %d",&pilot,&squadswitch); //scanning it
if(pilot==rising&&squadswitch==open) //running if loop
{
currentstate=Checkingbeforetakeoff; //if the if loop satisfies it will print the value
printf("\nstatus: %s\n",state_machine[currentstate].currentstatus);
printf("light colour: %s\n",state_machine[currentstate].lightstatus);
printf("direction of valve: %s\n",state_machine[currentstate].directionvalvestatus);
}
else
{
geardown(); //if the if loop doesnt satisfy the state will remain as it is
}
}
void checkingbeforetakeoff() //if the function runs successfully it will come to next state
{
printf("\nenter the pilot lever and squad switch");
fflush(stdout);
scanf("%d %d",&pilot,&squadswitch);
if(pilot==up)
{
currentstate=RisingGear;
printf("\nstatus: %s\n",state_machine[currentstate].currentstatus);
printf("light colour: %s\n",state_machine[currentstate].lightstatus);
printf("direction of valve: %s\n",state_machine[currentstate].directionvalvestatus);
}
else if(pilot==falling&&squadswitch==close)
{
currentstate=GearDown;
printf("\nstatus: %s\n",state_machine[currentstate].currentstatus);
printf("light colour: %s\n",state_machine[currentstate].lightstatus);
printf("direction of valve: %s\n",state_machine[currentstate].directionvalvestatus);
}
else
{
checkingbeforetakeoff();
}
}
void risinggear()
{
printf("\nenter the pilot lever limit switch");
fflush(stdout);
scanf("%d %d",&pilot,&limitswitch);
if(limitswitch==close)
{
currentstate=Gearup;
printf("\nstatus: %s\n",state_machine[currentstate].currentstatus);
printf("light colour: %s\n",state_machine[currentstate].lightstatus);
printf("direction of valve: %s\n",state_machine[currentstate].directionvalvestatus);
}
else if(pilot==falling)
{
currentstate=Checkingbeforelanding;
printf("\nstatus: %s\n",state_machine[currentstate].currentstatus);
printf("light colour: %s\n",state_machine[currentstate].lightstatus);
printf("direction of valve: %s\n",state_machine[currentstate].directionvalvestatus);
}
else
{
risinggear();
}
}
void gearup()
{
printf("\nenter the pilot lever");
fflush(stdout);
scanf("%d",&pilot);
if(pilot==falling)
{
currentstate=Checkingbeforelanding;
printf("\nstatus: %s\n",state_machine[currentstate].currentstatus);
printf("light colour: %s\n",state_machine[currentstate].lightstatus);
printf("direction of valve: %s\n",state_machine[currentstate].directionvalvestatus);
}
else
{
gearup();
}
}
void checkingbeforelanding()
{
printf("enter the pilot lever");
fflush(stdout);
scanf("%d",&pilot);
if(pilot==down)
{
currentstate=loweringgear;
printf("\nstatus: %s\n",state_machine[currentstate].currentstatus);
printf("light colour: %s\n",state_machine[currentstate].lightstatus);
printf("direction of valve: %s\n",state_machine[currentstate].directionvalvestatus);
}
else
{
checkingbeforelanding() ;
}
}
void lowergear()
{
printf("\nenter the pilot lever limit switch");
fflush(stdout);
scanf("%d %d",&pilot,&limitswitch);
if(limitswitch==open)
{
currentstate=GearDown;
printf("\nstatus: %s\n",state_machine[currentstate].currentstatus);
printf("light colour: %s\n",state_machine[currentstate].lightstatus);
printf("direction of valve: %s\n",state_machine[currentstate].directionvalvestatus);
}
else if(pilot==rising)
{
currentstate=Checkingbeforetakeoff;
printf("\nstatus: %s\n",state_machine[currentstate].currentstatus);
printf("light colour: %s\n",state_machine[currentstate].lightstatus);
printf("direction of valve: %s\n",state_machine[currentstate].directionvalvestatus);
}
else
{
lowergear();
}
}
int main()
{
init();
while(1)
{
state_table[currentstate]();
}
}
