If we want to go gear down position, we need to give '3','0'.
this continues, until we stop the program.
we call this intial state machine into main program....
#include
#include
//Functions declaration that used in project
void Initial_state_machine();
void Gear_down();
void Check_before_takeoff();
void Raising_gear();
void Gear_up();
void Check_before_landing();
void Lowering_gear();
// defining enum table with different structures
typedef enum states
{
GEAR_DOWN,
CHECK_BEFORE_TAKEOFF,
RAISING_GEAR,
GEAR_UP,
CHECK_BEFORE_LANDING,
LOWERING_GEAR,
}state_type;
typedef enum Switch
{
open, close
}Switch_status;
typedef enum pilot_lever
{
raising,
falling,
up,
down
}pilot_lever_status;
typedef enum hydraulic_mechanism
{
working,
malfunctioning
}hydraulic_mechanism_status;
// global variables
static volatile Switch_status squat_switch;// static for no to change the values by any other programmer
static volatile Switch_status limit_switch;// volatile for i can change the values inside my program
static volatile pilot_lever_status pilot_lever;
static volatile pilot_lever_status hydraulic_mechanism;
state_type current_state;
typedef struct //creating state table
{
char* current_state_status;
char* light_status;
char* direction_valve_status;
char* hydraulic_pump_ctrl_status;
char* gas_pressurised_spring_status[2];
}state_table;
static state_table state_machine[6]=// input the values to the state table
{
{"gear down","green","down","enables",{0,0}},
{"check before takeoff","green","down","enabled",{0,0}},
{"raising gear","red","up","enabled",{0,0}},
{"gear up","off","NULL","disabled",{0,0}},
{"check before landing","red","down","enabled",{0,0}},
{"lowering gear","green","down","enabled",{"disabled","enabled"}}
};
static void(*state_Table[])(void)={Gear_down,Check_before_takeoff,Raising_gear,Gear_up,Check_before_landing,Lowering_gear};
void Initial_state_machine()// sub-function expansion
{
current_state = GEAR_DOWN;
printf("\n state machine is initials and at first program is in gear down and its light status is : %s\n",
state_machine[current_state].light_status);
}
void Gear_down(void)// sub function expansion
{
current_state=GEAR_DOWN;
printf("\n Enter the pilot's lever position and squat switch status: \n");
scanf("%d %d",&pilot_lever,&squat_switch);
if (pilot_lever==raising && squat_switch==open)// values 0 0, it will go to check before takeoff
{
current_state=CHECK_BEFORE_TAKEOFF;
printf("\n The current state of the machine is : %s \n",state_machine[current_state].current_state_status);
printf("\n Light status of the current machine status is : %s\n",state_machine[current_state].light_status);
}
else
{
Gear_down();
}
}
void Check_before_takeoff()// sub function expansion
{
current_state=CHECK_BEFORE_TAKEOFF;
printf("\n Enter the pilot lever position, squat switch and hydraulic_mechanism: \n");
scanf("%d %d %d", &pilot_lever,&squat_switch,&hydraulic_mechanism);
printf("\n Just wait 2 sec \n");
if (pilot_lever==up && squat_switch==close)
{
Gear_down();
}
else if (pilot_lever==up && squat_switch==open&&hydraulic_mechanism==working)
{ // values 2 0 1 go to raising gear else go to gear down
current_state=RAISING_GEAR;
printf("\n current state :%s\n",state_machine[current_state].current_state_status);
printf("light_status: %s\n", state_machine[current_state].light_status);
printf("\n Direction status :%s\n\n", state_machine[current_state].direction_valve_status);
}
else if (pilot_lever==up && squat_switch==open &&hydraulic_mechanism==malfunctioning)
{
current_state=RAISING_GEAR;
printf("\n Current state : %s \n", state_machine[current_state].current_state_status);
printf("\n light status : %s\n", state_machine[current_state].light_status);
printf("\n Direction status : %s\n\n",state_machine[current_state].direction_valve_status);
printf("\n Gas pressurised spring status : %s \n", state_machine[current_state].gas_pressurised_spring_status[hydraulic_mechanism]);
}
}
void Raising_gear()
{
current_state=RAISING_GEAR;
printf("\n Enter the switch status and pilot lever status:\n");
scanf("%d %d",&limit_switch,&pilot_lever);
printf("\n wait for 1 sec\n");
if (pilot_lever==falling &&limit_switch==open) // values 1 0 go to check before landing
{
current_state=CHECK_BEFORE_LANDING;
printf("\n current state is : %s \n", state_machine[current_state].current_state_status);
printf("\n LIght status : %s \n", state_machine[current_state].light_status);
printf("\n hydraulic condition for landing gear status is: %s\n", state_machine[current_state].hydraulic_pump_ctrl_status);
}
else if (limit_switch==close && pilot_lever==up) // values 1 2 go tto gear up state
{
current_state=GEAR_UP;
printf("\n current state is : %s\n", state_machine[current_state].current_state_status);
printf("\n light status : %s\n",state_machine[current_state].light_status);
printf("\n Hydraulic condition for landing gear status is : %s\n", state_machine[current_state].hydraulic_pump_ctrl_status);
}
else
{
Raising_gear();
}
}
void Gear_up() // subfunction expansion
{
current_state=GEAR_UP;
printf("\n Enter the pilot lever position: \n");
scanf("%d",&pilot_lever);
if (pilot_lever==falling) // value 1 go to check before landing
{
current_state=CHECK_BEFORE_LANDING;
printf("\n current_state : %s \n", state_machine[current_state].current_state_status);
printf("\n light status : %s \n", state_machine[current_state].light_status);
printf("\n hydraulic condition for landing gear status is: %s \n", state_machine[current_state].hydraulic_pump_ctrl_status);
}
else
{
Gear_up();
}
}
void Check_before_landing() // subfunction expansion
{
current_state=CHECK_BEFORE_LANDING;
printf("\n Enter the pilot lever position, status of hydraulic mechanism:\n ");
scanf("%d %d", &pilot_lever,&hydraulic_mechanism);
if (pilot_lever==down && hydraulic_mechanism==working) // value 3 1 it will go to lowering gear
{
current_state=LOWERING_GEAR;
printf("\n current_state: %s \n", state_machine[current_state].current_state_status);
printf("\n light status: %s \n",state_machine[current_state].light_status);
printf("\n direcction status : %s\n\n", state_machine[current_state].direction_valve_status);
}
else if (pilot_lever==down && hydraulic_mechanism==malfunctioning) // values 3 0 it will go to lowering gear
{
current_state=LOWERING_GEAR;
printf("\n Current state : %s \n", state_machine[current_state].current_state_status);
printf("light status : %s\n", state_machine[current_state].light_status);
printf("\n direction status : %s\n\n",state_machine[current_state].direction_valve_status);
printf("\n Gas pressurised spring status: %s\n", state_machine[current_state].gas_pressurised_spring_status[hydraulic_mechanism]);
}
if(pilot_lever==(up || raising || falling))// other than pilot_lever= down, if we give anything it will stay in check before landing
{
Check_before_landing();
}
}
void Lowering_gear() // subfunction expansion
{
current_state=LOWERING_GEAR;
printf("\n Enter the pilot lever position, limit switch");
scanf("%d %d",&pilot_lever,&limit_switch);
if (pilot_lever== raising && limit_switch==close) // values 0 1 go to check before takeoff
{
current_state=CHECK_BEFORE_TAKEOFF;
printf("\n current state: %s\n", state_machine[current_state].current_state_status);
printf("\n light status : %s\n", state_machine[current_state].light_status);
}
if(pilot_lever==down && limit_switch==open) // values 3 0 go to gear down
{
current_state=GEAR_DOWN;
printf("\n current_state: %s\n", state_machine[current_state].current_state_status);
printf("\n light status: %s\n", state_machine[current_state].light_status);
printf("\n Hydraulic condition for landing geaar status is: %s\n", state_machine[current_state].hydraulic_pump_ctrl_status);
}
else
{
Lowering_gear();
}
}
int main() // main program
{
Initial_state_machine();
while(1)
{
state_Table[current_state]();
}
}
Result:
![]()
![]()
![]()