Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
typedef enum {GEAR_DOWN = 0, WTG_FOR_TKOFF, RAISING_GEAR,
GEAR_UP, LOWERING_GEAR} State_Type;
/* This table contains a pointer to the function to call in each
state.*/
void *state_table[] = {GearDown, WtgForTakeoff,RaisingGear, GearUp, LoweringGear};
State_Type curr_state;
void main()
{
InitializeLdgGearSM();
/* The heart of the state machine is this one loop.
The function corresponding to the current state is called
once per iteration. */
while (1)
{
state_table[curr_state]();
DecrementTimer();
/* Do other functions, not related to this state machine.*/
}
}
void InitializeLdgGearSM()
{
curr_state = GEAR_DOWN;
timer = 0.0;
/* Stop all the hardware, turn off the lights, etc.*/
}
void GearDown()
{
/* Raise the gear upon command, but not if the airplane is
on the ground.*/
if ((gear_lever == UP) && (prev_gear_lever == DOWN) && (squat_switch == UP))
{
timer = 2.0;
curr_state = WTG_FOR_TKOFF;
}
prev_gear_lever = gear_lever; /* Store for edge detection.*/
}
void RaisingGear()
{
/* Once all 3 legs are up, go to the GEAR_UP state.*/
if ((nosegear_is_up == MADE) && (leftgear_is_up == MADE) &&(rtgear_is_up == MADE))
{
Curr_state = GEAR_UP;
}
/* If the pilot changes his mind, start lowering
the gear.*/
if (gear_lever == DOWN)
{
curr_state = LOWERING_GEAR;
}
}
void GearUp()
{
/* If the pilot moves the lever to DOWN, lower the gear.*/
if (gear_lever == DOWN)
{
curr_state = LOWERING_GEAR;
}
}
void WtgForTakeoff()
{
/* Once we’ve been airborne for 2 sec., start raising
the gear.*/
if (timer <=0.0)
{
curr_state = RAISING_GEAR;
}
/*If we touch down again, or if the pilot changes his
mind, start over.*/
if ((squat_switch ==DOWN) || (gear_lever == DOWN))
{
timer = 2.0;
curr_state = GEAR_DOWN;
/* Don’t want to require that he toggle the lever
again this was just a bounce.*/
prev_gear_lever = DOWN;
}
}
void LoweringGear()
{
if (gear_lever == UP)
{
curr_state = RAISING_GEAR;
}
if ((nosegear_is_down == MADE) && (Leftgear_is_down == MADE)&& (rtgear_is_down == MADE))
{
curr_state = GEAR_DOWN;
}
}