Académique Documents
Professionnel Documents
Culture Documents
Module
ShootingSM.c
Description
This module contains the shooting state machine.
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
/* include header files for the framework and this service
*/
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_ssi.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h" // Define PART_TM4C123GH6PM in project
#include "driverlib/gpio.h"
#include "termio.h"
#include "hw_nvic.h"
#include "hw_pwm.h"
#include "hw_timer.h"
#include "hw_ssi.h"
#include "MotorDrive.h"
#include "ShootingSM.h"
#include "MasterSM.h"
/****************************************************************************
Function
RunShootingSM
Parameters
ES_Event_t ThisEvent, event to process
Returns
ES_Event_t ThisEvent, event to return
Description
Posts an event to the queue of the SM
Notes
Author
S. Park
****************************************************************************/
ES_Event_t RunShootingSM(ES_Event_t CurrentEvent)
{
bool MakeTransition = false;/* are we making a state transition? */
ShootingState_t NextState = CurrentState;
ES_Event_t EntryEventKind = { ES_ENTRY, 0 }; // default to normal entry to
new state
ES_Event_t ReturnEvent = { ES_NO_EVENT, 0 }; // assume no error
switch (CurrentState)
{
//if state is ResettingServos
case ResettingServos:
{
printf("RESETTING SERVOS\n\r");
ReturnEvent = CurrentEvent = DuringResettingServos(CurrentEvent);
//process any events
if (CurrentEvent.EventType != ES_NO_EVENT) //If an event is active
{
switch (CurrentEvent.EventType)
{
//if event is servo move timeout
case ES_TIMEOUT:
{
if (CurrentEvent.EventParam == SERVO_MOVE_TIMER)
{
//if no ball loaded
if (!BallLoaded)
{
//load a ball
MoveReloadServo(RELOAD_SERVO_LOAD);
BallLoaded = true;
MakeTransition = true;
NextState = LoadingBall;
}
//else if ball is already loaded
else
{
//load ruler
MoveLaunchServo(LAUNCH_SERVO_LOADED);
MakeTransition = true;
NextState = LoadingRuler;
}
}
}
break;
}
}
}
break;
//if state is loading ball
case LoadingBall:
{
printf("LOADING BALL\n\r");
ReturnEvent = CurrentEvent = DuringLoadingBall(CurrentEvent);
if (CurrentEvent.EventType != ES_NO_EVENT) //If an event is active
{
switch (CurrentEvent.EventType)
{
//if event is servo move timeout
case ES_TIMEOUT:
{
if (CurrentEvent.EventParam == SERVO_MOVE_TIMER)
{
//load ruler
MoveLaunchServo(LAUNCH_SERVO_LOADED);
MakeTransition = true;
NextState = LoadingRuler;
}
}
break;
}
}
}
break;
case LoadingRuler:
{
printf("LOADING RULER\n\r");
ReturnEvent = CurrentEvent = DuringLoadingRuler(CurrentEvent);
if (CurrentEvent.EventType != ES_NO_EVENT) //If an event is active
{
switch (CurrentEvent.EventType)
{
//if event is servo move timeout
case ES_TIMEOUT:
{
if (CurrentEvent.EventParam == SERVO_MOVE_TIMER)
{
//launch ball
MoveLaunchServo(LAUNCH_SERVO_LAUNCHED);
DecrementNumBalls();
BallLoaded = false;
MakeTransition = true;
NextState = ShootingBall;
}
}
break;
}
}
}
break;
case ShootingBall:
{
printf("SHOOTING BALL\n\r");
ReturnEvent = CurrentEvent = DuringShootingBall(CurrentEvent);
if (CurrentEvent.EventType != ES_NO_EVENT) //If an event is active
{
switch (CurrentEvent.EventType)
{
//if event is servo move timeout
case ES_TIMEOUT:
{
if (CurrentEvent.EventParam == SERVO_MOVE_TIMER)
{
//reset launch servo
MoveLaunchServo(LAUNCH_SERVO_IDLE);
/****************************************************************************
Function
StartShootingSM
Parameters
ES_Event_t CurrentEvent
Returns
nothing
Description
Does any required initialization for this state machine
Notes
Author
S. Park
****************************************************************************/
void StartShootingSM(ES_Event_t CurrentEvent)
{
printf("StartingShootingSM\n\r");
//Initialize to ResettingServos
CurrentState = ResettingServos;
//Run the SM
RunShootingSM(CurrentEvent);
return;
}
/***************************************************************************
private functions
***************************************************************************/
static ES_Event_t DuringResettingServos(ES_Event_t Event)
{
//During function for stopped
ES_Event_t ReturnEvent = Event; // assme no re-mapping or comsumption