Challenge

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

Ans:

according to the attachement we have different states like gear down, check before takeoff, raising gear, gearr up, check before landing, lowering gear. 

first i created subfunctions for all those states.

void Initial_state_machine();

void Gear_down();

void Check_before_takeoff();

void Raising_gear();

void Gear_up();

void Check_before_landing();

void Lowering_gear();

based on the data i have created a enum table

State_type

Current state

Switch_status

Squat Switch and Limit Switch

Pilot_lever_status Hydraulic_Mechanism_status

Gear_Down...0

Check_before Takeoff....1

Rising Gear...2

Gear up....3

Check before landing...4

lowering gear....5

Open....0

Close.....1

Raising...0

Falling....1

Up....2

Down....3

Malfunctioning....0

working......1

 

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
By using above i have created different structures for each column.
 
typedef enum states
{
    GEAR_DOWN,
    CHECK_BEFORE_TAKEOFF,
    RAISING_GEAR,
    GEAR_UP,
    CHECK_BEFORE_LANDING,
    LOWERING_GEAR,
}state_type;
 
typedef enum Switch 
{
    open, close
}Switch_status;
 
typedef enum pilot_lever
{
    raising,
    falling,
    up,
    down 
}pilot_lever_status;
 
typedef enum hydraulic_mechanism
{
    working,
    malfunctioning
}hydraulic_mechanism_status;
 
next i created the global variables which i'm further going to use in the program.
here i mention static because i don't want someone to change the values later, and volatile because, i can change the values anytime till the end of my program.
 
static volatile Switch_status squat_switch;
static volatile Switch_status limit_switch;
static volatile pilot_lever_status pilot_lever;
static volatile pilot_lever_status hydraulic_mechanism;
 
state_type current_state;
 
Then i created a state table and i have given inputs to it.
 
typedef struct
{
    char* current_state_status;
    char* light_status;
    char* direction_valve_status;
    char* hydraulic_pump_ctrl_status;
    char* gas_pressurised_spring_status[2];
}state_table;
 
 
static state_table state_machine[6]=
{
    {"gear down","green","down","enables",{0,0}},
    {"check before takeoff","green","down","enabled",{0,0}},
    {"raising gear","red","up","enabled",{0,0}},
    {"gear up","off","NULL","disabled",{0,0}},
    {"check before landing","red","down","enabled",{0,0}},
    {"lowering gear","green","down","enabled",{"disabled","enabled"}}
};
 
Then I call state table inside the function, i gave different states as inputs.
 
static void(*state_Table[])(void)={Gear_down,Check_before_takeoff,Raising_gear,Gear_up,Check_before_landing,Lowering_gear};
 
Then i start expanding the subfunctions.
first i start with initial state machine, this is my entire machine code, which i call in main function to execute.
inside this all the subfunctions and logics for them expanded.
since now we are in intial position, the gears are in down position, so i printed that first using printf statement.
void Initial_state_machine()
{
    current_state = GEAR_DOWN;
    printf("\n state machine is initials and at first program is in gear down and its light status is : %s\n",
    state_machine[current_state].light_status);
}
 
now we are going to expand the gear down logic.
void Gear_down(void)
{
    current_state=GEAR_DOWN;
    printf("\n Enter the pilot's lever position and squat switch status: \n");
    scanf("%d %d",&pilot_lever,&squat_switch);
    if (pilot_lever==raising && squat_switch==open)
    {
        current_state=CHECK_BEFORE_TAKEOFF;
        printf("\n The current state of the machine is : %s \n",state_machine[current_state].current_state_status);
        printf("\n Light status of the current machine status is : %s\n",state_machine[current_state].light_status);
    }
    else
    {
        Gear_down();
    }
}
 
now the above logic ......
here i gave an IF statement, where if the condition satisified, the machine goes to next state i.e., check before takeoff, else it will remain in same position.
the statement is pilot_lever==raising && squat_switch==open, from the enum table we know the values i.e., 0 0, first it will ask for values, if we gave the values as '0 0' then it goes to check before takefore, else it remain in gear down.
 
now check before takeoff....
void Check_before_takeoff()
{
    current_state=CHECK_BEFORE_TAKEOFF;
    printf("\n Enter the pilot lever position, squat switch and hydraulic_mechanism: \n");
    scanf("%d %d %d", &pilot_lever,&squat_switch,&hydraulic_mechanism);
    printf("\n Just wait 2 sec \n");
    if (pilot_lever==up && squat_switch==close)
    {
        Gear_down();
    }
    else if (pilot_lever==up && squat_switch==open&&hydraulic_mechanism==working)
    {
        current_state=RAISING_GEAR;
        printf("\n current state :%s\n",state_machine[current_state].current_state_status);
        printf("light_status: %s\n", state_machine[current_state].light_status);
        printf("\n Direction status :%s\n\n", state_machine[current_state].direction_valve_status);
    }
    else if (pilot_lever==up && squat_switch==open &&hydraulic_mechanism==malfunctioning)
    {
        current_state=RAISING_GEAR;
        printf("\n Current state : %s \n", state_machine[current_state].current_state_status);
        printf("\n light status : %s\n", state_machine[current_state].light_status);
        printf("\n Direction status : %s\n\n",state_machine[current_state].direction_valve_status);
        printf("\n Gas pressurised spring status : %s \n", state_machine[current_state].gas_pressurised_spring_status[hydraulic_mechanism]);
    }
    
}
 
