Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Aim:
- To implement the code for control retraction and extension of airplanes landing gear implementation using Finite State Machine (FSM).
Objective:
- Controlling the direction of valve.
- Controlling the retraction and extension of airplane’s landing gear.
Principle:
- The airplane has three gears for landing such as nose gear, left gear and right gear.
- There is a hydraulic pump which is exerting pressure tothe gears to bring it down or pull it up using electrical driven pump.
- When the direction of the valve is down then it is landed or gear down position.
- Similarly when the direction of valve is up then it is in rising gear or taking off.
- When gear is up then light status is off.
- Gear is down then the light status is green.
- Gear is raising or lowering then the colour of light is red.
Code: states
#ifndef AIR_H_ //INCLUDING HEADER FILES//
#define AIR_H_
#include <stdio.h> //PREPROCESSOR
void init_state_machine(); //FUNCTION DECLARING INITIAL STATE MACHINE//
void gear_down(); //FUNCTION DECLARING GEAR DOWN//
void checking_before_takeoff(); //FUNCTION DECLARING CHECKING BEFORE TAKE OFF//
void raising_gear(); //FUNCTION DECLARING RISING GEAR//
void gear_up(); //FUNCTION DECLARING GEAR UP//
void checking_before_landing(); //FUNCTION DECLARING CHECKING BEFORE LANDING//
void lowering_gear(); //FUNCTION DECLARING LOWERING GEAR//
typedef enum State { //CONDITIONS OF AEROPLANE//
GEARDOWN,
CHKBFRTKOFF,
RSGGEAR,
GEARUP,
CHKBFRLAND,
LWRGEAR
}State_Type;
typedef enum Switch {//CONDITIONS OF LIMIT SWITCH & SQUAT SWITCH//
OPENED,
CLOSED
}SwitchState;
typedef enum PilotLever {//STATUS OF PILOT LEVER//
RAISINGEDGE,
FALLINGEDGE,
UP,
DOWN
}LeverState;
typedef enum HydraulicMechanisam{//HYDRAULIC CONDITIONS//
WORKING,
MALFUNCTIONING
}HydraulicState;
static volatile SwitchState squat_switch; //INPUT VARIABLE FOR CHANGING THE STATES//
static volatile SwitchState limit_switch; //INPUT VARIABLE FOR CHANGING THE STATES//
static volatile LeverState PilotLever; //INPUT VARIABLE FOR CHANGING THE STATES//
static volatile HydraulicState HydraulicMechanism; //INPUT VARIABLE FOR CHANGING THE STATES//
typedef struct //DEFINING STATE TABLE VARIABLES AS TYPEDEF STRUCTURE WITH CHARACTER DATATYPE//
{
char* current_state_indication;
char* light_status;
char* direction_valve_status;
char* hydraulic_pump_control_status;
char* gas_pressurized_spring_status[2];
}State_table;
static State_table state_machine[6] = {
{"gear down","green","down","enabled",{0,0}},
{"checking before take off","green","down","enabled",{0,0}},
{"raising gear","red","up","enabled",{0,0}},
{"gear up","off","null","disabled",{0,0}},
{"checking before landing","red","down","enabled",{0,0}},
{"lowering gear","green","down","enabled",{"disabled","enabled"}
}}; //STATE TABLE ARRAY DECLARATION//
#endif /* AIR_H_ *///END OF HEADER FILE//
CODES: MAIN PROGRAM
#include <stdio.h> //PREPROCESSOR
#include "AIR.h" //HEADER FILE INCLUDING
static void (*state_table[])(void) = {gear_down, checking_before_takeoff, raising_gear,
gear_up, checking_before_landing, lowering_gear}; //DECLARING VARIABLES TO STATE TABLE
State_Type current_state; //DEFINING CURRENT STATE AS STATE TYPE//
int main() //MAIN FUNCTION//
{
init_state_machine(); //INITIALIZE STATE MACHINE//
while(1) //WHILE LOOP//
{
state_table[current_state](); //CALLING STATE TABLE//
}
}
void init_state_machine() //FUNCTION INITIAL STATE MACHINE CALLED//
{
current_state = GEARDOWN; //ASSIGNING CURRENT STATE TO LOWERING GEAR//
printf("\nFinite state machine is initialized & program is in gear down state & light status:%s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
}
void gear_down(void) //FUNCTION GEAR DOWN CALLED//
{
current_state = GEARDOWN; //ASSIGNING CURRENT STATE TO GEAR DOWN//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nEnter the pilot lever position & squat switch status:"); //PRINT THE MESSAGE//
fflush(stdout); //CLEAR CACHE MEMORY//
scanf("%d %d",&PilotLever,&squat_switch); //READS AND STORES THE DATA//
if(PilotLever==RAISINGEDGE && squat_switch==OPENED) //IF CONDITION TO CHECK THE CONDITIONS//
{
current_state = CHKBFRTKOFF; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO CHECK BEFORE TAKEOFF//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
}
else //IF CONDITIONS ARE NOT SATISFIED GO TO GEAR DOWN FUNCTION//
{
gear_down(); //REPEAT//
}
}
void checking_before_takeoff() //FUNCTION CHECKING BEFORE LANDING CALLED//
{
current_state=CHKBFRTKOFF; //ASSIGNING CURRENT STATE TO CHECK BEFORE TAKEOFF//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nEnter the pilot lever position,squat switch status & status of hydraulic mechanism:"); //PRINT THE MESSAGE//
fflush(stdout); //CLEAR CACHE MEMORY//
scanf("%d %d %d",&PilotLever,&squat_switch,&HydraulicMechanism); //READS AND STORES THE DATA//
printf("\nwait for 2 secs\n"); //PRINT THE MESSAGE//
if(PilotLever==FALLINGEDGE || squat_switch==CLOSED) //IF CONDITION TO CHECK THE CONDITIONS//
{
gear_down(); //IF CONDITION IS TRUE GO TO GEAR DOWN FUNCTION//
}
else if(PilotLever==UP && squat_switch==OPENED && HydraulicMechanism==WORKING) //NESTED IF CONDITION TO CHECK OTHER CONDITION//
{
current_state = RSGGEAR; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO RISING GEAR//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("direction valve status:%s\n",state_machine[current_state].direction_valve_status); //DISPLAY DIRECTION VALVE STATUS//
}
else if(PilotLever==UP && squat_switch==OPENED && HydraulicMechanism==MALFUNCTIONING) //NESTED IF CONDITION TO CHECK OTHER CONDITION//
{
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("direction valve status:%s\n",state_machine[current_state].direction_valve_status); //DISPLAY DIRECTION VALVE STATUS//
printf("gas pressurized spring status:%s\n",state_machine[current_state].gas_pressurized_spring_status[HydraulicMechanism]); //DISPLAY GAS PRESSURIZED SPRING STATUS//
}
}
void raising_gear() //FUNCTION RAISING GEAR CALLED//
{
current_state=RSGGEAR; //ASSIGNING CURRENT STATE TO RISING GEAR//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nEnter the limit switch status,Lever Status position:"); //PRINT THE MESSAGE//
fflush(stdout); //CLEAR CACHE MEMORY//
scanf("%d %d",&PilotLever,&limit_switch); //READS AND STORES THE DATA//
printf("\nwait for 1 sec\n"); //PRINT THE MESSAGE//
if(PilotLever==FALLINGEDGE && limit_switch==OPENED) //IF CONDITION TO CHECK THE CONDITIONS//
{
current_state = CHKBFRLAND; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO CHECK BEFORE LANDING//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("hydraulic control for landing gear status:%s\n",state_machine[current_state].hydraulic_pump_control_status); //DISPLAY HYDRAULIC PUMP CONTROL STATUS//
}
else if(PilotLever==UP && limit_switch==CLOSED) //NESTED IF CONDITION TO CHECK OTHER CONDITION//
{
current_state=GEARUP; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO GEAR UP//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("hydraulic control for landing gear status:%s\n",state_machine[current_state].hydraulic_pump_control_status); //DISPLAY HYDRAULIC PUMP CONTROL STATUS//
}
else //IF CONDITIONS ARE NOT SATISFIED GO TO RAISING GEAR FUNCTION//
{
raising_gear(); //REPEAT//
}
}
void gear_up() //FUNCTION GEAR UP CALLED//
{
current_state=GEARUP; //ASSIGNING CURRENT STATE TO GEAR UP//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nEnter the pilot lever position:"); //PRINT THE MESSAGE//
fflush(stdout); //CLEAR CACHE MEMORY//
scanf("%d",&PilotLever); //READS AND STORES THE DATA//
if(PilotLever==FALLINGEDGE) //IF CONDITION TO CHECK THE CONDITIONS//
{
current_state = CHKBFRLAND; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO CHECK BEFORE LANDING//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("hydraulic control for landing gear status:%s\n",state_machine[current_state].hydraulic_pump_control_status); //DISPLAY HYDRAULIC PUMP CONTROL STATUS//
}
else //IF CONDITIONS ARE NOT SATISFIED GO TO GEAR UP FUNCTION//
{
gear_up(); //REPEAT//
}
}
void checking_before_landing() //FUNCTION CHECKING BEFORE LANDING CALLED//
{
current_state=CHKBFRLAND; //ASSIGNING CURRENT STATE TO CHECK BEFORE LANDING//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nenter the pilot lever position & status of hydraulic mechanism:"); //PRINT THE MESSAGE//
fflush(stdout); //CLEAR CACHE MEMORY//
scanf("%d %d",&PilotLever,&HydraulicMechanism); //READS AND STORES THE DATA//
if(PilotLever==DOWN && HydraulicMechanism==WORKING) //IF CONDITION TO CHECK THE CONDITIONS//
{
current_state = LWRGEAR; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO LOWERING GEAR//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("\ndirection valve status:%s\n",state_machine[current_state].direction_valve_status); //DISPLAY DIRECTION VALVE STATUS//
}
else if(PilotLever==DOWN && HydraulicMechanism==MALFUNCTIONING) //NESTED IF CONDITION TO CHECK OTHER CONDITION//
{
current_state = LWRGEAR; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO LOWERING GEAR//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("\ndirection valve status:%s\n",state_machine[current_state].direction_valve_status); //DISPLAY DIRECTION VALVE STATUS//
printf("gas pressurized spring status:%s\n",state_machine[current_state].gas_pressurized_spring_status[HydraulicMechanism]); //DISPLAY GAS PRESSURIZED SPRING STATUS//
}
else if (PilotLever == ( RAISINGEDGE || FALLINGEDGE || UP )) //NESTED IF CONDITION TO CHECK OTHER CONDITION//
{
checking_before_landing(); //REPEAT//
}
}
void lowering_gear() //FUNCTION LOWERING GEAR CALLED//
{
current_state=LWRGEAR; //ASSIGNING CURRENT STATE TO LOWERING GEAR//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nenter the limit switch status,pilot lever position:"); //PRINT THE MESSAGE//
fflush(stdout); //CLEAR CACHE MEMORY//
scanf("%d %d",&limit_switch,&PilotLever); //READS AND STORES THE DATA//
if (limit_switch == CLOSED && PilotLever==RAISINGEDGE) //IF CONDITION TO CHECK THE CONDITIONS//
{
current_state = CHKBFRTKOFF; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO CHECK BEFORE TAKEOFF//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight state: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
}
else if(limit_switch==OPENED && PilotLever==DOWN) //NESTED IF CONDITION TO CHECK OTHER CONDITION//
{
current_state = GEARDOWN; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO GEAR DOWN//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("hydraulic control for landing gear status:%s\n",state_machine[current_state].hydraulic_pump_control_status); //DISPLAY HYDRAULIC PUMP CONTROL STATUS//
}
else //IF CONDITIONS ARE NOT SATISFIED GO TO LOWERING GEAR FUNCTION//
{
lowering_gear(); //REPEAT//
}
}
OUTPUT:
Finite state machine is initialized & program is in gear down state & light status:green
current state: gear down
Enter the pilot lever position & squat switch status:0 0
current state: checking before take off
light status: green
current state: checking before take off
Enter the pilot lever position,squat switch status & status of hydraulic mechanism:2 0 0
wait for 2 secs
current state: raising gear
light status: red
direction valve status:up
current state: raising gear
Enter the limit switch status,Lever Status position:2 1
wait for 1 sec
current state: gear up
light status: off
hydraulic control for landing gear status:disabled
current state: gear up
Enter the pilot lever position:1
current state: checking before landing
light status: red
hydraulic control for landing gear status:enabled
current state: checking before landing
enter the pilot lever position & status of hydraulic mechanism:3 1
current state: lowering gear
light status: green
direction valve status:down
gas pressurized spring status:enabled
current state: lowering gear
enter the limit switch status,pilot lever position:0 3
current state: gear down
light status: green
hydraulic control for landing gear status:enabled
current state: gear down
Enter the pilot lever position & squat switch status: