Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Code:
Aircraft.h file
#ifndef AIRCRAFT_H_
#define AIRCRAFT_H_
#include
//All the Function prototypes are defined here
void Initial_state();
void Gear_down();
void Checking_BeforeTakeoff();
void Raising_Gear();
void Gear_Up();
void Checking_BeforeLanding();
void Lowering_Gear();
typedef enum // Total 6 states of an aircraft is declared here
{
GearDown=0, CheckingBeforeTakeoff=1, RaisingGear=2, GearUp=3, CheckingBeforeLanding=4, LoweringGear=5
}state_type;
state_type current_status; // For better readability state type is taking as current status of an aircraft
typedef enum { // Status of squat and limit switch is declared
Open=0, Close=1
}Switch_status;
typedef enum // Position of pilot lever is declared here
{
Raising=0, Falling=1, Up=2, Down=3
}Pilot_lever;
typedef enum // Hydraulic mechanism is declared here
{
Working=0, Malfunction=1
}Hydraulic_mechanism;
static volatile Switch_status ss; // Here squat switch is defined as ss for convenience in writing
static volatile Switch_status ls; // Here limit switch is defined as ls for convenience in writing
static volatile Pilot_lever pl; // Here Pilot lever is defined as pl for convenience in writing
static volatile Hydraulic_mechanism hm; // Here Hydraulic mechanism is defined as hm for convenience in writing
typedef struct // For knowing the condition of various states state table is created
{
char* current_state_indication;
char* light_status;
char* direction_valve;
char* hydraulic_pump;
char* gas_spring[2];
}StateTable;
// State machine is declared here which takes reference from state table
static StateTable state_machine[6] = {
{"Gear Down","Green","Down","Enabled",{NULL,NULL}},
{"Checking Before Takeoff","Green","Down","Enabled",{NULL,NULL}},
{"Raising Gear","Red","Up","Enabled",{"Disabled","Enabled"}},
{"Gear Up","Off","NULL","Disabled",{NULL,NULL}},
{"Checking Before Landing","Red","Down","Enabled",{NULL,NULL}},
{"Lowering Gear","Green","Down","Enabled",{"Disabled","Enabled"}}
};
#endif /* AIRCRAFT_H_ */
main.c
#include
#include "Aircraft.h"
// Main program begins from here it will call for initial state
static void (*State_table[])(void) = {Gear_down, Checking_BeforeTakeoff, Raising_Gear, Gear_Up, Checking_BeforeLanding, Lowering_Gear };
int main()
{
Initial_state();
while(1)
{
State_table[current_status]();
}
}
initial.c
#include
#include "Aircraft.h"
void Initial_state() // By deafualt initial state is in Gear Down
{
current_status = GearDown; // Calling the state defined in enum
printf("The Aircraft is initialized and it is in Gear down state & Light is: %s",state_machine[current_status].light_status);
}
geardown.c
#include
#include "Aircraft.h"
void Gear_down() // Checking condition for gear down
{
current_status = GearDown; // Calling the state defined in enum
printf("\nEnter the status of pilot lever and squat switch: ");
fflush(stdout);
scanf("%d %d",&pl, &ss);
if(pl == Raising && ss == Open) // pl = pilot lever and ss = squat switch
{
current_status=CheckingBeforeTakeoff; // Calling the state defined in enum
printf("Current status %s\n", state_machine[current_status].current_state_indication);
printf("Light is: %s \n",state_machine[current_status].light_status);
printf("Direction valve status: %s\n\n",state_machine[current_status].direction_valve);
}
}
takeoff.c
#include
#include "Aircraft.h"
void Checking_BeforeTakeoff() // Checking condition for take off
{
current_status = CheckingBeforeTakeoff;
printf("Enter the status of pilot lever, squat switch and hydraulic: ");
fflush(stdout);
scanf("%d %d %d",&pl, &ss, &hm); // pl = pilot lever , ss = squat switch , hm = hydraulic mechanism
if (pl == Falling && ss == Close )
{
current_status = GearDown; // Calling the state defined in enum
printf("Current status %s\n", state_machine[current_status].current_state_indication);
printf("Light is: %s\n",state_machine[current_status].light_status);
printf("Direction valve status: %s\n",state_machine[current_status].direction_valve);
printf("Status of gas pressurised spring system: %s\n",state_machine[current_status].gas_spring[hm]);
}
else if (pl == Up && ss == Open && hm == Working) // pl = pilot lever , ss = squat switch , hm = hydraulic mechanism
{
current_status = RaisingGear; // Calling the state defined in enum
printf("Current status %s\n", state_machine[current_status].current_state_indication);
printf("Light is: %s\n",state_machine[current_status].light_status);
printf("Direction valve status: %s\n",state_machine[current_status].direction_valve);
printf("Status of gas pressurised spring system: %s\n",state_machine[current_status].gas_spring[hm]);
}
else if (pl == Up && ss == Open && hm == Malfunction) // pl = pilot lever , ss = squat switch , hm = hydraulic mechanism
{
current_status = RaisingGear; // Calling the state defined in enum
printf("Current status %s\n", state_machine[current_status].current_state_indication);
printf("Light is: %s\n",state_machine[current_status].light_status);
printf("Direction valve status: %s\n",state_machine[current_status].direction_valve);
printf("Status of gas pressurised spring system: %s\n",state_machine[current_status].gas_spring[hm]);
}
}
rising.c
#include
#include "Aircraft.h"
void Raising_Gear() // Checking condition for rising gear
{
current_status = RaisingGear; // Calling the state defined in enum
printf("Enter the status of pilot lever and limit switch:");
fflush(stdout);
scanf("%d %d", &pl, &ls); // pl = pilot lever and ls = limit switch
if(pl == Falling && ls == Close )
{
current_status = CheckingBeforeLanding; // Calling the state defined in enum
printf("Current status %s\n", state_machine[current_status].current_state_indication);
printf("Light is: %s \n",state_machine[current_status].light_status);
printf("Landing gear hydraulic control: %s\n",state_machine[current_status].hydraulic_pump);
}
else if(pl == Up && ls == Close) // pl = pilot lever and ls = limit switch
{
current_status = GearUp; // Calling the state defined in enum
printf("Current status %s\n", state_machine[current_status].current_state_indication);
printf("Light is: %s \n",state_machine[current_status].light_status);
printf("Landing gear hydraulic control: %s\n",state_machine[current_status].hydraulic_pump);
}
}
gearup.c
#include
#include "Aircraft.h"
void Gear_Up()
{
current_status = GearUp; // Checking condition for gear up
printf("Enter the status of pilot lever: ");
fflush(stdout);
scanf("%d",&pl); // pl = pilot lever
if(pl == Falling)
{
current_status=CheckingBeforeLanding; // Calling the state defined in enum
printf("Current status %s \n", state_machine[current_status].current_state_indication);
printf("Light is: %s \n",state_machine[current_status].light_status);
printf("Landing gear hydraulic control: %s\n",state_machine[current_status].hydraulic_pump);
}
}
landing.c
#include
#include "Aircraft.h"
void Checking_BeforeLanding() // Checking condition for check for landing
{
current_status = CheckingBeforeLanding; // Calling the state defined in enum
printf("Enter the status of pilot lever and hydraulic pump: ");
fflush(stdout);
scanf("%d %d",&pl, &hm);
if(pl == Down && hm == Working)
{
current_status = LoweringGear; // Calling the state defined in enum
printf("Current status %s\n", state_machine[current_status].current_state_indication);
printf("Light is: %s \n",state_machine[current_status].light_status);
printf("Direction valve status: %s\n",state_machine[current_status].direction_valve);
printf("Status of gas pressurised spring system: %s\n",state_machine[current_status].gas_spring[hm]);
}
else if (pl == Down && hm == Malfunction)
{
current_status = LoweringGear; // Calling the state defined in enum
printf("Current status %s\n", state_machine[current_status].current_state_indication);
printf("Light is: %s \n",state_machine[current_status].light_status);
printf("Direction valve status: %s\n",state_machine[current_status].direction_valve);
printf("Status of gas pressurised spring system: %s\n",state_machine[current_status].gas_spring[hm]);
}
}
lowering.c
#include
#include "Aircraft.h"
void Lowering_Gear() // Checking condition for lowering gear
{
current_status = LoweringGear; // Calling the state defined in enum
printf("Enter the status of pilot lever and limit switch:");
fflush(stdout);
scanf("%d %d", &pl, &ls); // pl = pilot lever and ls = limit switch
if(pl == Raising && ls == Close )
{
current_status = CheckingBeforeTakeoff; // Calling the state defined in enum
printf("Current status %s\n", state_machine[current_status].current_state_indication);
printf("Light is: %s \n",state_machine[current_status].light_status);
}
else if(pl == Down && ls == Open) // pl = pilot lever and ls = limit switch
{
current_status = GearDown; // Calling the state defined in enum
printf("Current status %s\n", state_machine[current_status].current_state_indication);
printf("Light is: %s \n",state_machine[current_status].light_status);
printf("Landing gear hydraulic control: %s\n",state_machine[current_status].hydraulic_pump);
}
}