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(using Finite State Machine (FSM)).
main.c
#include<stdio.h>//in preprocessor section include stdio.h to use printf function//
#include "sub.h"//header//
static void (*state_table[])(void) = {Gear_Down,Checking_Before_Takeoff,Raising_Gear,Gear_Up,Checking_Before_Landing,Lowering_Gear};
int main()//the main function inside it we have to write our logic//
{
void Initiate_State_Machine();
while(1)
{
state_table[current_state]();
}
}
sub.c
#include "sub.h" // header
#include<stdio.h>//in preprocessor section include stdio.h to use printf function//
void Initiate_State_Machine()
{
printf("Current State is : %s\n",State_Machine[current_state].current_state);
printf("\nLight Status is : %s\n",State_Machine[current_state].light_Status);
}
void Gear_Down(void)
{
current_state = GEAR_DOWN;
printf("\nEnter the position of Pilot's Lever and Squat Switch Status:");
fflush(stdout);
scanf("%d %d",&pilot_lever,&squat_switch);
if (pilot_lever == Rising && squat_switch == Open)
{
current_state = CHECK_BEFORE_TAKEOFF;
printf("\nCurrent State:%s\n",State_Machine[current_state].current_state);
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
}
else
{
printf("\nreturning to gear down state");
Gear_Down();
}
}
void Checking_Before_Takeoff()
{
current_state = CHECK_BEFORE_TAKEOFF;
printf("\nEnter the position of Pilot's Lever,Squat Switch Status, Hydraulic Mechanism Status :");
fflush(stdout);
scanf("%d %d %d",&pilot_lever,&squat_switch,&hydraulic_mechanism);
printf("\nDelay 4 secs\n");
if (pilot_lever == UP && squat_switch == Open )
{
fflush(stdout);
if(hydraulic_mechanism == Working)
{
current_state = RAISING_GEAR;
printf("\nCurrent State: %s\n",State_Machine[current_state].current_state);
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
printf("Direction Value Status:%s\n\n",State_Machine[current_state].direction_value_status);
}
else if ( hydraulic_mechanism == Not_Working)
{
printf("\nCurrent State: %s\n",State_Machine[current_state].current_state);
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
printf("Direction Value Status:%s\n",State_Machine[current_state].direction_value_status);
printf("Gas Pressurized Spring System Status:%s\n\n",State_Machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]);
}
}
else if(pilot_lever == Falling || squat_switch == Close)
{
printf("\nreturning to gear down");
Gear_Down();
}
}
void Raising_Gear()
{
current_state = RAISING_GEAR;
printf("\nCurrent State: %s\n",State_Machine[current_state].current_state);
printf("\nEnter the Limit Switch Status, Pilot's Lever Position :");
fflush(stdout);
scanf("%d %d",&limit_switch,&pilot_lever);
if(pilot_lever == Falling && limit_switch == Open)
{
current_state = CHECK_BEFORE_LANDING;
printf("\nCurrent State: %s\n",State_Machine[current_state].current_state);
printf ("Light Status:%s\n",State_Machine[current_state].light_Status);
printf("Hydraulic Control for Landing Gear Status:%s\n\n",State_Machine[current_state].hydraulic_pump_control_status);
}
else if (limit_switch == Close && pilot_lever == UP)
{
current_state = GEAR_UP;
printf("\nCurrent State: %s\n",State_Machine[current_state].current_state);
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
printf("Hydraulic Control for Landing Gear Status:%s\n\n",State_Machine[current_state].hydraulic_pump_control_status);
}
else
{
printf("\n returning to raising gear ");
Raising_Gear();
}
}
void Gear_Up()
{
current_state = GEAR_UP;
printf("\nCurrent State: %s\n",State_Machine[current_state].current_state);
printf("\nEnter the Pilot's Lever Position:");
fflush(stdout);
scanf("%d",&pilot_lever);
if(pilot_lever ==Falling)
{
current_state = CHECK_BEFORE_LANDING;
printf("\nCurrent State:%s\n",State_Machine[current_state].current_state);
printf("Light Status:%s\n",State_Machine[current_state].light_Status);
printf("Hydraulic Control for Landing Gear Status:%s\n\n",State_Machine[current_state].hydraulic_pump_control_status);
}
else
{
printf("\nreturning to gear up");
Gear_Up();
}
}
void Checking_Before_Landing()
{
current_state = CHECK_BEFORE_LANDING;
printf("\nCurrent State : %s\n",State_Machine[current_state].current_state);
printf("\nEnter the Pilot's Lever Position and Status of Hydraulic Mechanism : ");
fflush(stdout);
scanf("%d %d",&pilot_lever,&hydraulic_mechanism);
if(pilot_lever == DOWN && hydraulic_mechanism == Working)
{
current_state = LOWERING_GEAR;
printf("\nCurrent State : %s\n",State_Machine[current_state].current_state);
printf("Light Status : %s\n",State_Machine[current_state].light_Status);
printf("Direction Valve Status : %s\n\n",State_Machine[current_state].direction_value_status);
}
else if(pilot_lever == DOWN && hydraulic_mechanism == Not_Working)
{
current_state = LOWERING_GEAR;
printf("\nCurrent State : %s\n",State_Machine[current_state].current_state);
printf("Light Status : %s\n",State_Machine[current_state].light_Status);
printf("Direction Valve Status : %s\n",State_Machine[current_state].direction_value_status);
printf("Gas Pressurized Spring System Status : %s\n\n",State_Machine[current_state].gas_pressurized_spring_status[hydraulic_mechanism]);
}
else if(pilot_lever == (UP || Rising || Falling))
{
printf("\nreturning to Checking_Before_Landing ");
Checking_Before_Landing();
}
}
void Lowering_Gear()
{
current_state = LOWERING_GEAR;
printf("\nCurrent State: %s\n",State_Machine[current_state].current_state);
printf("\n Enter the Limit_Switch Status,Pilot_Lever Status : ");
fflush(stdout);
scanf("%d %d",&limit_switch,&pilot_lever);
if( limit_switch == Close && pilot_lever == Rising)
{
current_state = CHECK_BEFORE_TAKEOFF;
printf("\nCurrent State: %s\n",State_Machine[current_state].current_state);
printf("Light Status : %s\n\n",State_Machine[current_state].light_Status);
}
else if(limit_switch == Open && pilot_lever == DOWN)
{
current_state = GEAR_DOWN;
printf ("\nCurrent State: %s\n",State_Machine[current_state].current_state);
printf ("Light Status : %s\n",State_Machine[current_state].light_Status);
printf ("Hydraulic Control for Landing Gear Status : %s\n\n",State_Machine[current_state].hydraulic_pump_control_status);
}
else
{
printf("\nreturning to lower gear");
Lowering_Gear();
}
}
sub.h
#ifndef SUB_H_
#define SUB_H_
#include<stdio.h>//in preprocessor section include stdio.h to use printf function//
void Initiate_State_machine();
void Gear_Down();
void Checking_Before_Takeoff();
void Raising_Gear();
void Gear_Up();
void Checking_Before_Landing();
void Lowering_Gear();
//system state declaration//
typedef enum State
{
GEAR_DOWN,
CHECK_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR
}State_Type;
//hydraulic mechanism status declared//
typedef enum Hydraulic_Mechanism
{
Working=0,
Not_Working=1
}Hydraulic_Mechanism_Status;
//switch status declared//
typedef enum Switch
{
Open=0,
Close=1
}Switch_Status;
//pilot lever status declared//
typedef enum Pilot_Lever
{
Rising=0,
Falling=1,
UP=2,
DOWN=3
}Pilot_Lever_Status;
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;
//state table attributes declaration//
typedef struct
{
char* current_state;
char* light_Status;
char* direction_value_status;
char* hydraulic_pump_control_status;
char* gas_pressurized_spring_status[2];
}State_Table;
//state table formation//
static State_Table State_Machine[6] = {
{"GearDown","Green","Down","Enabled",{NULL,NULL}},
{"Checking Before Take Off ","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 /* SUB_H_ */
Makefile
all:main.c sub.c sub.h//all depedencies added//
gcc main.c sub.c sub.h -o main -I.//gcc command//
clean://to clean all the built files//
gm main.exe//the command to clean//
output:

