Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Main.c
/* * main.c * * Created on: 27-Aug-2022 * Author: Viraj */
#include#include "states.h" /*Including header file*/
static void(*StateTable[])(void)={GearDown,CheckingBeforeTakeoff,RaisingGear,GearUp,CheckingBeforeLanding,LoweringGear};
int main(){ Initiate_State_Machine(); //Calling the state initialization function
while(1) { StateTable[Current_State](); }
}
//Gear Downvoid GearDown(){ Current_State=GEAR_DOWN;
printf("\nCurrent state:%s",State_Machine[Current_State].Current_State_Indication); printf("\nLight is:%s\n",State_Machine[Current_State].Light);
printf("\nEnter 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("\nCurrent state:%s",State_Machine[Current_State].Current_State_Indication); printf("\nLight is:%s\n",State_Machine[Current_State].Light); } else { GearDown(); }}
void CheckingBeforeTakeoff(void){ Current_State=CHECKING_BEFORE_TAKEOFF;
printf("\nEnter the status of pilot's lever,squat switch and hydraulic mechanism:"); fflush(stdout); scanf("%d%d%d",&pilot_lever,&squat_switch,&hydraulic_mechanism);
if((pilot_lever==falling)||(squat_switch==off)) { GearDown(); }
else if((pilot_lever==raising)&&(squat_switch==on)&&(hydraulic_mechanism==working)) { Current_State=RAISING_GEAR; printf("\nCurrent state:%s",State_Machine[Current_State].Current_State_Indication); printf("\nLight is:%s",State_Machine[Current_State].Light); printf("\nStatus of direction valve:%s",State_Machine[Current_State].Status_Direction_Valve); }
else if((pilot_lever==raising)&&(squat_switch==on)&&(hydraulic_mechanism==not_working)) { Current_State=RAISING_GEAR; printf("\nCurrent state:%s",State_Machine[Current_State].Current_State_Indication); printf("\nLight is:%s",State_Machine[Current_State].Light); printf("\nStatus of direction valve:%s",State_Machine[Current_State].Status_Direction_Valve); printf("\nStatus of gas pressurized spring system:%s",State_Machine[Current_State].Status_Gas_Pressure_Spring); }}
void RaisingGear(void){ Current_State=RAISING_GEAR; printf("\nCurrent state:%s",State_Machine[Current_State].Current_State_Indication); printf("\nLight is:%s",State_Machine[Current_State].Light); printf("\nStatus of direction valve:%s",State_Machine[Current_State].Status_Direction_Valve);
printf("\nEnter 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=CHECKING_BEFORE_LANDING; printf("\nCurrent state:%s",State_Machine[Current_State].Current_State_Indication); printf("\nLight is:%s",State_Machine[Current_State].Light); printf("\nStatus of landing gear hydraulic control:%s",State_Machine[Current_State].Landing_Gear_Hydraulic_Control); }
else if((pilot_lever==raising)&&(limit_switch==off)) { Current_State=GEAR_UP; printf("\nCurrent state:%s",State_Machine[Current_State].Current_State_Indication); printf("\nLight is:%s",State_Machine[Current_State].Light); printf("\nStatus of direction valve:%s",State_Machine[Current_State].Status_Direction_Valve); }
else { RaisingGear(); }}
void GearUp(void){ Current_State=CHECKING_BEFORE_LANDING; printf("\nCurrent state:%s",State_Machine[Current_State].Current_State_Indication); printf("\nLight is:%s\n",State_Machine[Current_State].Light);
printf("\nEnter the status of pilot's lever:"); fflush(stdout); scanf("%d",&pilot_lever);
if(pilot_lever==falling) { Current_State=CHECKING_BEFORE_LANDING; printf("\nCurrent state:%s",State_Machine[Current_State].Current_State_Indication); printf("\nLight is:%s",State_Machine[Current_State].Light); printf("\nStatus of landing gear hydraulic control:%s",State_Machine[Current_State].Landing_Gear_Hydraulic_Control); } else { GearUp(); }}
void CheckingBeforeLanding(void){ Current_State=CHECKING_BEFORE_LANDING; printf("\nCurrent state:%s",State_Machine[Current_State].Current_State_Indication); printf("\nLight is:%s\n",State_Machine[Current_State].Light);
printf("\nEnter 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("\nCurrent state:%s",State_Machine[Current_State].Current_State_Indication); printf("\nLight is:%s",State_Machine[Current_State].Light); printf("\nStatus of direction valve:%s",State_Machine[Current_State].Status_Direction_Valve); }
else if((pilot_lever==raising)&&(squat_switch==on)&&(hydraulic_mechanism==not_working)) { Current_State=LOWERING_GEAR; printf("\nCurrent state:%s",State_Machine[Current_State].Current_State_Indication); printf("\nLight is:%s",State_Machine[Current_State].Light); printf("\nStatus of direction valve:%s",State_Machine[Current_State].Status_Direction_Valve); printf("\nStatus of gas pressurized spring system:%s",State_Machine[Current_State].Status_Gas_Pressure_Spring); }
if(pilot_lever==(raising||falling)) { CheckingBeforeLanding(); }}
void LoweringGear(void){ Current_State=LOWERING_GEAR; printf("\nCurrent state:%s",State_Machine[Current_State].Current_State_Indication); printf("\nLight is:%s\n",State_Machine[Current_State].Light);
printf("\nEnter 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("\nCurrent state:%s",State_Machine[Current_State].Current_State_Indication); printf("\nLight is:%s",State_Machine[Current_State].Light); }
if((pilot_lever==raising)&&(limit_switch==on)) { Current_State=GEAR_DOWN; printf("\nCurrent state:%s",State_Machine[Current_State].Current_State_Indication); printf("\nLight is:%s",State_Machine[Current_State].Light); printf("\nStatus of landing gear hydraulic control:%s",State_Machine[Current_State].Landing_Gear_Hydraulic_Control); }
if(limit_switch==off) { LoweringGear(); }}
void Initiate_State_Machine(){ Current_State=GEAR_DOWN; printf("Finite state machine is initiated and current program is in Gear down state and light is %s\n",State_Machine[Current_State].Light); printf("\nThe choices are as follow\n-----------------------------------------\n"); printf("Pilot lever RAISING=1\tPilot lever FALLING=0\nLimit switch OPEN=1\tLimit switch CLOSED=0\nSquat switch OPEN=1\tSquat switch CLOSED=0\nHydraulic mechanism WORKING=1\tHydraulic mechanism NOT WORKING=0\n\n");}
states.h
/* * states.h * * Created on: 27-Aug-2022 * Author: Viraj */
#ifndef STATES_H_#define STATES_H_
#include<stdio.h>
void Initiate_State_Machine();void GearDown();void CheckingBeforeTakeoff();void RaisingGear();void GearUp();void CheckingBeforeLanding();void LoweringGear();
typedef enum State{ GEAR_DOWN, CHECKING_BEFORE_TAKEOFF, RAISING_GEAR, GEAR_UP, CHECKING_BEFORE_LANDING, LOWERING_GEAR,}State_Type;
typedef enum Switch{ off, 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;
State_Type Current_State;
typedef struct{ char* Current_State_Indication; char* Light; char* Status_Direction_Valve; char* Status_Gas_Pressure_Spring; char* Landing_Gear_Hydraulic_Control;
}State_Table;
static State_Table;
State_Table State_Machine[6]={ {"GearDown","Green","Down","Enabled",{NULL,NULL}}, {"CheckingBeforeTakeoff","Green","Down","Enabled",{NULL,NULL}}, {"RaisingGear","Red","Up","Enabled",{"Disabled","Enabled"}}, {"GearUp","off","NULL","Disabled",{NULL,NULL}}, {"CheckingBeforeLanding","Red","Down","Enabled",{"NULL","NULL"}}, {"LoweringGear","Green","Down","Enabled",{"Disabled","Enabled"}},};
#endif /* STATES_H_ */
Result:

