Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Embedded live batch 12
Title- In this project, controlling the retraction and extension of Airplane’s landing gear can be implemented using Finite State Machine (FSM). FSM is the most efficient algorithm which is mathematical model of computation.
Objectives:-
- Controlling the direction of the Valve.
- Controlling the retraction and extension of the Airplane’s landing gear.

#include <stdio.h>
#include<stdlib.h>
void GearDown(); //declaring functions for each states
void CheckingBeforeTakeOff();
void RisingGear();
void GearUp();
void CheckingBeforeLanding();
void LoweringGear();
static void(*statetable[])(void)= //creating an array of the states
{ GearDown,
CheckingBeforeTakeOff,
RisingGear,
GearUp,
CheckingBeforeLanding,
LoweringGear };
typedef enum State //gear states declaration
{
gear_down,
checking_before_takeoff,
rising_gear,
gear_up,
check_before_landing,
lowering_gear,
}State_type;
typedef enum Switch //switch status declaration
{
open, closed
}Switch_status;
typedef enum pilot_lever //pilot lever states declaration
{
down , falling , rising, up
}pilot_lever;
typedef enum hydraulic_mechanism //hydraulic control states declaration
{
enabled, disabled
}hydraulic_mechanism;
static volatile Switch_status squat_switch;
static volatile Switch_status limit_switch;
static volatile pilot_lever p_lever;
static volatile hydraulic_mechanism h_mech;
State_type current_state;
typedef struct //state table elements
{
char*current_state_indication;
char*light;
char*direction_valve_status;
int time;
char*hydraulic_control;
char*pilot_lever_status;
}State_table;
static State_table state_machine [6]={ //creating state table
{"gear down","green","down", 3, "enabled", "down"},
{"checking before take-off","green","down", 3, "enabled", "rising"},
{"rising gear","red","up", 3, "enabled", "up"},
{"gear up", "off", "up",3 ,"disabled","up"},
{"check before landing", "red", "down", 3,"enabled", "falling"},
{"lowering gear", "green","down", 3, "enabled", "down"}
};
int main()
{
while(1)
{
statetable[current_state](); //calling each state functions
}
}
void GearDown (void) //airplane is grounded or parked in the bay
{
current_state= gear_down;
printf ("\n current state: %s\n" , state_machine[current_state].current_state_indication);
printf ("\n pilot lever status: %s\n" , state_machine[current_state].pilot_lever_status);
printf ("\n light status: %s\n" , state_machine[current_state].light);
printf ("\n direction valve status: %s\n" , state_machine[current_state].direction_valve_status);
printf ("\n hydraulic control status: %s\n" , state_machine[current_state].hydraulic_control);
printf ("\n time delay: %d\n" , state_machine[current_state].time);
printf("\n Enter the status of pilot lever and squat switch:\n");
fflush(stdout);
scanf("%d %d", &p_lever, &squat_switch);
if (p_lever== rising&& squat_switch==open)
{
current_state= checking_before_takeoff;
printf ("\n current state: %s\n" , state_machine[current_state].current_state_indication);
printf ("\n pilot lever status: %s\n" , state_machine[current_state].pilot_lever_status);
printf ("\n light status: %s\n" , state_machine[current_state].light);
printf ("\n direction valve status: %s\n" , state_machine[current_state].direction_valve_status);
printf ("\n hydraulic control status: %s\n" , state_machine[current_state].hydraulic_control);
printf ("\n time delay: %d\n" , state_machine[current_state].time);
}
else
{
GearDown();
}
}
void CheckingBeforeTakeOff(void) // airplane is about to takeoff
{
current_state= checking_before_takeoff;
printf("\n Enter the status of pilot lever and squat switch:");
fflush(stdout);
scanf("%d %d", &p_lever, &squat_switch);
if (p_lever== falling&& squat_switch==closed)
{
GearDown();
}
else if(p_lever== up&& squat_switch==open)
{
current_state= rising_gear;
printf ("\n current state: %s\n" , state_machine[current_state].current_state_indication);
printf ("\n pilot lever status: %s\n" , state_machine[current_state].pilot_lever_status);
printf ("\n light status: %s\n" , state_machine[current_state].light);
printf ("\n direction valve status: %s\n" , state_machine[current_state].direction_valve_status);
printf ("\n hydraulic control status: %s\n" , state_machine[current_state].hydraulic_control);
printf ("\n time delay: %d\n" , state_machine[current_state].time);
}
}
void RisingGear(void) //airplane is taking off
{
current_state= rising_gear;
printf("\n Enter the status of pilot lever and squat switch:");
fflush(stdout);
scanf("%d %d", &p_lever, &limit_switch);
if (p_lever== falling&& limit_switch==closed)
{
current_state= check_before_landing;
printf ("\n current state: %s\n" , state_machine[current_state].current_state_indication);
printf ("\n pilot lever status: %s\n" , state_machine[current_state].pilot_lever_status);
printf ("\n light status: %s\n" , state_machine[current_state].light);
printf ("\n direction valve status: %s\n" , state_machine[current_state].direction_valve_status);
printf ("\n hydraulic control status: %s\n" , state_machine[current_state].hydraulic_control);
printf ("\n time delay: %d\n" , state_machine[current_state].time);
}
else if(p_lever== up&& limit_switch==closed)
{
current_state= gear_up;
printf ("\n current state: %s\n" , state_machine[current_state].current_state_indication);
printf ("\n pilot lever status: %s\n" , state_machine[current_state].pilot_lever_status);
printf ("\n light status: %s\n" , state_machine[current_state].light);
printf ("\n direction valve status: %s\n" , state_machine[current_state].direction_valve_status);
printf ("\n hydraulic control status: %s\n" , state_machine[current_state].hydraulic_control);
printf ("\n time delay: %d\n" , state_machine[current_state].time);
}
}
void GearUp(void) //airplane is in airborne
{
current_state= gear_up;
printf("\n Enter the status of pilot lever and squat switch:");
fflush(stdout);
scanf("%d %d", &p_lever, &h_mech);
if (p_lever == falling&& h_mech ==enabled)
{
current_state= check_before_landing;
printf ("\n current state: %s\n" , state_machine[current_state].current_state_indication);
printf ("\n pilot lever status: %s\n" , state_machine[current_state].pilot_lever_status);
printf ("\n light status: %s\n" , state_machine[current_state].light);
printf ("\n direction valve status: %s\n" , state_machine[current_state].direction_valve_status);
printf ("\n hydraulic control status: %s\n" , state_machine[current_state].hydraulic_control);
printf ("\n time delay: %d\n" , state_machine[current_state].time);
}
else if(p_lever== up&&h_mech==disabled)
{
GearUp();
}
}
void CheckingBeforeLanding(void) //airplane is about to land
{
current_state=check_before_landing;
printf("\n Enter the status of pilot lever and squat switch:");
fflush(stdout);
scanf("%d %d", &p_lever, &h_mech);
if (p_lever== down)
{
current_state= lowering_gear;
printf ("\n current state: %s\n" , state_machine[current_state].current_state_indication);
printf ("\n pilot lever status: %s\n" , state_machine[current_state].pilot_lever_status);
printf ("\n light status: %s\n" , state_machine[current_state].light);
printf ("\n direction valve status: %s\n" , state_machine[current_state].direction_valve_status);
printf ("\n hydraulic control status: %s\n" , state_machine[current_state].hydraulic_control);
printf ("\n time delay: %d\n" , state_machine[current_state].time);
}
else if(p_lever==falling)
{
CheckingBeforeLanding();
}
}
void LoweringGear(void) //airplane is landing
{
current_state= lowering_gear;
printf("\n Enter the status of pilot lever and squat switch:");
fflush(stdout);
scanf("%d %d", &p_lever, &limit_switch);
if (p_lever== rising)
{
current_state= checking_before_takeoff;
printf ("\n current state: %s\n" , state_machine[current_state].current_state_indication);
printf ("\n pilot lever status: %s\n" , state_machine[current_state].pilot_lever_status);
printf ("\n light status: %s\n" , state_machine[current_state].light);
printf ("\n direction valve status: %s\n" , state_machine[current_state].direction_valve_status);
printf ("\n hydraulic control status: %s\n" , state_machine[current_state].hydraulic_control);
printf ("\n time delay: %d\n" , state_machine[current_state].time);
}
else if(p_lever== down && limit_switch==open)
{
current_state= gear_down;
printf ("\n current state: %s\n" , state_machine[current_state].current_state_indication);
printf ("\n pilot lever status: %s\n" , state_machine[current_state].pilot_lever_status);
printf ("\n light status: %s\n" , state_machine[current_state].light);
printf ("\n direction valve status: %s\n" , state_machine[current_state].direction_valve_status);
printf ("\n hydraulic control status: %s\n" , state_machine[current_state].hydraulic_control);
printf ("\n time delay: %d\n" , state_machine[current_state].time);
}
}
Output -


