Challenge

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

Project 2

Embedded system

  1. No. 1 Implement the Code for controlling the retraction and extension of Airplane’s landing gear.

ANS:

Main C program for airplane

#include<stdio.h>//preprocessor section

#include "Air.h"//header file for Air

static void(*stateTable[])(void)={GearDown,CheckingBeforetakeOFF,RaisingGear,Gearup,CheckingBeforeLanding,LoweringGear};

//using state table, conditions controlling the retraction and extension of airplane's landing gear are called

int main()//main function//calling function the function should return an integer value

{

Initiate_State_Machine();//initial state machine is declared

while(1)//used for infinite loop, as 1 is there so condition is always true

{

stateTable[current_state]();//current state is declared in state table

}

}

void GearDown(void)//gear down condition

{

current_state=GEAR_DOWN;//current state is gear down

printf("Enter the status of pilot's lever and squat switch:");//printing the statement

fflush(stdout);//to use for immediate flush out the contents of output stream

scanf("%d %d", &p, &squat_switch);//taking input from the user

if((p==Raising) && (squat_switch==On))//used to decide whether a certain statement will be executed or not

{

current_state=CHECKING_BEFORE_TAKEOFF;

printf("Current state: %s\n", State_Machine[current_state].current_state_indication);//printing the statement

printf("Light is : %s\n", State_Machine[current_state].light);//printing the statement

}

else//used to specify a block of code to be executed, if the same condition is false

{

GearDown();//gear down statement

}

}

void CheckingBeforetakeOFF(void)//checking before take off condition

{

current_state=CHECKING_BEFORE_TAKEOFF;//current state is checking before take off

printf("Enter the status of pilot's lever and squat switch and hydraulic mechanism:");//printing the statement

fflush(stdout);//to use for immediate flush out the contents of output stream

scanf("%d %d %d", &p, &squat_switch, &h);//taking input from the user

if((p==falling) && (squat_switch==Off))//used to decide whether a certain statement will be executed or not

{

GearDown();//gear down condition

}

else if((p==Raising) && (squat_switch==On) && (h==notworking))//to specify new condition to test if the first condition false

{

current_state=RAISING_GEAR;//current state is raising gear

printf("Current state: %s\n", State_Machine[current_state].current_state_indication);//printing the statement

printf("Light is : %s\n", State_Machine[current_state].light);//printing the statement

printf("Status of Direction valve : %s\n", State_Machine[current_state].direction_valve_status);//printing the statement

printf("Status of Gas Pressurized Spring system: %s\n", State_Machine[current_state].Gas_Pressurized_Spring_system_status[0]);//printing the statement

}

}

void RaisingGear(void)//raising gear condition

{

current_state=RAISING_GEAR;//current state is raising gear

printf("Enter the status of pilot's lever and limit switch:");//printing the statement

fflush(stdout);//to use for immediate flush out the contents of output stream

scanf("%d %d", &p, &limit_switch);//taking input from the user

if((p==falling) && (limit_switch==On))//used to decide whether a certain statement will be executed or not

{

current_state=CHECK_BEFORE_LANDING;//current state is check before landing

printf("Current state : %s\n", State_Machine[current_state].current_state_indication);//printing the statement

printf("Light is : %s\n", State_Machine[current_state].light);//printing the statement

printf("Landing Gear Hydraulic Control : %s\n", State_Machine[current_state].landing_gear_hydraulic_control);//printing the statement

}

else if((p==Raising) && (limit_switch==Off))//to specify new condition to test if the first condition false

{

current_state=GEAR_UP;//current state is gear up

printf("Current state : %s\n", State_Machine[current_state].current_state_indication);//printing the statement

printf("Light is : %s\n", State_Machine[current_state].light);//printing the statement

printf("Landing Gear Hydraulic Control : %s\n", State_Machine[current_state].landing_gear_hydraulic_control);//printing the statement

}

else//used to specify a block of code to be executed, if the same condition is false

{

RaisingGear();//raising gear operation

}

}

