Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include <stdio.h> // preprocessor directives
// fun prototype
void initial_state_machine();
void geardown();
void gearup();
void checking_before_takeoff();
void checking_before_landing();
void loweringgear();
void raisinggear();
// decl of states in enum type
typedef enum states
{
gear_down,
check_before_takeoff,
raising_gear,
gear_up,
check_before_landing,
lowering_gear
}state_type;
state_type current_state;
// decl switch states as enum data type
typedef enum switchs
{
close,
open
}switch_status;
// decl pilot lever states as enum data type
typedef enum pilot_lever
{
falling,
rising,
upe,
downe
}pilot_lever_status;
//decl directive valve states as enum data type
typedef enum direction
{
down,
up
}direction_valve;
typedef enum hydralic_mechanism
{
malfunction,
working
}hydralic_mechanism_status;
// decl of variables in static
static volatile switch_status squart_switch;
static volatile switch_status limit_switch;
static volatile pilot_lever_status pilot_lever;
static volatile pilot_lever_status hydralic_mechanism;
static volatile direction_valve_status;
// structure
typedef struct
{
char* current_state_status; // structure members
char* light_status;
char* direction_valve_status;
char* hydralic_pump_ctrl_status;
}state_table;
// structure instance
static state_table state_machine[6]={
{"geardown","green","down","enable"},
{"checking before take off","red","down","enabled"},
{"raising gear","off","up","enabled"},
{"gear up","red","NULL","disabled"},
{"checking before landing","red","down","enabled"},
{"lowering gear","green","down","enabled"}
};
void geardown()
{
current_state = gear_down; // current state of the machine
printf("\n");
// print the state of the machine
printf("\n current state of the machine is : %s\n",state_machine[current_state].current_state_status);
printf("\n light status of the machine : %s\n",state_machine[current_state].light_status);
printf("\n direction status of the machine is : %s\n",state_machine[current_state].direction_valve_status);
printf("\n hydralic pump status of the machine is : %s\n",state_machine[current_state].hydralic_pump_ctrl_status);
printf("\n");
// for moving the next state it ask some input from user
printf("\n enter the pilot lever state ,squate switch status : \n");
scanf("%d %d",&pilot_lever,&squart_switch );
if(pilot_lever== rising && squart_switch == open)//assigning the check the condition
{
checking_before_takeoff();//fun call
}
else
{
geardown();
}
}
void checking_before_takeoff()
{
current_state = check_before_takeoff; //assigning the current state of the machine
printf("\n");
// print the state of the machine
printf("\n current state of the machine is : %s\n",state_machine[current_state].current_state_status);
printf("\nlight status of the machine : %s\n",state_machine[current_state].light_status);
printf("\ndirection status of the machine is : %s\n",state_machine[current_state].direction_valve_status);
printf("\n hydralic pump status of the machine is : %s\n",state_machine[current_state].hydralic_pump_ctrl_status);
printf("\n");
printf("\n enter the pilot lever state ,direction valve status :\n");
scanf("%d %d",&pilot_lever,&direction_valve_status); //ask input from user
if(pilot_lever== upe && direction_valve_status == up )//if the condition satisfy goto the raisinggear() fun
{
raisinggear();
}
else
geardown();
}
void raisinggear()
{
current_state = raising_gear;//assigning the current state of the machine
printf("\n");
// print the state of the machine
printf("\n current state of the machine is : %s\n",state_machine[current_state].current_state_status);
printf("\nlight status of the machine : %s\n",state_machine[current_state].light_status);
printf("\ndirection status of the machine is : %s\n",state_machine[current_state].direction_valve_status);
printf("\n hydralic pump status of the machine is : %s\n",state_machine[current_state].hydralic_pump_ctrl_status);
printf("\n");
printf("\n---for gear up enter the limit switch status closed---\n");
printf("\n---for check before landing enter switch value is 1 ---\n");
printf("\n enter the limit switch states :\n");
scanf("%d",&limit_switch);
if(limit_switch==close )//if the condition satisfy goto the gear up fun
{
gearup();
}
else
{
printf("\n enter the pilot lever states :\n");
scanf("%d",&pilot_lever);
if(pilot_lever==falling)
checking_before_landing();//fun call
}
}
void gearup()
{
printf("\n");
current_state = gear_up;//assigning the current state of the machine
// print the state of the machine
printf("\n current state of the machine is : %s\n",state_machine[current_state].current_state_status);
printf("\nlight status of the machine : %s\n",state_machine[current_state].light_status);
printf("\ndirection status of the machine is : %s\n",state_machine[current_state].direction_valve_status);
printf("\n hydralic pump status of the machine is : %s\n",state_machine[current_state].hydralic_pump_ctrl_status);
printf("\n");
printf("\n enter the pilot lever states :\n");
scanf("%d",&pilot_lever);
if(pilot_lever==falling)
checking_before_landing();//fun call
}
void checking_before_landing()
{
current_state= check_before_landing;//assigning the current state of the machine
// print the state of the machine
printf("\n current state of the machine is : %s\n",state_machine[current_state].current_state_status);
printf("\nlight status of the machine : %s\n",state_machine[current_state].light_status);
printf("\ndirection status of the machine is : %s\n",state_machine[current_state].direction_valve_status);
printf("\n hydralic pump status of the machine is : %s\n",state_machine[current_state].hydralic_pump_ctrl_status);
printf("\n");
printf("\n enter the pilot lever states and direction valve status :\n");
scanf("%d%d",&pilot_lever,&direction_valve_status);
if(pilot_lever==downe && direction_valve_status==down)//if the condition satisfy goto the loweringgear() fun
loweringgear();
}
void loweringgear()
{
current_state=lowering_gear;//assigning the current state of the machine
printf("\n");
// print the state of the machine
printf("\n current state of the machine is : %s\n",state_machine[current_state].current_state_status);
printf("\nlight status of the machine : %s\n",state_machine[current_state].light_status);
printf("\ndirection status of the machine is : %s\n",state_machine[current_state].direction_valve_status);
printf("\n hydralic pump status of the machine is : %s\n",state_machine[current_state].hydralic_pump_ctrl_status);
printf("\n");
printf("----for gear down status enter the switch value 1-----");
printf("-----for going to check before takeoff enter the switch value 0-----");
printf("\n enter the limit switch status :\n");
scanf("%d",&limit_switch);
if(limit_switch==open)
geardown();
printf("\n enter the pilot lever status :\n");
scanf("%d",&pilot_lever);
if(pilot_lever==rising)
checking_before_takeoff();
}
void initial_state_machine() //start of the fun inside the fun i am calling the geardown fun
{
printf("\t---pilot lever--- \n");
printf("\tfalling = 0\n" );
printf("\trising = 1\n");
printf("\tupe = 2\n");
printf("\tdowne = 3\n");
printf("\tgeardown = 4\n");
printf("\n");
printf("\t---switch status---\n");
printf("\tclose = 0\n");
printf("\topen = 1\n");
printf("\n");
printf("\t---direction valve---\n");
printf("\tup = 1\n");
printf("\tdown = 0\n");
geardown();
}
int main()
{
while(1)//while loop for creating the infinite loop
{
initial_state_machine();//fun call
}
}