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
Ans:
/*
* Airoplane_landing_gear.c
*
* Created on: Sep 10, 2022
* Author: SUSHMA
*/
#include<stdio.h>
#include<stdlib.h>
typedef enum
{
Gear_Down,
Checking_before_takeoff_Delay_3s,
Raising_Gear,
Gear_UP,
Checking_before_landing,
Lowering_Gear,
}state_types;
typedef enum
{
close,
open,
}swich;
typedef enum
{
Raising,
Falling,
Up,
Down,
}Pilot_lever;
typedef enum
{
Working,
Not_working,
}Hydraulic_mechanism;
static volatile swich Squat,Limit; //enum switch variables
static volatile Pilot_lever variable1;//enum Pilot_lever variables
static volatile Hydraulic_mechanism variable2;//enum Hydraulic_mechanism variables
state_types current_state;
typedef struct
{
char* currentstate;
char* light_value;
char* direction_valve;
char* hydraulic_control;
}states;
/*array of function pointer*/
static void(*state_table[])(void)={Gear_down,Checking_before_takeoff,Raising_gear,Gear_up,Checking_beforelanding,LoweringGear};
/*array of structure variable*/
static states state_machine[6]={
{"Gear_Down","Green","Down","Enabled"},
{"Checking_before_takeoff_Delay_3s","Green","Down","Enabled"},
{"Raising Gear","Red","Up","Enabled"},
{"Gear_Up","Off","Null","Disabled"},
{"Checking_before_landing","Red","Down","Enabled"},
{"Lowering_Gear","Green","Down","Enabled"}
};
/*Function prototypes*/
void LoweringGear();
void Checking_beforelanding();
void Gear_up();
void Raising_gear();
void Checking_before_takeoff();
void Gear_down();
/*main function*/
int main()
{
current_state=Gear_Down;//initial state
while(1)
{
state_table[current_state]();//function call
}
}
void Gear_down()
{
current_state=Gear_Down;
printf("\nEnter value of variable1 and Squat:");
fflush(stdout);
scanf("%d %d",&variable1,&Squat);
if((variable1==Raising) && (Squat==close))
{
current_state=Checking_before_takeoff_Delay_3s;
printf("\nCurrent state is %s: ",state_machine[current_state].currentstate);
printf("%s: ",state_machine[current_state].direction_valve);
printf("%s: ",state_machine[current_state].light_value);
printf("%s: ",state_machine[current_state].hydraulic_control);
}
else
{
Gear_down();
}
}
void Checking_before_takeoff()
{
current_state=Checking_before_takeoff_Delay_3s;
printf("\nEnter value of variable1,Squat and variable2:");
fflush(stdout);
scanf("%d %d %d",&variable1,&Squat,&variable2);
if((variable1==Falling) && (Squat==close))
{
current_state=Gear_Down;
printf("\nCurrent state is %s: ",state_machine[current_state].currentstate);
printf("%s: ",state_machine[current_state].direction_valve);
printf("%s: ",state_machine[current_state].light_value);
printf("%s: ",state_machine[current_state].hydraulic_control);
}
else if((variable1==Up) && (variable2==Working))
{
current_state=Raising_Gear;
printf("\nCurrent state is %s: ",state_machine[current_state].currentstate);
printf("%s: ",state_machine[current_state].direction_valve);
printf("%s: ",state_machine[current_state].light_value);
printf("%s: ",state_machine[current_state].hydraulic_control);
}
else
{
Checking_before_takeoff();
}
}
void Raising_gear()
{
current_state=Raising;
printf("\nEnter value of variable1 and Limit:");
fflush(stdout);
scanf("%d %d",&variable1,&Limit);
if(variable1==Falling)
{
current_state=Checking_before_landing;
printf("\nCurrent state is %s: ",state_machine[current_state].currentstate);
printf("%s: ",state_machine[current_state].direction_valve);
printf("%s: ",state_machine[current_state].light_value);
printf("%s: ",state_machine[current_state].hydraulic_control);
}
if(Limit==close)
{
current_state=Gear_UP;
printf("\nCurrent state is %s: ",state_machine[current_state].currentstate);
printf("%s: ",state_machine[current_state].direction_valve);
printf("%s: ",state_machine[current_state].light_value);
printf("%s: ",state_machine[current_state].hydraulic_control);
}
else
{
Raising_gear();
}
}
void Gear_up()
{
current_state=Gear_UP;
printf("\nEnter value of variable1:");
fflush(stdout);
scanf("%d",&variable1);
if(variable1==Falling)
{
current_state=Checking_before_landing;
printf("\nCurrent state is %s: ",state_machine[current_state].currentstate);
printf("%s: ",state_machine[current_state].direction_valve);
printf("%s: ",state_machine[current_state].light_value);
printf("%s: ",state_machine[current_state].hydraulic_control);
}
else
{
Gear_up();
}
}
void Checking_beforelanding()
{
current_state=Checking_before_landing;
printf("\nEnter value of variable1 and variable2:");
fflush(stdout);
scanf("%d %d",&variable1,&variable2);
if(variable1==Down|| variable2==Not_working)
{
current_state=Lowering_Gear;
printf("\nCurrent state is %s: ",state_machine[current_state].currentstate);
printf("%s: ",state_machine[current_state].direction_valve);
printf("%s: ",state_machine[current_state].light_value);
printf("%s: ",state_machine[current_state].hydraulic_control);
}
else
{
Checking_beforelanding();
}
}
void LoweringGear()
{
current_state=Lowering_Gear;
printf("\nEnter value of Limit and variable1:");
fflush(stdout);
scanf("%d %d",&Limit,&variable1);
if(Limit==open)
{
current_state=Gear_Down;
printf("\nCurrent state is %s: ",state_machine[current_state].currentstate);
printf("%s: ",state_machine[current_state].direction_valve);
printf("%s: ",state_machine[current_state].light_value);
printf("%s: ",state_machine[current_state].hydraulic_control);
}
if(variable1==Raising)
{
current_state=Checking_before_takeoff_Delay_3s;
printf("\nCurrent state is %s: ",state_machine[current_state].currentstate);
printf("%s: ",state_machine[current_state].direction_valve);
printf("%s: ",state_machine[current_state].light_value);
printf("%s: ",state_machine[current_state].hydraulic_control);
}
else
{
LoweringGear();
}
}
Result:
Enter value of variable1 and Squat:1 0
Enter value of variable1 and Squat:0 0
Current state is Checking_before_takeoff_Delay_3s: Down: Green: Enabled:
Enter value of variable1,Squat and variable2:0 1 1
Enter value of variable1,Squat and variable2:1 0 0
Current state is Gear_Down: Down: Green: Enabled:
Enter value of variable1 and Squat:0 0
Current state is Checking_before_takeoff_Delay_3s: Down: Green: Enabled:
Enter value of variable1,Squat and variable2:2 0 0
Current state is Raising Gear: Up: Red: Enabled:
Enter value of variable1 and Limit:0 1
Enter value of variable1 and Limit:1 0
Current state is Checking_before_landing: Down: Red: Enabled:
Current state is Gear_Up: Null: Off: Disabled:
Enter value of variable1:0
Enter value of variable1:1
Current state is Checking_before_landing: Down: Red: Enabled:
Enter value of variable1 and variable2:0 0
Enter value of variable1 and variable2:3 0
Current state is Lowering_Gear: Down: Green: Enabled:
Enter value of variable1 and Limit:0 1
Enter value of variable1 and Limit:1 0
Current state is Gear_Down: Down: Green: Enabled:
Current state is Checking_before_takeoff_Delay_3s: Down: Green: Enabled:
Enter value of variable1,Squat and variable2: