Challenge

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

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

#include<stdio.h>
/*----------function prototypes----------*/
void Gear_Down();
void Checking_Before_Takeoff_Delay_3s();
void Raising_Gear();
void Gear_Up();
void Checking_Before_Landing();
void Lowering_Gear();

/*----------------Enumerated datatypes-------*/
typedef enum
{
GEAR_DOWN,//0
CHECKING_BEFORE_TAKEOFF_DELAY_3S,//1
RAISING_GEAR,//2
GEAR_UP,//3
CHECKING_BEFORE_LANDING,//4
LOWERING_GEAR//5
}State_type;

typedef enum
{
CLOSE,//0
OPEN//1
}Switch;

typedef enum
{
RISING,//0
FALLING,//1
UP,//2
DOWN//3
}Pilot_lever;

typedef enum
{
WORKING,//0
NOT_WORKING//1
}Hydraulic_mechanism;

/*--------static variable can shared among all instances,
Volatile variables are those which are read and written to the main memory and for code optimization-------*/
static volatile Switch squat;
static volatile Switch limit;
static volatile Pilot_lever pil;
static volatile Hydraulic_mechanism hyd;


State_type currentstate;//enum type

/*---------Declaration of Structure-----------*/
typedef struct
{
char* current_state;
char* light;
char* direction_valve;
char* hydraulic_control;
}state;

/*--------------Structure objects and the accessing values-----------*/
state state_machine[6]={
{"Gear Down","Green","Down","Enabled"},
{"Checking before takeoff delay 3 sec","Green","Down","Enabled"},
{"Raising Gear","Red","Up","Enabled"},
{"Gear Up","Light Off","Null","Disabled"},
{"Checking before landing","Red","Down","Enabled"},
{"Lowering Gear","Green","Down","Enabled"}
};

/*--------------------array of function pointers-------------*/
static void (*state_table[])(void)={Gear_Down,Checking_Before_Takeoff_Delay_3s,Raising_Gear,Gear_Up,Checking_Before_Landing,Lowering_Gear};