void Gearup(void)//gear up condition

{

current_state=GEAR_UP;//current state is gear up

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

fflush(stdout);//to use for immediate flush out the contents of output stream

scanf("%d", &p);//taking input from the user

if(p==falling)//used to decide whether a certain statement will be executed or not

{

current_state=CHECK_BEFORE_LANDING;//current state is check before landing

printf("Current state : %s\n", State_Machine[current_state].current_state_indication);//printing the statement

printf("Light is : %s\n", State_Machine[current_state].light);//printing the statement

printf("Landing Gear Hydraulic Control : %s\n", State_Machine[current_state].landing_gear_hydraulic_control);//printing the statement

}

else//used to specify a block of code to be executed, if the same condition is false

{

Gearup();//gear up condition

}

}

void CheckingBeforeLanding(void)//checking before landing condition

{

current_state=CHECK_BEFORE_LANDING;//current state is check before landing

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

fflush(stdout);//to use for immediate flush out the contents of output stream

scanf("%d %d", &p, &h);//taking input from the user

if((p==falling) && (h==working))//used to decide whether a certain statement will be executed or not

{

current_state=LOWERING_GEAR;//current state is lowering gear

printf("Current state: %s\n", State_Machine[current_state].current_state_indication);//printing the statement

printf("Light is : %s\n", State_Machine[current_state].light);//printing the statement

printf("Status of Direction valve : %s\n", State_Machine[current_state].direction_valve_status);//printing the statement

}

else if((p==falling) && (squat_switch==On) && (h==notworking))//to specify new condition to test if the first condition false

{

current_state=LOWERING_GEAR;//current state is lowering gear

printf("Current state: %s\n", State_Machine[current_state].current_state_indication);//printing the statement

printf("Light is : %s\n", State_Machine[current_state].light);//printing the statement

printf("Status of Direction valve : %s\n", State_Machine[current_state].direction_valve_status);//printing the statement

printf("Status of Gas Pressurized Spring system: %s\n", State_Machine[current_state].Gas_Pressurized_Spring_system_status[0]);//printing the statement

}

/*if((p==Raising) || (p==falling))//used to decide whether a certain statement will be executed or not

{

CheckingBeforeLanding();//checking before landing operation

}*/

}

void LoweringGear(void)//lowering gear condition

{

current_state=LOWERING_GEAR;//current state is lowering gear

printf("Enter the status of pilot's lever and limit switch:");//printing the statement

fflush(stdout);//to use for immediate flush out the contents of output stream

scanf("%d %d", &p, &limit_switch);//taking input from the user

if((p==falling) && (limit_switch==On))//used to decide whether a certain statement will be executed or not

{

current_state=CHECKING_BEFORE_TAKEOFF;//current state is checking before take off

printf("Current state: %s\n", State_Machine[current_state].current_state_indication);//printing the statement

printf("Light is : %s\n", State_Machine[current_state].light);//printing the statement

}

else if((p==Raising) && (limit_switch==Off))//to specify new condition to test if the first condition false

{

current_state=GEAR_DOWN;//current state is gear down

printf("Current state : %s\n", State_Machine[current_state].current_state_indication);//printing the statement

printf("Light is : %s\n", State_Machine[current_state].light);//printing the statement

printf("Landing Gear Hydraulic Control : %s\n", State_Machine[current_state].landing_gear_hydraulic_control);//printing the statement

}

if(limit_switch==Off)//used to decide whether a certain statement will be executed or not

{

LoweringGear();//lowering gear condition

}

}

void Initiate_State_Machine()//initiate state machine condition

{

current_state=GEAR_DOWN;//current state is 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);//printing the statement of State_Machine[current_state].light

}

Header file

#ifndef AIR_H_//allows conditional compilation of air.h

#define AIR_H_//defines the air.h

#include<stdio.h>//preprocessor section

void Initiate_State_Machine();//initial state machine is declared using void

void GearDown();//gear down is declared using void

void CheckingBeforetakeOFF();//checking before take off is declared using void

void RaisingGear();//raising gear is declared using void

void Gearup();//gear up is declared using void

void CheckingBeforeLanding();//checking before landing is declared using void

void LoweringGear();//lowering gear is declared using void

typedef enum State//declares an alternative name as state

{

GEAR_DOWN, CHECKING_BEFORE_TAKEOFF, RAISING_GEAR, GEAR_UP, CHECK_BEFORE_LANDING, LOWERING_GEAR

}State_Type;//state type is declared

typedef enum Switch//declares the alternative name as switch

{

On,//open condition of switch

Off//close condition of switch

}Switch_status;//status of switch

typedef enum pilot_lever//declares the alternative name as pilot lever

{

Raising,//raising state of lever

falling//falling state of lever

}pilot_lever;//pilot lever declared

typedef enum hydraulic_mechanism//alternative name as hydraulic mechanism

{

working,//device is working

notworking//device is not working

}hydraulic_mechanism;//hydraulic mechanism declared

static volatile Switch_status squat_switch;//switch status stored in main memory as squat switch

static volatile Switch_status limit_switch;//switch status stored in main memory as limit switch

static volatile pilot_lever p;//pilot lever stored in main memory as p

static volatile hydraulic_mechanism h;//hydraulic mechanism stored in main memory as h

State_Type current_state;//current state is state type

typedef struct state_table// structure is declared using typedef

{

char* current_state_indication;//current state indication is declared using character

char* light;//light is declared using character

char* direction_valve_status;//direction valve status is declared using character

char* landing_gear_hydraulic_control;//landing gear hydraulic control is declared using character

char* Gas_Pressurized_Spring_system_status[2];//gas pressurized spring system status is declared using character

}State_Table;//state table declaration

static State_Table State_Machine[6]=

{

{"GearDown", "Green", "Down", "Enabled", {NULL, NULL}},//gear down, green light, down enabled table declared

{"CheckingBeforeTakeOFF", "Green", "Down", "Enabled", {NULL, NULL}},//checking before take off, green light, down, enabled table declared

{"RaisingGear", "Red", "Up", "Enabled", {"Disabled", "Enabled"}},//raising gear, red light, up direction, enabled table declared

{"GearUp", "Off", "Null", "Disabled", {NULL, NULL}},//gear up, off state switch, null and disabled condition declared

{"CheckingBeforeLanding", "Red", "Down", "Enabled", {NULL, NULL}},//checking before landing, red light, down, enabled table declared

{"LoweringGear", "Green", "Down", "Enabled", {"Disabled", "Enabled"}}//lowering gear, green light, down, enabled declared

};

#endif /* AIR_H_ *///end of declaration

Output:

The Finite State machine is initialized and current the program is inGearDown State & Light is: Green

Enter the status of pilot's lever and squat switch:0 0

Current state: CheckingBeforeTakeOFF

Light is : Green

Enter the status of pilot's lever and squat switch and hydraulic mechanism:0 0 1

Current state: RaisingGear

Light is : Red

Status of Direction valve : Up

Status of Gas Pressurized Spring system: Disabled

Enter the status of pilot's lever and limit switch:0 1

Current state : GearUp

Light is : Off

Landing Gear Hydraulic Control : Disabled

Enter the status of pilot's lever:1

Current state : CheckingBeforeLanding

Light is : Red

Landing Gear Hydraulic Control : Enabled

Enter the status of pilot's lever and hydraulic mechanism status:1 0

Current state: LoweringGear

Light is : Green

Status of Direction valve : Down

Enter the status of pilot's lever and limit switch:1 0

Current state: CheckingBeforeTakeOFF

Light is : Green

Enter the status of pilot's lever and squat switch and hydraulic mechanism:

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.