Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include
typedef enum States
{
Gear_down,
Checking_before_Take_off,
Raising_Gear,
Gear_Up,
Checking_before_Landing,
Lowering_Gear
}State_Type;
typedef enum switches
{
Open,
Close
}Switch;
typedef enum pilot
{
Rising,
Falling,
Up,
Down,
}Pilot_Lever;
typedef enum Hydraulic
{
Working,
Not_Working
}Hydraulic_Mechanism;
static volatile Switch limit;
static volatile Switch squat;
static volatile Pilot_Lever a;
static volatile Hydraulic_Mechanism b;
State_Type current_state;
typedef struct{
char* next_state;
char* light;
char* direction_valve;
char* hydraulic_control;
}position;
void gear_down();
void checking_before_take_off();
void raising_gear();
void gear_up();
void checking_before_landing();
void lowering_gear();
static void(*statefunctions[])(void)={gear_down,checking_before_take_off,raising_gear,gear_up,checking_before_landing,lowering_gear};
static position state_machine[6]={{"GEAR DOWN","GREEN","DOWN","ENABLED"},
{"CHECKING BEFORE TAKEOFF","GREEN","DOWN","ENABLED"},
{"RAISING GEAR","RED","UP","ENABLED"},
{"GEAR UP","OFF","NULL","DISABLED"},
{"CHECKING BEFORE LANDING","RED","DOWN","ENABLED"},
{"LOWERING GEAR","GREEN","DOWN","ENABLED"}};
int main()
{
current_state=Gear_down;//Enum Value - Gear Down
while(1)
{
statefunctions[current_state]();
}
}
void gear_down()
{
current_state=Gear_down;//Enum Value - Gear Down
printf("Enter the pilot lever and squat switch\n");
scanf("%d %d",(int*)&a,(int*)&squat);
if((a==Rising)&&(squat==Open))//0 0
{
current_state=Checking_before_Take_off;//enum value
printf("Next state is %s\n",state_machine[current_state].next_state);
printf("Light is %s\n",state_machine[current_state].light);
printf("Direction valve is %s\n",state_machine[current_state].direction_valve);
printf("Hydraulic control is %s\n",state_machine[current_state].hydraulic_control);
}
else
{
gear_down();
}}
void checking_before_take_off()
{
current_state=Checking_before_Take_off;
printf("Enter the pilot lever, squat switch and hydraulic machine\n");
scanf("%d %d",(int*)&a,(int*)&squat);
if((a==Falling)||(squat==Close))
{
current_state=Gear_down;
printf("Next state is %s\n",state_machine[current_state].next_state);
printf("Light is %s\n",state_machine[current_state].light);
printf("Direction valve is %s\n",state_machine[current_state].direction_valve);
printf("Hydraulic control is %s\n",state_machine[current_state].hydraulic_control);
}
else if((a==Up)&&(b==Working))
{
current_state=Raising_Gear;
printf("Next state is %s\n",state_machine[current_state].next_state);
printf("Light is %s\n",state_machine[current_state].light);
printf("Direction valve is %s\n",state_machine[current_state].direction_valve);
printf("Hydraulic control is %s\n",state_machine[current_state].hydraulic_control);
}
else
{
checking_before_take_off();
}
}
void raising_gear()
{
current_state=Raising_Gear;
printf("Enter the pilot lever and limit switch\n");
scanf("%d %d",(int*)&a,(int*)&limit);
if(a==Falling)
{
current_state=Checking_before_Landing;
printf("Next state is %s\n",state_machine[current_state].next_state);
printf("Light is %s\n",state_machine[current_state].light);
printf("Direction valve is %s\n",state_machine[current_state].direction_valve);
printf("Hydraulic control is %s\n",state_machine[current_state].hydraulic_control);
}
else if(limit==Close)
{
current_state=Gear_Up;
printf("Next state is %s\n",state_machine[current_state].next_state);
printf("Light is %s\n",state_machine[current_state].light);
printf("Direction valve is %s\n",state_machine[current_state].direction_valve);
printf("Hydraulic control is %s\n",state_machine[current_state].hydraulic_control);
}
}
void gear_up()
{
current_state=Gear_Up;
printf("Enter the pilot switch position\n");
scanf("%d",(int*)&a);
if(a==Falling)
{
current_state=Checking_before_Landing;
}
else
{
gear_up();
}
}
void checking_before_landing()
{
current_state=Checking_before_Landing;
printf("Enter the pilot lever and hydraulic machine position\n");
scanf("%d %d",(int*)&a,(int*)&b);
if(a==Down||b==Not_Working)
{
current_state=Lowering_Gear;
}
else
{
checking_before_landing();
}
}
void lowering_gear()
{
current_state=Lowering_Gear;
printf("Enter the limit switch and pilot lever position\n");
scanf("%d %d",(int*)&limit,(int*)&a);
if(limit==Open)
{
current_state= Gear_down;
}
else if(a==Rising)
{
current_state=Checking_before_Take_off;
}
else
{
lowering_gear();
}
}
Explanation:
As per flow diagram, there are six state types : Gear down, checking before taking off with a delay of 3 seconds, raising gear, gear up, checking before landing, lowering gear. I have taken two switch positions open and close. For pilot lever, I have considered four positions: rising , falling, up and down. For hydraulic mechanism, I have considered two enum data variable: working and not working. All these are declared as global variables using enum. Initially the current state is taken as gear down. If the pilot lever is rising and squat switch is open that means the airplane is at next stage that is checking before take off else the gear is still down.
- For the next state(rising up), the pilot lever is up and the hydraulic mechanism is working.
- For Gearing up state, the limit switch is closed.
- For checking before landing state, the pilot lever is falling.
- For Lowering gear, pilot's lever is down.
- For gearing down, limit switch is opened
Using the if else statement , the above mentiones states are assigned to the next state accordingly.