/*---------------------Function Definitions based on State diagram------------------*/
void Gear_Down()
{
printf("I am in Gear Down...\n");
currentstate=GEAR_DOWN;//0
printf("Go to next state pil=0 and squat=1\n");
printf("Enter the status of Pilot lever and Squat switch :\n");
fflush(stdout);
scanf("%d %d",&pil,&squat);

if((pil==RISING)&&(squat==OPEN))//go to next state
{
currentstate=CHECKING_BEFORE_TAKEOFF_DELAY_3S;
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
else
{
Gear_Down();//go to same state
}
}

void Checking_Before_Takeoff_Delay_3s()
{
int b;
printf("I am in Checking_Before_Takeoff_Delay_3s...\n");
currentstate=CHECKING_BEFORE_TAKEOFF_DELAY_3S;//1
printf("Enter 1 to next state and 0 to previous state :");
fflush(stdout);
scanf("%d",&b);
if(b=1)
{
printf("Enter the status for next state of Pilot lever = 2 and Hydraulic_control= 0 :\n");
fflush(stdout);
scanf("%d %d",&pil,&hyd);
}
else
{
printf("Enter the status for previous state of Pilot lever = 1 and Squat_switch = 0 :\n");
fflush(stdout);
scanf("%d %d",&pil,&squat);
}

if((pil==FALLING)&&(squat==CLOSE))//go to previous state
{
currentstate=GEAR_DOWN;//0
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
if((pil==UP)&&(hyd==WORKING))//go to next state
{
currentstate=RAISING_GEAR;//2
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
else
{
Checking_Before_Takeoff_Delay_3s();//go to same state
}
}

void Raising_Gear()
{
int c;
printf("I am in Raising Gear...\n");
currentstate=RAISING_GEAR;
printf("Enter 1 to next state and 0 to next-next state :\n");
fflush(stdout);
scanf("%d",&c);

if(c==1)
{
printf("Enter the status for next state of Pilot lever=0 :\n");
fflush(stdout);
scanf("%d",&pil);
}
else
{
printf("Enter the status for next next state of Pilot lever=1 :\n");
fflush(stdout);
scanf("%d",&pil);
}

if(pil==FALLING)// go to 5th(next-next) state 1
{
currentstate=CHECKING_BEFORE_LANDING;//4
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
if(limit==CLOSE)//go to next state 0
{
currentstate=GEAR_UP;//3
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
else
{
Raising_Gear();//go to same state
}
}

void Gear_Up()
{
printf("I am in Gear Up...\n");
currentstate=GEAR_UP;//3
printf("Enter the status for next state of Pilot lever=1 :\n");
fflush(stdout);
scanf("%d",&pil);
if(pil==FALLING)//go to next state 1
{
currentstate=CHECKING_BEFORE_LANDING;//4
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
else
{
Gear_Up();//go to same state
}
}

void Checking_Before_Landing()
{
printf("I am in Checking_Before_Landing...\n");
currentstate=CHECKING_BEFORE_LANDING;//4
printf("Enter the status for next state of Pilot_lever= 3 or Hydraulic_control= 1 :\n");
fflush(stdout);
scanf("%d %d",&pil,&hyd);
if(pil==DOWN||hyd==NOT_WORKING)//go to next state
{
currentstate=LOWERING_GEAR;//5
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
else
{
Checking_Before_Landing();//go to same state
}
}

void Lowering_Gear()
{
int a;
printf("I am in Lowering Gear...\n");
currentstate=LOWERING_GEAR;//5
printf("Enter 1 to next state and 0 to 2nd state :\n");
fflush(stdout);
scanf("%d",&a);
if(a==1)
{
printf("Enter the status for next state of Limit_switch=1: \n");
fflush(stdout);
scanf("%d",&limit);
}
else
{
printf("Enter the status for second state of Pilot_lever=0: \n");
fflush(stdout);
scanf("%d",&pil);
}
if(limit==OPEN)//go to next state
{
currentstate=GEAR_DOWN;//0
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
if(pil==RISING)//go to 2nd state
{
currentstate=CHECKING_BEFORE_TAKEOFF_DELAY_3S;//1
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
else
{
Lowering_Gear();//go to same state
}
}

int main()
{
currentstate=GEAR_DOWN;
while(1)
{
state_table[currentstate]();
}
return 0;
}
#include<stdio.h>
/*----------function prototyes----------*/
void Gear_Down();
void Checking_Before_Takeoff_Delay_3s();
void Raising_Gear();
void Gear_Up();
void Checking_Before_Landing();
void Lowering_Gear();

/*----------------Enumerated datatypes-------*/
typedef enum
{
GEAR_DOWN,//0
CHECKING_BEFORE_TAKEOFF_DELAY_3S,//1
RAISING_GEAR,//2
GEAR_UP,//3
CHECKING_BEFORE_LANDING,//4
LOWERING_GEAR//5
}State_type;

typedef enum
{
CLOSE,//0
OPEN//1
}Switch;

typedef enum
{
RISING,//0
FALLING,//1
UP,//2
DOWN//3
}Pilot_lever;

typedef enum
{
WORKING,//0
NOT_WORKING//1
}Hydraulic_mechanism;

/*--------static variabe can shared among all instances,
Volatile variables are those which are read and written to main memory and for code optimization-------*/
static volatile Switch squat;
static volatile Switch limit;
static volatile Pilot_lever pil;
static volatile Hydraulic_mechanism hyd;


State_type currentstate;//enum type

/*---------Decleration of Structure-----------*/
typedef struct
{
char* current_state;
char* light;
char* direction_valve;
char* hydraulic_control;
}state;

/*--------------Structure objects and the accessing values-----------*/
state state_machine[6]={
{"Gear Down","Green","Down","Enabled"},
{"Checking before takeoff delay 3 sec","Green","Down","Enabled"},
{"Raising Gear","Red","Up","Enabled"},
{"Gear Up","Light Off","Null","Disabled"},
{"Checking before landing","Red","Down","Enabled"},
{"Lowering Gear","Green","Down","Enabled"}
};

/*--------------------array of function pointere-------------*/
static void (*state_table[])(void)={Gear_Down,Checking_Before_Takeoff_Delay_3s,Raising_Gear,Gear_Up,Checking_Before_Landing,Lowering_Gear};

/*---------------------Function Definitions based on State diagram------------------*/
void Gear_Down()
{
printf("I am in Gear Down...\n");
currentstate=GEAR_DOWN;//0
printf("Go to next state pil=0 and squat=1\n");
printf("Enter the status of Pilot lever and Squat switch :\n");
fflush(stdout);
scanf("%d %d",&pil,&squat);

if((pil==RISING)&&(squat==OPEN))//go to next state
{
currentstate=CHECKING_BEFORE_TAKEOFF_DELAY_3S;
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
else
{
Gear_Down();//go to same state
}
}

void Checking_Before_Takeoff_Delay_3s()
{
int b;
printf("I am in Checking_Before_Takeoff_Delay_3s...\n");
currentstate=CHECKING_BEFORE_TAKEOFF_DELAY_3S;//1
printf("Enter 1 to next state and 0 to previous state :");
fflush(stdout);
scanf("%d",&b);
if(b=1)
{
printf("Enter the status for next state of Pilot lever = 2 and Hydraulic_control= 0 :\n");
fflush(stdout);
scanf("%d %d",&pil,&hyd);
}
else
{
printf("Enter the status for previous state of Pilot lever = 1 and Squat_switch = 0 :\n");
fflush(stdout);
scanf("%d %d",&pil,&squat);
}

if((pil==FALLING)&&(squat==CLOSE))//go to previous state
{
currentstate=GEAR_DOWN;//0
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
if((pil==UP)&&(hyd==WORKING))//go to next state
{
currentstate=RAISING_GEAR;//2
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
else
{
Checking_Before_Takeoff_Delay_3s();//go to same state
}
}

void Raising_Gear()
{
int c;
printf("I am in Raising Gear...\n");
currentstate=RAISING_GEAR;
printf("Enter 1 to next state and 0 to next-next state :\n");
fflush(stdout);
scanf("%d",&c);

if(c==1)
{
printf("Enter the status for next state of Pilot lever=0 :\n");
fflush(stdout);
scanf("%d",&pil);
}
else
{
printf("Enter the status for next next state of Pilot lever=1 :\n");
fflush(stdout);
scanf("%d",&pil);
}

if(pil==FALLING)// go to 5th(next-next) state 1
{
currentstate=CHECKING_BEFORE_LANDING;//4
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
if(limit==CLOSE)//go to next state 0
{
currentstate=GEAR_UP;//3
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
else
{
Raising_Gear();//go to same state
}
}

void Gear_Up()
{
printf("I am in Gear Up...\n");
currentstate=GEAR_UP;//3
printf("Enter the status for next state of Pilot lever=1 :\n");
fflush(stdout);
scanf("%d",&pil);
if(pil==FALLING)//go to next state 1
{
currentstate=CHECKING_BEFORE_LANDING;//4
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
else
{
Gear_Up();//go to same state
}
}

void Checking_Before_Landing()
{
printf("I am in Checking_Before_Landing...\n");
currentstate=CHECKING_BEFORE_LANDING;//4
printf("Enter the status for next state of Pilot_lever= 3 or Hydraulic_control= 1 :\n");
fflush(stdout);
scanf("%d %d",&pil,&hyd);
if(pil==DOWN||hyd==NOT_WORKING)//go to next state
{
currentstate=LOWERING_GEAR;//5
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
else
{
Checking_Before_Landing();//go to same state
}
}

void Lowering_Gear()
{
int a;
printf("I am in Lowering Gear...\n");
currentstate=LOWERING_GEAR;//5
printf("Enter 1 to next state and 0 to 2nd state :\n");
fflush(stdout);
scanf("%d",&a);
if(a==1)
{
printf("Enter the status for next state of Limit_switch=1: \n");
fflush(stdout);
scanf("%d",&limit);
}
else
{
printf("Enter the status for second state of Pilot_lever=0: \n");
fflush(stdout);
scanf("%d",&pil);
}
if(limit==OPEN)//go to next state
{
currentstate=GEAR_DOWN;//0
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
if(pil==RISING)//go to 2nd state
{
currentstate=CHECKING_BEFORE_TAKEOFF_DELAY_3S;//1
printf("Current Status...\n");
printf("%s\n",state_machine[currentstate].current_state);
printf("%s\n",state_machine[currentstate].light);
printf("%s\n",state_machine[currentstate].direction_valve);
printf("%s\n",state_machine[currentstate].hydraulic_control);
}
else
{
Lowering_Gear();//go to same state
}
}

/*--------------main----------------*/
int main()
{
currentstate=GEAR_DOWN;
while(1)
{
state_table[currentstate]();//swiching to different functions from the array of function pointers as decleared
}
return 0;
}

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.