Challenge

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

#include<stdio.h>
#include<stdlib.h>

void In_State_Machine();
void Gear_Down();
void Check_Before_Takeoff();
void Raising_Gear();
void Gear_Up();
void Check_Before_Landing();
void Lowering_Gear();

typedef enum
{
GR_DWN,
CHK_BFR_TKFF,
RSING_GR,
GR_UP,
CHK_BFR_LND,
LWRNG_GR
}Sate_Type;

typedef enum
{
Open,
Close
}Switch_Status;

typedef enum
{
Raising,
Falling,
UP,
DOWN
}Pilot_Lever_Status;

typedef enum
{
Working,
Malfunctioning
}Hydraulic_Mechanism_Satuts;

static volatile Switch_Status squat_switch;
static volatile Switch_Status limit_switch;
static volatile Pilot_Lever_Status pilot_lever;
static volatile Hydraulic_Mechanism_Satuts hydraulic_mechanism;

Sate_Type current_state;

typedef struct
{
char* current_state_indication;
char* light_status;
char* direction_valve_status;
char* hydaulic_pump_control_status;
char* gas_pressurized_spring_status[2];
}State_Table;

static State_Table state_machine[6]={
{"Gear Down","Green","Down","Enabled",{0,0}},
{"Check Before Takeoff","Green","Down","Enabled",{0,0}},
{"Raising Gear","Red","Up","Enabled",{"Disabled","Enabled"}},
{"Gear Up","Red","NULL","Disabled",{0,0}},
{"Check Before Landing","Red","Down","Enabled",{0,0}},
{"Lowering Gear","Green","Down","Enabled",{"Disabled","Enabled"}}
};

static void (*state_table[])(void)={Gear_Down,Check_Before_Takeoff,Raising_Gear,
Gear_Up,Check_Before_Landing,Lowering_Gear};

int main(){
In_State_Machine();
while(1){
state_table[current_state]();
}
}

void In_State_Machine(){
current_state=GR_DWN;
printf("\nState machine is initialized & program is in Gear_Down state and light status is: %s\n",state_machine[current_state].light_status);
}

void Gear_Down(){
current_state=GR_DWN;
printf("\nEnter Pilot lever position, Squat switch status:");
fflush(stdout);
scanf("%d %d",&pilot_lever,&squat_switch);
if(pilot_lever==Raising && squat_switch==Open){
current_state=CHK_BFR_TKFF;
printf("\nCurrent state: %s",state_machine[current_state].current_state_indication);
printf("\nLight status: %s",state_machine[current_state].light_status);
}
else{
Gear_Down();
}
}

void Check_Before_Takeoff(){
current_state=CHK_BFR_TKFF;
printf("\nEnter Pilot lever position, Direction valve position, Squat switch status:");
fflush(stdout);
scanf("%d %d %d",&pilot_lever,&squat_switch,&hydraulic_mechanism);

if(pilot_lever==Falling || squat_switch==Close){
Gear_Down();
}
else if(pilot_lever==UP && squat_switch==Open && hydraulic_mechanism==Working){
current_state=RSING_GR;
printf("\nCurrent state: %s",state_machine[current_state].current_state_indication);
printf("\nLight status: %s",state_machine[current_state].light_status);
printf("\nDirection valve: %s",state_machine[current_state].direction_valve_status);
}
else{
Check_Before_Takeoff();
}
}

void Raising_Gear(){
current_state=RSING_GR;
printf("\nEnter Limit switch status, Pilot lever position:");
fflush(stdout);
scanf("%d %d",&limit_switch,&pilot_lever);
if(limit_switch==Open && pilot_lever==Falling){
current_state=CHK_BFR_LND;
printf("\nCurrent state: %s",state_machine[current_state].current_state_indication);
printf("\nLight status: %s",state_machine[current_state].light_status);
}
else if(limit_switch==Close && pilot_lever==UP){
current_state=GR_UP;
printf("\nCurrent state: %s",state_machine[current_state].current_state_indication);
printf("\nLight status: %s",state_machine[current_state].light_status);
}
else{
Raising_Gear();
}
}

void Gear_Up(){
current_state=GR_UP;
printf("\nEnter Pilot lever position:");
fflush(stdout);
scanf("%d",&pilot_lever);
if(pilot_lever==Falling){
current_state=CHK_BFR_LND;
printf("\nCurrent state: %s",state_machine[current_state].current_state_indication);
printf("\nLight status: %s",state_machine[current_state].light_status);
printf("\nDirection valve: %s",state_machine[current_state].direction_valve_status);
}
else{
Gear_Up();
}
}

void Check_Before_Landing(){
current_state=CHK_BFR_LND;
printf("\nEnter Pilot lever position, Direction valve position:");
fflush(stdout);
scanf("%d %d",&pilot_lever,&hydraulic_mechanism);
if(pilot_lever==DOWN && hydraulic_mechanism==Working){
current_state=LWRNG_GR;
printf("\nCurrent state: %s",state_machine[current_state].current_state_indication);
printf("\nDirection valve: %s",state_machine[current_state].direction_valve_status);
}
else if(pilot_lever==DOWN && hydraulic_mechanism==Malfunctioning){
current_state=LWRNG_GR;
printf("\nCurrent state: %s",state_machine[current_state].current_state_indication);
printf("\nDirection valve: %s",state_machine[current_state].direction_valve_status);
printf("\nGas pressurized spring system: %s",state_machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]);
}
if(pilot_lever==(UP || Raising || Falling)){
Check_Before_Landing();
}
}

void Lowering_Gear(){
current_state=LWRNG_GR;
printf("\nEnter Limit switch status, Pilot lever position:");
fflush(stdout);
scanf("%d %d",&limit_switch,&pilot_lever);
if(limit_switch==Close && pilot_lever==Raising){
current_state=CHK_BFR_TKFF;
printf("\nCurrent state: %s",state_machine[current_state].current_state_indication);
printf("\nLight status: %s",state_machine[current_state].light_status);
}
if(limit_switch==Open && pilot_lever==DOWN){
current_state=GR_DWN;
printf("\nCurrent state: %s",state_machine[current_state].current_state_indication);
printf("\nLight status: %s",state_machine[current_state].light_status);
}
else{
Lowering_Gear();
}
}

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.