Challenge

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

Problem Statement: Implement the Code for controlling the retraction and extension of Airplane’s landing gear

State Tables:

State Diagram:

/*
* FSM.c
*
* Created on: 04-Feb-2023
* Author: Rishabh Dev Shukla
*/

#include <stdio.h>
void Initi_state_mach(); //function declaration to initiate the state Machine
void Gr_down(); // function declaration for Gear down
void Check_Be4_TOFF(); // function declaration for checking before taking off
void Raising_Gr(); // function declaration for raising Gear
void Gr_up(); // function declaration for Gear up
void Check_Be4_Ldg(); //function declaration for Checking before landing
void Lowig_Gr(); //function declaration for lowering Gear

//defining a array state_table of function pointers
static void(*state_table[6])(void)={Gr_down, Check_Be4_TOFF, Raising_Gr, Gr_up, Check_Be4_Ldg, Lowig_Gr};

typedef enum State //System State Declaration by creating a enumerated type structure with states as elements
{
GEAR_DOWN,
CHECKING_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR,
}state_type; //creating state type alias (enum structure) name as st_type using typedef

typedef enum Switch //creating a enumerated type structure for switch status with ON & OFF as elements
{
on,
off //plane is on groung
}SW_status; //creating Switching alias (enum structure) name as SW_status using typedef

typedef enum pilot_lever //creating a enumerated type structure for pilot lever with Raising & falling as elements
{
Raising,
falling
}pit_lever; //creating pilot lever alias (enum structure) name as pit_lever using typedef

typedef enum hydraulic_mechanism //creating a enumerated type structure for hydraulic mechanism with working & Not_working as elements
{
working,
not_working
}hyd_mech; //creating hydraulic mechanism alias (enum structure) name as hyd_mech using typedef

//creating the object name for the enum type structures
static volatile SW_status squat_switch;
static volatile SW_status limit_switch;
static volatile pit_lever pl;
static volatile hyd_mech hm;
state_type present_state;

typedef struct // State table attributes declaration by defining a structure with all char type variables
{
char* present_state_indication;
char* light;
char* direction_valve_status;
char* landing_gear_hydraulic_control;
char* Gas_Pressurized_Spring_system_status[2];
}State_Table; //creating state table alias (structure) name as st_tab using typedef

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"}}
};
int main()
{
Initi_state_mach();
while(1)
{
state_table[present_state]();
}
}
void Gr_down(void) //function definition for Gear down
{
present_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)
{
present_state=CHECKING_BEFORE_TAKEOFF;
printf("Present state:%s\n",State_Machine[present_state].present_state_indication);
printf("Light is :%s\n", State_Machine[present_state].light);
}
else
{
Gr_down();
}
}

void Check_Be4_TOFF(void) //function definition for checking before take-off
{
present_state=CHECKING_BEFORE_TAKEOFF;
printf("Enter the status of pilot lever and squat switch and hydraulic mechanism:");
fflush(stdout);
scanf("%d %d %d",&pl,& squat_switch,&hm);
if(pl==falling && squat_switch==off)
{
Gr_down();
}
else if(pl==Raising && squat_switch==on && hm==working)
{
present_state=RAISING_GEAR;
printf("Present State:%s\n",State_Machine[present_state].present_state_indication);
printf("Light is:%s\n",State_Machine[present_state].light);
printf("Status of Direction valve:%s\n",State_Machine[present_state].direction_valve_status);
}
else if(pl==Raising && squat_switch==on && hm==not_working)
{
present_state=RAISING_GEAR;
printf("Current State:%s\n",State_Machine[present_state].present_state_indication);
printf("Light is:%s\n",State_Machine[present_state].light);
printf("Status of Direction valve:%s\n",State_Machine[present_state].direction_valve_status);
printf("Status of gas pressurised Spring System:%s\n",State_Machine[present_state].Gas_Pressurized_Spring_system_status);
}
}
//RaisingGear
void Raising_Gr(void) // function definition for Raising Gear
{
present_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)
{
present_state=CHECK_BEFORE_LANDING;
printf("Present State:%s\n",State_Machine[present_state].present_state_indication);
printf("Light is:%s\n",State_Machine[present_state].light);
printf("Landing Gear Hydraulic Control:%s\n",State_Machine[present_state].landing_gear_hydraulic_control);
}
else if(pl==Raising && limit_switch==off)
{
present_state=GEAR_UP;
printf("Present State:%s\n",State_Machine[present_state].present_state_indication);
printf("Light is:%s\n",State_Machine[present_state].light);
printf("Landing Gear Hydraulic Control:%s\n",State_Machine[present_state].landing_gear_hydraulic_control);
}
else
{
Raising_Gr();
}
}
//GearUp
void Gr_up(void) // function definition for Gear up
{
present_state=GEAR_UP;
printf("Enter the status of pilot's lever:");
fflush(stdout);
scanf("%d",&pl);
if(pl==falling)
{
present_state=CHECK_BEFORE_LANDING;
printf("Present State:%s\n",State_Machine[present_state].present_state_indication);
printf("Light is:%s\n",State_Machine[present_state].light);
printf("Landing Gear Hydraulic Control:%s\n",State_Machine[present_state].landing_gear_hydraulic_control);
}
else
{
Gr_up();
}

}
//CheckBeforeLanding
void Check_Be4_Ldg(void) //function definition for Checking before landing
{
present_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)
{
present_state=LOWERING_GEAR;
printf("Present State:%s\n",State_Machine[present_state].present_state_indication);
printf("Light is:%s\n",State_Machine[present_state].light);
printf("Status of Direction valve:%s\n",State_Machine[present_state].direction_valve_status);
}
else if(pl==falling && hm==not_working)
{
present_state=LOWERING_GEAR;
printf("Present State:%s\n",State_Machine[present_state].present_state_indication);
printf("Light is:%s\n",State_Machine[present_state].light);
printf("Status of Direction valve:%s\n",State_Machine[present_state].direction_valve_status);
printf("Status of gas pressurised Spring System:%s\n",State_Machine[present_state].Gas_Pressurized_Spring_system_status);
}
else if(pl==(Raising))
{
Check_Be4_Ldg();
}
}
//LoweringGear.
void Lowig_Gr(void) //function definition for lowering the gear
{
present_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)
{
present_state=CHECKING_BEFORE_TAKEOFF;
printf("Present State:%s\n",State_Machine[present_state].present_state_indication);
printf("Light is:%s\n",State_Machine[present_state].light);
}
else if(pl==Raising && limit_switch==off)
{
present_state=GEAR_DOWN;
printf("Present State:%s\n",State_Machine[present_state].present_state_indication);
printf("Light is:%s\n",State_Machine[present_state].light);
printf("Landing Gear Hydraulic Control:%s\n",State_Machine[present_state].landing_gear_hydraulic_control);
}
if(limit_switch==off)
{
Lowig_Gr();
}
}
void Initi_state_mach() // function definition to initiate the state Machine
{
present_state==GEAR_DOWN;
printf("The FSM is initialized and present state is Gear Down State & Light is:%s\n",State_Machine[present_state].light);
}

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.