Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
1. Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Using finite state machine,
Goals:
Valve's direction can be controlled.
Controlling the retraction and extension of the landing gear on an aeroplane.
#include <stdio.h>
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_Typecurrent_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 (*position[])(void)={gear_down,checking_before_take_off,raising_gear,gear_up,checking_before_landing,lowering_gear};
static positionstate_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)
{
position[current_state]();
}
}
void gear_down()
{
current_state=Gear_down;//Enum Value - Gear Down
scanf("%d %d",&a,&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;
scanf ("%d %d %d",&a,&squat,&b);
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);
}
elseif ((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;
scanf ("%d %d",&a,&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);
}
elseif(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;
scanf("%d",&a);
if(a==Falling)
{
current_state=Checking_before_Landing;
}
else
{
gear_up();
}
}
void checking_before_landing()
{
current_state=Checking_before_Landing;
scanf("%d %d",&a,&b);
if(a==Down||b==Not_Working)
{
current_state=Lowering_Gear;
}
else
{
checking_before_landing();
}
}
void lowering_gear()
{
current_state=Lowering_Gear;
scanf("%d %d",&limit,&a);
if(limit==Open)
{
current_state= Gear_down;
}
elseif(a==Rising)
{
current_state=Checking_before_Take_off;
}
else
{
lowering_gear();
}
}