Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
/*
* main.c
*
* Created on: Jun 16, 2024
* Author: anups
*/
#include<stdio.h>
//Frist we are creating seven function for different condition
void Initiate_State_Machine();
void GearDown();
void CheckingBeforeTakeOFF();
void RaisingGear();
void GearUp();
void CheckBeforeLanding();
void LoweringGear();
//then we are entering this function into array of 6 element to access the fiction
static void(*statetable[])(void)= {GearDown, CheckingBeforeTakeOFF, RaisingGear,
GearUp, CheckBeforeLanding, LoweringGear};
//then we are creating four types of enum in this program
typedef enum State
{
GEAR_DOWN,
CHECKING_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR,
}State_Type;
typedef enum Switch
{
on,
off //plane is on ground
}Switch_Status;
typedef enum pilot_liver
{
Raising,
falling
}pilot_liver;
typedef enum hydraulic_mechanism
{
working,
not_working
}hydraulic_mechanism;
//And then we are creating the object name for four enum structure
static volatile Switch_Status squat_switch;
static volatile Switch_Status limit_switch;
static volatile pilot_liver p1;
static volatile hydraulic_mechanism hm;
State_Type current_state;
//creating structure where we are giving different conditon where how to control landing gear
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;
//we are giving the name to the structure as 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"}}
};
//we are creating the main Function
int main()
{
Initiate_State_Machine();
while(1)
{
statetable[current_state]();
}
}
void GearDown(void)
{
current_state = GEAR_DOWN;
printf("Enter the status of pilot's lever and squate switch ");
fflush(stdout);
scanf("%d %d",&p1,&squat_switch);
if(p1==Raising && squat_switch==on)
{
current_state =CHECKING_BEFORE_TAKEOFF;
printf("Current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("light is: %s\n",State_Machine[current_state].light);
}
else
{
GearDown();
}
}
//Checking Before TakeOFF
void CheckingBeforeTakeOFF(void)
{
current_state= CHECKING_BEFORE_TAKEOFF;
printf("Enter the status of pilot's lever and squat switch and Hydraulic mechanism");
fflush(stdout);
scanf("%d %d",&p1,&squat_switch,&hm);
if(p1==falling && squat_switch == off)
{
GearDown();
}
else if(p1==Raising && squat_switch==on && hm==working)
{
current_state = RAISING_GEAR;
printf("Current State is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
printf("Status of Direction Valve is: %s\n",State_Machine[current_state].direction_valve_status);
}
else if(p1==Raising && squat_switch==on && hm==not_working)
{
current_state = RAISING_GEAR;
printf("Current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
printf("Status of Direction Valve is: %s\n",State_Machine[current_state].direction_valve_status);
printf("Status of gas pressurized Spring system is: %s\n",State_Machine[current_state].Gas_pressurized_spring_system_status);
}
}
//RaisingGear
void RaisingGear(void)
{
current_state = RAISING_GEAR;
printf("Enter the status of pilot's lever and Limit switch");
fflush(stdout);
scanf("%d %d",&p1,&limit_switch);
if(p1==falling && limit_switch ==on)
{
current_state = CHECK_BEFORE_LANDING;
printf("Current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control %s\n",State_Machine[current_state].landing_gear_hydraulic_control);
}
else if(p1==Raising && limit_switch ==off)
{
current_state = GEAR_UP;
printf("Current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control %s\n",State_Machine[current_state].landing_gear_hydraulic_control);
}
else
{
RaisingGear();
}
}
//GearUp
void GearUp(void)
{
current_state = GEAR_UP;
printf("Enter the status of pilot's lever");
fflush(stdout);
scanf("%d %d",&p1);
if(p1==falling)
{
current_state = CHECK_BEFORE_LANDING;
printf("Current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control %s\n",State_Machine[current_state].landing_gear_hydraulic_control);
}
else
{
GearUp();
}
}
//CheckBeforeLanding
void CheckBeforeLanding(void)
{
current_state = CHECK_BEFORE_LANDING;
printf("Enter the status of pilot's lever and Hydraulic Mechanism");
fflush(stdout);
scanf("%d %d",&p1,&hm);
if(p1==falling && hm==working)
{
current_state = LOWERING_GEAR;
printf("Current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
printf("Status of Direction Valve is:%s\n",State_Machine[current_state].direction_valve_status);
}
else if(p1==falling && hm==not_working)
{
current_state = LOWERING_GEAR;
printf("Current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
printf("Status of Direction Valve is:%s\n",State_Machine[current_state].direction_valve_status);
printf("Status of Gas pressurized Spring System:%s\n",State_Machine[current_state].Gas_pressurized_spring_system_status);
}
else if(p1==(Raising))
{
CheckBeforeLanding();
}
}
//LoweringGear
void LoweringGear(void)
{
current_state = LOWERING_GEAR;
printf("Enter the pilot's lever and limit switch:");
fflush(stdout);
scanf("%d %d",&p1,&limit_switch);
if(p1==falling && limit_switch==on)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("Current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
}
else if(p1==Raising && limit_switch==off)
{
current_state=GEAR_DOWN;
printf("Current state is: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is: %s\n",State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control:%s\n",State_Machine[current_state].landing_gear_hydraulic_control);
}
if(limit_switch==off)
{
LoweringGear();
}
}
//Initiate_State_Machine
void Initiate_State_Machine()
{
current_state=GEAR_DOWN;
printf("The Finite State Machine is Initialized and The Current Program is in"" GearDown State & Light is:%s\n",State_Machine[current_state].light);
}












First we are creating seven function we want to carry out the process or controlling sets of gear under different condition.Based on above chart.
And then we are entering this finction into the array of 6 element to access the finction based on require ment.In the name of static table.
And we are creating enum type structure to creating the alise number to our function which is easy to carrry out comparison and easy way giving inputs.
We are creating four type of enum in program:
And then we are creating the object name for the 4 enum structures:
And we mentioned that as static type because we can use this any where in the program volatile means it values are changable:
This step is very imporatant we are creating the structure where we are giving the different condition where how to control the landing gears.
We are giving the name to structure as state_table.
we are creating the object named as statemachine which is an array in which it contain six different type of condtional output:
we can access the inside array one element using dot sign.
Then creating main function
In main function we are calling the function name is Initiate State machine function:with no argument
Inside we are changing the current state to Gear down which means it is 0 in the enum function
And printing the staemachine[0].light status which is in statetable.
In the gear down function we are changing the current state to gear down
And getting the pilot inputs if the pilot give inputs as raising and switch on it means ready to take off and changing th state as checking before takeooff state
And then printing current indication and light
Now current state changes to checking before takeoff in current state.
It enters to current state function which is checking before take off
In the checking before takeoff function we are getting the input from pilot for liver,squat switch.
If the liver is falling it goes to the geardown fuction part.
otherwise when the liver is raising and switch is up the current state is changes to next state that is Raising gear
Print the direction of valve,light,indication statemachine
Next state is Raising gear
In the raising gear function getting input from the pilot as lever is falling and switch is on it means it change state is check before landing and print the respectine value.If the lever is up then state changes to gear up and following output we will get
current state: CheckBeforeLanding
Light is: Red
Landing Gear Hydraulic Control: Enabled
Next Function is Checking Before Landing:
In this function pilot gives input as lever is falling and haydraulis finction as same then state changes to lowering gear and show corresponding output from the statemachine array.
If the lever is falling and hydralics not working it shows the gas pressure system
And the function:
If we are getting input as lever falling and limit switch is on then state is checking before landing and its output shown from state machine.
If we are getting input as lever raising and limit switch is off then state is gear down and its output is shown from state machine.
If limit switch is off the it goes to lowering gear function itself .
And the output has been shown in the frist screenshot