Challenge

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

Live Batch(preeti)

1.

/*
* Airplanecontrol.c
*
* Created on: Jan 30, 2023
* Author: Dell
*/
#include <stdio.h>
//function declarations
void Initiate_State_Machine();
void GearDown();
void CheckingBeforeTakeOFF();
void RaisingGear();
void GearUp();
void CheckBeforeLanding();
void LoweringGear();
//we are entering these above function into the array of 6 element to access the function based on requirement
//Array of function pointer with no argument and no return type
static void(*statetable[])(void)={GearDown, CheckingBeforeTakeOFF, RaisingGear, GearUp, CheckBeforeLanding, LoweringGear}; //pointer array function with no return type and no arguments
// create enum datatype for the state of airplane
typedef enum State
{
GEAR_DOWN,
CHECKING_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR,
}State_Type;
// create enum datatype for the switch_status of the switches involved in airplane landing
typedef enum Switch
{
on,
off
}Switch_status;
//create enum for the pilot lever status
typedef enum pilot_lever
{
Raising,
falling
}pilot_lever;
//create enum for the hydraulic mechanism status
typedef enum hydraulic_mechanism
{
working,
not_working
}hydraulic_mechanism;
// create variables for above defined enum datatype variables by using their alias names
// static is used so that through out the program the value of the variable will be same and volatile is used to indicate the compiler not to assume the value of particular variable
//static is used to give permanent address to a variable and the value is preserved even after the function call where the particular variable is there is over
//volatile is used in a variable declaration for optimization of the variable while code optimization ,from an outside interrupt
static volatile Switch_status squat_switch;
static volatile Switch_status limit_switch;
static volatile pilot_lever pl;
static volatile hydraulic_mechanism hm;

State_Type current_state; // used to access the variables of State_type enumerated data type.
// structure is created to get the parameters which we want to display in output ,according to the given state of input.
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;
// declaring an array of state_machine to traverse through all the 6 states of airplane and their respective outputs to be shown by accessing the structure elements.
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","Disbled",{NULL,NULL}},
{"CheckBeforeLanding","Red","Down","Enabled",{NULL,NULL}},
{"LoweringGear","Green","Down","Enabled",{"Disabled","Enabled"}}
};
// function for gear down
void GearDown(void)
{
current_state=GEAR_DOWN;
printf("Enter the status of pilot's lever and squat switch");
fflush(stdout);
scanf("%d %d",&pl,&squat_switch);
if(pl==Raising && squat_switch==on)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("current state:%s\n",State_Machine[current_state].current_state_indication);
printf("Light is :%s\n", State_Machine[current_state].light);
}
else
{
GearDown();
}
}

// function for CheckingBeforeTakeOFF
void CheckingBeforeTakeOFF(void)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("Enter the status of pilot's lever and squat switch:");
fflush(stdout);
scanf("%d %d",&pl,&squat_switch);
if(pl==falling&&squat_switch==off)
{
GearDown();
}
else if(pl==Raising && squat_switch==on)
{
current_state=RAISING_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);
}
else
{
CheckingBeforeTakeOFF();
}
}
// function for RaisingGear
void RaisingGear(void)
{
current_state=RAISING_GEAR;
printf("Enter the status of pilot's lever and limit switch:");
fflush(stdout);
scanf("%d %d",&pl,&limit_switch);
if(pl==falling && limit_switch==on)
{
current_state=CHECK_BEFORE_LANDING;
printf("Current State:%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(pl==Raising && limit_switch==off)
{
current_state=GEAR_UP;
printf("Current State:%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();
}
}
// function for GearUp
void GearUp(void)
{
current_state=GEAR_UP;
printf("Enter the status of pilot's lever:");
fflush(stdout);
scanf("%d",&pl);
if(pl==falling)
{
current_state=CHECK_BEFORE_LANDING;
printf("Current State:%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();
}

}
// function for CheckBeforeLanding
void CheckBeforeLanding(void)
{
current_state=CHECK_BEFORE_LANDING;
printf("Enter the status of pilot's lever and hydraulic mechanism status:");
fflush(stdout);
scanf("%d %d",&pl,&hm);
if(pl==falling && hm==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);
}
else if(pl==falling && hm==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);
printf("Status of gas pressurised Spring System:%s\n",State_Machine[current_state].Gas_Pressurized_Spring_system_status);
}
else if(pl==(Raising))
{
CheckBeforeLanding();
}
}
// function for LoweringGear.
void LoweringGear(void)
{
current_state=LOWERING_GEAR;
printf("Enter the status of pilot's lever and limit switch:");
fflush(stdout);
scanf("%d %d",&pl,&limit_switch);
if(pl==falling && limit_switch==on)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("Current State:%s\n",State_Machine[current_state].current_state_indication);
printf("Light is:%s\n",State_Machine[current_state].light);
}
else if(pl==Raising && limit_switch==off)
{
current_state=GEAR_DOWN;
printf("Current State:%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(limit_switch==off)
{
LoweringGear();
}
}
// function for Initiate_State_Machine
void Initiate_State_Machine()
{
current_state==GEAR_DOWN;
printf("The Finite State Machine is Intialized and current the program is in""GearDown State & Light is:%s\n",State_Machine[current_state].light);
}

int main()
{
Initiate_State_Machine(); // function call .declaring the initial state of the airplane
while(1) //infinite while loop
{
statetable[current_state]();//accessing the Array of function pointer
}
}

output:

The Finite State Machine is Intialized and current the program is inGearDown State & Light is:Green
Enter the status of pilot's lever and squat switch0 0
current state:CheckingBeforeTakeOFF
Light is :Green
Enter the status of pilot's lever and squat switch:0 0
Current State:RaisingGear
Light is:Red
Status of Direction valve:Up
Enter the status of pilot's lever and limit switch:0 1
Current State:GearUp
Light is:Off
Landing Gear Hydraulic Control:Disbled
Enter the status of pilot's lever:1
Current State:CheckBeforeLanding
Light is:Red
Landing Gear Hydraulic Control:Enabled
Enter the status of pilot's lever and hydraulic mechanism status:1 1
Current State:LoweringGear
Light is:Green
Status of Direction valve:Down
Status of gas pressurised Spring System:�`@
Enter the status of pilot's lever and limit switch:0 1
Current State:GearDown
Light is:Green
Landing Gear Hydraulic Control:Enabled
Enter the status of pilot's lever and squat switch

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.