now it is asking for lever position, squat swith, and hydraulic mecanism values.
after scaning those inputs, it will check with if condition, here first condition is if (pilot_lever==up && squat_switch==close)
    {
        Gear_down();
if we give '2' and '1', the machine immeddiatly went to gear down position, else, it will check for another condition i.e., else if (pilot_lever==up && squat_switch==open&&hydraulic_mechanism==working)
    {
        current_state=RAISING_GEAR;
if we give '2', '0','1', it will go to next position i.e., raising gear.
here, even if we give hydraulic mechanisam woriking or non working, it doesn't matter to it it will go to raising gear condition.
 
next Raising Gear logic....
 
void Raising_gear()
{
    current_state=RAISING_GEAR;
    printf("\n Enter the switch status and pilot lever status:\n");
    scanf("%d %d",&limit_switch,&pilot_lever);
    printf("\n wait for 1 sec\n");
    if (pilot_lever==falling &&limit_switch==open)
    {
        current_state=CHECK_BEFORE_LANDING;
        printf("\n current state is : %s \n", state_machine[current_state].current_state_status);
        printf("\n LIght status : %s \n", state_machine[current_state].light_status);
        printf("\n hydraulic condition for landing gear status is: %s\n", state_machine[current_state].hydraulic_pump_ctrl_status);
    }
    else if (limit_switch==close && pilot_lever==up)
    {
        current_state=GEAR_UP;
        printf("\n current state is : %s\n", state_machine[current_state].current_state_status);
        printf("\n light status : %s\n",state_machine[current_state].light_status);
        printf("\n Hydraulic condition for landing gear status is : %s\n", state_machine[current_state].hydraulic_pump_ctrl_status);
    }
    else
    {
        Raising_gear();
    
    }
}
 
In this we can go to two states, one is check before takeoff, second on eis gear up, based on the values we gave, it will go to that state, else it will stay in same position.
first is it is asking for switch status and pilot lever status.
 if (pilot_lever==falling &&limit_switch==open)
    {
        current_state=CHECK_BEFORE_LANDING;
here if we gave, '1','0' it will go to check before landing, else it check for another condition
else if (limit_switch==close && pilot_lever==up)
    {
        current_state=GEAR_UP;
i.e., if we give '1','2', it will go to gear up state. other than ;'1','0' and '1','2', if we gave any other value it will stay in raising gear state.
 
next gear up logic....
 
 
 
void Gear_up()
{
    current_state=GEAR_UP;
    printf("\n Enter the pilot lever position: \n");
    scanf("%d",&pilot_lever);
    
    if (pilot_lever==falling)
    {
        current_state=CHECK_BEFORE_LANDING;
        printf("\n current_state : %s \n", state_machine[current_state].current_state_status);
        printf("\n light status : %s \n", state_machine[current_state].light_status);
        printf("\n hydraulic condition for landing gear status is: %s \n", state_machine[current_state].hydraulic_pump_ctrl_status);
    }
    else
    {
        Gear_up();
    }
}

now from according to state diagram, if pilot lever switch is falling, it will to check before landing from gear up postion.

 if (pilot_lever==falling)
    {
        current_state=CHECK_BEFORE_LANDING;
we need to give lever position value from the state table, if we give '1', it will go to next position, else it will stay in same position.
next is check before landing.....
void Check_before_landing()
{
    current_state=CHECK_BEFORE_LANDING;
    printf("\n Enter the pilot lever position, status of hydraulic mechanism:\n ");
    scanf("%d %d", &pilot_lever,&hydraulic_mechanism);
    
    if (pilot_lever==down && hydraulic_mechanism==working)
    {
        current_state=LOWERING_GEAR;
        printf("\n current_state: %s \n", state_machine[current_state].current_state_status);
        printf("\n light status: %s \n",state_machine[current_state].light_status);
        printf("\n direcction status : %s\n\n", state_machine[current_state].direction_valve_status);
    }
    else if (pilot_lever==down && hydraulic_mechanism==malfunctioning)
    {
        current_state=LOWERING_GEAR;
        printf("\n Current state : %s \n", state_machine[current_state].current_state_status);
        printf("light status : %s\n", state_machine[current_state].light_status);
        printf("\n direction status : %s\n\n",state_machine[current_state].direction_valve_status);
        printf("\n Gas pressurised spring status: %s\n", state_machine[current_state].gas_pressurised_spring_status[hydraulic_mechanism]);
    }
    if(pilot_lever==(up || raising || falling))
    {
        Check_before_landing();
    }
}
 
here it first ask for pilot lever, and hydraulic mechanism position then it check for following condition,
 if (pilot_lever==down && hydraulic_mechanism==working)
    {
        current_state=LOWERING_GEAR;
if we give  '3','1', then it will go lowering gear position,
here, hydraulic mechanism is working or non working it does not matter, it will go to lowering gear state till pilot lever = down, other than down if we give anything, it will remain in same position.
 
next is lowering gear....
 void Lowering_gear()
{
    current_state=LOWERING_GEAR;
    printf("\n Enter the pilot lever position, limit switch");
    scanf("%d %d",&pilot_lever,&limit_switch);
    if (pilot_lever== raising && limit_switch==close)
    {
        current_state=CHECK_BEFORE_TAKEOFF;
        printf("\n current state: %s\n", state_machine[current_state].current_state_status);
        printf("\n light status : %s\n", state_machine[current_state].light_status);
    }
    if(pilot_lever==down && limit_switch==open)
    {
        current_state=GEAR_DOWN;
        printf("\n current_state: %s\n", state_machine[current_state].current_state_status);
        printf("\n light status: %s\n", state_machine[current_state].light_status);
        printf("\n Hydraulic condition for landing geaar status is: %s\n", state_machine[current_state].hydraulic_pump_ctrl_status);
    }
    else
    {
        Lowering_gear();
    }
}
here, it is asking for pilot lever and  limit switch position, if it satisifies the following condition, it will go to check before takeoff state, else it will check for anothe condition.
 if (pilot_lever== raising && limit_switch==close)
    {
        current_state=CHECK_BEFORE_TAKEOFF;
here if we give '0','1', it will go to check before takeoff
 
if(pilot_lever==down && limit_switch==open)
    {
        current_state=GEAR_DOWN;
 
If we want to go gear down position, we need to give '3','0'.
 
this continues, until we stop the program.
we call this intial state machine into main program....
 
Code:
 
#include
#include
//Functions declaration that used in project
void Initial_state_machine();
void Gear_down();
void Check_before_takeoff();
void Raising_gear();
void Gear_up();
void Check_before_landing();
void Lowering_gear();
 
// defining enum table with different structures
typedef enum states
{
    GEAR_DOWN,
    CHECK_BEFORE_TAKEOFF,
    RAISING_GEAR,
    GEAR_UP,
    CHECK_BEFORE_LANDING,
    LOWERING_GEAR,
}state_type;
 
typedef enum Switch 
{
    open, close
}Switch_status;
 
typedef enum pilot_lever
{
    raising,
    falling,
    up,
    down 
}pilot_lever_status;
 
typedef enum hydraulic_mechanism
{
    working,
    malfunctioning
}hydraulic_mechanism_status;
 
// global variables
static volatile Switch_status squat_switch;// static for no to change the values by any other programmer
static volatile Switch_status limit_switch;// volatile for i can change the values inside my program
static volatile pilot_lever_status pilot_lever;
static volatile pilot_lever_status hydraulic_mechanism;
 
 
state_type current_state;
 
typedef struct //creating state table
{
    char* current_state_status;
    char* light_status;
    char* direction_valve_status;
    char* hydraulic_pump_ctrl_status;
    char* gas_pressurised_spring_status[2];
}state_table;
 
 
static state_table state_machine[6]=// input the values to the state table
{
    {"gear down","green","down","enables",{0,0}},
    {"check before takeoff","green","down","enabled",{0,0}},
    {"raising gear","red","up","enabled",{0,0}},
    {"gear up","off","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};
 
void Initial_state_machine()// sub-function expansion
{
    current_state = GEAR_DOWN;
    printf("\n state machine is initials and at first program is in gear down and its light status is : %s\n",
    state_machine[current_state].light_status);
}
 
void Gear_down(void)// sub function expansion
{
    current_state=GEAR_DOWN;
    printf("\n Enter the pilot's lever position and squat switch status: \n");
    scanf("%d %d",&pilot_lever,&squat_switch);
    if (pilot_lever==raising && squat_switch==open)// values 0 0, it will go to check before takeoff
    {
        current_state=CHECK_BEFORE_TAKEOFF;
        printf("\n The current state of the machine is : %s \n",state_machine[current_state].current_state_status);
        printf("\n Light status of the current machine status is : %s\n",state_machine[current_state].light_status);
    }
    else
    {
        Gear_down();
    }
}
 
void Check_before_takeoff()// sub function expansion
{
    current_state=CHECK_BEFORE_TAKEOFF;
    printf("\n Enter the pilot lever position, squat switch and hydraulic_mechanism: \n");
    scanf("%d %d %d", &pilot_lever,&squat_switch,&hydraulic_mechanism);
    printf("\n Just wait 2 sec \n");
    if (pilot_lever==up && squat_switch==close)
    {
        Gear_down();
    }
    else if (pilot_lever==up && squat_switch==open&&hydraulic_mechanism==working)
    {           // values 2 0 1 go to raising gear else go to gear down
        current_state=RAISING_GEAR;
        printf("\n current state :%s\n",state_machine[current_state].current_state_status);
        printf("light_status: %s\n", state_machine[current_state].light_status);
        printf("\n Direction status :%s\n\n", state_machine[current_state].direction_valve_status);
    }
    else if (pilot_lever==up && squat_switch==open &&hydraulic_mechanism==malfunctioning)
    {
        current_state=RAISING_GEAR;
        printf("\n Current state : %s \n", state_machine[current_state].current_state_status);
        printf("\n light status : %s\n", state_machine[current_state].light_status);
        printf("\n Direction status : %s\n\n",state_machine[current_state].direction_valve_status);
        printf("\n Gas pressurised spring status : %s \n", state_machine[current_state].gas_pressurised_spring_status[hydraulic_mechanism]);
    }
    
}
 
void Raising_gear()
{
    current_state=RAISING_GEAR;
    printf("\n Enter the switch status and pilot lever status:\n");
    scanf("%d %d",&limit_switch,&pilot_lever);
    printf("\n wait for 1 sec\n");
    if (pilot_lever==falling &&limit_switch==open) // values 1 0 go to check before landing
    {
        current_state=CHECK_BEFORE_LANDING;
        printf("\n current state is : %s \n", state_machine[current_state].current_state_status);
        printf("\n LIght status : %s \n", state_machine[current_state].light_status);
        printf("\n hydraulic condition for landing gear status is: %s\n", state_machine[current_state].hydraulic_pump_ctrl_status);
    }
    else if (limit_switch==close && pilot_lever==up) // values 1 2 go tto gear up state
    {
        current_state=GEAR_UP;
        printf("\n current state is : %s\n", state_machine[current_state].current_state_status);
        printf("\n light status : %s\n",state_machine[current_state].light_status);
        printf("\n Hydraulic condition for landing gear status is : %s\n", state_machine[current_state].hydraulic_pump_ctrl_status);
    }
    else
    {
        Raising_gear();
    
    }
}
 
void Gear_up() // subfunction expansion
{
    current_state=GEAR_UP;
    printf("\n Enter the pilot lever position: \n");
    scanf("%d",&pilot_lever);
    
    if (pilot_lever==falling) // value 1 go to check before landing
    {
        current_state=CHECK_BEFORE_LANDING;
        printf("\n current_state : %s \n", state_machine[current_state].current_state_status);
        printf("\n light status : %s \n", state_machine[current_state].light_status);
        printf("\n hydraulic condition for landing gear status is: %s \n", state_machine[current_state].hydraulic_pump_ctrl_status);
    }
    else
    {
        Gear_up();
    }
}
 
void Check_before_landing() // subfunction expansion
{
    current_state=CHECK_BEFORE_LANDING;
    printf("\n Enter the pilot lever position, status of hydraulic mechanism:\n ");
    scanf("%d %d", &pilot_lever,&hydraulic_mechanism);
    
    if (pilot_lever==down && hydraulic_mechanism==working) // value 3 1 it will go to lowering gear
    {
        current_state=LOWERING_GEAR;
        printf("\n current_state: %s \n", state_machine[current_state].current_state_status);
        printf("\n light status: %s \n",state_machine[current_state].light_status);
        printf("\n direcction status : %s\n\n", state_machine[current_state].direction_valve_status);
    }
    else if (pilot_lever==down && hydraulic_mechanism==malfunctioning) // values 3 0 it will go to lowering gear 
    {
        current_state=LOWERING_GEAR;
        printf("\n Current state : %s \n", state_machine[current_state].current_state_status);
        printf("light status : %s\n", state_machine[current_state].light_status);
        printf("\n direction status : %s\n\n",state_machine[current_state].direction_valve_status);
        printf("\n Gas pressurised spring status: %s\n", state_machine[current_state].gas_pressurised_spring_status[hydraulic_mechanism]);
    }
    if(pilot_lever==(up || raising || falling))// other than pilot_lever= down, if we give anything it will stay in check before landing
    {
        Check_before_landing();
    }
}
 
void Lowering_gear() // subfunction expansion
{
    current_state=LOWERING_GEAR;
    printf("\n Enter the pilot lever position, limit switch");
    scanf("%d %d",&pilot_lever,&limit_switch);
    if (pilot_lever== raising && limit_switch==close) // values  0 1 go to check before takeoff
    {
        current_state=CHECK_BEFORE_TAKEOFF;
        printf("\n current state: %s\n", state_machine[current_state].current_state_status);
        printf("\n light status : %s\n", state_machine[current_state].light_status);
    }
    if(pilot_lever==down && limit_switch==open) // values 3 0 go to gear down 
    {
        current_state=GEAR_DOWN;
        printf("\n current_state: %s\n", state_machine[current_state].current_state_status);
        printf("\n light status: %s\n", state_machine[current_state].light_status);
        printf("\n Hydraulic condition for landing geaar status is: %s\n", state_machine[current_state].hydraulic_pump_ctrl_status);
    }
    else
    {
        Lowering_gear();
    }
}
 
int main() // main program
{
    Initial_state_machine();
    while(1)
    {
        state_Table[current_state]();
    }
}

Result:

 
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.