Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Live batch
1)Airplane’s landing gear can be implemented using Finite State Machine (FSM).
Ans)
#include
#include
//declaring functions for each states
void GearDown();
void CheckingBeforeTakeOFF();
void RisingGear();
void GearUp();
void CheckBeforeLanding();
void LoweringGear();
//creating an array of 6 states in order to call the function upon requirement
static void(*statetable[])(void)={GearDown, CheckingBeforeTakeOFF, RisingGear, GearUp, CheckBeforeLanding, LoweringGear};
//gear states
typedef enum State
{
GEAR_DOWN,
CHECKING_BEFORE_TAKEOFF,
RISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR,
}State_Type;
//switch states
typedef enum Switch
{
open, closed
}Switch_status;
//pilot lever states
typedef enum pilot_lever
{
down, falling, rising, up
}pilot_lever;
//hydraulic control states
typedef enum hydraulic_mechanism
{
enabled, disabled
}hydraulic_mechanism;
static volatile Switch_status squat_switch;
static volatile Switch_status limit_switch;
static volatile pilot_lever pl;
static volatile hydraulic_mechanism hm;
State_Type current_state;
//state table elements
typedef struct
{
char* current_state_indication;
char* light;
char* direction_valve_status;
int time;
char* hydraulic_control;
char* pilot_lever_status;
}State_Table;
//creating state table
static State_Table State_Machine[6]={
{"Gear Down", "Green", "Down", 3, "Enabled", "Down"},
{"Checking Before TakeOFF", "Green", "Down", 3, "Enabled", "rising"},
{"Rising Gear", "Red", "Up", 3, "Enabled", "Up"},
{"Gear Up", "Off", "Up", 3, "Disabled", "Up"},
{"Check Before Landing", "Red", "Down", 3, "Enabled", "falling"},
{"Lowering Gear", "Green", "Down", 3, "Enabled", "Down"}
};
int main()
{
while(1)
{
statetable[current_state](); //calling each state functions
}
}
//when airplane is grounded or parked
void GearDown(void)
{
current_state = GEAR_DOWN;
printf("Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("Pilots lever status : %s\n",State_Machine[current_state].pilot_lever_status);
printf("Light status : %s\n", State_Machine[current_state].light);
printf("Direction valve status : %s\n", State_Machine[current_state].direction_valve_status);
printf("Hydraulic control status : %s\n", State_Machine[current_state].hydraulic_control);
printf("Time delay : %d seconds\n\n", State_Machine[current_state].time);
printf("Enter the status of pilot's lever and squat switch: ");
fflush(stdout);
scanf("%d %d", &pl, &squat_switch);
if(pl == rising && squat_switch == open)
{
current_state = CHECKING_BEFORE_TAKEOFF;
printf("Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("Pilots lever status : %s\n",State_Machine[current_state].pilot_lever_status);
printf("Light status : %s\n", State_Machine[current_state].light);
printf("Direction valve status : %s\n", State_Machine[current_state].direction_valve_status);
printf("Hydraulic control status : %s\n", State_Machine[current_state].hydraulic_control);
printf("Time delay : %d seconds\n\n", State_Machine[current_state].time);
}
else
{
GearDown();
}
}
//when airplane is about to take off
void CheckingBeforeTakeOFF(void)
{
current_state = CHECKING_BEFORE_TAKEOFF;
printf("Enter the status of pilot's lever and squat switch: ");
fflush(stdout);
scanf("%d %d", &pl, &squat_switch);
if(pl == falling && squat_switch == closed)
{
GearDown();
}
else if(pl == up && squat_switch == open)
{
current_state = RISING_GEAR;
printf("Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("Pilots lever status : %s\n",State_Machine[current_state].pilot_lever_status);
printf("Light status : %s\n", State_Machine[current_state].light);
printf("Direction valve status : %s\n", State_Machine[current_state].direction_valve_status);
printf("Hydraulic control status : %s\n", State_Machine[current_state].hydraulic_control);
printf("Time delay : %d seconds\n\n", State_Machine[current_state].time);
}
}
//when airplane is taking off
void RisingGear(void)
{
current_state = RISING_GEAR;
printf("Enter the status of pilot's lever and limit switch:");
fflush(stdout);
scanf("%d %d", &pl, &limit_switch);
if(pl == falling && limit_switch == closed)
{
current_state = CHECK_BEFORE_LANDING;
printf("Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("Pilots lever status : %s\n",State_Machine[current_state].pilot_lever_status);
printf("Light status : %s\n", State_Machine[current_state].light);
printf("Direction valve status : %s\n", State_Machine[current_state].direction_valve_status);
printf("Hydraulic control status : %s\n", State_Machine[current_state].hydraulic_control);
printf("Time delay : %d seconds\n\n", State_Machine[current_state].time);
}
else if(pl == up && limit_switch == closed)
{
current_state = GEAR_UP;
printf("Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("Pilots lever status : %s\n",State_Machine[current_state].pilot_lever_status);
printf("Light status : %s\n", State_Machine[current_state].light);
printf("Direction valve status : %s\n", State_Machine[current_state].direction_valve_status);
printf("Hydraulic control status : %s\n", State_Machine[current_state].hydraulic_control);
printf("Time delay : %d seconds\n\n", State_Machine[current_state].time);
}
}
//when airplane is in airborne
void GearUp(void)
{
current_state = GEAR_UP;
printf("Enter the status of pilot's lever and hydraulic control: ");
fflush(stdout);
scanf("%d %d",&pl, &hm);
if(pl == falling && hm == enabled)
{
current_state = CHECK_BEFORE_LANDING;
printf("Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("Pilots lever status : %s\n",State_Machine[current_state].pilot_lever_status);
printf("Light status : %s\n", State_Machine[current_state].light);
printf("Direction valve status : %s\n", State_Machine[current_state].direction_valve_status);
printf("Hydraulic control status : %s\n", State_Machine[current_state].hydraulic_control);
printf("Time delay : %d seconds\n\n", State_Machine[current_state].time);
}
else if(pl == up && hm == disabled)
{
GearUp();
}
}
//when airplane is about to land
void CheckBeforeLanding(void)
{
current_state = CHECK_BEFORE_LANDING;
printf("Enter the status of pilot's lever and hydraulic control: ");
fflush(stdout);
scanf("%d %d", &pl, &hm);
if(pl == down)
{
current_state = LOWERING_GEAR;
printf("Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("Pilots lever status : %s\n",State_Machine[current_state].pilot_lever_status);
printf("Light status : %s\n", State_Machine[current_state].light);
printf("Direction valve status : %s\n", State_Machine[current_state].direction_valve_status);
printf("Hydraulic control status : %s\n", State_Machine[current_state].hydraulic_control);
printf("Time delay : %d seconds\n\n", State_Machine[current_state].time);
}
else if(pl == falling)
{
CheckBeforeLanding();
}
}
//when airplane is landing
void LoweringGear(void)
{
current_state = LOWERING_GEAR;
printf("Enter the status of pilot's lever and limit switch:");
fflush(stdout);
scanf("%d %d", &pl, &limit_switch);
if(pl == rising)
{
current_state = CHECKING_BEFORE_TAKEOFF;
printf("Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("Pilots lever status : %s\n",State_Machine[current_state].pilot_lever_status);
printf("Light status : %s\n", State_Machine[current_state].light);
printf("Direction valve status : %s\n", State_Machine[current_state].direction_valve_status);
printf("Hydraulic control status : %s\n", State_Machine[current_state].hydraulic_control);
printf("Time delay : %d seconds\n\n", State_Machine[current_state].time);
}
else if(pl == down && limit_switch == open)
{
current_state = GEAR_DOWN;
printf("Current state : %s\n",State_Machine[current_state].current_state_indication);
printf("Pilots lever status : %s\n",State_Machine[current_state].pilot_lever_status);
printf("Light status : %s\n", State_Machine[current_state].light);
printf("Direction valve status : %s\n", State_Machine[current_state].direction_valve_status);
printf("Hydraulic control status : %s\n", State_Machine[current_state].hydraulic_control);
printf("Time delay : %d seconds\n\n", State_Machine[current_state].time);
}
}