Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Aim: Implementing the control for the retraction and extension of Airplane's landing gear using the Finite state machine with the knowledge of various states of switch and gear.
Code as per requirements of given states machine:
#include <stdio.h>
//created enum data types for pilot lever, squat switch, direction valve, limit switch and control for landing gear for better readability of codes.
enum pilot_lever { ON, OFF };
enum squat_switch { OPEN, CLOSE };
enum direction_valve { UP, DOWN };
enum limit_switch { OPEN_LIMIT, CLOSE_LIMIT }; // Renamed to avoid conflict with squat_switch
enum ControlforLandingGear { ENABLE, DISABLE };
// Declared variables to be used in the scanf functions
int pilot_lever;
int squat_switch;
int direction_valve;
int limit_switch;
//created all the six function as per states diagram
void GearDown();
void CheckingBeforeTakeoff();
void RaisingGear();
void GearUp();
void CheckingBeforeLanding();
void LoweringGear();
//defines all the six functions as per states diagram
void GearDown()
{
printf("machine is in Gear Down state\n");
printf("enter the pilot lever: ");
scanf("%d", &pilot_lever);
printf("enter the squat switch: ");
scanf("%d", &squat_switch);
if (pilot_lever == 0 && squat_switch == 0)
{
CheckingBeforeTakeoff();
}
else
{
GearDown();
}
}
void CheckingBeforeTakeoff()
{
printf("machine is in Checking Before Takeoff state\n");
printf("enter the pilot lever: ");
scanf("%d", &pilot_lever);
printf("enter the direction valve: ");
scanf("%d", &direction_valve);
if (pilot_lever == 0)
{
RaisingGear();
}
else
{
GearDown();
}
}
void RaisingGear()
{
printf("machine is in Raising Gear state\n");
printf("enter the pilot lever: ");
scanf("%d", &pilot_lever);
printf("enter the limit switch: ");
scanf("%d", &limit_switch);
if (limit_switch == 1)
{
GearUp();
}
else
{
CheckingBeforeLanding();
}
}
void GearUp()
{
printf("machine is in Gear Up state\n");
printf("enter the pilot lever: ");
scanf("%d", &pilot_lever);
if (pilot_lever == 1)
{
CheckingBeforeLanding();
}
else
{
GearUp();
}
}
void CheckingBeforeLanding()
{
printf("machine is in Checking Before Landing state\n");
printf("enter the pilot lever: ");
scanf("%d", &pilot_lever);
if (pilot_lever == 1)
{
LoweringGear();
}
else
{
CheckingBeforeLanding();
}
}
void LoweringGear()
{
printf("machine is in Lowering Gear state\n");
printf("enter the pilot lever: ");
scanf("%d", &pilot_lever);
printf("enter the limit switch: ");
scanf("%d", &limit_switch);
if (pilot_lever == 0)
{
CheckingBeforeTakeoff();
}
else if (limit_switch == 0)
{
GearDown();
}
else
{
LoweringGear();
}
}
//driver codes
int main()
{
GearDown(); // Start the process
return 0;
}
Output:
machine is in Gear Down state
enter the pilot lever: 0
enter the squat switch: 1
machine is in Gear Down state
enter the pilot lever: 1
enter the squat switch: 0
machine is in Gear Down state
enter the pilot lever: 1
enter the squat switch: 1
machine is in Gear Down state
enter the pilot lever: 0
enter the squat switch: 0
machine is in Checking Before Takeoff state
