Challenge

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

Filght landing gear retraction and extension using FSM

#include<stdio.h>

void Initiate_state_machine();
void GearDown();
void CheckingBeforeTakeoff();
void RaisingGear();
void GearUp();
void CheckBeforeLanding();
void LoweringGear();
static void(*state_table[])(void)={GearDown, CheckingBeforeTakeoff, RaisingGear, GearUp, CheckBeforeLanding, LoweringGear};

typedef enum State
{
Gear_down,
Checking_before_takeoff,
Raising_gear,
Gear_up,
Checking_before_landing,
Lower_gear,
}State_type ;

typedef enum Switch
{
On,
Off,
}Switch_state;

typedef enum Pilot_lever
{
Raising,
Falling,
}Pilot_lever;

typedef enum Hydraulic_mechanism
{
Working,
Not_working,
}Hydraulic_mechanism;

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;

State State_machine[6] = {
{"Gear_down","Green","Down","Enabled",{NULL,NULL}},
{"Checking_before_takeoff","Green","Down","Enabled",{NULL,NULL}},
{"Raising_gear","Red","Up","Enabled",{NULL,NULL}},
{"Gear_up","Off","NULL","Disabled",{NULL,NULL}},
{"Checking_before_landing","Red","Down","Enabled",{NULL,NULL}},
{"Lower_gear","Green","Down","Enabled",{"Disabled","Disabled"}}
};

State_type Current_state;
static volatile Switch_state Squat_switch;
static volatile Switch_state Limit_switch;
static volatile Pilot_lever pl;
static volatile Hydraulic_mechanism hm;

int main()
{
Initiate_state_machine();
while(1)
{
state_table[Current_state]();
}
}

//Initiate_State_Machine
void Initiate_state_machine()
{
Current_state = Gear_down;
printf("The Finite State Machine is Initialized and current the program is in Gear_Down State & Light is:%s\n",State_machine[Current_state].Light);
}

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();
}
}

//CheckingBeforeTakeOFF
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 %d",&pl,&Squat_switch,&hm);
if(pl == Falling && Squat_switch == Off)
{
GearDown();
}
else if(pl == Raising && Squat_switch == On && hm == Working)
{
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 if(pl == Raising && Squat_switch == On && hm == Not_working)
{
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);
printf("Status of gas pressurized Spring System:%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",&pl,&Limit_switch);
if(pl == Falling && Limit_switch == On)
{
Current_state = Checking_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();
}
}

//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=Checking_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();
}

}

//CheckBeforeLanding
void CheckBeforeLanding(void)
{
Current_state=Checking_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 = Lower_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 = Lower_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();
}
}

//LoweringGear.
void LoweringGear(void)
{
Current_state = Lower_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);
}
if(Limit_switch == Off)
{
LoweringGear();
}

}

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.