Challenge

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

AIM: Controlling the retraction and extension of the Airplane’s landing gear can be implemented using a Finite State Machine

CODE:

// program to implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include<stdio.h>//declare C libraries
#include<string.h>//declare string header files
#include<stdlib.h>//declare standard libraries
//declaring structure for states
typedef enum state{
GEAR_DOWN,
CHECK_BEFORE_TAKEOFF,
RISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR
}flight_state;
flight_state state;
//declaring functions
static volatile int clock;
void intial();
void gear_down();
void check_before_takeoff();
void rising_gear();
void gear_up();
void check_before_landing();
void lowering_gear();

//declaring function pointer
void (*gear[]) (void)={gear_down,check_before_takeoff,rising_gear,gear_up,check_before_landing,lowering_gear};

int main(){
intial();//calling initial function
while(1){
printf("\n");
gear[state]();//calls the function according to the state set in each function
}
}
void intial(){
state=GEAR_DOWN;//state has been set
printf("The flight state is in gear_down state with led light green");//output is printed

}
void gear_down(){
char s1[10],s2[10];
printf("Enter the lever status : ");
scanf("%s",s1);
printf("Enter squat switch status : ");
scanf("%s",s2);
if(strcmp(s1,"raising")==0&&strcmp(s2,"open")==0){//comparing the lever status and switch status
state=CHECK_BEFORE_TAKEOFF;//state has been set
printf("The flight is in 'ready to take off' state with led light 'green'");//output is printed
}
else{
printf("please enter valid input");//if wrong input program flow redirects here
}

}
void check_before_takeoff(){
if(clock==3){
char s[10];
printf("Enter the lever status or squat switch status: ");
scanf("%s",s);
if(strcmp(s,"up")==0){//comparing the lever status
state=RISING_GEAR;//state has been set
//output is printed
printf("The flight state is in 'rising_gear' state with led light 'RED' and directional value 'UP'");
}
else if(strcmp(s,"close")==0||strcmp(s,"falling")==0){//comparing the switch status
state=GEAR_DOWN;//state has been set
printf("The flight state is in gear_down state with led light 'RED'");//output is printed
}
else{
printf("please enter valid input");//if wrong input program flow redirects here
}
}
else{
printf("clock value in seconds : %d",clock);//clock is updated and printed
clock++;
}
}
void rising_gear(){
char s[10];
printf("Enter the lever status or limit switch status : ");
scanf("%s",s);
if(strcmp(s,"closed")==0){//comparing the lever status
state=GEAR_UP;//state has been set
//output is printed
printf("The flight state is in 'gear_up' state with led light 'OFF' and directional value is in 'Control of hydraulic pump for the landing gear part is disabled' state");
}
else if(strcmp(s,"falling")==0){//comparing the switch status
state=CHECK_BEFORE_LANDING;//state has been set
//output is printed
printf("The flight state is in 'check_before_landing' state with led light 'RED'");
}
else{
printf("please enter valid input");//if wrong input program flow redirects here
}
}
void gear_up(){
char s[10];
printf("Enter the lever status : ");
scanf("%s",s);
if(strcmp(s,"falling")==0){//comparing the lever status
state=CHECK_BEFORE_LANDING;//state has been set
//output is printed
printf("The flight state is in 'check_before_landing' state with led light 'RED'");
}
else{
printf("please enter valid input");//if wrong input program flow redirects here
}
}
void check_before_landing(){
char s[10];
printf("Enter the lever status : ");
scanf("%s",s);
if(strcmp(s,"down")==0){//comparing the lever status
state=LOWERING_GEAR;//state has been set
//output is printed
printf("The flight state is in 'lowering_gear' state with led light 'RED'");
}
else{
printf("please enter valid input");//if wrong input program flow redirects here
}
}
void lowering_gear(){
char s[10];
printf("Enter the limit switch status or lever status : ");
scanf("%s",s);
if(strcmp(s,"opened")==0){//comparing the lever status
state=GEAR_DOWN;//state has been set
//output is printed
printf("The flight state is in 'gear_down' state with led light 'GREEN'");
}
else if (strcmp(s,"raising")==0){//comparing the switch status
state=CHECK_BEFORE_TAKEOFF;//state has been set
//output is printed
printf("The flight state is in 'check_before_takeoff' state with led light 'RED'");
}
else{
printf("please enter valid input");//if wrong input program flow redirects here
}

}

OUTPUT:The flight state is in gear_down state with led light green
Enter the lever status : raising
Enter squat switch status : open
The flight is in 'ready to take off' state with led light 'green'
clock value in seconds : 0
clock value in seconds : 1
clock value in seconds : 2
Enter the lever status or squat switch status: up
The flight state is in 'rising_gear' state with led light 'RED' and directional value 'UP'
Enter the lever status or limit switch status : closed
The flight state is in 'gear_up' state with led light 'OFF' and directional value is in 'Control of hydraulic pump for the landing gear part is disabled' state
Enter the lever status : falling
The flight state is in 'check_before_landing' state with led light 'RED'
Enter the lever status : down
The flight state is in 'lowering_gear' state with led light 'RED'
Enter the limit switch status or lever status : opened
The flight state is in 'gear_down' state with led light 'GREEN'
Enter the lever status :

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.

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.