Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
/*
* main.c
*
* Created on: Aug 10, 2022
* Author: 91823
*/
#include
#include"states.h"
static void (*state_table[])(void) = {Gear_Down,Checking_Before_Take_off, Raising_Gear, Gear_Up, Checking_Before_Landing, Lowering_Gear};
int main()
{
Initiate_State_Machine();
while(1)
{
state_table[current_state]();
}
}
/*
* states.h
*
* Created on: Aug 10, 2022
* Author: 91823
*/
#ifndef STATES_H_
#define STATES_H_
#include
/*....functions.....*/
void Initate_State_Machine();
void Gear_Down();
void Checking_Before_Take_off();
void Raising_Gear();
void Gear_Up();
void Checking_Before_Landing();
void Lowering_Gear();
typedef enum State
{
GEAR_DOWN,
CHECK_BEFORE_TAKE_OFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR
}State_Type;
typedef enum Switch
{
Open,
Close //plane is in ground
}Switch_Status;
typedef enum Pilot_Lever
{
Rising,
Falling,
UP,
DOWN
}Pilot_Lever_Status;
typedef enum Hydraulic_Mechanism
{
Working,
Malfunctioning
}Hydraulic_Mechanism_Status;
/*.....input for changing state...*/
static volatile Switch_Status Squat_Switch;
static volatile Switch_Status limit_switch;
static volatile Pilot_Lever_Status pilot_lever;
static volatile Hydraulic_Mechanism_Status hydraulic_mechanism;
State_Type current_state;
typedef struct
{
char* current_state_indication;
char* light_status;
char* direction_valve_status;
char* hydraulic_pump_control_status;
char* gas_pressurized_spring_status[2];
}State_Table;
static State_Table State_Machine[6]=
{
{"GearDown","Green","Down","Enabled",0,0},
{"Checking Before Take Off","Green","Down","Enabled",0,0},
{"Raising Gear","Red","Up","Enabled",{"Disabled","Enabled"}},
{"Gear Up","Off","Null","Disabled",0,0},
{"Checking Before Landing","Red","Down","Enabled",0,0},
{"Lowering Gear","Green","Down","Enabled",{"Disabled","Enabled"}}
};
#endif /* STATES_H_ */
#include"States.h"
#include
void Gear_Down(void)
{
current_state = GEAR_DOWN;
printf("\n Enter the pilot's Lever Position, Squat Switch Status ");
fflush(stdout);
scanf("%d%d",&pilot_lever, &Squat_Switch);
if(pilot_lever == Rising && Squat_Switch == Open)
{
current_state=CHECK_BEFORE_TAKE_OFF;
printf("\n Current State:%s\n",State_Machine[current_state].current_state_indication);
fflush(stdout);
printf("\nLight State: %s",State_Machine[current_state].light_status);
fflush(stdout);
}
else
{
Gear_Down();
}
}
/*
* checkingbeforetakeoff.c
*
* Created on: Aug 10, 2022
* Author: 91823
*/
#include"States.h"
#include
void Checking_Before_Take_off()
{
current_state = CHECK_BEFORE_TAKE_OFF;
printf("\n Enter the pilot's lever Position, Squat Switch Status and Hydraulic Mechanism 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=RAISING_GEAR;
printf("\n Current State:%s\n",State_Machine[current_state].current_state_indication);
fflush(stdout);
printf("\nLight Status: %s",State_Machine[current_state].light_status);
fflush(stdout);
printf("Direction valve Status:%s\n\n",State_Machine[current_state].direction_valve_status);
fflush(stdout);
}
else if (pilot_lever== UP && Squat_Switch == Open && hydraulic_mechanism ==Malfunctioning)
{
printf("Light Status:%s\n", State_Machine[current_state].light_status);
fflush(stdout);
printf("Direction Valve Status:%s\n",State_Machine[current_state].direction_valve_status);
fflush(stdout);
printf("Gas Pressurized Spring System Status:%s\n",State_Machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]);
fflush(stdout);
}
}
/*
* raisinggear.c
*
* Created on: Aug 10, 2022
* Author: 91823
*/
#include"States.h"
#include
void Raising_Gear()
{
current_state = RAISING_GEAR;
printf("\n Enter the Limit Switch Status,Pilots Lever Status:");
fflush(stdout);
scanf("%d%d",&limit_switch,&pilot_lever);
if(pilot_lever == Falling && limit_switch == Open)
{
current_state=CHECK_BEFORE_LANDING;
printf("\n Current State:%s\n",State_Machine[current_state].current_state_indication);
fflush(stdout);
printf("\nLight State: %s",State_Machine[current_state].light_status);
fflush(stdout);
printf("Hydraulic Control for Landing Gear State:%s\n\n",State_Machine[current_state].hydraulic_pump_control_status);
fflush(stdout);
}
else if(limit_switch == Close && pilot_lever == UP)
{
current_state=GEAR_UP;
printf("\n Current State:%s\n",State_Machine[current_state].current_state_indication);
fflush(stdout);
printf("\n Light State: %s",State_Machine[current_state].light_status);
fflush(stdout);
printf("\n Hydraulic Control for landing Gear Status:%s\n",State_Machine[current_state].hydraulic_pump_control_status);
fflush(stdout);
}
else
{
Raising_Gear();
}
}
/*
* gearup.c
*
* Created on: Aug 10, 2022
* Author: 91823
*/
#include"States.h"
#include
void Gear_Up()
{
current_state = GEAR_UP;
printf("\n Enter the Pilot's lever Position ");
fflush(stdout);
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_indication);
fflush(stdout);
printf("\nLight Status: %s",State_Machine[current_state].light_status);
fflush(stdout);
printf("Hydraulic Control for Landing Gear Status:%s\n",State_Machine[current_state].hydraulic_pump_control_status);
fflush(stdout);
}
else
{
Gear_Up();
}
}
#include"States.h"
#include
void Checking_Before_Landing()
{
current_state = CHECK_BEFORE_LANDING;
printf("\n Enter the Pilot's Lever Position and Hydraulic Mechanism Status:");
fflush(stdout);
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_indication);
fflush(stdout);
printf("\nLight State: %s",State_Machine[current_state].light_status);
fflush(stdout);
printf("Direction Valve Status:%s\n\n",State_Machine[current_state].direction_valve_status);
fflush(stdout);
}
else if(pilot_lever== DOWN && hydraulic_mechanism == Malfunctioning)
{
current_state = LOWERING_GEAR;
printf("\n Current State:%s\n",State_Machine[current_state].current_state_indication);
fflush(stdout);
printf("\nLight Status: %s",State_Machine[current_state].light_status);
fflush(stdout);
printf("Direction Valve Status:%s\n",State_Machine[current_state].direction_valve_status);
fflush(stdout);
printf("Gas Pressurized Spring System Status:%s\n",State_Machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]);
fflush(stdout);
}
if(pilot_lever == (UP||Rising||Falling))
{
Checking_Before_Landing();
}
}
/*
* lowering.c
*
* Created on: Aug 10, 2022
* Author: 91823
*/
#include"States.h"
#include
void Lowering_Gear()
{
current_state = LOWERING_GEAR;
printf("\n Enter the limit Switch Status,pilot's lever Position");
fflush(stdout);
scanf("%d%d",&pilot_lever, &limit_switch);
if(limit_switch == Close && pilot_lever == Rising )
{
current_state=CHECK_BEFORE_TAKE_OFF;
printf("\n Current State:%s\n",State_Machine[current_state].current_state_indication);
fflush(stdout);
printf("\nLight Status: %s",State_Machine[current_state].light_status);
fflush(stdout);
}
if(limit_switch == Open && pilot_lever ==DOWN)
{
current_state = GEAR_DOWN;
printf("\n Current State:%s\n",State_Machine[current_state].current_state_indication);
fflush(stdout);
printf("\nLight Status: %s",State_Machine[current_state].light_status);
fflush(stdout);
printf("Hydraulic Control for Landing Gear Status:%s\n",State_Machine[current_state].hydraulic_pump_control_status);
fflush(stdout);
}
else
{
Lowering_Gear();
}
}
/*
* initiatestatemachine.c
*
* Created on: Aug 10, 2022
* Author: 91823
*/
#include"States.h"
void Initiate_State_Machine()
{
current_state = GEAR_DOWN;
printf("\n State Machine is initialized & Program is in Gear_Down_state & Light_Status: %s\n",State_Machine[current_state].light_status);
fflush(stdout);
}