Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Offline Embedded Batch - 2
//code for controlling the retraction & extention of airplane's landing gear
#include <stdio.h> //preprocessor header file
#include <stdlib.h>
void initial_state_machine(); //initial state function declaration
void Gear_down(); //gear down function declaration
void checking_before_takeoff(); //checking before takeoff function declaration
void Raising_gear(); //raising gear function declaration
void Gear_up(); //gear up function declaration
void checking_before_landing(); //checking before landing function declaration
void Lowering_gear(); //lowering gear function declaration
typedef enum state //enum datatype for different states
{
gear_down,
check_before_takeoff,
raising_gear,
gear_up,
check_before_landing,
lowering_gear
}state_type;
typedef enum pilot_lev //enum for different position of pilot's lever
{
raising,
falling,
up,
down
}pilot;
typedef enum Switch //enum for position of the switch
{
open,
close
}Switch_status;
typedef struct //create a structure to check the position of airplane to print given input and output
{
char* current_state_status;
char* light_status;
char* valve_status;
}state_table;
static volatile state_type current_state;
static volatile pilot pilot_lever;
static volatile Switch_status limit,squat; //create global variable declaration
static state_table state_machine[6]={{"Gear_Down","GREEN","down"},
{"Checking_before_takeoff","GREEN","down"},
{"Raising_Gear","RED","up"},
{"Gear_UP","OFF","disabled" },
{"Checking_before_landing","RED","down"},
{"Lowering_Gear","GREEN","down"}}; //create a airplane input position list using sturcture
static void (*state_tab[])(void)={Gear_down,checking_before_takeoff,Raising_gear,Gear_up,checking_before_landing,Lowering_gear}; //function array using to call the sub functions
int main() //default main function
{
initial_state_machine(); //sub function call to initialize the current state
while(1)
{
state_tab[current_state](); //infinite loop to call the function based on the current state
}
}
void initial_state_machine() //sub function to initiate the current state and program
{
current_state = gear_up;
printf("\nThe Airplane current stage is %s\n",state_machine[current_state].current_state_status);
}
void Gear_up(void) //sub function call to gear up
{
current_state = gear_up;
printf("\nYou are in %s stage",state_machine[current_state].current_state_status);
printf("\nEnter the pilot lever position : ");
fflush(stdout);
scanf("%d",&pilot_lever); //input from user for pilot lever
if(pilot_lever == falling) //check whether the condition is true and it will execute the loop
{
current_state =check_before_landing; //check and store the current state
printf("\nCurrent state is %s\n",state_machine[current_state].current_state_status); //print the airpalne current status
printf("\nLight status is %s\n",state_machine[current_state].light_status); //print the light status
printf("\nDirection valve is %s\n",state_machine[current_state].valve_status); //print the value status
}
}
void Gear_down(void) //sub function to gear down
{
current_state = gear_down;
printf("\nYou are in %s stage",state_machine[current_state].current_state_status);
printf("\nEnter the input for pilot lever status and squat switch status : ");
scanf("%d%d",&pilot_lever,&squat); //input from user for pilot lever and squat switch
if(pilot_lever == raising && squat == open)
{
current_state = check_before_takeoff; //check and store the current state
printf("\nCurrent state is %s\n",state_machine[current_state].current_state_status); //print the current status
printf("\nLight status is %s\n",state_machine[current_state].light_status);
printf("\nDirection valve is %s\n",state_machine[current_state].valve_status);
}
}
void checking_before_landing(void) //sub function call to checking before landing
{
current_state = check_before_landing;
printf("\nYou are in %s stage",state_machine[current_state].current_state_status);
printf("\nEnter the pilot lever position : ");
fflush(stdout);
scanf("%d",&pilot_lever); //input from user for pilot lever
if(pilot_lever == falling) //check the given condition is equal and it will be executed
{
current_state =lowering_gear; //check and store the current state
printf("\nCurrent state is %s\n",state_machine[current_state].current_state_status); //print the airplane condition status
printf("\nLight status is %s\n",state_machine[current_state].light_status);
printf("\nDirection valve is %s\n",state_machine[current_state].valve_status);
}
}
void checking_before_takeoff(void) //sub function to checking before takeoff
{
current_state = check_before_takeoff;
printf("\nYou are in %s stage",state_machine[current_state].current_state_status);
printf("\nEnter the input for pilot lever status and squat switch status : ");
scanf("%d%d",&pilot_lever,&squat); //input from user for pilot lever and squat switch
if(pilot_lever == up && squat == open) //check the given condition is equal and it will be executed
{
current_state = raising_gear; //check and store the current state
printf("\nCurrent state is %s\n",state_machine[current_state].current_state_status); //print all the status
printf("\nLight status is %s\n",state_machine[current_state].light_status);
printf("\nDirection valve is %s\n",state_machine[current_state].valve_status);
}
else
{
Gear_down(); //call the gear down function to print the given output to console
}
}
void Raising_gear(void) //sub function to raising gear
{
current_state = raising_gear;
printf("\nYou are in %s stage",state_machine[current_state].current_state_status);
printf("\nEnter the input for pilot lever status and squat switch status : ");
scanf("%d%d",&pilot_lever,&limit); //input from user for pilot lever and squat switch
if(limit=close)
{
current_state = gear_up; //check and store the current state
printf("\nCurrent state is %s\n",state_machine[current_state].current_state_status);
printf("\nLight status is %s\n",state_machine[current_state].light_status);
printf("\nDirection valve is %s\n",state_machine[current_state].valve_status);
}
else
{
checking_before_landing(); //call the checking before landing function to print the given output to console
}
}
void Lowering_gear(void) //sub function to lowering gear
{
current_state = lowering_gear;
printf("\nYou are in %s stage",state_machine[current_state].current_state_status);
printf("\nEnter the input for pilot lever status and squat switch status : ");
scanf("%d%d",&pilot_lever,&limit); //input from user for pilot lever and squat switch
if(pilot_lever=raising)
{
current_state = check_before_takeoff; //check and store the current state
printf("\nCurrent state is %s\n",state_machine[current_state].current_state_status); //print the current status
printf("\nLight status is %s\n",state_machine[current_state].light_status); //print the light status
printf("\nDirection valve is %s\n",state_machine[current_state].valve_status); //print the direction value status
}
else
{
Gear_down(); //call the gear down function to print the given output to console
}
}