Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
AIRPLANE'S LANDING GEAR
#include
// Function prototypes
void geardown();
void checking_before_takeoff();
void raising_gear();
void gearup();
void checking_before_landing();
void lowering_gear();
// Enum definitions
typedef enum
{
STATE_GearDown,
STATE_CheckingBeforeTakeoff,
STATE_RaisingGear,
STATE_GearUp,
STATE_CheckingBeforeLanding,
STATE_LoweringGear
} StateType;
typedef enum
{
SWITCH_Open,
SWITCH_Close
} Switch;
typedef enum
{
PILOT_LEVER_Rising,
PILOT_LEVER_Falling,
PILOT_LEVER_Up,
PILOT_LEVER_Down
} PilotLever;
typedef enum
{
HYDRAULIC_WORKING,
HYDRAULIC_NOT_WORKING
} HydraulicMechanism;
// Structure definition
typedef struct
{
//char* current_state_status;
char* nextstate;
char* light;
char* direction_valve;
char* hydraulic_control;
} StateData;
// State table
static void (*state_table[])(void) = {
geardown,
checking_before_takeoff,
raising_gear,
gearup,
checking_before_landing,
lowering_gear
};
// State machine data
static StateData state_machine[] = {
{"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"},
};
// Current state variable
static StateType Currentstate = STATE_GearDown;
// Function implementations
int main()
{
while (1)//Continous run to display current state
{
state_table[Currentstate]();
}
return 0;
}
// Geardown Operation
void geardown()
{
printf("Current state: %s\n", state_machine[Currentstate].nextstate);
fflush(stdout);
PilotLever pil;
Switch squat;
scanf("%d %d", &pil, &squat);// Input for Gear down PilotLever and Switch
if (pil == PILOT_LEVER_Rising && squat == SWITCH_Open)//if conditon for CheckingBeforeTakeoff
{
Currentstate = STATE_CheckingBeforeTakeoff;
printf("Light: %s, Direction Valve: %s, Hydraulic Control: %s\n",
state_machine[Currentstate].light, state_machine[Currentstate].direction_valve,
state_machine[Currentstate].hydraulic_control);
}
else
{
geardown();
}
}
// Check Before takeoff Operation
void checking_before_takeoff()
{
printf("Current state: %s\n", state_machine[Currentstate].nextstate);
fflush(stdout);
PilotLever pil;
Switch squat;
HydraulicMechanism hyd;
scanf("%d %d %d", &pil, &squat, &hyd);// Input for Pilotlever, Switch and Hydraulic Mechanics
if (pil == PILOT_LEVER_Falling && squat == SWITCH_Close)//Condition for GearDown
{
Currentstate = STATE_GearDown;
printf("Light: %s, Direction Valve: %s, Hydraulic Control: %s\n",
state_machine[Currentstate].light, state_machine[Currentstate].direction_valve,
state_machine[Currentstate].hydraulic_control);
}
else if (pil == PILOT_LEVER_Up && hyd == HYDRAULIC_WORKING)//Condition for Raising Gear
{
Currentstate = STATE_RaisingGear;
printf("Light: %s, Direction Valve: %s, Hydraulic Control: %s\n",
state_machine[Currentstate].light, state_machine[Currentstate].direction_valve,
state_machine[Currentstate].hydraulic_control);
}
else
{
checking_before_takeoff();
}
}
// Raising Gear Operation
void raising_gear()
{
printf("Current state: %s\n", state_machine[Currentstate].nextstate);
fflush(stdout);
PilotLever pil;
Switch limit;
scanf("%d %d", &pil, &limit);//Input for PilotLever & Switch
if (pil == PILOT_LEVER_Falling)// Condition for CheckingBeforeLanding
{
Currentstate = STATE_CheckingBeforeLanding;
printf("Light: %s, Direction Valve: %s, Hydraulic Control: %s\n",
state_machine[Currentstate].light, state_machine[Currentstate].direction_valve,
state_machine[Currentstate].hydraulic_control);
}
else if (limit == SWITCH_Close)// Condtion for GearUp
{
Currentstate = STATE_GearUp;
printf("Light: %s, Direction Valve: %s, Hydraulic Control: %s\n",
state_machine[Currentstate].light, state_machine[Currentstate].direction_valve,
state_machine[Currentstate].hydraulic_control);
}
else
{
raising_gear();
}
}
//GearUp Operation
void gearup()
{
printf("Current state: %s\n", state_machine[Currentstate].nextstate);
fflush(stdout);
PilotLever pil;
scanf("%d", &pil);//Input for PilotLever
if (pil == PILOT_LEVER_Falling)//Condition for Checking Before Landing
{
Currentstate = STATE_CheckingBeforeLanding;
}
else
{
gearup();
}
}
//Check Befor Landing
void checking_before_landing()
{
printf("Current state: %s\n", state_machine[Currentstate].nextstate);
fflush(stdout);
PilotLever pil;
HydraulicMechanism hyd;
scanf("%d %d", &pil, &hyd);//Input for PilotLever & Hydraulic Mechanism
if (pil == PILOT_LEVER_Down || hyd == HYDRAULIC_NOT_WORKING)
{
Currentstate = STATE_LoweringGear;
}
else
{
checking_before_landing();
}
}
// Lowering gear
void lowering_gear()
{
printf("Current state: %s\n", state_machine[Currentstate].nextstate);
fflush(stdout);
Switch limit;
PilotLever pil;
scanf("%d %d", &limit, &pil);//Input For Switch & PilotLever
if (limit == SWITCH_Open)//Condtion for GearDown
{
Currentstate = STATE_GearDown;
}
else if (pil == PILOT_LEVER_Rising)// Condtion for Checking Before Takeoff
{
Currentstate = STATE_CheckingBeforeTakeoff;
}
else
{
lowering_gear();
}
}
OUTPUT
Current state: Gear Down
0
0
Light: Green, Direction Valve: Down, Hydraulic Control: Enabled
Current state: Checking before takeoff
2
2
0
Light: Red, Direction Valve: Up, Hydraulic Control: Enabled
Current state: Raising Gear
0
1
Light: off, Direction Valve: Null, Hydraulic Control: Disabled
Current state: Gear up
1
Current state: Checking before landing
0
1
Current state: Lowering gear
1
0
Current state: Checking before takeoff
2
2
0
Light: Red, Direction Valve: Up, Hydraulic Control: Enabled
Current state: Raising Gear
1
0
Light: Red, Direction Valve: Down, Hydraulic Control: Enabled
Current state: Checking before landing
0
1
Current state: Lowering gear
0
1
Current state: Gear Down