Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
.c File:-
#include<stdio.h>
#include"states.h"
static void(*state_Table[])(void)={GearDown, CheckingBeforeTakeOFF, RaisingGear, GearUp, CheckBeforeLanding, LoweringGear};
int main()
{
Initiate_State_Machine();
while(1)
{
state_Table[current_state]();
}
}
void GearDown(void)
{
current_state = GEAR_DOWN;
printf("Enter the status of pilot's lever and squat switch:");
fflush(stdout);
scanf("%d %d", &pilot_lever, &squat_switch);
if(pilot_lever==raising && squat_switch==On)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("Current state:%s\n", State_Machine[current_state].current_state_indication);
printf("Light is:%s\n", State_Machine[current_state].light);
}
else
{
GearDown();
}
}
void CheckingBeforeTakeOFF(void)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("Enter the status of pilot's lever and squat switch and hydraulic mechanism:");
fflush(stdout);
scanf("%d %d %d",&pilot_lever, &squat_switch, &hydraulic_mechanism);
if(pilot_lever==falling && squat_switch==Off)
{
GearDown();
}
else if(pilot_lever==raising && squat_switch==On && hydraulic_mechanism==working)
{
current_state=RAISING_GEAR;
printf("Current state:%s\n", State_Machine[current_state].current_state_indication);
printf("Light is:%s\n", State_Machine[current_state].light);
printf("Status of Direction valve:%s\n",State_Machine[current_state].direction_valve_status);
}
else if(pilot_lever==raising && squat_switch==On && hydraulic_mechanism==notworking)
{
current_state=RAISING_GEAR;
printf("Current state:%s\n", State_Machine[current_state].current_state_indication);
printf("Light is:%s\n", State_Machine[current_state].light);
printf("Status of Direction valve:%s\n",State_Machine[current_state].direction_valve_status);
printf("Status of Gas Pressurised Spring System:%s\n", State_Machine[current_state].Gas_Pressurized_Spring_system_status);
}
}
void RaisingGear(void)
{
current_state=RAISING_GEAR;
printf("Enter the status of pilot's lever and limit switch:");
fflush(stdout);
scanf("%d %d", &pilot_lever, &limit_switch);
if(pilot_lever==falling && limit_switch==On)
{
current_state=CHECK_BEFORE_LANDING;
printf("Current state:%s\n", State_Machine[current_state].current_state_indication);
printf("Light is:%s\n", State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control:%s\n", State_Machine[current_state].landing_gear_hydraulic_control);
}
else if(pilot_lever==raising && limit_switch==Off)
{
current_state=GEAR_UP;
printf("Current state:%s\n", State_Machine[current_state].current_state_indication);
printf("Light is:%s\n", State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control:%s\n", State_Machine[current_state].landing_gear_hydraulic_control);
}
else
{
RaisingGear();
}
}
void GearUp
(void)
{
current_state=GEAR_UP;
printf("Enter the status of pilot's lever:");
fflush(stdout);
scanf("%d", &pilot_lever);
if(pilot_lever==falling)
{
current_state=CHECK_BEFORE_LANDING;
printf("Current state:%s\n", State_Machine[current_state].current_state_indication);
printf("Light is:%s\n", State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control:%s\n", State_Machine[current_state].landing_gear_hydraulic_control);
}
else
{
GearUp();
}
}
void CheckBeforeLanding(void)
{
current_state=CHECK_BEFORE_LANDING;
printf("Enter the status of pilot's lever and hydraulic mechanism status:");
fflush(stdout);
scanf("%d %d",&pilot_lever, &hydraulic_mechanism);
if(pilot_lever==falling && hydraulic_mechanism==working)
{
current_state=LOWERING_GEAR;
printf("Current state:%s\n", State_Machine[current_state].current_state_indication);
printf("Light is:%s\n", State_Machine[current_state].light);
printf("Status of Direction valve:%s\n",State_Machine[current_state].direction_valve_status);
}
else if(pilot_lever==falling && squat_switch==On && hydraulic_mechanism==notworking)
{
current_state=LOWERING_GEAR;
printf("Current state:%s\n", State_Machine[current_state].current_state_indication);
printf("Light is:%s\n", State_Machine[current_state].light);
printf("Status of Direction valve:%s\n",State_Machine[current_state].direction_valve_status);
printf("Status of Gas Pressurised Spring System:%s\n", State_Machine[current_state].Gas_Pressurized_Spring_system_status);
}
if(pilot_lever==(raising || falling))
{
CheckBeforeLanding();
}
}
void LoweringGear(void)
{
current_state=LOWERING_GEAR;
printf("Enter the status of pilot's lever and limit switch:");
fflush(stdout);
scanf("%d %d", &pilot_lever, &limit_switch);
if(pilot_lever==raising && limit_switch==On)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("Current state:%s\n", State_Machine[current_state].current_state_indication);
printf("Light is:%s\n", State_Machine[current_state].light);
}
if(pilot_lever==raising && limit_switch==Off)
{
current_state=GEAR_DOWN;
printf("Current state:%s\n", State_Machine[current_state].current_state_indication);
printf("Light is:%s\n", State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control:%s\n", State_Machine[current_state].landing_gear_hydraulic_control);
}
if(limit_switch==Off)
{
LoweringGear();
}
}
void Initiate_State_Machine()
{
current_state=GEAR_DOWN;
printf("The Finite State Machine is Initialized and current the program is in GearDown State & Light is: %s\n", State_Machine[current_state].light);
}
.h File:-
#ifndef STATES_H_
#define STATES_H_
#include<stdio.h>
void Initiate_State_Machine();
void GearDown();
void CheckingBeforeTakeOFF();
void RaisingGear();
void GearUp();
void CheckBeforeLanding();
void LoweringGear();
typedef enum State
{
GEAR_DOWN,
CHECKING_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR,
}State_Type;
typedef enum Switch
{
On,
Off
}Switch_status;
typedef enum pilot_lever
{
raising,
falling
}pilot;
typedef enum hydraulic_mechanism
{
working,
notworking
}hydraulic;
static volatile Switch_status squat_switch;
static volatile Switch_status limit_switch;
static volatile pilot pilot_lever;
static volatile hydraulic hydraulic_mechanism;
State_Type current_state;
typedef struct
{
char* current_state_indication;
char* light;
char* direction_valve_status;
char* landing_gear_hydraulic_control;
char* Gas_Pressurized_Spring_system_status[2];
}State_Table;
static State_Table
State_Machine[6]=
{
{"GearDown", "Green", "Down", "Enabled",{NULL,NULL}},
{"CheckingBeforeTakeOFF", "Green", "Down", "Enabled",{NULL, NULL}},
{"RaisingGear","Red", "Up", "Enabled", {"Disabled","Enabled"}},
{"GearUp", "Off", "Null", "Disabled", {NULL,NULL}},
{"CheckBeforeLanding", "Red", "Down", "Enabled",{NULL,NULL}},
{"LoweringGear", "Green", "Down", "Enabled",{"Disabled","Enabled"}}
};
#endif /* STATES_H_ */