BallLoadHold.c
/****************************************************************************
Module
MovementSM.c
Revision
2.0.1
Description
This is a template file for implementing state machines.
Notes
History
When Who What/Why
-------------- --- --------
02/13/13 02:32 mt ME218B Project
02/08/12 09:56 jec revisions for the Events and Services Framework Gen2
02/13/10 14:29 jec revised Start and run to add new kind of entry function
to make implementing history entry cleaner
02/13/10 12:29 jec added NewEvent local variable to During function and
comments about using either it or Event as the return
02/11/10 15:54 jec more revised comments, removing last comment in during
function that belongs in the run function
02/09/10 17:21 jec updated comments about internal transitions on During funtion
02/18/09 10:14 jec removed redundant call to RunLowerlevelSM in EV_Entry
processing in During function
02/20/07 21:37 jec converted to use enumerated type for events & states
02/13/05 19:38 jec added support for self-transitions, reworked
to eliminate repeated transition code
02/11/05 16:54 jec converted to implment hierarchy explicitly
02/25/03 10:32 jec converted to take a passed event parameter
02/18/99 10:19 jec built template from MasterMachine.c
02/14/99 10:34 jec Began Coding
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
// Basic includes for a program using the Events and Services Framework
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_ServiceHeaders.h"
/* include header files for this state machine as well as any machines at the
next lower level in the hierarchy that are sub-machines to this machine
*/
/*----------------------------- Module Defines ----------------------------*/
// define constants for the states for this machine
// and any other local defines
#define SERVOMIN 0
#define SERVOMAX 0
#define AngleZero 213
#define AngleIncrement 17
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this machine, things like during
functions, entry & exit functions.They should be functions relevant to the
behavior of this state machine
*/
/*---------------------------- Module Variables ---------------------------*/
// keeps track of which balls are loaded
static boolean ballsLoaded[5] =
{
TRUE, TRUE, TRUE, TRUE, TRUE
};
//Array of positions of 360 servo
static int Position[8] =
{
AngleZero, AngleZero-AngleIncrement, AngleZero-2*AngleIncrement, AngleZero-3*AngleIncrement, AngleZero-4*AngleIncrement, AngleZero-5*AngleIncrement, AngleZero-6*AngleIncrement, AngleZero-7*AngleIncrement
};
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
Parameters
ES_Event: the event to process
Returns
ES_Event: an event to return
Description
add your description here
Notes
uses nested switch/case to implement the machine.
Author
****************************************************************************/
ES_Event RunBallSM( ES_Event CurrentEvent)
{
unsigned char MakeTransition = False;/* are we making a state transition? */
static int i; //Keeps track of which ball/position
ES_Event EntryEventKind = { ES_ENTRY, 0 };// default to normal entry to new state
ES_Event ReturnEvent = CurrentEvent; // assume we are not consuming event
ES_Event currEvent;
//1 second to move into loading position, 3 seconds for each ball afterwards
if (CurrentEvent.EventType == ES_ENTRY)
{
i = 0;
Servo_SetAngle(BallSpinServo, Position[1]);
ES_Timer_SetTimer(6, 1000);
ES_Timer_StartTimer(6);
}
if (CurrentEvent.EventType == ES_TIMEOUT && CurrentEvent.EventParam == 6)
{
if (i == 0)
{
setEmitter(1);
}
if (i < 5)
{
if (getColor() == 1)
{
//Handles disabled power supply
if (getRedDisable() == FALSE)
{
Servo_SetAngle(BallSpinServo, Position[i + 1]);
ES_Timer_SetTimer(6, 3000);
ES_Timer_StartTimer(6);
ballsLoaded[i] = TRUE;
i++;
changeAmmo(1);
} else if (i >= 2) //if more than 2 balls and power disabled, go to shoot
{
setEmitter(0);
currEvent.EventType = LoadingFinished;
PostMasterSM(currEvent);
} else
{
ES_Timer_SetTimer(6, 500);
ES_Timer_StartTimer(6);
}
} else if (getColor() == 2)
{
if (getBlueDisable() == FALSE)
{
Servo_SetAngle(BallSpinServo, Position[i + 1]);
ES_Timer_SetTimer(6, 3000);
ES_Timer_StartTimer(6);
ballsLoaded[i] = TRUE;
i++;
changeAmmo(1);
} else if (i >= 2)
{
setEmitter(0);
currEvent.EventType = LoadingFinished;
PostMasterSM(currEvent);
} else
{
ES_Timer_SetTimer(6, 500);
ES_Timer_StartTimer(6);
}
} else //Load balls if nothing is wrong
{
Servo_SetAngle(BallSpinServo, Position[i + 1]);
ES_Timer_SetTimer(6, 3000);
ES_Timer_StartTimer(6);
ballsLoaded[i] = TRUE;
i++;
changeAmmo(1);
}
} else //After we finish send event back
{
setEmitter(0);
currEvent.EventType = LoadingFinished;
PostMasterSM(currEvent);
}
}
}
/****************************************************************************
Function
StartBallSM
Parameters
None
Returns
None
Description
Does any required initialization for this state machine
Notes
Author
J. Edward Carryer, 2/18/99, 10:38AM
****************************************************************************/
void StartBallSM ( ES_Event CurrentEvent)
{
// local variable to get debugger to display the value of CurrentEvent
ES_Event LocalEvent = CurrentEvent;
// to implement entry to a history state or directly to a substate
// you can modify the initialization of the CurrentState variable
// otherwise just start in the entry state every time the state machine
// is started
// call the entry function (if any) for the ENTRY_STATE
RunBallSM(CurrentEvent);
}
/*******************************
Private Functions
*******************************/
/*******************************
Public Functions
*******************************/
void loadShooter(void )
{
int i;
for (i = 4; i > -1; i--)
{
if (ballsLoaded[i]) //Moves 360 servo to different positions to load a ball into the shooter
{
Servo_SetAngle(BallSpinServo, Position[i]);
ballsLoaded[i] = FALSE;
return;
}
}
}
Module
MovementSM.c
Revision
2.0.1
Description
This is a template file for implementing state machines.
Notes
History
When Who What/Why
-------------- --- --------
02/13/13 02:32 mt ME218B Project
02/08/12 09:56 jec revisions for the Events and Services Framework Gen2
02/13/10 14:29 jec revised Start and run to add new kind of entry function
to make implementing history entry cleaner
02/13/10 12:29 jec added NewEvent local variable to During function and
comments about using either it or Event as the return
02/11/10 15:54 jec more revised comments, removing last comment in during
function that belongs in the run function
02/09/10 17:21 jec updated comments about internal transitions on During funtion
02/18/09 10:14 jec removed redundant call to RunLowerlevelSM in EV_Entry
processing in During function
02/20/07 21:37 jec converted to use enumerated type for events & states
02/13/05 19:38 jec added support for self-transitions, reworked
to eliminate repeated transition code
02/11/05 16:54 jec converted to implment hierarchy explicitly
02/25/03 10:32 jec converted to take a passed event parameter
02/18/99 10:19 jec built template from MasterMachine.c
02/14/99 10:34 jec Began Coding
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
// Basic includes for a program using the Events and Services Framework
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_ServiceHeaders.h"
/* include header files for this state machine as well as any machines at the
next lower level in the hierarchy that are sub-machines to this machine
*/
/*----------------------------- Module Defines ----------------------------*/
// define constants for the states for this machine
// and any other local defines
#define SERVOMIN 0
#define SERVOMAX 0
#define AngleZero 213
#define AngleIncrement 17
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this machine, things like during
functions, entry & exit functions.They should be functions relevant to the
behavior of this state machine
*/
/*---------------------------- Module Variables ---------------------------*/
// keeps track of which balls are loaded
static boolean ballsLoaded[5] =
{
TRUE, TRUE, TRUE, TRUE, TRUE
};
//Array of positions of 360 servo
static int Position[8] =
{
AngleZero, AngleZero-AngleIncrement, AngleZero-2*AngleIncrement, AngleZero-3*AngleIncrement, AngleZero-4*AngleIncrement, AngleZero-5*AngleIncrement, AngleZero-6*AngleIncrement, AngleZero-7*AngleIncrement
};
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
Parameters
ES_Event: the event to process
Returns
ES_Event: an event to return
Description
add your description here
Notes
uses nested switch/case to implement the machine.
Author
****************************************************************************/
ES_Event RunBallSM( ES_Event CurrentEvent)
{
unsigned char MakeTransition = False;/* are we making a state transition? */
static int i; //Keeps track of which ball/position
ES_Event EntryEventKind = { ES_ENTRY, 0 };// default to normal entry to new state
ES_Event ReturnEvent = CurrentEvent; // assume we are not consuming event
ES_Event currEvent;
//1 second to move into loading position, 3 seconds for each ball afterwards
if (CurrentEvent.EventType == ES_ENTRY)
{
i = 0;
Servo_SetAngle(BallSpinServo, Position[1]);
ES_Timer_SetTimer(6, 1000);
ES_Timer_StartTimer(6);
}
if (CurrentEvent.EventType == ES_TIMEOUT && CurrentEvent.EventParam == 6)
{
if (i == 0)
{
setEmitter(1);
}
if (i < 5)
{
if (getColor() == 1)
{
//Handles disabled power supply
if (getRedDisable() == FALSE)
{
Servo_SetAngle(BallSpinServo, Position[i + 1]);
ES_Timer_SetTimer(6, 3000);
ES_Timer_StartTimer(6);
ballsLoaded[i] = TRUE;
i++;
changeAmmo(1);
} else if (i >= 2) //if more than 2 balls and power disabled, go to shoot
{
setEmitter(0);
currEvent.EventType = LoadingFinished;
PostMasterSM(currEvent);
} else
{
ES_Timer_SetTimer(6, 500);
ES_Timer_StartTimer(6);
}
} else if (getColor() == 2)
{
if (getBlueDisable() == FALSE)
{
Servo_SetAngle(BallSpinServo, Position[i + 1]);
ES_Timer_SetTimer(6, 3000);
ES_Timer_StartTimer(6);
ballsLoaded[i] = TRUE;
i++;
changeAmmo(1);
} else if (i >= 2)
{
setEmitter(0);
currEvent.EventType = LoadingFinished;
PostMasterSM(currEvent);
} else
{
ES_Timer_SetTimer(6, 500);
ES_Timer_StartTimer(6);
}
} else //Load balls if nothing is wrong
{
Servo_SetAngle(BallSpinServo, Position[i + 1]);
ES_Timer_SetTimer(6, 3000);
ES_Timer_StartTimer(6);
ballsLoaded[i] = TRUE;
i++;
changeAmmo(1);
}
} else //After we finish send event back
{
setEmitter(0);
currEvent.EventType = LoadingFinished;
PostMasterSM(currEvent);
}
}
}
/****************************************************************************
Function
StartBallSM
Parameters
None
Returns
None
Description
Does any required initialization for this state machine
Notes
Author
J. Edward Carryer, 2/18/99, 10:38AM
****************************************************************************/
void StartBallSM ( ES_Event CurrentEvent)
{
// local variable to get debugger to display the value of CurrentEvent
ES_Event LocalEvent = CurrentEvent;
// to implement entry to a history state or directly to a substate
// you can modify the initialization of the CurrentState variable
// otherwise just start in the entry state every time the state machine
// is started
// call the entry function (if any) for the ENTRY_STATE
RunBallSM(CurrentEvent);
}
/*******************************
Private Functions
*******************************/
/*******************************
Public Functions
*******************************/
void loadShooter(void )
{
int i;
for (i = 4; i > -1; i--)
{
if (ballsLoaded[i]) //Moves 360 servo to different positions to load a ball into the shooter
{
Servo_SetAngle(BallSpinServo, Position[i]);
ballsLoaded[i] = FALSE;
return;
}
}
}