Challenge

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

HEADER FILE

"fsm.h"

#ifndef FSM_H_

#define FSM_H_

#include

// Function Declarations

void Initiate_state_machine();

void GearDown();

void CheckBeforeTakeoff();

void RaisingGear();

void GearUp();

void CheckBeforeLanding();

void LoweringGear();

// Calling different states

typedef enum State

{

GEAR_DOWN,

CHECK_BEFORE_TAKEOFF,

RAISING_GEAR,

GEAR_UP,

CHECK_BEFORE_LANDING,

LOWERING_GEAR

}State_type;

// Calling different switch status

typedef enum Switch

{

// value of on = 0 , off = 1

on,

off

}Switch_status;

// Calling different pilot lever status

typedef enum pilot_lever

{

// value of raising =0, falling = 1, up =2, down=3

Raising,

falling,

up,

down

}Pilot_lever_status;

// Calling different hydraulic mechanism status

typedef enum hydraulic_mechanism

{

// value of working = 0, not working = 1

working,

not_working

}Hydraulic_mechanism_status;

static volatile Switch_status squat_switch;

static volatile Switch_status limit_switch;

static volatile Pilot_lever_status pilot_lever;

static volatile Hydraulic_mechanism_status hydraulic_mechanism;

State_type curr_state;

typedef struct

{

char* curr_state_indication;

char* light;

char* direction_valve_status;

char* gas_pressurized_spring_status[2];

char* landing_gear_hydraulic_control;

}State_table;

// calling State Table

static State_table State_machine[6] =

{

{"GearDown", "Green", "Down", "Enabled",0,0},

{"CheckBeforeTakeoff", "Green", "Down", "Enabled",0,0},

{"RaisingGear", "Red", "Up", "Enabled", "Disabled", "Enabled"},

{"GearUp", "Off", "Null", "Disabled",0,0},

{"CheckBeforeLanding", "Green", "Down", "Enabled",0,0},

{"LoweringGear", "Green", "Down", "Enabled", "Disabled", "Enabled"}

};

#endif /* FSM_H_ */

SOURCE FILE

"main.c"

#include

#include "fsm.h"

static void(*state_table[])(void) = {GearDown, CheckBeforeTakeoff, RaisingGear, GearUp,

CheckBeforeLanding, LoweringGear};

int main()

{

Initiate_state_machine(); //calling function

while(1)

{

state_table[curr_state]();

}

}

// GEAR DOWN

void GearDown(void)

{

curr_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)

{

curr_state = CHECK_BEFORE_TAKEOFF;

printf("Current state : %s\n", State_machine[curr_state].curr_state_indication);

printf("Light is : %s\n", State_machine[curr_state].light);

}

else

{

GearDown();

}

}

// CHECK BEFORE TAKEOFF

void CheckBeforeTakeoff(void)

{

curr_state = CHECK_BEFORE_TAKEOFF;

printf("Enter the status of pilot's lever, 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 == up && squat_switch == on && hydraulic_mechanism == working)

{

curr_state = RAISING_GEAR;

printf("Current state : %s\n", State_machine[curr_state].curr_state_indication);

printf("Light : %s\n", State_machine[curr_state].light);

printf("Status of Direction valve :%s\n", State_machine[curr_state].direction_valve_status);

}

else if(pilot_lever == up && squat_switch == on && hydraulic_mechanism == not_working)

{

curr_state = RAISING_GEAR;

printf("Current state : %s\n", State_machine[curr_state].curr_state_indication);

printf("Light : %s\n", State_machine[curr_state].light);

printf("Status of Direction valve :%s\n", State_machine[curr_state].direction_valve_status);

printf("Status of Gas Pressurized Spring System :%s\n",

State_machine[curr_state].gas_pressurized_spring_status[hydraulic_mechanism]);

}

}

//RAISING GEAR

void RaisingGear(void)

{

curr_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)

{

curr_state = CHECK_BEFORE_LANDING;

printf("Current State is : %s\n", State_machine[curr_state].curr_state_indication);

printf("Light is : %s\n", State_machine[curr_state].light);

printf("Landing Gear Hydraulic Control : %s\n", State_machine[curr_state].landing_gear_hydraulic_control);

}

else if(pilot_lever == up && limit_switch == off)

{

curr_state = GEAR_UP;

printf("Current State is : %s\n", State_machine[curr_state].curr_state_indication);

printf("Light is : %s\n", State_machine[curr_state].light);

printf("Landing Gear Hydraulic Control : %s\n", State_machine[curr_state].landing_gear_hydraulic_control);

}

else

{

RaisingGear();

}

}

// GEAR UP

void GearUp(void)

{

curr_state = GEAR_UP;

printf("Enter the status of pilot's lever:");

fflush(stdout);

scanf("%d", &pilot_lever);

if(pilot_lever == falling)

{

curr_state = CHECK_BEFORE_LANDING;

printf("Current State is : %s\n", State_machine[curr_state].curr_state_indication);

printf("Light is : %s\n", State_machine[curr_state].light);

printf("Landing Gear Hydraulic Control : %s\n", State_machine[curr_state].landing_gear_hydraulic_control);

}

else

{

GearUp();

}

}

// CHECK BEFORE LANDING

void CheckBeforeLanding(void)

{

curr_state = CHECK_BEFORE_LANDING;

printf("Enter the status of pilot's lever and hydraulic mechanism:");

fflush(stdout);

scanf("%d %d", &pilot_lever, &hydraulic_mechanism);

if(pilot_lever == down && hydraulic_mechanism == working)

{

curr_state = LOWERING_GEAR;

printf("Current State is : %s\n", State_machine[curr_state].curr_state_indication);

printf("Light is : %s\n", State_machine[curr_state].light);

printf("Status of Direction valve : %s\n", State_machine[curr_state].direction_valve_status);

}

else if(pilot_lever == down && hydraulic_mechanism == not_working)

{

curr_state = LOWERING_GEAR;

printf("Current State is : %s\n", State_machine[curr_state].curr_state_indication);

printf("Light is : %s\n", State_machine[curr_state].light);

printf("Status of Direction valve : %s\n", State_machine[curr_state].direction_valve_status);

printf("Status of Gas Pressurized Spring System :%s\n",

State_machine[curr_state].gas_pressurized_spring_status[hydraulic_mechanism]);

}

if(pilot_lever == (up||Raising || falling))

{

CheckBeforeLanding();

}

}

// LOWERING GEAR

void LoweringGear(void)

{

curr_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 == off)

{

curr_state = CHECK_BEFORE_TAKEOFF;

printf("Current State is : %s\n", State_machine[curr_state].curr_state_indication);

printf("Light is : %s\n", State_machine[curr_state].light);

}

if(pilot_lever == down && limit_switch == on)

{

curr_state = GEAR_DOWN;

printf("Current State is : %s\n", State_machine[curr_state].curr_state_indication);

printf("Light is : %s\n", State_machine[curr_state].light);

printf("Landing Gear Hydraulic Control : %s\n", State_machine[curr_state].landing_gear_hydraulic_control);

}

else

{

LoweringGear();

}

}

void Initiate_state_machine()

{

curr_state = GEAR_DOWN;                 // initial state of machine is Gear Down state //

printf("The Finite State Machine is Initialized and current the program is in GearDown and Light is :

%s\n",State_machine[curr_state].light);

}

MAKEFILE

all: main.c fsm.h
          gcc main.c fsm.h -o main -I.

clean:
          rm main.exe

OUTPUT

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.