Challenge

Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear

code


/*
* fsm.c
*
* Created on: 22-Dec-2022
* Author: fsm.c
*/


#include <stdio.h>

void Initiate_State_Machine();// inialize the function
void GearDown();// inialize the function
void CheckingBeforeTakeOFF();// inialize the function
void RaisingGear();// inialize the function
void GearUp();// inialize the function
void CheckBeforeLanding();// inialize the function
void LoweringGear();// inialize the function

//Accessing the function by an array of function.
static void (*statetable[])(void) = {GearDown, CheckingBeforeTakeOFF, RaisingGear, GearUp, CheckBeforeLanding, LoweringGear};

//enum creation for state,switch,pilot lever,hydraulic mechanism
typedef enum State
{
GEAR_DOWN,
CHECKING_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR,
} State_Type;
typedef enum Switch
{
on, //on = 0 as input
off // off = 1 as input ,,plane is on ground
} Switch_status;
typedef enum pilot_lever
{
Raising, //0
falling //1
} pilot_lever;
typedef enum hydraulic_mechanism
{
working, //0
not_working//1
} hydraulic_mechanism;
/*call these function under the program and input for changing the state*/
static volatile Switch_status squat_switch;
static volatile Switch_status limit_switch;
static volatile pilot_lever pl;
static volatile hydraulic_mechanism hm;

//Declare the current_sate as a global variable.

State_Type current_state;

//initialize the structure for dispalying the status to pilot

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", {NULL, NULL}},
{"GearUp", "Off", "NULL", "Disabled", {NULL, NULL}},
{"CheckBeforeLanding", "Red", "Down", "Enabled", {NULL, NULL}},
{"LoweringGear", "Green", "Down", "Enabled", {"Disabled", "Enabled"}}};//initialize the state machine probabilities.
int main()//main function
{
Initiate_State_Machine();//as on gear down state
while (1)//infinite loop condition is always true.
{
statetable[current_state]();//here statetable[0](); to statetable[5](); is the function calling
}
}

// here write the code for indicating the status with suitable condition
void GearDown(void)
{
current_state = GEAR_DOWN;
printf("Please Enter the status of pilot's lever and squat switch :\n");
fflush(stdout);
scanf("%d %d", &pl, &squat_switch);
if (pl == Raising && squat_switch == on)
{
current_state = CHECKING_BEFORE_TAKEOFF;
printf("current state:%s\n", State_Machine[current_state].current_state_indication);
fflush(stdout);
printf("Light is :%s\n", State_Machine[current_state].light);
fflush(stdout);
}
else
{
GearDown();
}
}

// CheckingBeforeTakeOFF
void CheckingBeforeTakeOFF(void)
{
current_state = CHECKING_BEFORE_TAKEOFF;
printf("Enter the status of pilot's lever and squat switch and hydraulic mechanism:\n");
fflush(stdout);
scanf("%d %d %d", &pl, &squat_switch, &hm);
if (pl == falling && squat_switch == off)
{
GearDown();
}
else if (pl == Raising && squat_switch == on && hm == working)
{
current_state = RAISING_GEAR;
printf("Current State:%s\n", State_Machine[current_state].current_state_indication);
fflush(stdout);
printf("Light is:%s\n", State_Machine[current_state].light);
fflush(stdout);
printf("Status of Direction valve:%s\n", State_Machine[current_state].direction_valve_status);
fflush(stdout);
}
else if (pl == Raising && squat_switch == on && hm == not_working)
{
current_state = RAISING_GEAR;
printf("Current State:%s\n", State_Machine[current_state].current_state_indication);
fflush(stdout);
printf("Light is:%s\n", State_Machine[current_state].light);
fflush(stdout);
printf("Status of Direction valve:%s\n", State_Machine[current_state].direction_valve_status);
fflush(stdout);
printf("Status of gas pressurised Spring System:%s\n", State_Machine[current_state].Gas_Pressurized_Spring_system_status);
fflush(stdout);
}
}
// RaisingGear
void RaisingGear(void)
{
current_state = RAISING_GEAR;
printf("give the status of pilot's lever and limit switch: \n");
fflush(stdout);
scanf("%d %d", &pl, &limit_switch);
if (pl == falling && limit_switch == on)
{
current_state = CHECK_BEFORE_LANDING;
printf("Current State:%s\n", State_Machine[current_state].current_state_indication);
fflush(stdout);
printf("Light is:%s\n", State_Machine[current_state].light);
fflush(stdout);
printf("Landing Gear Hydraulic Control:%s\n", State_Machine[current_state].landing_gear_hydraulic_control);
fflush(stdout);
}
else if (pl == Raising && limit_switch == off)
{
current_state = GEAR_UP;
printf("Current State:%s\n", State_Machine[current_state].current_state_indication);
fflush(stdout);
printf("Light is:%s\n", State_Machine[current_state].light);
fflush(stdout);
printf("Landing Gear Hydraulic Control:%s\n", State_Machine[current_state].landing_gear_hydraulic_control);
fflush(stdout);
}
else
{
RaisingGear();
}
}
// GearUp
void GearUp(void)
{
current_state = GEAR_UP;
printf("Enter the status of pilot's lever: \n");
fflush(stdout);
scanf("%d", &pl);
if (pl == falling)
{
current_state = CHECK_BEFORE_LANDING;
printf("Current State:%s\n", State_Machine[current_state].current_state_indication);
fflush(stdout);
printf("Light is:%s\n", State_Machine[current_state].light);
fflush(stdout);
printf("Landing Gear Hydraulic Control:%s\n", State_Machine[current_state].landing_gear_hydraulic_control);
fflush(stdout);
}
else
{
GearUp();
}
}
// CheckBeforeLanding
void CheckBeforeLanding(void)
{
current_state = CHECK_BEFORE_LANDING;
printf("Enter the status of pilot's lever and hydraulic mechanism status: \n");
fflush(stdout);
scanf("%d %d", &pl, &hm);
if (pl == falling && hm == working)
{
current_state = LOWERING_GEAR;
printf("Current State:%s\n", State_Machine[current_state].current_state_indication);
fflush(stdout);
printf("Light is:%s\n", State_Machine[current_state].light);
fflush(stdout);
printf("Status of Direction valve:%s\n", State_Machine[current_state].direction_valve_status);
fflush(stdout);
}
else if (pl == falling && hm == not_working)
{
current_state = LOWERING_GEAR;
printf("Current State:%s\n", State_Machine[current_state].current_state_indication);
fflush(stdout);
printf("Light is:%s\n", State_Machine[current_state].light);
fflush(stdout);
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);
fflush(stdout);
}
else if (pl == (Raising))
{
CheckBeforeLanding();
}
}
// LoweringGear.
void LoweringGear(void)
{
current_state = LOWERING_GEAR;
printf("Enter the status of pilot's lever and limit switch: \n");
fflush(stdout);
scanf("%d %d", &pl, &limit_switch);
if (pl == falling && limit_switch == on)
{
current_state = CHECKING_BEFORE_TAKEOFF;
printf("Current State:%s\n", State_Machine[current_state].current_state_indication);
fflush(stdout);
printf("Light is:%s\n", State_Machine[current_state].light);
fflush(stdout);
}
else if (pl == Raising && limit_switch == off)
{
current_state = GEAR_DOWN;
printf("Current State:%s\n", State_Machine[current_state].current_state_indication);
fflush(stdout);
printf("Light is:%s\n", State_Machine[current_state].light);
fflush(stdout);
printf("Landing Gear Hydraulic Control:%s\n", State_Machine[current_state].landing_gear_hydraulic_control);
fflush(stdout);
}

if (limit_switch == off)
{
LoweringGear();
}
}
// Initiate_State_Machine
void Initiate_State_Machine()
{
current_state == GEAR_DOWN;
printf("The Finite State Machine is Intialized and run the program is in GearDown State & Light is:%s\n",State_Machine[current_state].light);
}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.