Challenge

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

Live Embedded  Batch 5 : Revathi

The below mention for the code for "Implement the Code for controlling the retraction and extension of Airplane’s landing gear"

 

This is code for fsm as per the above question 

#include //preprocessor
void Initiate_State_Machine();//function prototype declaration
void GearDown();//function prototype declaration
void CheckingBeforeTakeOFF();//function prototype declaration
void RaisingGear();//function prototype declaration
void GearUp();//function prototype declaration
void CheckBeforeLanding();//function prototype declaration
void LoweringGear();//function prototype declaration
static void(*statetable[])(void)={GearDown, CheckingBeforeTakeOFF, RaisingGear, GearUp, CheckBeforeLanding, LoweringGear};//function declaration using static
typedef enum State//typedef function prototype definition
{
GEAR_DOWN,//0
CHECKING_BEFORE_TAKEOFF,//1
RAISING_GEAR,//2
GEAR_UP,//3
CHECK_BEFORE_LANDING,//4
LOWERING_GEAR,//5
}type;//new function name
typedef enum Switch//typedef function prototype definition
{
open,//0
close//1
}status;//new function name
typedef enum lever//typedef function prototype definition
{
Raising,//0
falling,//1
up,//2
down//3
}lever;//new function name
typedef enum hydraulic//typedef function prototype definition
{
working,//0
not_working//1
}hydraulic;//new function name

static volatile status squat_switch;//change function name using static
static volatile status limit_switch;//change function name using static
static volatile lever lvr;//change function name using static
static volatile hydraulic hydra;//change function name using static
static volatile type current_state;//change function name using static

typedef struct//typedef function prototype definition
{
char* current_state_indication;//defined state
char* light;//defined state
char* direction_valve_status;//defined state
char* landing_gear_hydraulic_control;//defined state


}state_table;//new function name

static state_table state_machine[6]={{"GearDown","Green","Down","Enabled"},//[0]
{"CheckingBeforeTakeOFF","Red","Up","Enabled"},//[1]
{"RaisingGear","Red","Up","off"},//[2]
{"GearUp","Red","Falling","Disabled"},//[3]
{"CheckBeforeLanding","Red","Down","Enabled"},//[4]
{"LoweringGear","Green","Red","Enabled",}//[5]
};
int main()//main function
{
printf("state = GearDown(0), CheckingBeforeTakeOFF(1), RaisingGear(2), GearUp(3), CheckBeforeLanding(4), LoweringGear(6)");
printf("pilot lever = Raisisng(0) falling(1) up(2) down(3)\n hydraulics = working(0) not working(1)");
printf("\nswitch = open(0) close(1)\n");
Initiate_State_Machine();//caller function
while(1)//loop status
{
statetable[current_state]();//caller function
}
}
void GearDown(void)//function declaration and called function
{
current_state=GEAR_DOWN;//initialization
printf("The Finite State Machine is Intialized and current the program is in");//print on the console

current_state=GEAR_DOWN;//initialization
printf("Enter the status of pilot's lever and squat switch");//print on the console
fflush(stdout);//buffer clear
scanf("%d %d",&lvr,&squat_switch);//scan the input from console
if(lvr==Raising && squat_switch==open)//check the condition
{
printf("Light is :%s\n", state_machine[current_state].light);//print on the console
current_state=CHECKING_BEFORE_TAKEOFF;//initalization
printf("direction of valve :%s\n",state_machine[current_state].current_state_indication);//print on the console
printf("Light is :%s\n", state_machine[current_state].light);//print on the console
}
else// if the condition doesnt satisfies it will move to this condition
{
GearDown();//caller function
}
}

//CheckingBeforeTakeOFF
void CheckingBeforeTakeOFF(void)//function declaration and called function
{
current_state=CHECKING_BEFORE_TAKEOFF;//initialization
printf("Enter the status of pilot's lever and squat switch and hydraulic mechanism:");//print on the console
fflush(stdout);//buffer clear
scanf("%d %d %d",&lvr,&squat_switch,&hydra);//scan the input from console
if(lvr!=up||squat_switch!=open||hydra!=working)//check the condition
{
GearDown();
}
else if(lvr==up )//check the condition
{
current_state=RAISING_GEAR;//initialization
printf("Current State:%s\n",state_machine[current_state].current_state_indication);//print on the console

printf("Status of Direction valve:%s\n",state_machine[current_state].direction_valve_status);//print on the console
}

}
//RaisingGear
void RaisingGear(void)//function declaration and called function
{
current_state=RAISING_GEAR;//initialization
printf("Enter the status of pilot's lever and limit switch:");//print on the console
fflush(stdout);//buffer clear
scanf("%d %d",&lvr,&limit_switch);//scan the input from console
if(lvr==falling )//check the condition
{
printf("Light is:%s\n",state_machine[current_state].light);//print on the console
current_state=CHECK_BEFORE_LANDING;//initialization
printf("Current State:%s\n",state_machine[current_state].current_state_indication);//print on the console

printf("Landing Gear Hydraulic Control:%s\n",state_machine[current_state].landing_gear_hydraulic_control);//print on the console
}
else if( limit_switch==close)//check the condition
{printf("Light is:%s\n",state_machine[current_state].landing_gear_hydraulic_control);//print on the console
current_state=GEAR_UP;//initialization
printf("Current State:%s\n",state_machine[current_state].current_state_indication);//print on the console

printf("Landing Gear Hydraulic Control:%s\n",state_machine[current_state].landing_gear_hydraulic_control);//print on the console
}
else// if the condition doesnt satisfies it will move to this condition
{
RaisingGear();//caller function
}
}
//GearUp
void GearUp(void)//function declaration and called function
{
current_state=GEAR_UP;//initialization
printf("Enter the status of pilot's lever:");//print on the console
fflush(stdout);//buffer clear
scanf("%d",&lvr);//scan the input from consol
if(lvr==falling)//check the condition
{
printf("Light is:%s\n",state_machine[current_state].light);//print on the console
current_state=CHECK_BEFORE_LANDING;//initialization
printf("Current State:%s\n",state_machine[current_state].current_state_indication);//print on the console

printf("Landing Gear Hydraulic Control:%s\n",state_machine[current_state].landing_gear_hydraulic_control);//print on the console
printf("Status of Direction valve:%s\n",state_machine[current_state].direction_valve_status);//print on the console
}
else// if the condition doesnt satisfies it will move to this condition
{
GearUp();//caller function
}

}
//CheckBeforeLanding
void CheckBeforeLanding(void)//function declaration and called function
{
current_state=CHECK_BEFORE_LANDING;//initialization
printf("Enter the status of pilot's lever and hydraulic mechanism status:");//print on the console
fflush(stdout);//buffer clear
scanf("%d %d",&lvr,&hydra);//scan the input from consol
if(lvr!=down && hydra!=working)//check the condition
{
CheckBeforeLanding();//caller function
}
else if(lvr==down || hydra==working)//check the condition
{
printf("Status of Direction valve:%s\n",state_machine[current_state].direction_valve_status);//print on the console
current_state=LOWERING_GEAR;//initialization
printf("Current State:%s\n",state_machine[current_state].current_state_indication);//print on the console


}
/*else if(lvr==falling && hydra==not_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);
}*/

}
//LoweringGear.
void LoweringGear(void)//function declaration and called function
{
current_state=LOWERING_GEAR;//caller function
printf("Enter the status for lowering gear pilot's lever up/down and limit switch onoff:");//print on the console
fflush(stdout);//buffer clear
scanf("%d %d",&lvr,&limit_switch);//scan the input from consol


if(lvr==Raising && limit_switch==close )//check the condition
{
printf("Light is:%s\n",state_machine[current_state].light);//print on the console
current_state=CHECKING_BEFORE_TAKEOFF;//caller function
printf("Current State:%s\n",state_machine[current_state].current_state_indication);//print on the console

}
else if( lvr == Raising && limit_switch==open)//check the condition
{
printf("Light is:%s\n",state_machine[current_state].direction_valve_status);//print on the console
current_state=GEAR_DOWN;//caller function
printf("Current State:%s\n",state_machine[current_state].current_state_indication);//print on the console

printf("Landing Gear Hydraulic Control:%s\n",state_machine[current_state].landing_gear_hydraulic_control);//print on the console
}
else// if the condition doesnt satisfies it will move to this condition
{
LoweringGear();//caller function
}

}
//Initiate_State_Machine
void Initiate_State_Machine()//function declaration and called function
{
current_state=GEAR_DOWN;//caller function
printf("The Finite State Machine is Intialized and current the program is in""GearDown State & Light is:%s\n",state_machine[current_state].light);}//print on the console

output: 

The Finite State Machine is Intialized and current the program is inGearDown State & Light is:Green
The Finite State Machine is Intialized and current the program is inEnter the status of pilot's lever and squat switch0
0
Light is :Green
direction of valve :CheckingBeforeTakeOFF
Light is :Red
Enter the status of pilot's lever and squat switch and hydraulic mechanism:2
0
0
Current State:RaisingGear
Status of Direction valve:Up
Enter the status of pilot's lever and limit switch:1
0
Light is:Red
Current State:CheckBeforeLanding
Landing Gear Hydraulic Control:Enabled
Enter the status of pilot's lever and hydraulic mechanism status:1
0
Status of Direction valve:Down
Current State:LoweringGear
Enter the status for lowering gear pilot's lever up/down and limit switch onoff:1
0
Enter the status for lowering gear pilot's lever up/down and limit switch onoff:0
0
Light is:Red
Current State:GearDown
Landing Gear Hydraulic Control:Enabled
The Finite State Machine is Intialized and current the program is inEnter the status of pilot's lever and squat switch

loop will stop when you terminate the program

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.