Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Overview of the project:
In this project, controlling the retraction and extension of Airplane’s landing gear can be implemented using Finite State Machine (FSM). FSM is the most efficient algorithm which is mathematical model of computation.
Aim of the project:
Implement the Code for controlling the retraction and extension of Airplane’s landing gear.
outcome:
The student will be getting the Knowledge of Finite State Machine algorithm which is used to achieve the control over the retraction and extension of Airplane’s landing gear using the switch and gear states of Airplane.
program code:
/*
* file name:FSM.c
* objective:Implement the Code for controlling the retraction and extension of Airplane’s landing gear.
* Created on: 13-oct-2022
* Author: Zeba khanum
*/
//preprocessor section
#include<stdio.h>
#include<stdlib.h>
//function declaration
void Gear_down_func(void);
void CheckingBefore_takeoff_func(void);
void Raising_gear_func(void);
void Gear_up_func(void);
void checkingBefore_landing_func(void);
void Lowering_gear_func(void);
//enumerated datatype states allias name state_Type
typedef enum states
{
Gear_down,
CheckingBefore_takeoff,
Raising_gear,
Gear_up,
checkingBefore_landing,
Lowering_gear
}state_Type;
//current_state of enumerated datatype state_Type
state_Type current_state;
//enum datatype switches
typedef enum
{
on,
off
}switches;
//enum datatype lever
typedef enum
{
raising,
falling
}lever;
//enum datatype control_gas_system
typedef enum
{
enabled,
disabled
}control_gas_system;
static lever pilot_lever;
static switches squat_switch;
static switches limit_switch;
static control_gas_system Hydraulic_system_control;
//structure of char* named state_table1
typedef struct
{
char* indication;
char* light;
char* direction_valve_status;
char* gas_pressurized_system[2];
char* hydraulic;
}State_table1;
//state_machine implementation
static State_table1 state_machine[6]=
{
{"Gear_down","Green", "Down", "Enabled", {NULL,NULL}},
{"CheckingBefore_takeoff","Green", "Down", "Enabled", {NULL,NULL}},
{"Raising_gear", "Red", "up", "Enabled", {"Disabled", "Enabled"}},
{"Gear_up", "off", "Null", "Disabled", {NULL, NULL}},
{"checkingBefore_landing", "Red", "Down", "Enabled", {NULL,NULL}},
{"Lowering_gear", "Green", "Down","Enabled", {"Disabled","Enabled"}}
};
//function pointer
static void (*state_table[]) (void) = {Gear_down_func,
CheckingBefore_takeoff_func,
Raising_gear_func,
Gear_up_func,
checkingBefore_landing_func,
Lowering_gear_func };
//main function
int main()
{
while(1)
{
state_table[current_state]();
}
}
//function definition of Gear_down_func
void Gear_down_func(void)
{
current_state = Gear_down;
printf("enter the lever and squat switch status\n");
fflush(stdout);
scanf("%d %d", &pilot_lever, &squat_switch);
if(pilot_lever==raising && squat_switch==on)
{
current_state=CheckingBefore_takeoff;
printf("current state = %s\n ",state_machine[current_state].indication);
printf("light = %s\n", state_machine[current_state].light );
}
else
{
Gear_down_func();
}
}
//function definition of CheckingBefore_takeoff_func
void CheckingBefore_takeoff_func(void)
{
current_state = CheckingBefore_takeoff;
printf("enter the lever, squat switch and hydraulic system status\n");
fflush(stdout);
scanf("%d %d %d", &pilot_lever, &squat_switch, &Hydraulic_system_control);
if(pilot_lever==raising && squat_switch==on && Hydraulic_system_control == enabled)
{
current_state=Raising_gear;
printf("current state = %s\n" ,state_machine[current_state].indication);
printf("light = %s\n",state_machine[current_state].light );
printf("direction valve status = %s\n", state_machine[current_state].direction_valve_status);
}
else if(pilot_lever==raising && squat_switch==on && Hydraulic_system_control == disabled)
{
current_state=Raising_gear;
printf("current state = %s\n" ,state_machine[current_state].indication);
printf("light = %s\n",state_machine[current_state].light );
printf("direction valve status = %s\n", state_machine[current_state].direction_valve_status);
printf("gas pressuried system status = %s\n", state_machine[current_state].gas_pressurized_system);
}
else
{
Gear_down_func();
}
}
//function definition of Raising_gear_func
void Raising_gear_func(void)
{
current_state = Raising_gear;
printf("enter the pilot lever and limit_switch\n");
fflush(stdout);
scanf("%d %d", &pilot_lever, &limit_switch);
if(pilot_lever==falling && limit_switch==off)
{
current_state=Gear_up;
printf("current state = %s\n ",state_machine[current_state].indication);
printf("light = %s\n", state_machine[current_state].light );
printf("landing gear hydraulic control = %s\n", state_machine[current_state].hydraulic);
}
else if(pilot_lever==falling && limit_switch==on)
{
current_state=checkingBefore_landing;
printf("current state = %s\n ",state_machine[current_state].indication);
printf("light = %s\n", state_machine[current_state].light );
printf("landing gear hydraulic control = %s\n", state_machine[current_state].hydraulic);
}
else
{
Raising_gear_func();
}
}
//function definition of Gear_up_func
void Gear_up_func(void)
{
current_state = Gear_up;
printf("enter the pilot lever status\n");
fflush(stdout);
scanf("%d",&pilot_lever);
if(pilot_lever==falling)
{
current_state=checkingBefore_landing;
printf("current state = %s\n ",state_machine[current_state].indication);
printf("light = %s\n", state_machine[current_state].light );
printf("landing gear hydraulic control = %s\n", state_machine[current_state].hydraulic);
}
else
{
Gear_down_func();
}
}
//function definition of checkingBefore_landing_func
void checkingBefore_landing_func(void)
{
current_state = checkingBefore_landing;
printf("enter the pilot lever and hydraulic mechanism status\n");
fflush(stdout);
scanf("%d %d",&pilot_lever, &Hydraulic_system_control);
if(pilot_lever== falling && Hydraulic_system_control==enabled)
{
current_state=Lowering_gear;
printf("current state = %s\n" ,state_machine[current_state].indication);
printf("light = %s\n",state_machine[current_state].light );
printf("direction valve status = %s\n", state_machine[current_state].direction_valve_status);
}
else if(pilot_lever== falling && squat_switch==on && Hydraulic_system_control==disabled)
{
current_state=Lowering_gear;
printf("current state = %s\n" ,state_machine[current_state].indication);
printf("light = %s\n",state_machine[current_state].light );
printf("direction valve status = %s\n", state_machine[current_state].direction_valve_status);
printf("gas pressurized system = %s\n", state_machine[current_state].gas_pressurized_system);
}
else
{
checkingBefore_landing_func();
}
}
//function definition of Lowering_gear_func
void Lowering_gear_func(void)
{
current_state = Lowering_gear;
printf("enter the pilot's lever and limit switch status\n");
fflush(stdout);
scanf("%d %d", &pilot_lever, &limit_switch);
if(pilot_lever==raising && limit_switch==off)
{
current_state=Gear_down;
printf("current state = %s\n ",state_machine[current_state].indication);
printf("light = %s\n", state_machine[current_state].light );
printf("landing gear hydraulic control = %s\n", state_machine[current_state].hydraulic);
}
else if(pilot_lever==raising && limit_switch==on)
{
current_state=CheckingBefore_takeoff;
printf("current state = %s\n ",state_machine[current_state].indication);
printf("light = %s\n", state_machine[current_state].light );
}
else
{
Lowering_gear_func();
}
}
Output of the code: