Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
/*source file
* project2.c
*
* Created on: 09-Jan-2023
* Author: Pratyush Sharma
*/
#include<stdio.h>
#include "states.h"
static void (*stateTable[]) (void)= {Gear_Down, Checking_Before_Take_OFF, Raising_Gear, Gear_Up, Check_Before_Landing, Lowering_Gear};
int main()
{Initiate_State_Machine();
while(1)
{
stateTable[current_state]();
}
return 0;
}
//GearDown
void Gear_Down(void)
{
current_state=GEAR_DOWN;
printf("Enter the status of pilot's lever and squat switch:");
fflush(stdout);
scanf("%d %d", &Pilot_Lever, &squat_switch);
if (Pilot_Lever==raising && squat_switch==On)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("Current state: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is :%s\n",State_Machine[current_state].light);
}
else
{
Gear_Down();
}
}
//CheckingBeforeTakeOFF
void Checking_Before_Take_OFF (void)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("Enter the status of pilot's lever and squat switch and hydraulic mechanism: ");
fflush(stdout);
scanf("%d %d", &Pilot_Lever, &squat_switch, &Hydraulic_Mechanism);
if(Pilot_Lever==falling && squat_switch==Off)
{
Gear_Down();
}
else if(Pilot_Lever==raising && squat_switch==On && Hydraulic_Mechanism== working)
{
current_state=RAISING_GEAR;
printf("Current state: %s\n", State_Machine [current_state].current_state_indication);
printf("Light is :%s\n", State_Machine [current_state].light);
printf("Status of Direction valve :%s\n",State_Machine [current_state].direction_valve_status);
}
else if(Pilot_Lever==raising && squat_switch==On && Hydraulic_Mechanism== not_working)
{
current_state=RAISING_GEAR;
printf("Current state: %s\n", State_Machine [current_state].current_state_indication);
printf("Light is :%s\n", State_Machine [current_state].light);
printf("Status of Direction valve :%s\n",State_Machine[current_state].direction_valve_status);
printf("Status of Gas Pressurised Spring System: %s\n",State_Machine [current_state].Gas_Pressurized_Spring_system_status);
}
}
//RaisingGear
void Raising_Gear(void)
{
current_state=RAISING_GEAR;
printf("Enter the status of pilot's lever and limit switch: ");
fflush(stdout);
scanf("%d %d", &Pilot_Lever, &limit_switch);
if (Pilot_Lever==falling && limit_switch==On)
{
current_state=CHECK_BEFORE_LANDING;
printf("Current state: %s\n",State_Machine [current_state].current_state_indication);
printf("Light is :%s\n", State_Machine [current_state].light);
printf("Landing Gear Hydraulic Control:%s\n",State_Machine[current_state].landing_gear_hydraulic_control);
}
else if(Pilot_Lever==raising && limit_switch==Off)
{
current_state=GEAR_UP;
printf("Current state: %s\n",State_Machine[current_state].current_state_indication);
printf("Light is :%s\n",State_Machine[current_state].light);
printf("Landing Gear Hydraulic Control:%s\n", State_Machine[current_state].landing_gear_hydraulic_control);
}
else
{
Raising_Gear();
}
}
//GearUP
void Gear_Up (void)
{
current_state=GEAR_UP;
printf("Enter the status of pilot's lever: ");
fflush(stdout);
scanf("%d", &Pilot_Lever);
if (Pilot_Lever==falling)
{
current_state=CHECK_BEFORE_LANDING;
printf("Current state: %s\n", State_Machine [current_state].current_state_indication);
printf("Light is :%s\n", State_Machine [current_state].light);
printf("Landing Gear Hydraulic Control:%s\n",State_Machine [current_state].landing_gear_hydraulic_control);
}
else
{
Gear_Up();
}
}
//Check Before Landing
void Check_Before_Landing (void)
{
current_state=CHECK_BEFORE_LANDING;
printf("Enter the status of pilot's lever and hydraulic mechanism status: ");
fflush(stdout);
scanf("%d %d", &Pilot_Lever, &Hydraulic_Mechanism);
if (Pilot_Lever==falling && Hydraulic_Mechanism==working)
{
current_state=LOWERING_GEAR;
/* printf("Current state: %s\n", State_Machine [current_state].current_state_indication);
printf("Light is :%s\n", State_Machine [current_state]. light);
printf("Status of Direction valve :%s\n",State_Machine[current_state].direction_valve_status);*/
}
else if(Pilot_Lever==falling && squat_switch==On && Hydraulic_Mechanism== not_working)
{
current_state=LOWERING_GEAR;
printf("Current state: %s\n", State_Machine [current_state].current_state_indication);
printf("Status of Direction valve :%s\n",State_Machine[current_state].direction_valve_status);
printf("Light is :%s\n", State_Machine [current_state].light);
printf("Status of Gas Pressurised Spring System: %s\n",State_Machine [current_state]. Gas_Pressurized_Spring_system_status);
}
if(Pilot_Lever==(raising || falling))
{
Check_Before_Landing();
}
}
//LoweringGear
void Lowering_Gear (void)
{
current_state=LOWERING_GEAR;
printf("Enter the status of pilot's lever and limit switch:");
fflush(stdout);
scanf("%d %d", &Pilot_Lever, &limit_switch);
if (Pilot_Lever==raising && limit_switch==On)
{
current_state=CHECKING_BEFORE_TAKEOFF;
printf("Current state: %s\n", State_Machine [current_state].current_state_indication);
printf("Light is :%s\n",State_Machine [current_state].light);
}
if(Pilot_Lever==raising && limit_switch==Off)
{
current_state=GEAR_DOWN;
printf("Current state: %s\n", State_Machine[current_state].current_state_indication);
printf("Light is :%s\n", State_Machine [current_state].light);
printf("Landing Gear Hydraulic Control: %s\n",State_Machine[current_state]. landing_gear_hydraulic_control);
}
if(limit_switch==Off)
{
Lowering_Gear();
}
}
void Initiate_State_Machine()
{
current_state=GEAR_DOWN;
printf("\nThe Finite State Machine is Initialized and current the program is in\n""GearDown State & Light is: %s\n", State_Machine [current_state].light);
}




(output)

/*header file
* states.h
*
* Created on: 09-Jan-2023
* Author: Pratyush Sharma
*/
#ifndef STATES_H_
#define STATES_H_
#include<stdio.h>
void Initiate_State_Machine();
void Gear_Down();
void Checking_Before_Take_OFF();
void Raising_Gear();
void Gear_Up();
void Check_Before_Landing();
void Lowering_Gear();
typedef enum State{
GEAR_DOWN,
CHECKING_BEFORE_TAKEOFF,RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR,
}state_Type;
typedef enum Switch_status{
Off,// Plane is on Grnd
On
}Switch_status;
typedef enum pilot_lever
{
falling,
raising
}pilot_lever;
typedef enum hydraulic_mechanism
{
not_working,
working
}hydraulic_mechanism;
static volatile Switch_status squat_switch;
static volatile Switch_status limit_switch;
static volatile pilot_lever Pilot_Lever;
static volatile hydraulic_mechanism Hydraulic_Mechanism;
static volatile state_Type current_state;
typedef struct state_table{
char* current_state_indication;
char* light;
char* direction_valve_status;
char* Gas_Pressurized_Spring_system_status;
char* landing_gear_hydraulic_control;
}state_table;
static volatile state_table 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}"},
{"Check_Before_Landing", "Red", "Down","Enabled", "{NULL, NULL}"},
{"Lowering_Gear", "Green", "Down", "Enabled", "{Disabled, Enabled}"}
};
#endif /* STATES_H_ */

