Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Online live batch 2
1.
// Include Library
#include <stdio.h>
//Initialize global variables not to repeat the same state
int i = 0,j = 0;
// Create data state type
typedef enum State
{
GD,
BT,
RG,
GU,
BL,
LG
}State_Type;
// Create data switch status
typedef enum Switch
{
Open = 0,
Close = 1
}Switch_Status;
// Create data pilot lever
typedef enum Pilot_Lever
{
Rising = 0,
Falling = 1,
UP = 2,
DOWN = 3
}Pilot_Lever_Status;
// Create data for output
typedef struct
{
char* current_state_indication;
char* light_Status;
char* direction_value_status;
}State_Table;
//Transistion table
static State_Table State_Machine[6] = {
{"GearDown","Green","Down"},
{"Checking Before Take Off ","Green","Down"},
{"Raising Gear","Red","Up"},
{"Gear Up","Off","DISABLED"},
{"Checking Before Landing","Red","Down"},
{"Lowering Gear","Green","Down"}
};
// Initialize the rename for our convineance
static volatile Switch_Status switchs;
static volatile Pilot_Lever_Status pilot_lever;
State_Type current_state;
int main()
{
// Initially it will be in down state
current_state = GD;
printf("\nState Machine is initialized & Program is in Gear_Down State & Light_Status : %s\n",State_Machine[current_state].light_Status);
while(1)
{
printf("\n Enter the Pilot's Lever Position and Switch Status:");
scanf("%d %d",&pilot_lever,&switchs); // Get the user data
// Condition for Before takeoff
if (pilot_lever == Rising && switchs == Open)
{
current_state = BT;
printf("\n Current State:%s\n",State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
i=0;
}
// Condition for gear down
else if((pilot_lever == Falling && i==0)|| (switchs == Close && i==0))
{
current_state = GD;
printf("\n Current State:%s\n",State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
i=1;
}
// Condition for rising gear
else if (pilot_lever == UP && switchs == Open)
{
current_state = RG;
printf("\n Wait for 3 secs\n");
printf("\n Current State: %s\n",State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
printf("Direction Value Status:%s\n\n",State_Machine[current_state].direction_value_status);
i=1;
}
// Condition for Before landing
else if(pilot_lever == Falling && switchs == Open && i==0)
{
current_state = BL;
printf ("\n Current State: %s\n",State_Machine[current_state].current_state_indication);
printf ("Light Status:%s\n",State_Machine[current_state].light_Status);
i=1;
j=0;
}
// Condition for gear up
else if (switchs == Close && pilot_lever == UP)
{
current_state = GU;
printf ("\n Current State: %s\n",State_Machine[current_state].current_state_indication);
printf ("Light Status:%s\n",State_Machine[current_state].light_Status);
}
// Condition for Before landing
else if(pilot_lever == Falling && i==1)
{
current_state = BL;
printf("\n Current State:%s\n",State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
j=0;
}
// Condition for lowering gear
else if(pilot_lever == DOWN && j==0)
{
current_state = LG;
printf("\nCurrent State : %s\n",State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
printf("Direction Valve Status:%s\n",State_Machine[current_state].direction_value_status);
j=1;
}
// Condition for Before takeoff
else if(switchs == Close && pilot_lever == Rising)
{
current_state = BT;
printf("\n Current State: %s\n",State_Machine[current_state].current_state_indication);
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
}
// Condition for gear down
else if(switchs == Open && pilot_lever == DOWN && j==1)
{
current_state = GD;
printf ("\n Current State: %s\n",State_Machine[current_state].current_state_indication);
printf ("Light Status:%s\n",State_Machine[current_state].light_Status);
j=0;
i=0;
}
}
}
OUTPUT:
State Machine is initialized & Program is in Gear_Down State & Light_Status : Green
Enter the Pilot's Lever Position and Switch Status:0
0
Current State:Checking Before Take Off
Light Status:Green
Enter the Pilot's Lever Position and Switch Status:1
1
Current State:GearDown
Light Status:Green
Enter the Pilot's Lever Position and Switch Status:0
0
Current State:Checking Before Take Off
Light Status:Green
Enter the Pilot's Lever Position and Switch Status:2
0
Wait for 3 secs
Current State: Raising Gear
Light Status:Red
Direction Value Status:Up
Enter the Pilot's Lever Position and Switch Status:2
1
Current State: Gear Up
Light Status:Off
Enter the Pilot's Lever Position and Switch Status:3
1
Current State : Lowering Gear
Light Status:Green
Direction Valve Status:Down
Enter the Pilot's Lever Position and Switch Status:0
1
Current State: Checking Before Take Off
Light Status:Green
Enter the Pilot's Lever Position and Switch Status:3
0
Current State: GearDown
Light Status:Green
Enter the Pilot's Lever Position and Switch Status: