Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
OFFLINE BATCH -2
PROJECT-2
#include <stdio.h>
#include <stdlib.h> // preprocessor
typedef enum State //using enum data type and for next state
{
Gear_down,
Check_Before_Takeoff,
Raising_Gear,
Gear_Up,
Check_Before_Landing,
Lowering_Gear
}State_Type;
typedef enum Switch //switch -limit,squat to check
{
Open, //0
Close //1
}Switch;
typedef enum poilet//using enum
{
Rising, //0
Up, //1
Falling, //2
Down //3
}Pilot;
// input from user to change the state
static volatile Switch squat_Switch;//input declare
static volatile Switch limit_switch;
static volatile Pilot pilot_lever;
State_Type current_state; // used to change one state to another
void init_state_machine()
{
current_state=Gear_down;
char light[100]="green";
printf("\nthe position of the gear is : %s\n",light); //printing output on console
}
void Gear_Down() // first state
{
current_state=Gear_down;
printf("\ngear_down state--> pilot_lever and squat_switch:"); //input from user
scanf("%d %d",&pilot_lever,&squat_Switch);
if(pilot_lever==Rising && squat_Switch==Open) //checking the condition
{
char light[10]="green";
char pilot_lever[10]="Rising";
char squat_Switch[20]="Open";
printf("current_state=Check_Before_Takeoff\n");
printf("light:%s\npilot_lever:%s\nsquat_switch:%s\n",light,pilot_lever,squat_Switch);//printing output on console
current_state=Check_Before_Takeoff;
}
else
{
Gear_Down(); // if enter input wrong it will return to same function
}
}
void Checking_Before_Takeoff() // second state
{
current_state=Check_Before_Takeoff;
printf("\nChecking_Before_Takeoff state --> pilot_lever and squat_switch:"); //input from user
scanf("%d %d",&pilot_lever,&squat_Switch);
if(pilot_lever== Up&& squat_Switch==Open) //checking the condition
{
char light[10]="Red";
char pilot_lever[10]="Up";
char squat_Switch[20]="Open";
printf("current_state=Raising_Gear\n");
printf("light:%s\npilot_lever:%s\nsquat_switch:%s\n",light,pilot_lever,squat_Switch);
current_state=Raising_Gear;
}
else if(pilot_lever== Falling || squat_Switch==Close) //checking the condition
{
char light[10]="Red";
char pilot_lever[10]="Falling";
char squat_Switch[20]="close";
printf("current_state=Gear_down\n");
printf("light:%s\npilot_lever:%s\nsquat_switch:%s\n",light,pilot_lever,squat_Switch); //printing output on console
current_state=Gear_down;
}
else
{
Checking_Before_Takeoff(); // if enter input wrong it will return to same function
}
}
void Raising_gear() // third state
{
current_state=Raising_Gear;
printf("\nRaising_gear state --> pilot_lever and limit_switch:"); //input from user
scanf("%d %d",&pilot_lever,&limit_switch);
if(pilot_lever== Up&&limit_switch==Close)
{
char light[10]="OFF";
char pilot_lever[10]="up";
char limit_Switch[20]="close";
printf("current_state=Gear_Up\n");
printf("light:%s\npilot_lever:%s\nlimit_switch:%s\n",light,pilot_lever,limit_Switch); //printing output on console
current_state=Gear_Up;
}
else if (pilot_lever==Falling||limit_switch==Open)
{
char light[10]="red";
char pilot_lever[10]="falling";
char limit_Switch[20]="open";
printf("current_state=Check_Before_Landing\n");
printf("light:%s\npilot_lever:%s\nlimit_switch:%s\n",light,pilot_lever,limit_Switch); //printing output on console
current_state=Check_Before_Landing;
}
else
{
Raising_gear(); // if enter input wrong it will return to same function
}
}
void Gear_up() // state four
{
current_state=Gear_Up;
printf("\nGear_up state --> pilot_lever :"); //input from user
scanf("%d",&pilot_lever);
if(pilot_lever==Falling)
{
char light[10]="red";
char pilot_lever[10]="falling";
char limit_Switch[20]="open";
printf("current_state=Check_Before_Landing\n");
printf("light:%s\npilot_lever:%s\nlimit_switch:%s\n",light,pilot_lever,limit_Switch); //printing output on console
current_state=Check_Before_Landing;
}
else
{
Gear_up(); // if enter input wrong it will return to same function
}
}
void Checking_Before_Landing() // state five
{
current_state=Check_Before_Landing;
printf("\nChecking_Before_Landing state --> pilot_lever:"); //input from user
scanf("%d",&pilot_lever);
if(pilot_lever==Down)
{
char light[10]="red";
char pilot_lever[10]="Down";
char limit_Switch[20]="open";
printf("current_state=Lowering_Gear\n");
printf("light:%s\npilot_lever:%s\nlimit_switch:%s\n",light,pilot_lever,limit_Switch); //printing output on console
current_state=Lowering_Gear;
}
else
{
Checking_Before_Landing();
}
}
void Lowering_gear() // state six
{
current_state=Lowering_Gear;
printf("\nLowering_gear state --> pilot_lever and limit_switch:"); //input from user
scanf("%d %d",&pilot_lever,&limit_switch);
if(pilot_lever== Rising && limit_switch==Close)
{
char light[10]="red";
char pilot_lever[10]="rising";
char limit_Switch[20]="close";
printf("current_state=Check_Before_Takeoff\n");
printf("light:%s\npilot_lever:%s\nlimit_switch:%s\n",light,limit_Switch,limit_Switch); //printing output on console
current_state=Check_Before_Takeoff;
}
else if(pilot_lever== Down && limit_switch==Open)
{
char light[10]="green";
char pilot_lever[10]="down";
char limit_Switch[20]="open";
printf("current_state=Gear_down\n");
printf("light:%s\npilot_lever:%s\nsquat_switch:%s\n",light,pilot_lever,limit_Switch); //printing output on console
current_state=Gear_down;
}
else
{
Lowering_gear();
}
}
// array of function pointer which leads to the respective function
static void (*state_table[])(void) = {Gear_Down,Checking_Before_Takeoff, Raising_gear, Gear_up, Checking_Before_Landing, Lowering_gear};
int main() // main function
{
printf("\nopen close rising up falling down\n ");
printf("\n 0 1 0 1 2 3\n ");
init_state_machine();
while(1)
{
state_table[current_state]();
}
return 0;
}
OUTPUT::
open close rising up falling down
0 1 0 1 2 3
the position of the gear is : green
gear_down state--> pilot_lever and squat_switch:0 0
current_state=Check_Before_Takeoff
light:green
pilot_lever:Rising
squat_switch:Open
Checking_Before_Takeoff state --> pilot_lever and squat_switch:1 0
current_state=Raising_Gear
light:Red
pilot_lever:Up
limit_switch:Open
Raising_gear state --> pilot_lever and limit_switch:1 1
current_state=Gear_Up
light:OFF
pilot_lever:up
limit_switch:close
Gear_up state --> pilot_lever :2
current_state=Check_Before_Landing
light:red
pilot_lever:falling
limit_switch:open
Checking_Before_Landing state --> pilot_lever:3
current_state=Lowering_Gear
light:red
pilot_lever:Down
limit_switch:open
Lowering_gear state --> pilot_lever and limit_switch:3 0
current_state=Gear_down
light:green
pilot_lever:down
squat_switch:open
gear_down state--> pilot_lever and squat_switch: