Challenge

Implement the Code for controlling the retraction and extension of Airplane’s landing gear.

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

void Intitialize();
void G_Down();
void Ch_bfr_toff();
void Rsng_gr();              // declaring functions for various states of the finite machine 
void G_up();
void Ch_bfr_lndg();
void Lrng_gr();

typedef enum state           // defining states with values which are used to trigger function call using swtich statement
{
    Gear_down,
    Check_before_toff,
    Raising_gear,
    Gear_up,
    check_bfr_landing,
    lowering_gear
}state_type;

typedef enum switchstate    //assigning the values for states using enum
{
    open,closed
}S_state;

typedef enum pilot_lever    //defining the directionvalve values
{
    up,down,rising,falling
}P_lever;

typedef enum hydraulic_mech //defining the hydraulic pump state
{
    working,malfunction
}H_mech;

static volatile S_state squat_switch;  // variables of enum types to be used to take input from user
static volatile S_state limit_switch;
static volatile P_lever pilot_lever;
static volatile H_mech hydraulic_mech;

state_type current_state;

typedef struct 
{
    char* currentstatus;             // the variables of 6 states of the state diagram 
    char* light_status;
    char* directionvalve_status;
    char* hydralic_pump;
    char* spring_status[2];
    
}state_table;


// The six states of the state diagram

state_table statemachine[6]= {
                    {"Gear_down","lights_green","directionvalve_down","enable",{NULL,NULL}},
                    {"Check_before_toff","lights_green","directionvalve_down","enable",{NULL,NULL}},
                    {"Raising_gear","light_red","directionvalve_up","enable",{"Disabled","Enabled"}},
                    {"Gear_up","light_off","NULL","Disabled",{NULL,NULL}},
                    {"check_bfr_landing","light_red","directionvalve_down","enable",{NULL,NULL}},
                    {"lowering_gear","lights_green","directionvalve_down","enable",{"Disabled","Enabled"}}
};



// function definitions

void G_down()         
{
    printf("in Geardown\n");
    current_state = Gear_down;
    printf("enter the pilot lever and squat_switch states\n");
    scanf("%d %d",&pilot_lever,&squat_switch);
    if((pilot_lever==rising)&&(squat_switch==open))
    {
        current_state = Check_before_toff;
        printf("Current_state is %s\n",statemachine[current_state].currentstatus);
        printf("Light_status is %s\n\n",statemachine[current_state].light_status);
    }
    else
    {
        G_down();
    }
    
}

void Ch_bfr_toff()
{
    printf("in Check_before_toff\n");
    current_state = Check_before_toff;
    printf("enter the pilot lever and squat_switch states and Hydralic_pump_status\n");
    scanf("%d %d %d",&pilot_lever,&squat_switch,&hydraulic_mech);
    printf("Wait for 3 seconds\n");
    sleep(3);
    if((pilot_lever==up)&&(hydraulic_mech==working)&&(squat_switch==open))
    {
        current_state = Raising_gear;
        printf("Current_state is %s \n",statemachine[current_state].currentstatus);
        printf("Light_status is %s \n",statemachine[current_state].light_status);
        printf("directionvalve_status is %s \n\n",statemachine[current_state].directionvalve_status);
    }
    else if((pilot_lever==falling)||(squat_switch==closed))
    {
        G_down();
    }
    else if((pilot_lever==up)&&(hydraulic_mech==malfunction)&&(squat_switch==open))
    {
        current_state= Raising_gear;
        printf("Current_state is %s\n",statemachine[current_state].currentstatus);
        printf("Light_status is %s\n",statemachine[current_state].light_status);
        printf("directionvalve_status is %s\n",statemachine[current_state].directionvalve_status); 
        printf("spring_status %s\n\n",statemachine[current_state].spring_status[hydraulic_mech]);
    }
}

void Rsng_gr()
{
    printf("In Raising_gear\n\n");
    current_state=Raising_gear;
    printf("enter the pilot_lever and limit_switch states\n");
    scanf("%d %d",&pilot_lever,&limit_switch);
    if((pilot_lever==falling)&&(limit_switch==open))
    {
        current_state=check_bfr_landing;
         printf("Current_state is %s\n",statemachine[current_state].currentstatus);
         printf("Light_status is %s\n",statemachine[current_state].light_status);
         printf("directionvalve_status is %s\n",statemachine[current_state].directionvalve_status);
         printf("Hydralic_pump_status is %s\n\n",statemachine[current_state].hydralic_pump);
    }
    else if((pilot_lever==up)&&(limit_switch==closed))
    {
        current_state=Gear_up;
         printf("Current_state is %s\n",statemachine[current_state].currentstatus);
         printf("Light_status is %s\n",statemachine[current_state].light_status);
         printf("directionvalve_status is %s\n",statemachine[current_state].directionvalve_status);
         printf("Hydralic_pump_status is %s\n\n",statemachine[current_state].hydralic_pump);
    }
    else
    {
        Rsng_gr();
    }
}

void G_up()
{
    printf("In Gear_up\n\n");
    current_state=Gear_up;
    printf("enter the status pilot_lever\n");
    scanf("%d",&pilot_lever);
    if(pilot_lever==falling)
    {
        current_state=check_bfr_landing;
        printf("Current_state is %s\n",statemachine[current_state].currentstatus);
        printf("Light_status is %s\n",statemachine[current_state].light_status);
        printf("directionvalve_status is %s\n\n",statemachine[current_state].directionvalve_status);
    }
    else
    {
        G_up();
    }
}

void Ch_bfr_lndg()
{
    printf("In check_bfr_landing\n\n");
    current_state=check_bfr_landing;
    printf("enter the status pilot_lever and hydraulic_mech\n");
    scanf("%d %d",&pilot_lever,&hydraulic_mech);
    if(pilot_lever==down&&hydraulic_mech==working)
    {
        current_state=lowering_gear;
        printf("Current_state is %s\n",statemachine[current_state].currentstatus);
        printf("Light_status is %s\n",statemachine[current_state].light_status);
        printf("directionvalve_status is %s\n\n",statemachine[current_state].directionvalve_status);
    }
    else if(pilot_lever==down&&hydraulic_mech==malfunction)
    {
        current_state=lowering_gear;
        printf("Current_state is %s\n",statemachine[current_state].currentstatus);
        printf("Light_status is %s\n",statemachine[current_state].light_status);
        printf("directionvalve_status is %s\n",statemachine[current_state].directionvalve_status);
        printf("spring_status %s\n\n",statemachine[current_state].spring_status[hydraulic_mech]);
    }
    else if(pilot_lever==(up||falling||rising))
    {
        Ch_bfr_lndg();
    }
}

void Lrng_gr()
{
    printf("In lowering_gear\n\n");
    current_state=lowering_gear;
    printf("enter the status pilot_lever and limit_switch\n");
    scanf("%d %d",&pilot_lever,&limit_switch);
    
    if((pilot_lever==down)&&(limit_switch==open))
    {
        current_state=Gear_down;
        printf("Current_state is %s\n",statemachine[current_state].currentstatus);
        printf("Light_status is %s\n\n",statemachine[current_state].light_status);
    }
    
    else if((pilot_lever==rising))
    {
        current_state=Check_before_toff;
        printf("Current_state is %s\n",statemachine[current_state].currentstatus);
        printf("Light_status is %s\n",statemachine[current_state].light_status);
        printf("directionvalve_status  %s\n\n",statemachine[current_state].directionvalve_status);
    }
    
    if(limit_switch==closed)
    {
        Lrng_gr();
    }
}

void Intitialize()
{
    current_state=Gear_down;
    printf("Current_state is %s\n",statemachine[current_state].currentstatus);
    printf("Light_status is %s\n\n",statemachine[current_state].light_status);
}

//end of function definitions


int main()
{
    
    printf("\n\n\nsquat_switch/limit_switch = 0 is open \t squat_switch/limit_switch = 1 is closed\n\n");
    printf("pilot_lever =  0 is UP \n pilot_lever = 1 is DOWN \n pilot_lever = 2 is rising \n pilot_lever = 3 is falling \n\n");
    printf("hydraulic_mech = 0 is working \t hydraulic_mech = 1 is malfunction\n\n");
    
    Intitialize();  //initialization of state 
    while(1)        
    {
        switch(current_state)
        {
        case 0:
        G_down();
        break;
        case 1:
        Ch_bfr_toff();
        break;
        case 2:
        Rsng_gr();
        break;
        case 3:
        G_up();
        break;
        case 4:
        Ch_bfr_lndg();
        break;
        case 5:
        Lrng_gr();
        break;
        default:
        break;
        }
    }
}

OUTPUT:

squat_switch/limit_switch = 0 is open squat_switch/limit_switch = 1 is closed

pilot_lever = 0 is UP
pilot_lever = 1 is DOWN
pilot_lever = 2 is rising
pilot_lever = 3 is falling

hydraulic_mech = 0 is working hydraulic_mech = 1 is malfunction

Current_state is Gear_down
Light_status is lights_green

in Geardown
enter the pilot lever and squat_switch states
2 0
Current_state is Check_before_toff
Light_status is lights_green

in Check_before_toff
enter the pilot lever and squat_switch states and Hydralic_pump_status
0 0 0
Wait for 3 seconds checking the hydraulic_mech
Current_state is Raising_gear
Light_status is light_red
directionvalve_status is directionvalve_up

In Raising_gear

enter the pilot_lever and limit_switch states
0 1
Current_state is Gear_up
Light_status is light_off
directionvalve_status is NULL
Hydralic_pump_status is Disabled

In Gear_up

enter the status pilot_lever
0 3
Current_state is check_bfr_landing
Light_status is light_red
directionvalve_status is directionvalve_down

In check_bfr_landing

enter the status pilot_lever and hydraulic_mech
1 1
Current_state is lowering_gear
Light_status is lights_green
directionvalve_status is directionvalve_down
spring_status Enabled

In lowering_gear

enter the status pilot_lever and limit_switch
1 0
Current_state is Gear_down
Light_status is lights_green

in Geardown
enter the pilot lever and squat_switch states



-----end of program----

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.