Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
/*
* C_proj_2_Landing Gear.c
*
* Created on: Apr 12, 2023
* Author: Anbudan Balaji
*/
#include <stdio.h> //preprocessor directives
typedef enum{ //enumerated data type and create FSM states
Gear_down,
Checking_before_takeoff,
Raising_gear,
Gear_up,
Check_before_landing,
Lowering_gear,
}state_type; //aliased name for states
typedef enum{ //create switch states
on,off
}switch_status;
typedef enum{ //create pilot lever states
raising,falling
}pilot_lever;
typedef enum{ //create hydraulic mechanism states
working,not_working
}hydraulic_mechanism;
typedef struct{ //create structure for state table declaration
char* state_indication; //character pointer(string)
char* light;
char* direction_valve;
char* hydraulic_control;
}state;
static state state_machine[6]={ //state table formation (array of structures)
{"Gear down","Green","Down","Enabled"}, //state 1 to 6
{"Checking before takeoff","Green","Down","Enabled"},
{"Raising gear","Red","Up","Enabled"},
{"Gear up","Off","NULL","Disabled"},
{"Check before landing","Red","Down","Enabled"},
{"Lowering gear","Red","Down","Enabled"}
};
static volatile switch_status squat; //maintain the same state value across multiple executions even program terminated
static volatile switch_status limit;
static volatile pilot_lever lever;
static volatile hydraulic_mechanism hydraulic;
state_type current_state; //assign type of value to enum
void geardown() //function defenition
{
printf("\nPilot lever and Squat switch : ");
fflush(stdout);
scanf("%d %d",&lever,&squat);
if(lever==raising && squat==on) //check condition, true means go to next state
{
current_state=Checking_before_takeoff; //moving next new state
printf("Current state : %s\n",state_machine[current_state].state_indication); //print string from state machine array
printf("Light is : %s\n",state_machine[current_state].light);
}
}
void checkingbeforetakeoff() //function defenition
{
printf("\nPilot lever and hydraulic mechanism : ");
fflush(stdout);
scanf("%d %d",&lever,&squat,&hydraulic);
if(lever==falling && squat==off) //check condition, true means go to previous state
{
printf("Return Gear Down\n");
printf("Light is : Red\n");
geardown(); //return previous state
}
else if(lever==raising && squat==on && hydraulic==working) //check condition, true means go to next state
{
current_state=Raising_gear; //moving next new state
printf("Current State : %s\n",state_machine[current_state].state_indication); //print string from state machine array
printf("Light is : %s\n",state_machine[current_state].light);
printf("Direction valve : %s\n",state_machine[current_state].direction_valve);
}
}
void raisinggear() //function defenition
{
printf("\nPilot lever and limit switch : ");
fflush(stdout);
scanf("%d %d",&lever,&limit);
if(lever==falling && limit==on) //check condition, true means go to next to next state
{
current_state=Check_before_landing; //moving next to next new state
printf("Current State : %s\n",state_machine[current_state].state_indication); //print string from state machine array
printf("Light is : %s\n",state_machine[current_state].light);
}
else if (lever==raising && limit==off) //check condition, true means go to next state
{
current_state=Gear_up; //moving next new state
printf("Current State : %s\n",state_machine[current_state].state_indication); //print string from state machine array
printf("Light is : %s\n",state_machine[current_state].light);
printf("Hydraulic Control : %s\n",state_machine[current_state].hydraulic_control);
}
}
void gearup() //function defenition
{
printf("\nPilot lever and limit switch : ");
fflush(stdout);
scanf("%d %d",&lever,&limit);
if(lever==falling && limit==on) //check condition, true means go to next state
{
current_state=Check_before_landing; //moving next new state
printf("Current State : %s\n",state_machine[current_state].state_indication); //print string from state machine array
printf("Light is : %s\n",state_machine[current_state].light);
printf("Hydraulic Control : %s\n",state_machine[current_state].hydraulic_control);
}
}
void checkbeforelanding() //function defenition
{
printf("\nPilot lever and hydraulic mechanism : ");
fflush(stdout);
scanf("%d %d",&lever,&hydraulic);
if(lever==falling && hydraulic==working) //check condition, true means go to next state
{
current_state=Lowering_gear; //moving next new state
printf("Current State : %s\n",state_machine[current_state].state_indication); //print string from state machine array
printf("Light is : %s\n",state_machine[current_state].light);
printf("Direction valve : %s\n",state_machine[current_state].direction_valve);
}
}
void loweringgear() //function defenition
{
printf("\nPilot lever and limit switch : ");
fflush(stdout);
scanf("%d %d",&lever,&limit);
if(lever==raising && limit==on) //check condition, true means go to next to next state
{
current_state=Checking_before_takeoff; //moving next to next new state
printf("Current State : %s\n",state_machine[current_state].state_indication); //print string from state machine array
printf("Light is : Red\n");
}
else if(lever==falling && limit==off) //check condition, true means go to next state
{
current_state=Gear_down; //moving next new state
printf("Current State : %s\n",state_machine[current_state].state_indication); //print string from state machine array
printf("Light is : %s\n",state_machine[current_state].light);
printf("Hydraulic Control : %s\n",state_machine[current_state].hydraulic_control);
}
}
static void(*states[])()={ //accessing functions by an array of functions (accessing address of functions)
geardown, checkingbeforetakeoff, raisinggear,
gearup, checkbeforelanding, loweringgear};
int main() //main functions
{
current_state==Gear_down; //FSM initialization
printf("**** Initial condition ****\n");
printf(" FSM Intialized and current state is : %s\n",state_machine[current_state].state_indication); //print string from state machine array
printf(" Light is : %s\n",state_machine[current_state].light);
while(1)
{
states[current_state](); //array of function
}
}
