/**********************************************************************
ME218B
Project
Date:
Winter Quarter 2008
File
Name: RobotProjectMain.c
Description:
Main Function
**********************************************************************/
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
#include
"EventNames.h"
#include
"CheckEvents.h"
#include
"MasterSM.h"
/* */
void
main(void)
{
StartMasterSM();
printf("here we go!\r\n");
while(1)
{
RunMasterSM(CheckEvents());
}
}
#ifndef
MASTERSM_H_
#define
MASTERSM_H_
/**********************************************************************ME218B
Project: MasterSM.h
Date:
Winter Quarter 2008
File
Name: MasterSm.h
Description:
**********************************************************************/
unsigned
char RunMasterSM(unsigned char CurrentEvent);
void
StartMasterSM(void);
unsigned
char QueryMasterSM ( void );
static
void DuringGameStartState(unsigned char Event);
static
void DuringDriveToDispenserState(unsigned char Event);
static
void DuringDumpingState(unsigned char Event);
static
void DuringAtDispenserState(unsigned char Event);
#endif
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
File
Name: MasterSM.c
Description:
Master State Machine code
************************************************************************/
/*-----------------------------
Include Files -----------------------------*/
/*
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
*/
#include
"MasterSM.h"
#include
"EventNames.h"
#include
"GameStartSM.h"
#include
"ToDispenserSM.h"
#include
"RightTapeSensor.h"
#include
"LeftTapeSensor.h"
#include
"ServoModule.h"
#include
"GettingBallsSM.h"
#include
"DumpingSM.h"
#include
"MotorModule.h"
#include
"TimerModule.h"
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
/*----------------------------
Module Variables ---------------------------*/
static
unsigned char CurrentState;
/*------------------------------
Module Code ------------------------------*/
/****************************************************************************
Function
RunMasterSM
Parameters
unsigned char: the event to process
Returns
unsigned char: an event to return
Description
This function runs the master state machine.
Notes
uses nested switch/case to implement the
machine.
****************************************************************************/
//
make recursive call warning into info
#pragma
MESSAGE INFORMATION C1855
unsigned
char RunMasterSM( unsigned char CurrentEvent )
{
unsigned char MakeTransition = FALSE;/* are
we making a state transition? */
unsigned char NextState = CurrentState;
if
(CurrentEvent == EV_ENTRY)
{
//
do nothing
}
if (CurrentEvent == EV_EXIT)
{
// do nothing
}
/*Respond to the event GAME_OVER*/
if(CurrentEvent==GAME_OVER)
{
RunMasterSM(EV_EXIT);
StartMasterSM();
}
switch ( CurrentState )
{
case GAME_START_STATE : // If current state is Game Start
DuringGameStartState(CurrentEvent);
if ( CurrentEvent != EV_NO_EVENT
) //If an event is active
{
switch (CurrentEvent)
{
case FIFTY_PC_DETECTED : //If
front becaon detects 50% DC beacon
// Execute action function
for state one : event one
NextState =
TO_DISPENSER_STATE;//next state is to drive to dispenser
MakeTransition = TRUE; //mark that we are
taking a transition
printf(" Fifty percent
duty cycle event detected, transitioning to drive to dispenser\r\n") ;
break;
}
}
break;
case
TO_DISPENSER_STATE: // if current state is
DuringDriveToDispenserState(CurrentEvent);
if(
CurrentEvent != EV_NO_EVENT)
{
switch(CurrentEvent)
{
case
ARRIVED_AT_T: // if both R and L tape //sensors have
changed to HI
NextState =
AT_DISPENSER_STATE; // next //state is to At Dispenser
MakeTransition =
TRUE; // mark that //we are taking a transition
printf("getting
out of drive to //dispenser
state\r\n");
break;
}
}
break;
case
AT_DISPENSER_STATE: //if current state is At Dispenser State
DuringAtDispenserState(CurrentEvent);
if(CurrentEvent
!= EV_NO_EVENT)
{
switch(CurrentEvent)
{
case
TOTAL_BALLS_15: // if total ball count reaches
15
NextState
= DUMPING_STATE; // next state //is to go dump balls
MakeTransition
= TRUE; // mark //transition
printf("transitioning
to dumping \r\n");
break;
}
}
break;
case DUMPING_STATE: //robot
has hit the tape stop
DuringDumpingState(CurrentEvent);
printf("have entered dumping state
\r\n");
break;
}
//
If we are making a state transition
if (MakeTransition == TRUE)
{
//
Execute exit function for current state
RunMasterSM(EV_EXIT);
CurrentState = NextState; //Modify state
variable
//
Execute entry function for new state
RunMasterSM(EV_ENTRY);
}
return(CurrentEvent);
}
/****************************************************************************
Function
StartMasterSM
Parameters
None
Returns
None
Description
Does any required initialization for this
state machine
Notes
Author
Christine Tower, Nathan Parkhill, Stephanie
Lue
****************************************************************************/
void
StartMasterSM ( void )
{
//Start Master SM in Game Start
CurrentState = GAME_START_STATE;
// call the entry function (if any) for the
ENTRY_STATE
RunMasterSM(EV_ENTRY);
}
/****************************************************************************
Function
QueryMasterSM
Parameters
None
Returns
unsigned char The current state of the
Master state machine
Description
returns the current state of the Master
state machine
Notes
Author
Christine Tower, Nathan Parkhill, Stephanie
Lue
****************************************************************************/
unsigned
char QueryMasterSM ( void )
{
return(CurrentState);
}
/***************************************************************************
private functions
***************************************************************************/
/****************************************************************************
Function
DuringGameStartState
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
GameStart
****************************************************************************/
static
void DuringGameStartState(unsigned char Event)
{
// process EV_ENTRY & EV_EXIT events
if ( Event == EV_ENTRY)
{
// implement any entry actions required
for this state machine
// after that start any lower level
machines that run in this state
InitializePWM();
StopDriving();
StartGameStartSM();
}else if ( Event == EV_EXIT)
{
// on exit, give the lower levels a
chance to clean up first
RunGameStartSM(Event);
}else
// do the 'during' function for this state
{
// run any lower level state machine
RunGameStartSM(Event);
}
return;
}
/****************************************************************************
Function
DuringToDispenserState
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
ToDispenserState
****************************************************************************/
static
void DuringDriveToDispenserState(unsigned char Event)
{
// process EV_ENTRY & EV_EXIT events
if ( Event == EV_ENTRY)
{
// implement any entry actions required
for this state machine
// after that start any lower level
machines that run in this state
StartToDispenserSM();
}else if ( Event == EV_EXIT)
{
// on exit, give the lower levels a
chance to clean up first
RunToDispenserSM(Event);
// repeat for any concurrently running
state machines
// now do any local exit functionality
}else
// do the 'during' function for this state
{
RunToDispenserSM(Event);
}
return;
}
/****************************************************************************
Function
DuringAtDispenserState
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
AtDispenserState
****************************************************************************/
void
DuringAtDispenserState(unsigned char Event)
{
// process EV_ENTRY & EV_EXIT events
if ( Event == EV_ENTRY)
{
// implement any entry actions required
for this state machine
// after that start any lower level
machines that run in this state
InitTimer1(); //Initialize timer 1 for ball
detect and sorting sensors
printf("entered during at
dispenser state, timer 1 initialized \r\n");
StartGettingBallsSM();
}else if ( Event == EV_EXIT)
{
// on exit, give the lower levels a
chance to clean up first
RunGettingBallsSM(Event);
// repeat for any concurrently running
state machines
// now do any local exit functionality
}else
// do the 'during' function for this state
{
RunGettingBallsSM(Event);
}
return;
}
/****************************************************************************
Function
DuringDumpingState
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
DumpingState
****************************************************************************/
static
void DuringDumpingState(unsigned char Event)
{
// process EV_ENTRY & EV_EXIT events
if ( Event == EV_ENTRY)
{
// implement any entry actions required
for this state machine
// after that start any lower level
machines that run in this state
StartDumpingSM();
}else if ( Event == EV_EXIT)
{
// on exit, give the lower levels a
chance to clean up first
RunDumpingSM(Event);
}else
// do the 'during' function for this state
{
// run any lower level state machine
RunDumpingSM(Event);
}
return;
}
#ifndef
TODISPENSER_H_
#define
TODISPENSER_H_
/************************************************************************
ME218B
Project: MasterSM.h
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: ToDispenserSm.h
Description:
Master State machine header file
************************************************************************/
unsigned
char RunToDispenserSM(unsigned char CurrentEvent);
static
void DuringDriveToFrontHit(unsigned char CurrentEvent);
void
StartToDispenserSM(void);
unsigned
char QueryToDispenserSM (void);
static
void DuringOnRightSide(unsigned char CurrentEvent);
static
void DuringOnLeftSide(unsigned char CurrentEvent);
unsigned
char SetFieldSide(void);
static
void DuringDrivetoCenterHit(unsigned char CurrentEvent);
static
void DuringBlindRotate(unsigned char Event);
static
void DuringDriveToTape(unsigned char Event);
static
void DuringRotate1(unsigned char Event);
static
void DuringRotate2(unsigned char Event);
static
void DuringDriveToT(unsigned char Event);
static
void DuringAtDispenser(unsigned char Event);
#endif
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: ToDispenserSM.c
Description:
To Dispenser State Machine code
************************************************************************/
/*-----------------------------
Include Files -----------------------------*/
/*
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
*/
#include
"ToDispenserSM.h"
#include
"EventNames.h"
#include
"LeftTapeSensor.h"
#include
"RightTapeSensor.h"
#include
"CenterTapeSensor.h"
#include
"MotorModule.h"
#include
"TimerModule.h"
#include
"DriveToTSM.h"
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
/*----------------------------
Module Variables ---------------------------*/
static
unsigned char CurrentState;
static
unsigned char Side=0;
/*------------------------------
Module Code ------------------------------*/
/****************************************************************************
Function
RunToDispenserSM
Parameters
unsigned char: the event to process
Returns
unsigned char: an event to return
Description
This function runs the master state machine.
Notes
uses nested switch/case to implement the
machine.
****************************************************************************/
//
make recursive call warning into info
#pragma
MESSAGE INFORMATION C1855
unsigned
char RunToDispenserSM( unsigned char CurrentEvent )
{
unsigned char MakeTransition = FALSE;/* are
we making a state transition? */
unsigned char NextState = CurrentState;
switch ( CurrentState )
{
case DRIVE_TO_FRONT_HIT : // If current
state is Wait for flash
DuringDriveToFrontHit(CurrentEvent);
if ( CurrentEvent != EV_NO_EVENT )
//If an event is active
{
switch (CurrentEvent)
{
case LEFT_ON: //if left or right
tape changed to hit
NextState =
ON_LEFT_SIDE;//next state is to blind rotate
MakeTransition = TRUE; //mark
that we are taking a transition
printf("Making
Transition from Drive to front hit to on left side \r\n");
break;
case RIGHT_ON: //if left or
right tape changed to hit
NextState =
ON_RIGHT_SIDE;//next state is to blind rotate
MakeTransition = TRUE; //mark
that we are taking a transition
printf("***Making
Transition from Drive to front hit to on right side*** \r\n");
break;
}
}
break;
case ON_RIGHT_SIDE : // If current
state is Wait for flash
DuringOnRightSide(CurrentEvent);
if ( CurrentEvent != EV_NO_EVENT )//If
an event is active
{
switch (CurrentEvent)
{
case FIELD_SIDE_DETECTED: //if
left or right tape //changed to hit
NextState =
DRIVE_TO_CENTER_HIT;//next state is //to blind rotate
MakeTransition = TRUE; //mark
that we are taking //a transition
printf("***Making
transition from on right side to drive to center***
\r\n");
break;
}
}
break;
case ON_LEFT_SIDE :// If current
state is On Left Side
DuringOnLeftSide(CurrentEvent);
if ( CurrentEvent != EV_NO_EVENT )//If
an event is active
{
switch (CurrentEvent)
{
case FIELD_SIDE_DETECTED: //if
left or right tape //changed to hit
NextState = DRIVE_TO_CENTER_HIT;//next
state is //to blind rotate
MakeTransition = TRUE; //mark
that we are taking //a transition
printf("***Making
transition from on left side to drive to center***
\r\n");
break;
}
}
break;
case DRIVE_TO_CENTER_HIT :// If
current state is Drive to Center Hit
DuringDrivetoCenterHit(CurrentEvent);
if ( CurrentEvent != EV_NO_EVENT
) //If an event //is active
{
switch (CurrentEvent)
{
case CENTER_ON: //if left or
right tape changed to //hit
NextState = BLIND_ROTATE;//next
state is to //blind rotate
MakeTransition = TRUE; //mark
that we are taking //a transition
printf("***Making
transition from drive to center hit to blind rotate***
\r\n");
break;
}
}
break;
case BLIND_ROTATE : // If current
state is Blind Rotate
DuringBlindRotate(CurrentEvent);
//process any events
if ( CurrentEvent != EV_NO_EVENT )
//If an event is //active
{
switch (CurrentEvent)
{
case BLIND_ROTATE_TIMER_EXPIRED:
//if left or right //tape changed to hit
NextState =
DRIVE_TO_TAPE;//next state is to //blind rotate
MakeTransition = TRUE; //mark
that we are taking //a transition
printf("***Making
transition from blind rotate to
drive straight*** \r\n");
break;
}
}
break;
case DRIVE_TO_TAPE : // If current
state is Drive to Tape
DuringDriveToTape(CurrentEvent);
//process any events
if ( CurrentEvent != EV_NO_EVENT
) //If an event is //active
{
switch (CurrentEvent)
{
case CENTER_ON: //if left or
right tape changed to //hit
NextState = ROTATE_1;//next
state is to blind //rotate
MakeTransition = TRUE; //mark
that we are taking //a transition
printf("***Making transition
from drive to tape //to rotate 1***
\r\n");
break;
}
}
break;
case ROTATE_1 : // If current state is
Rotate 1
DuringRotate1(CurrentEvent);
//process any events
if ( CurrentEvent != EV_NO_EVENT ) //If an event is active
{
switch (CurrentEvent)
{
case RIGHT_ON: //if right tape
changed to hit
NextState = ROTATE_2;//next state is to
rotate 2
MakeTransition = TRUE; //mark
that we are taking a //transition
printf("***Making
transition from rotate 1 to rotate 2 (right tape
on)\r\n***");
break;
case
LEFT_ON: // if left tape changed to hit
NextState
= ROTATE_2; // next state is //to rotate 2
MakeTransition
= TRUE; // mark taht we //are taking a transition
printf("***Making
transition from rotate 1 to rotate 2 (left tape
on)\r\n***");
break;
}
}
break;
case ROTATE_2 : // If current state is Rotate 2
printf("have gotten inside of rotate
2\r\n"); //
DuringRotate2(CurrentEvent);
//process any events
if ( CurrentEvent != EV_NO_EVENT ) //If
an event is active
{
switch (CurrentEvent)
{
case STRADDLING_TAPE: //if right
tape changed to hit
NextState = DRIVE_TO_T;//next
state is to rotate 2
MakeTransition = TRUE; //mark
that we are taking a //transition
printf("***Making
transition from straddling tape to Drive to
tape\r\n***");
break;
}
}
break;
case DRIVE_TO_T:// if current state
is driving to T
DuringDriveToT(CurrentEvent);
break;
}
//
If we are making a state transition
if (MakeTransition == TRUE)
{
//
Execute exit function for current state
RunToDispenserSM(EV_EXIT);
CurrentState = NextState; //Modify state
variable
//
Execute entry function for new state
RunToDispenserSM(EV_ENTRY);
}
return(CurrentEvent);
}
/****************************************************************************
Function
StartToDispenserSM
Parameters
None
Returns
None
Description
Does any required initialization for this
state machine
Notes
Author
Christine Tower, Nathan Parkhill,
Stephanie Lue
****************************************************************************/
void
StartToDispenserSM(void)
{
// call the entry function (if any) for the
ENTRY_STATE
CurrentState = DRIVE_TO_FRONT_HIT;
RunToDispenserSM(EV_ENTRY);
}
/****************************************************************************
Function
QueryToDispenseSM
Parameters
None
Returns
unsigned char The current state of the
Template state machine
Description
returns the current state of the Template
state machine
Notes
Author
Christine Tower, Nathan Parkhill,
Stephanie Lue
****************************************************************************/
unsigned
char QueryToDispenserSM ( void )
{
return(CurrentState);
}
/****************************************************************************
Function
SetFieldSide()
Parameters
unsigned char FieldSide
Returns
nothing
Description
This function saves the static value of
FieldSide to be used FOREVER
****************************************************************************/
unsigned
char SetFieldSide(void)
{
static unsigned char FieldSide=0;
FieldSide=Side; //Set the field side to
Right or Left
return FieldSide;
}
/***************************************************************************
private functions
***************************************************************************/
/****************************************************************************
Function
DuringDriveToFrontHit
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
DuringDriveToFrontHit
****************************************************************************/
static
void DuringDriveToFrontHit(unsigned char Event)
{
// process EV_ENTRY & EV_EXIT events
if ( Event == EV_ENTRY)
{
InitTimer1();
InitLeftTapeSensor(); //Enable left tape sensor
InitRightTapeSensor();
//Enable right tape sensor
InitCenterTapeSensor(); //
enable center tape sensor
InitFrontBumpSensor();
printf("the right and
left tape sensors have been initialized\r\n");
// Drive Straight Forward
DriveStraightForward();
}
else if ( Event == EV_EXIT)
{
printf("Exiting
Drive front sense to tape state \r\n");
}
else
// do the 'during' function for this state
{
}
return;
}
/****************************************************************************
Function
DuringOnRightSide
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
DuringOnRightSide
****************************************************************************/
static
void DuringOnRightSide(unsigned char Event)
{
// process EV_ENTRY & EV_EXIT events
if ( Event == EV_ENTRY)
{
Side = RIGHT; //set the
side of field we are on
printf("we've set the
field side to be right\r\n");
SetFieldSide(); //save the
value FieldSide
printf("The value is
SetFieldSide is: %u\r\n", SetFieldSide());
}else if ( Event == EV_EXIT)
{
printf("exited on right side state
\r\n");
}else
// do the 'during' function for this state
{
}
return;
}
/****************************************************************************
Function
DuringOnLeftSide
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
DuringOnLeftSide
****************************************************************************/
static
void DuringOnLeftSide(unsigned char Event)
{
// process EV_ENTRY & EV_EXIT events
if ( Event == EV_ENTRY)
{
Side = LEFT; //set the side
of field we are on
printf("we've set the
field side to be left \r\n");
SetFieldSide(); //save the
value FieldSide
printf("The value is
SetFieldSide is: %u\r\n", SetFieldSide());
}else if ( Event == EV_EXIT)
{
printf("exited on left side state
\r\n");
}else
// do the 'during' function for this state
{
}
return;
}
/****************************************************************************
Function
DuringDrivetoCenterHit
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
DuringDrivetoCenterHit
****************************************************************************/
static
void DuringDrivetoCenterHit(unsigned char Event)
{
// process EV_ENTRY & EV_EXIT events
if ( Event == EV_ENTRY)
{
printf("entering
drive to center hit \r\n");
//InitCenterTapeSensor();
//Enable center tape sensor interrupts
}else if ( Event == EV_EXIT)
{
//DeInitCenterTapeSensor();
//Disable center tape sensor interrupts
StopDriving();
}else
// do the 'during' function for this state
{
}
return;
}
/****************************************************************************
Function
DuringBlindRotate
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
DuringOnRightSide
****************************************************************************/
static
void DuringBlindRotate(unsigned char Event)
{
// process EV_ENTRY & EV_EXIT events
if ( Event == EV_ENTRY)
{
InitRotateTimer();
//Start counting overflows for blind rotate timer
printf("Started blind
rotate timer\r\n");
if(Side
== RIGHT) // rotate counterclockwise if on right side of field
{
SetMotors(ROTATE_DUTY+ROTATE_DUTY,
0, FORWARD, BACKWARD);
printf("the
field side is right so we're rotating accordingly\r\n");
}
if(Side
== LEFT) // rotate clockwise if on
left side of field
{
SetMotors(0,
ROTATE_DUTY+ROTATE_DUTY, FORWARD, FORWARD);
printf("the
field side is left so we're rotating accordingly\r\n");
}
}
else if (Event == EV_EXIT)
{
DeInitRotateTimer(); //Stop counting overflows for blind rotate timer
printf("exiting blind rotate state \r\n");
}else
// do the 'during' function for this state
{
}
return;
}
/****************************************************************************
Function
DuringDriveToTape
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
DuringOnRightSide
****************************************************************************/
static
void DuringDriveToTape(unsigned char Event)
{
unsigned int i=0;
// process EV_ENTRY & EV_EXIT events
if ( Event == EV_ENTRY)
{
//Drive forward
DifferentialDriveForward(12,12);
}else if ( Event == EV_EXIT)
{
//Stop Driving
StopDriving();
}else
// do the 'during' function for this state
{
}
return;
}
/****************************************************************************
Function
DuringRotate1(unsigned char Event)
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
DuringRotate1
****************************************************************************/
static
void DuringRotate1(unsigned char Event)
{
if ( Event == EV_ENTRY)
{
/* If on right side,
the left tape sensor will be the first to hit the line
going towards the dispenser, so we initialize the left tape sensor and
rotate clockwise to start getting onto the tape */
if(Side == RIGHT)
{
Rotate(CCW,
ROTATE_DUTY); // Rotate Clockwise
}
/* If on left side,
the right tape sensor will be the first to hit the line
going towards the dispenser, so we initialize the right tape sensor and
rotate counterclockwise to start getting onto the tape */
if(Side == LEFT)
{
printf("the
left tape sensor was init inside during rotate 1\r\n");
Rotate(CW,
ROTATE_DUTY); // Rotate counterclockwise
}
}
else if ( Event == EV_EXIT)
{
printf("exiting rotate1
state \r\n");
}
else
// do the 'during' function for this state
{
}
return;
}
/****************************************************************************
Function
DuringRotate2(unsigned char Event)
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
DuringRotate2
****************************************************************************/
static
void DuringRotate2(unsigned char Event)
{
if ( Event == EV_ENTRY)
{
printf("entering
rotate 2\r\n");
/* If on right side we want
to rotate clockwise to continue rotating
to position robot to follow the tape. keep rotating until
the only
the center tape sensor is still detecting
the tape */
if(Side == RIGHT)
{
Rotate(CCW, 9); // Rotate Clockwise
}
/* If on left side we want
to rotate counterclockwise to continue rotating
to position robot to follow the tape. keep rotating until
the only
the center tape sensor is still detecting
the tape */
if(Side == LEFT)
{
Rotate(CW, 9); // Rotate counterclockwise
}
}
else if ( Event == EV_EXIT)
{
StopDriving();
//Stop moving
printf("exiting
rotate2 state \r\n");
}
else
// do the 'during' function for this state
{
}
return;
}
/****************************************************************************
Function
DuringDriveToT(unsigned char Event)
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
DuringDriveToT
****************************************************************************/
static
void DuringDriveToT(unsigned char Event)
{
if ( Event == EV_ENTRY)
{
StartDriveToTSM ();
/* upon entry, drive
straight */
DriveStraightForward();
}
else if ( Event == EV_EXIT)
{
/* Stop Driving once the T has
been reached */
StopDriving();
printf("exiting During
Drive to T state \r\n");
InitInBetweenBallSetTimer();
//Pause for 3 seconds upon arrival of //dispenser
DeInitInBetweenBallSetTimer();
//Stop pause, continue on to //AtDispenserSM
}
else
// do the 'during' function for this state
{
RunDriveToTSM(Event);
}
return;
}
#ifndef
DRIVETOTSM_H_
#define
DRIVETOTSM_H_
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: DriveToT.h
Description:
Drive To T Header File
************************************************************************/
unsigned
char RunDriveToTSM(unsigned char CurrentEvent);
void
StartDriveToTSM(void);
unsigned
char QueryDriveToTSM(void);
static
void DuringHeadingRight(unsigned char CurrentEvent);
static
void DuringHeadingLeft(unsigned char CurrentEvent);
static
void DuringHeadingStraight(unsigned char CurrentEvent);
static
void DuringArrivedAtT(unsigned char Event);
#endif
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: DriveToT.c
Description:
Drive To T State machine Code
************************************************************************/
/*-----------------------------
Include Files -----------------------------*/
/*
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
*/
#include
"MasterSM.h"
#include
"MotorModule.h"
#include
"EventNames.h"
#include
"GameStartSM.h"
#include
"ToDispenserSM.h"
#include
"DriveToTSM.h"
#include
"LeftTapeSensor.h"
#include
"RightTapeSensor.h"
#include
"CenterTapeSensor.h"
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
/*-----------------------------
Module Defines ----------------------------*/
//
define constants for the states for this machine
//
and any other local defines
/*----------------------------
Module Variables ---------------------------*/
//
everybody needs a state variable, you may need others as well
static
unsigned char CurrentState;
/*------------------------------
Module Code ------------------------------*/
/****************************************************************************
Function
RunDriveToTSM
Parameters
unsigned char: the event to process
Returns
unsigned char: an event to return
Description
add your description here
Notes
uses nested switch/case to implement the
machine.
Author
Christine Tower, Nathan Parkhill, Stephanie
Lue
****************************************************************************/
//
make recursive call warning into info
#pragma
MESSAGE INFORMATION C1855
unsigned
char RunDriveToTSM( unsigned char CurrentEvent )
{
unsigned char MakeTransition = FALSE;/* are
we making a state transition? */
unsigned char NextState = CurrentState;
switch ( CurrentState )
{
/* Robot is pointing right. During Heading Right will correct the
misalignment
and get robot pointing straight ahead */
case HEADING_RIGHT :
printf("currently heading
right\r\n");
// Execute During function for state
one. EV_ENTRY & EV_EXIT are
// processed here
DuringHeadingRight(CurrentEvent);
//process any events
if ( CurrentEvent != EV_NO_EVENT )
//If an event is active
{
switch (CurrentEvent)
{
case VEERED_LEFT :
NextState = HEADING_LEFT;
MakeTransition = TRUE; //mark
that we are taking a transition
break;
case STRADDLING_TAPE: // if straddling tape
NextState
= HEADING_STRAIGHT;
MakeTransition = TRUE; //
mark that we are taking //a transition'
break;
}
}
break;
/* Robot is pointing left. During Heading Left will correct the
misalignment
and get robot pointing straight ahead
*/
case HEADING_LEFT :
DuringHeadingLeft(CurrentEvent);
printf("currently heading
left\r\n");
//process any events
if ( CurrentEvent != EV_NO_EVENT )
//If an event is active
{
switch (CurrentEvent)
{
case VEERED_RIGHT :
NextState = HEADING_RIGHT;
MakeTransition = TRUE; //mark
that we are taking a transition
break;
case
STRADDLING_TAPE: // if straddling tape
NextState
= HEADING_STRAIGHT;
MakeTransition
= TRUE; // mark that we are taking //a transition
break;
}
}
break;
/* Robot is pointing straight
ahead. Don't do anything because we are locked onto target */
case HEADING_STRAIGHT: //if heading straight to ball dispenser
DuringHeadingStraight(CurrentEvent);
printf("currently
heading straight\r\n");
if(CurrentEvent
!= EV_NO_EVENT)
{
switch(CurrentEvent)
{
case
VEERED_RIGHT:
NextState
= HEADING_RIGHT; //
MakeTransition
= TRUE;
break;
case
VEERED_LEFT:
NextState
= HEADING_LEFT;
MakeTransition
= TRUE;
break;
}
}
break;
}
//
If we are making a state transition
if (MakeTransition == TRUE)
{
//
Execute exit function for current state
RunDriveToTSM(EV_EXIT);
CurrentState = NextState; //Modify state
variable
//
Execute entry function for new state
RunDriveToTSM(EV_ENTRY);
}
return(CurrentEvent);
}
/****************************************************************************
Function
StartDriveToTSM
Parameters
None
Returns
None
Description
Does any required initialization for this
state machine
Notes
Author
Christine Tower, Nathan Parkhill,
Stephanie Lue
****************************************************************************/
void
StartDriveToTSM ( void )
{
CurrentState = HEADING_STRAIGHT;
printf("have run start drive to
T\r\n");
// call the entry function (if any) for the
ENTRY_STATE
RunDriveToTSM(EV_ENTRY);
}
/****************************************************************************
Function
QueryDriveToTSM
Parameters
None
Returns
unsigned char The current state of the
Template state machine
Description
returns the current state of the Template
state machine
Notes
Author
Christine Tower, Nathan Parkhill,
Stephanie Lue
****************************************************************************/
unsigned
char QueryDriveToTSM ( void )
{
printf("query drive to T is
ok\r\n");
return(CurrentState);
}
/***************************************************************************
private functions
***************************************************************************/
/****************************************************************************
Function
DuringHeadingLeft
Author
Christine Tower, Nathan Parkhill,
Stephanie Lue
****************************************************************************/
static
void DuringHeadingLeft( unsigned char Event)
{
// process EV_ENTRY & EV_EXIT events
if ( Event == EV_ENTRY)
{
//
increase left motor PWM
DifferentialDriveForward(6,12);
}else if ( Event == EV_EXIT)
{
}else
// do the 'during' function for this state
{
}
return;
}
/****************************************************************************
Function
DuringHeadingRight
Author
Christine Tower, Nathan Parkhill,
Stephanie Lue
****************************************************************************/
static
void DuringHeadingRight( unsigned char Event)
{
// process EV_ENTRY & EV_EXIT events
if (
Event == EV_ENTRY)
{
//increase right motor PWM
DifferentialDriveForward(12,6);
}else if ( Event == EV_EXIT)
{
//DeInitRightTapeSensor();
//DeInitLeftTapeSensor();
}else
// do the 'during' function for this state
{
}
return;
}
/****************************************************************************
Function
DuringHeadingStraight
Author
Christine Tower, Nathan Parkhill,
Stephanie Lue
****************************************************************************/
static
void DuringHeadingStraight(unsigned char Event)
{
if ( Event == EV_ENTRY)
{
//Drive straight
DriveStraightForward();
}else if ( Event == EV_EXIT)
{
}else
// do the 'during' function for this state
{
}
return;
}
static
void DuringArrivedAtT(unsigned char Event)
{
if ( Event == EV_ENTRY)
{
//Stop moving
StopDriving();
}else if ( Event == EV_EXIT)
{
}else
// do the 'during' function for this state
{
}
return;
}
#ifndef
AT_DISPENSER_H_
#define
AT_DISPENSER_H_
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: AtDispenswerSM.h
Description:
At Dispenser Header File
************************************************************************/
unsigned
char RunAtDispenserSM( unsigned char CurrentEvent ) ;
void
StartAtDispenserSM ( void );
unsigned
char QueryAtDispenserSM ( void ) ;
#endif
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: AtDispenswerSM.c
Description:
At Dispenser State Machine code
************************************************************************/
/*-----------------------------
Include Files -----------------------------*/
/*
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
*/
#include
"AtDispenserSM.h"
#include
"EventNames.h"
#include
"TimerModule.h"
#include
"MasterSM.h"
#include
"ShooterModule.h"
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
/*----------------------------
Module Variables ---------------------------*/
//
everybody needs a state variable, you may need others as well
static
unsigned char CurrentState;
/*------------------------------
Module Code ------------------------------*/
/****************************************************************************
Function
RunAtDispenserSM
Parameters
unsigned char: the event to process
Returns
unsigned char: an event to return
Description
This function runs the master state machine.
Notes
uses nested switch/case to implement the
machine.
****************************************************************************/
//
make recursive call warning into info
#pragma
MESSAGE INFORMATION C1855
unsigned
char RunAtDispenserSM( unsigned char CurrentEvent )
{
if(CurrentEvent == EV_ENTRY)
{
}
if(CurrentEvent == EV_EXIT)
{
}
DuringAtDispenserState(CurrentEvent);
return(CurrentEvent);
}
/****************************************************************************
Function
StartAtDispenserSM
Parameters
None
Returns
None
Description
Does any required initialization for this
state machine
Notes
Author
Christine Tower, Nathan Parkhill,
Stephanie Lue
****************************************************************************/
void
StartAtDispenserSM ( void )
{
//Turn on shooter motor
// call the entry function (if any) for the ENTRY_STATE
RunAtDispenserSM(EV_ENTRY);
}
/****************************************************************************
Function
QueryAtDispenserSM
Parameters
None
Returns
unsigned char The current state of the Template
state machine
Description
returns the current state of the Template
state machine
Notes
Author
Christine Tower, Nathan Parkhill,
Stephanie Lue
****************************************************************************/
unsigned
char QueryAtDispenserSM ( void )
{
return(CurrentState);
}
#ifndef
GETTING_BALLS_H_
#define
GETTING_BALLS_H_
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: GettingBalls.c
Description:
Getting Balls Header File
************************************************************************/
/*Function
Prototypes*/
unsigned
char RunGettingBallsSM( unsigned char CurrentEvent );
void
StartGettingBallsSM ( void );
unsigned
char QueryGettingBallsSM ( void );
static
void DuringRequestingBalls( unsigned char Event);
static
void DuringBetweenPushes( unsigned char Event);
static
void DuringWaitingForNextSet(unsigned char Event);
unsigned
char GetTotalBalls(void);
unsigned
char GetPushCounts(void);
static
void IncrementPushCounts(void);
static
void IncrementTotalBalls(void);
static
void ClearPushCount(void);
void
ClearTotalBalls(void);
#endif
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: GettingBalls.c
Description:
Getting Balls State machine Code
************************************************************************/
/*-----------------------------
Include Files -----------------------------*/
/*
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
*/
#include
"MasterSM.h"
#include
"MotorModule.h"
#include
"EventNames.h"
#include
"GettingBallsSM.h"
#include
"TimerModule.h"
#include
"ServoModule.h"
#include
"CenterTapeSensor.h"
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
/*----------------------------
Module Variables ---------------------------*/
//
everybody needs a state variable, you may need others as well
static
unsigned char CurrentState;
static
unsigned char PushCount;
static
unsigned char TotalBallCount;
/*------------------------------
Module Code ------------------------------*/
/****************************************************************************
Function
RunTemplateSM
Parameters
unsigned char: the event to process
Returns
unsigned char: an event to return
Description
add your description here
Notes
uses nested switch/case to implement the
machine.
Author
Christine Tower, Nathan Parkhill, Stephanie
Lue
****************************************************************************/
//
make recursive call warning into info
#pragma
MESSAGE INFORMATION C1855
unsigned
char RunGettingBallsSM( unsigned char CurrentEvent )
{
unsigned char MakeTransition = FALSE;/* are
we making a state transition? */
unsigned char NextState = CurrentState;
switch ( CurrentState )
{
/* Requesting Balls. Will exit when we
have requested 5 balls or when
we need to retract our pusher*/
case REQUESTING_BALLS :
DuringRequestingBalls(CurrentEvent);
//process any events
if ( CurrentEvent != EV_NO_EVENT
) //If an event is active
{
switch (CurrentEvent)
{
case PUSH_COUNT_5 :
NextState =
WAITING_FOR_NEXT_SET;//if we've requested 5 balls
MakeTransition = TRUE;
printf("push count =
5making transition from Requesting Balls to
Waiting for next set \r\n");
break;
case PUSH_TIMER_EXPIRED: // if we've pushed long enough
NextState
= BETWEEN_PUSHES;
MakeTransition = TRUE;
printf("push
timer expired, making transition from Requesting Balls to Waiting for
next set \r\n");
break;
}
}
break;
/* Waiting between pushes. Allows for a
pause between ball requests. Will
exit when we have waited and can request
for a ball again*/
case BETWEEN_PUSHES :
DuringBetweenPushes(CurrentEvent);
if ( CurrentEvent != EV_NO_EVENT
) //If an event is active
{
switch (CurrentEvent)
{
case RETRACT_TIMER_EXPIRED :
//if we've pulled back long enough
NextState = REQUESTING_BALLS;
MakeTransition = TRUE;
break;
}
}
break;
/* Waiting for next set. Must pause for
3 seconds after we have
requested 5 balls. After this pause we
can return to requesting balls */
case WAITING_FOR_NEXT_SET:
DuringWaitingForNextSet(CurrentEvent);
printf("waiting
for nextset of 5 ballst\r\n");
if(CurrentEvent
!= EV_NO_EVENT)
{
switch(CurrentEvent)
{
case
BETWEEN_BALL_SET_TIMER_EXPIRED: //we've //waited long enough b/w
sets of 5
NextState
= REQUESTING_BALLS;
MakeTransition
= TRUE;
break;
}
}
break;
}
//
If we are making a state transition
if (MakeTransition == TRUE)
{
//
Execute exit function for current state
RunGettingBallsSM(EV_EXIT);
CurrentState = NextState; //Modify state
variable
//
Execute entry function for new state
RunGettingBallsSM(EV_ENTRY);
}
return(CurrentEvent);
}
/****************************************************************************
Function
StartGettingBallsSM
Parameters
None
Returns
None
Description
Does any required initialization for this
state machine
Notes
Author
Christine Tower, Nathan Parkhill,
Stephanie Lue
****************************************************************************/
void
StartGettingBallsSM ( void )
{
CurrentState = REQUESTING_BALLS;
//Set push count=0
ClearPushCount();
printf("have run start requesting
balls\r\n");
// call the entry function (if any) for the
ENTRY_STATE
RunGettingBallsSM(EV_ENTRY);
}
/****************************************************************************
Function
QueryGettingBallsSM
Parameters
None
Returns
unsigned char The current state of the
Template state machine
Description
returns the current state of the Template
state machine
Notes
Author
Christine Tower, Nathan Parkhill,
Stephanie Lue
****************************************************************************/
unsigned
char QueryGettingBallsSM ( void )
{
return(CurrentState);
}
/****************************************************************************
Public
Total Ball and Push Count Functions
****************************************************************************/
unsigned
char GetTotalBalls(void)
{
return TotalBallCount;
}
unsigned
char GetPushCounts(void)
{
return PushCount;
}
/*Clears
Total Ball Count. Not private because we
want to clear upon game
entry
in GameStartSM */
void
ClearTotalBalls(void)
{
TotalBallCount=0;
return;
}
/***************************************************************************
private functions
***************************************************************************/
/****************************************************************************
Function
DuringRequestingBalls
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to DuringRequestingBalls
****************************************************************************/
static
void DuringRequestingBalls( unsigned char Event)
{
int i;
// process EV_ENTRY & EV_EXIT events
if ( Event == EV_ENTRY)
{
InitPushTimer(); //Start
incrementing Push Timer overflows
//Drive forward into
dispenser
DriveStraightForward();
printf("Entered
Requesting Balls \r\n");
}else if ( Event == EV_EXIT)
{
//Drive backward from
dispenser
SetMotors(RIGHT_MOTOR_STRAIGHT_DUTY,
RIGHT_MOTOR_STRAIGHT_DUTY, BACKWARD, BACKWARD);
IncrementPushCounts();
//Increment push counts
DeInitPushTimer(); //Stop
incrementing Push Timer overflows
printf("exiting
requesting balls \r\n");
}else
// do the 'during' function for this state
{
}
return;
}
/****************************************************************************
Function
DuringBetweenPushes
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
DuringBetweenPushes
****************************************************************************/
static
void DuringBetweenPushes( unsigned char Event)
{
// process EV_ENTRY & EV_EXIT events
if ( Event == EV_ENTRY)
{
InitRetractTimer(); //Start
incrementing retract timer overflows
IncrementTotalBalls();
//Increment total number of balls
printf("Total Balls:
%d\r\n",GetTotalBalls());
printf("Entering
Between Pushes \r\n");
}else if ( Event == EV_EXIT)
{
DeInitRetractTimer();
//Stop incrementing retract timer overflows
}else
// do the 'during' function for this state
{
}
return;
}
/****************************************************************************
Function
DuringWaitingForNextSet
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
DuringWaitingForNextSet
****************************************************************************/
static
void DuringWaitingForNextSet(unsigned char Event)
{
if ( Event == EV_ENTRY)
{
StopDriving();
InitInBetweenBallSetTimer();
//Start incrementing in between ball set timer overflows
}else if ( Event == EV_EXIT)
{
DeInitInBetweenBallSetTimer();
//Stop incrementing in between ball set timer overflows
ClearPushCount();//Clear
push count
}else
// do the 'during' function for this state
{
}
return;
}
/****************************************************************************
Push
Count Functions
****************************************************************************/
static
void IncrementPushCounts(void)
{
PushCount++;
return;
}
static
void ClearPushCount(void)
{
PushCount=0;
return;
}
/****************************************************************************
Total
Ball Functions
****************************************************************************/
static
void IncrementTotalBalls(void)
{
TotalBallCount++;
return;
}
#ifndef
DUMPING_SM_H_
#define
DUMPING_SM_H
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: DumpingSM.c
Description:
Dumping State machine Code
************************************************************************/
/*function
prototypes*/
unsigned
char RunGettingBallsSM( unsigned char CurrentEvent );
void
StartDumpingSM ( void );
unsigned
char QueryDumpingSM ( void );
static
void DuringRotateTo90( unsigned char Event);
static
void DuringNinetyDetected(unsigned char Event);
static
void DuringAtWall(unsigned char Event);
#endif
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: DumpingSM.c
Description:
Dumping State machine Code
************************************************************************/
/*-----------------------------
Include Files -----------------------------*/
/*
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
*/
#include
"MasterSM.h"
#include
"MotorModule.h"
#include
"EventNames.h"
#include
"DumpingSM.h"
#include
"TimerModule.h"
#include
"ServoModule.h"
#include
"FrontBeacon.h"
#include
"DumpingSM.h"
#include
"RearBeaconModule.h"
#include
"ToDispenserSM.h"
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
/*----------------------------
Module Variables ---------------------------*/
//
everybody needs a state variable, you may need others as well
static
unsigned char CurrentState;
/*------------------------------
Module Code ------------------------------*/
/****************************************************************************
Function
RunDumpingSM
Parameters
unsigned char: the event to process
Returns
unsigned char: an event to return
Description
add your description here
Notes
uses nested switch/case to implement the
machine.
Author
Christine Tower, Nathan Parkhill, Stephanie
Lue
****************************************************************************/
//
make recursive call warning into info
#pragma
MESSAGE INFORMATION C1855
unsigned
char RunDumpingSM( unsigned char CurrentEvent )
{
unsigned char MakeTransition = FALSE;/* are
we making a state transition? */
unsigned char NextState = CurrentState;
switch ( CurrentState )
{
/* Requesting Balls. Will exit when we
have requested 5 balls or when
we need to retract our pusher*/
case ROTATE_TO_90 :
DuringRotateTo90(CurrentEvent);
//process any events
if ( CurrentEvent != EV_NO_EVENT
) //If an event is active
{
switch (CurrentEvent)
{
case NINETY_PC_DETECTED :
//we've found goal 3, changed to goal //1 later
NextState = NINETY_DETECTED;
MakeTransition = TRUE;
printf("making
transition to ninety detected\r\n");
break;
}
}
break;
case NINETY_DETECTED :
DuringNinetyDetected(CurrentEvent);
//process any events
if ( CurrentEvent != EV_NO_EVENT
) //If an event is active
{
switch (CurrentEvent)
{
case
STOP_TO_FIND_BEACON:
NextState = AT_WALL;
MakeTransition = TRUE;
printf("stopping to
rotate again\r\n");
break;
case WALL_DETECTED :
NextState = AT_WALL;
MakeTransition = TRUE;
printf("making
transition to at wall\r\n");
break;
}
}
break;
case AT_WALL: //robot
has hit the tape stop
DuringAtWall(CurrentEvent);
printf("making
transition, we're done \r\n");
break;
}
//
If we are making a state transition
if (MakeTransition == TRUE)
{
//
Execute exit function for current state
RunDumpingSM(EV_EXIT);
CurrentState = NextState; //Modify state
variable
//
Execute entry function for new state
RunDumpingSM(EV_ENTRY);
}
return(CurrentEvent);
}
/****************************************************************************
Function
StartDumpingSM
Parameters
None
Returns
None
Description
Does any required initialization for this
state machine
Notes
Author
Christine Tower, Nathan Parkhill,
Stephanie Lue
****************************************************************************/
void
StartDumpingSM ( void )
{
CurrentState = ROTATE_TO_90;
// call the entry function (if any) for the
ENTRY_STATE
RunDumpingSM(EV_ENTRY);
}
/****************************************************************************
Function
QueryDumpingSM
Parameters
None
Returns
unsigned char The current state of the
Template state machine
Description
returns the current state of the Template
state machine
Notes
Author
Christine Tower, Nathan Parkhill,
Stephanie Lue
****************************************************************************/
unsigned
char QueryDumpingSM ( void )
{
return(CurrentState);
}
/***************************************************************************
private functions
***************************************************************************/
/****************************************************************************
Function
DuringRotateTo90
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
DuringRotateTo90
****************************************************************************/
static
void DuringRotateTo90( unsigned char Event)
{
// process EV_ENTRY & EV_EXIT events
if ( Event == EV_ENTRY)
{
printf("initializing a
fun loop\r\n");
//Backup slightly
SetMotors(LEFT_MOTOR_STRAIGHT_DUTY,RIGHT_MOTOR_STRAIGHT_DUTY,BACKWARD,BAC KWARD);
//Start the rotate timer
InitRotateTimer();
while(BlindRotateTimerExpired()==NO)
{
}
printf("back that thang up\r\n");
//Stop moving
StopDriving();
//Initialize the rear beacon sensor
InitRearBeacon();
if(SetFieldSide()==LEFT)
{
//if the field side is on the left, rotate cw
//Rotate until the 90 percent beacon is
detected by the //front beacon sensor
printf("Field side is left, starting to
rotate \r\n");
Rotate(CCW,ROTATE_DUTY);
}
if(SetFieldSide()==RIGHT)
{
//if the field side is on the right, rotate
ccw
printf("Field side is right, starting to
rotate \r\n");
//Rotate until the 90 percent beacon is
detected by the //front beacon sensor
Rotate(CW,ROTATE_DUTY);
}
printf("the front
beacon is initialized\r\n");
}else if ( Event == EV_EXIT)
{
//Stop Rotating
StopDriving();
printf("90 found,
turned off the beacon\r\n");
}else
// do the 'during' function for this state
{
}
return;
}
/****************************************************************************
Function
DuringNinetyDetected
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
DuringNinetyDetected
****************************************************************************/
static
void DuringNinetyDetected(unsigned char Event)
{
if ( Event == EV_ENTRY)
{
//Init the rear bump sensor
InitWallDetectorSensor();
//Start the dump timer
InitDumpTimer();
//Drive Backwards
SetMotors(LEFT_MOTOR_STRAIGHT_DUTY+2,
RIGHT_MOTOR_STRAIGHT_DUTY+2, BACKWARD,
BACKWARD);
}else if ( Event == EV_EXIT)
{
//Turn off rear bump sensor
DeInitWallDetectorSensor();
//clear dump timer
DeInitDumpTimer();
}else
// do the 'during' function for this state
{
}
return;
}
/****************************************************************************
Function
DuringAtWall
Parameters
unsigned char EVENT
Returns
nothing
Description
This function handles events sent to
DuringAtWall
****************************************************************************/
static
void DuringAtWall(unsigned char Event)
{
int i;
if ( Event == EV_ENTRY)
{
//initialize port for dump motor
PTP=PTP&BIT1LO; //initialize port U7 as low
DDRP=DDRP|BIT1HI; //set port U7 as
an output for the dump motor
//start dump timer (3 seconds)
InitInBetweenBallSetTimer();
PTP=PTP|BIT1HI; //set port U7
high, turn on dump motor
while(InBetweenBallSetTimerExpired()==NO)
{
}
PTP=PTP&BIT1LO; //set port U7 low,
turn off dump motor
}else if ( Event == EV_EXIT)
{
//Stop moving
StopDriving();
}else
// do the 'during' function for this state
{
}
return;
}
#ifndef
EVENTNAMES_H_
#define
EVENTNAMES_H_
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: EventNames.h
Description:
Event Names Header File
************************************************************************/
/**************
State Names *****************/
#define
ENTRY_STATE 0
#define
GAME_START_STATE 1
#define
TO_DISPENSER_STATE 2
#define
AT_DISPENSER_STATE 3
#define
DUMPING_STATE 4
/*Game
Start States*/
#define
WAIT_FOR_FLASH 5
#define
ROTATE_TO_50 6
/*To
Dispenser States*/
#define
DRIVE_TO_FRONT_HIT 7
#define
ON_RIGHT_SIDE 8
#define
ON_LEFT_SIDE 9
#define
DRIVE_TO_CENTER_HIT 10
#define
BLIND_ROTATE 11
#define
ROTATE_1 12
#define
ROTATE_2 13
#define
DRIVE_TO_T 14
#define
HEADING_RIGHT 15
#define
HEADING_STRAIGHT 16
#define
HEADING_LEFT 17
#define
DRIVE_TO_TAPE 18
#define
AT_DISPENSER 19
/*At
Dispenser States*/
#define
GETTING_BALLS 26
#define
REQUESTING_BALLS 20
#define
WAITING_FOR_NEXT_SET 21
#define
BETWEEN_PUSHES 22
/*Dumping
States*/
#define
ROTATE_TO_90 23
#define
NINETY_DETECTED 24
#define
AT_WALL 25
/**************
Event Names *****************/
#define
EV_NO_EVENT 0
#define
GAME_OVER 1
#define
FLASH_DETECTED 2
#define
FIFTY_PC_DETECTED 3
#define
LEFT_ON 4
#define
RIGHT_ON 5
#define
CENTER_ON 6
#define
FIELD_SIDE_DETECTED 7
#define
BLIND_ROTATE_TIMER_EXPIRED 8
#define
STRADDLING_TAPE 9
#define
VEERED_LEFT 10
#define
VEERED_RIGHT 11
#define
ARRIVED_AT_T 12
#define
EV_ENTRY 13
#define
EV_EXIT 14
#define
TOTAL_BALLS_15 15
#define
PUSH_COUNT_5 16
#define
PUSH_TIMER_EXPIRED 17
#define
RETRACT_TIMER_EXPIRED 18
#define
BETWEEN_BALL_SET_TIMER_EXPIRED 19
#define
STOPPED_AT_T 20
#define
NINETY_PC_DETECTED 21
#define
WALL_DETECTED 22
#define
STOP_TO_FIND_BEACON 23
/*****
other useful things ******/
#define
NO 0
#define
YES 1
#define
TRUE 1
#define
FALSE 0
#define
LEFT 2
#define
RIGHT 1
#endif
#ifndef
CHECK_EVENTS_H_
#define
CHECK_EVENTS_H_
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: CheckEvents.h
Description:
CheckEvents Header File
************************************************************************/
/*Function
Prototypes*/
unsigned
char CheckEvents(void);
#endif
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: CheckEvents.c
Description:
CheckEvents code
************************************************************************/
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
#include
"EventNames.h"
#include
"CheckEvents.h"
#include
"TimerModule.h"
#include
"FlashModule.h"
#include
"FrontBeacon.h"
#include
"RightTapeSensor.h"
#include
"LeftTapeSensor.h"
#include
"CenterTapeSensor.h"
#include
"WallDetectorModule.h"
#include
"GameStartSM.h"
#include
"ToDispenserSM.h"
#include
"GettingBallsSM.h"
#include
"DumpingSM.h"
#include
"MasterSM.h"
#include
"FrontBumpSensor.h"
#include
"RearBeaconModule.h"
/*Function
Prototypes
unsigned
char CheckEvents(void);
*/
/****************************************************************************
Function
CheckEvents
Parameters
none
Returns
unsigned char: the current event
Description
This function checks the events by calling
the approrpiate check functions
Notes
****************************************************************************/
unsigned
char CheckEvents(void)
{
//Always
start with CurrentEvent= No Event
unsigned
char CurrentEvent = EV_NO_EVENT;
/*Check for game over*/
if(GameOver()==YES)
{
CurrentEvent=GAME_OVER;
return CurrentEvent;
}
/*CHECK FUNCTIONS DEPENDANT ON TIMER1
OVERFLOW PERIOD*/
/*Check to see if the Timer1 check
sensors overflow has occured*/
if(SensorCheckTimerExpired()==YES)
{
if(QueryToDispenserSM()==DRIVE_TO_FRONT_HIT)
{
/*Check
for Right hit*/
if (RightTapeOn()==YES)
{
CurrentEvent=RIGHT_ON;
printf("right
tape is on!! \r\n");
return CurrentEvent;
}
/*Check for Left hit*/
if (LeftTapeOn() ==YES)
{
CurrentEvent=LEFT_ON;
printf("left
tape is on!!!\r\n");
return CurrentEvent;
}
}
if(QueryToDispenserSM()==DRIVE_TO_CENTER_HIT)
{
/*Check to see if center
tape sensor is on tape*/
if(CenterTapeOn()==YES)
{
CurrentEvent=CENTER_ON;
return CurrentEvent;
}
}
if(QueryToDispenserSM()==DRIVE_TO_TAPE)
{
/*Check to see if center
tape sensor is on tape*/
if(CenterTapeOn()==YES)
{
CurrentEvent=CENTER_ON;
return CurrentEvent;
}
}
/*Checks for inside heading left*/
if(QueryDriveToTSM()==HEADING_RIGHT)
{
if(LeftTapeOn()==YES)
{
CurrentEvent=VEERED_RIGHT;
return CurrentEvent;
}
if((LeftTapeOn()==NO)&&(RightTapeOn()==NO))
{
CurrentEvent=STRADDLING_TAPE;
return CurrentEvent;
}
}
/*Checks for inside heading
right*/
if(QueryDriveToTSM()==HEADING_LEFT)
{
if(RightTapeOn()==YES)
{
CurrentEvent=VEERED_LEFT;
return CurrentEvent;
}
if((LeftTapeOn()==NO)&&(RightTapeOn()==NO))
{
CurrentEvent=STRADDLING_TAPE;
return CurrentEvent;
}
}
/*Checks for inside heading
straight*/
if(QueryDriveToTSM()==HEADING_STRAIGHT)
{
if(LeftTapeOn()==YES)
{
CurrentEvent=VEERED_RIGHT;
return CurrentEvent;
}
if(RightTapeOn()==YES)
{
CurrentEvent=VEERED_LEFT;
return CurrentEvent;
}
}
/*Check to see if arrived at T*/
if((PTIAD&BIT7HI)!=0)
{
CurrentEvent=ARRIVED_AT_T;
return CurrentEvent;
}
/*Check to see if tape sensor is
detected after Rotate 1*/
if(QueryToDispenserSM()==ROTATE_1)
{
/*Check to see we're on the
right field*/
if(SetFieldSide()==RIGHT)
{
if(RightTapeOn()==YES)
{
CurrentEvent=RIGHT_ON;
return
CurrentEvent;
}
}
/*Check to see if we're on
the left field*/
if(SetFieldSide()==LEFT)
{
if(LeftTapeOn()==YES)
{
CurrentEvent=LEFT_ON;
return
CurrentEvent;
}
}
}
/*Check to see if Left,Right is
off and Center is on*/
if(QueryToDispenserSM()==ROTATE_2)
{
if((RightTapeOn()==NO)
&& (LeftTapeOn()==NO))
{
printf("Straddling
tape in Rotate 2 s tate...this is going to exit soon
\r\n");
CurrentEvent=STRADDLING_TAPE;
return
CurrentEvent;
}
}
/*Check to see if center tape
sensor is on tape*/
if(QueryToDispenserSM()==DRIVE_TO_CENTER_HIT)
{
if(CenterTapeOn()==YES)
{
CurrentEvent=CENTER_ON;
return CurrentEvent;
}
}
}
/*CHECK FUNCTIONS NOT DEPENDANT ON TIMER1
OVERFLOW PERIOD*/
/*Check to see if the dump timer has
expired*/
if(QueryDumpingSM()==NINETY_DETECTED)
{
if(DumpTimerExpired()==YES)
{
CurrentEvent=STOP_TO_FIND_BEACON;
return
CurrentEvent;
}
}
/*Check to see if the wall has been
sensed*/
if(QueryDumpingSM()==NINETY_DETECTED)
{
if(WallDetected()==YES)
{
CurrentEvent=WALL_DETECTED;
return CurrentEvent;
}
}
/*Check to see if the 30 percent duty
cycle is detected*/
if(QueryDumpingSM()==ROTATE_TO_90)
{
printf("in checking events,
checking for thirty beacon, state is rotate to 90 \r\n");
if(CheckForThirty()==YES)
{
CurrentEvent=NINETY_PC_DETECTED;
return CurrentEvent;
}
}
/*Check to see if total ball count is
15*/
if(QueryMasterSM()==AT_DISPENSER_STATE)
{
if(GetTotalBalls()==10)
{
printf("we got 10
balls \r\n");
CurrentEvent=TOTAL_BALLS_15;
return CurrentEvent;
}
}
/*Check to see if the push count is 5*/
if(QueryGettingBallsSM()==REQUESTING_BALLS)
{
if(GetPushCounts()==5)
{
CurrentEvent=PUSH_COUNT_5;
return CurrentEvent;
}
}
/*Check to see if the push timer is
expired (have pushed long enough to request ball)*/
if(QueryGettingBallsSM()==REQUESTING_BALLS)
{
//printf("Current state is: Requesting
balls \r\n");
if(PushTimerExpired()==YES)
{
printf("push timer
expired \r\n");
CurrentEvent=PUSH_TIMER_EXPIRED;
return CurrentEvent;
}
}
/*Check to see if retract timer is
expired (have pulled back long enough between ball requests)*/
if(QueryGettingBallsSM()==BETWEEN_PUSHES)
{
if(RetractTimerExpired()==YES)
{
CurrentEvent=RETRACT_TIMER_EXPIRED;
return CurrentEvent;
}
}
/*Check to see if enought time has passed
between sets of 5*/
if(QueryGettingBallsSM()==WAITING_FOR_NEXT_SET)
{
if(InBetweenBallSetTimerExpired()==YES)
{
CurrentEvent=BETWEEN_BALL_SET_TIMER_EXPIRED;
return CurrentEvent;
}
}
/*check for blind rotate timer expired*/
if(QueryToDispenserSM()==BLIND_ROTATE)
{
if(BlindRotateTimerExpired()==YES)
{
CurrentEvent=BLIND_ROTATE_TIMER_EXPIRED;
return CurrentEvent;
}
}
/*check to see if field side has been
detected and set*/
if((QueryToDispenserSM()==ON_RIGHT_SIDE)||
(QueryToDispenserSM()==ON_LEFT_SIDE))
{
printf("Current State in
ToDispenser %u\r\n", QueryToDispenserSM()) ;
if(SetFieldSide()!=0) /*Tests to
see if FieldSide has been set in ToDispenseSM*/
{
printf(" field side
has been detected \r\n");
CurrentEvent=FIELD_SIDE_DETECTED;
return CurrentEvent;
}
}
/*Check to see if flash is detected*/
if(CheckForFlash()==YES)
{
CurrentEvent=FLASH_DETECTED;
return CurrentEvent;
}
/*Check for 50 percent beacon to be
detected*/
if(QueryGameStartSM()==ROTATE_TO_50)
{
if(CheckForFifty()==YES)
{
CurrentEvent=FIFTY_PC_DETECTED;
return CurrentEvent;
}
}
else
{
CurrentEvent=EV_NO_EVENT;
}
return
CurrentEvent;
}
#ifndef
FLASHMODULE_H_
#define
FLASHMODULE_H_
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: FlashModule.h
Description:
Flash Module header file
************************************************************************/
/*FlashModule.h*/
void
InitFlash(void);
void
DeInitFlash(void);
unsigned
char CheckForFlash(void);
#endif
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: FlashModule.c
Description:
Flash Module code
************************************************************************/
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
#include
"EventNames.h"
#include
"FlashModule.h"
/*Function
Prototypes
void
InitFlash(void);
void
DeInitFlash(void);
void
CheckForFlash(void);
*/
/*Module
Level Variables*/
static
unsigned char FlashDetected=NO;
/****************************************************************************
Function
InitFlash
Parameters
none
Returns
none
Description
This function initializes the ports for
flash detection
****************************************************************************/
void
InitFlash(void)
{
FlashDetected=NO;
TIM0_TIE |= _S12_C6I; // enable local
timer interrupt for PT2; tmr0 //channel
6 Flash Detect
//set to input capture by default
TIM0_TCTL3|=_S12_EDG6B; //set input capture to capture falling
edges
TIM0_TCTL3 &= ~ _S12_EDG6A;
TIM0_TFLG1=_S12_C6F; //clear interrupt
flag
printf("got inside InitFlash\r\n");
EnableInterrupts;
}
/****************************************************************************
Function
DeInitFlash
Parameters
none
Returns
none
Description
This function initializes the ports for
flash detection
****************************************************************************/
void
DeInitFlash(void)
{
TIM0_TFLG1=_S12_C6F; //clear interrupt
flag
FlashDetected=NO;
TIM0_TIE &= ~(_S12_C6I); //disable
local timer interrupt for PT2, tmr0 //channel
6 Flash Detect
}
/****************************************************************************
Interrupt
Response
FlashDetect
Parameters
none
Returns
none
Description
This ISR sets FlashDetected flag once the
input capture fires
****************************************************************************/
void
interrupt _Vec_tim0ch6 FlashDetectT2(void)
{
FlashDetected=YES;
TIM0_TFLG1=_S12_C6F; //clear interrupt
flag
}
/****************************************************************************
Function
CheckForFlash
Parameters
none
Returns
unsigned char YES or NO when flash has been
detected
Description
****************************************************************************/
unsigned
char CheckForFlash(void)
{
unsigned char Event = NO;
if(FlashDetected==YES)
{
Event = YES;
printf("flash! \r\n");
}
else
{
Event = NO;
}
return Event;
}
#ifndef
FRONTBEACON_H_
#define
FRONTBEACON_H_
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: FrontBeacon.h
Description:
FrontBeacon header file
************************************************************************/
/*Function
Prototypes*/
void
InitFrontBeacon(void);
void
DeInitFrontBeacon(void);
unsigned
char CheckForFifty(void);
unsigned
char CheckForNinety(void);
#endif
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: FrontBeacon.c
Description:
FrontBeacon Module code
************************************************************************/
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
#include
"EventNames.h"
#include
"FrontBeacon.h"
/*Function
Prototypes
void
InitFrontBeacon(void);
void
DeInitFrontBeacon(void);
*/
/*Definitions*/
#define
RISING_EDGE 1
#define
FALLING_EDGE 0
/*Module
Level Variables*/
static
unsigned int DC_Period = 0;
static
unsigned int HI_Time = 0;
static
unsigned char DutyCycle;
static
unsigned char Correct50Duty;
static
unsigned char Correct90Duty;
static
unsigned char Correct50DutyCount=0;
static
unsigned char Correct90DutyCount=0;
/****************************************************************************
Function
InitFrontBeacon
Parameters
none
Returns
none
Description
This function will initialize the necessary
Timer channel and interrupts
to be able to detect a front beacon.
Notes
****************************************************************************/
void
InitFrontBeacon(void)
{
TIM0_TIE |= _S12_C4I; // enable local timer interrupt for front
beacon
TIM0_TCTL3 |= (_S12_EDG4A |
_S12_EDG4B); // set to capture on both
//rising and falling edges
TIM0_TFLG1 = _S12_C4F; // clear interrupt flag for front beacon
EnableInterrupts;
}
/****************************************************************************
Function
DeInitFrontBeacon
Parameters
none
Returns
none
Description
This function turn off the input capture
for the beacon so we no longer
detect the 50% beacon unless re-initialized
Notes
****************************************************************************/
void
DeInitFrontBeacon(void)
{
TIM0_TFLG1 = _S12_C4F; // clear the flag
TIM0_TIE &= ~ _S12_C4I; // disable the
local timer interrupt for front beacon
}
/****************************************************************************
Function
IsFrontBeaconFifty(void)
Parameters
none
Returns
none
Description
This function returns Yes if front beacon
is 50% and No if front beacon is
not 50%.
Notes
****************************************************************************/
unsigned
char CheckForFifty(void)
{
if(Correct50DutyCount>75)
{
Correct50Duty=YES;
}
else
{
Correct50Duty=NO;
}
return Correct50Duty;
}
/****************************************************************************
Function
IsFrontBeaconNinety(void)
Parameters
none
Returns
none
Description
This function returns Yes if front beacon
is 90% and No if front beacon is
not 50%.
Notes
****************************************************************************/
unsigned
char CheckForNinety(void)
{
if(Correct90DutyCount>10)
{
Correct90Duty=YES;
}
else
{
Correct90Duty=NO;
}
return Correct90Duty;
}
/****************************************************************************
Function
CaptureFrontBeaconDuty
Parameters
none
Returns
none
Description
Interrupt response routine to an input
capture which triggers on any edge.
Calculates duty cycle after first 3 edges
have been captured
Notes
First 2 iterations do not calculate a valid
duty cycle.
****************************************************************************/
void
interrupt _Vec_tim0ch4 CaptureFrontBeaconDuty(void)
{
static unsigned int EdgeCount = 0;
unsigned int Current_LO = 0;
static unsigned int Last_LO = 0;
unsigned int Current_HI = 0;
static unsigned int Last_HI = 0;
if((PTT & BIT0HI) == RISING_EDGE) //
rising edge detected
{
Current_HI = TIM0_TC4;
DC_Period = Current_HI - Last_HI;
HI_Time = Last_LO - Last_HI;
Last_HI = Current_HI;
}
if((PTT & BIT0HI) == FALLING_EDGE) // falling edge detected
{
Current_LO
= TIM0_TC4;
DC_Period
= Current_LO - Last_LO;
HI_Time
= Current_LO - Last_HI;
Last_LO
= Current_LO;
}
if (DC_Period > 220)// if DC frequency
detected is greater than frequency //of
beacon
{
DC_Period = 0;
EdgeCount = 0;
}
if(HI_Time > DC_Period)// if long time
passes between last hi and last lo
{ //
or if long time passes between current lo and last hi
HI_Time = 0;
EdgeCount = 0;
}
if (EdgeCount < 3)// make sure we don't
get garbage data on the first 2 //edges
detected
{
DutyCycle
= 0;
EdgeCount
++;
}
else
{
//calculate
duty cycle,
DutyCycle
= ((unsigned int)HI_Time*100)/DC_Period;
}
if(DutyCycle > 100) // make sure it is a valid duty cycle
{
DutyCycle = 0;
}
if(DC_Period == 0) // no duty cycle if DC_Period is 0
{
DutyCycle = 0;
}
/*Check to see if Duty Cycle falls within
appropriate bounds for 50 percent*/
if ((DutyCycle > 45) &&
(DutyCycle < 55))
{
Correct50DutyCount++;
//CorrectDuty = YES;
} else
{
Correct50DutyCount=0;
//CorrectDuty = NO;
}
/*Check
to see if Duty Cycle falls within appropriate bounds for 30 percent*/
if (DutyCycle < 45)
{
Correct90DutyCount++;
//CorrectDuty = YES;
} else
{
Correct90DutyCount=0;
//CorrectDuty = NO;
}
TIM0_TFLG1 = _S12_C4F; // clear interrupt
flag
}
#ifndef
LEFT_TAPE_SENSOR_H_
#define
LEFT_TAPE_SENSOR_H_
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: LeftTapeSensor.h
Description:
Left Tape Sensor header file
************************************************************************/
/*Function
Prototypes*/
void
InitLeftTapeSensor(void);
unsigned
char LeftTapeOn(void);
#endif
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: LeftTapeSensor.c
Description:
Left Tape Sensor code
************************************************************************/
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
#include
"EventNames.h"
#include
"LeftTapeSensor.h"
#include
"RightTapeSensor.h"
/*Function
Prototypes
void
InitLeftTapeSensor(void);
void
LeftTapeOn(void);
*/
#define
LEFT_RISING_EDGE 1
#define
LEFT_FALLING_EDGE 0
/*Module
Level Variables*/
static
unsigned char LeftTapeDetected=NO;
/****************************************************************************
Function
InitLeftTapeSensor
Parameters
none
Returns
none
Description
This function initializes the ports for Left
Tape Sensor on tape detection
****************************************************************************/
void
InitLeftTapeSensor(void)
{
PTT=PTT&BIT4LO;//clear port
DDRT=DDRT&BIT4LO; //set port t4 as an
input for left tape sensor
LeftTapeDetected=NO;
}
/****************************************************************************
Function
LeftTapeOn
Parameters
none
Returns
Event YES or NO if Left Tape Sensor on tape is
detected
Description
****************************************************************************/
unsigned
char LeftTapeOn(void)
{
static unsigned char Event = NO;
printf("checking left \r\n");
LeftTapeDetected=NO;
if((PTT & BIT4HI)!= 0)//if the state is hi
{
LeftTapeDetected = NO;
}
if((PTT & BIT4HI) == 0)//if
the state is lo
{
printf("left tape
detected \r\n");
LeftTapeDetected = YES;
}
Event = LeftTapeDetected;
return Event;
}
#ifndef
CENTER_TAPE_SENSOR_H_
#define
CENTER_TAPE_SENSOR_H_
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: CenterTapeSensor.h
Description:
Center Tape Sensor header file
************************************************************************/
/*Function
Prototypes*/
void
InitCenterTapeSensor(void);
unsigned
char CenterTapeOn(void);
#endif
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: CenterTapeSensor.c
Description:
Center Tape Sensor code
************************************************************************/
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
#include
"EventNames.h"
#include
"CenterTapeSensor.h"
/*Function
Prototypes
void
InitCenterTapeSensor(void);
unsigned
char CenterTapeOn(void);
*/
/*Module
Level Variables*/
static
unsigned char CenterTapeDetected = NO;
/****************************************************************************
Function
InitCenterTapeSensor
Parameters
none
Returns
none
Description
This function initializes the ports for
Center Tape Sensor on tape detection
****************************************************************************/
void
InitCenterTapeSensor(void)
{
//Clear the port
PTT&=BIT5LO;
//Set T5 as an input for the center tape
sensor
DDRT=DDRT&BIT5LO;
CenterTapeDetected=NO;
}
/****************************************************************************
Function
CenterTapeOn
Parameters
none
Returns
Event YES or NO if Center Tape Sensor on
tape is detected
Description
****************************************************************************/
unsigned
char CenterTapeOn(void)
{
static unsigned char Event = NO;
CenterTapeDetected=NO;
if((PTT & BIT5HI)!= 0)//if state is lo
{
CenterTapeDetected = NO;
}
if((PTT & BIT5HI) == 0)//if
state is hi
{
CenterTapeDetected = YES;
}
Event = CenterTapeDetected;
return Event;
}
#ifndef
FRONTBEACON_H_
#define
FRONTBEACON_H_
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: RightTapeSensor.h
Description:
Right Tape Sensor header file
************************************************************************/
/*Function
Prototypes*/
void
InitRightTapeSensor(void);
unsigned
char RightTapeOn(void);
#endif
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: RightTapeSensor.c
Description:
Right Tape Sensor code
************************************************************************/
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
#include
"EventNames.h"
#include
"RightTapeSensor.h"
#include
"TimerModule.h"
#include
"LeftTapeSensor.h"
/*Function
Prototypes
void
InitRightTapeSensor(void);
unsigned
char RightTapeOn(void);
*/
#define
RIGHT_RISING_EDGE 1
#define
RIGHT_FALLING_EDGE 0
/*Module
Level Variables*/
static
unsigned char RightTapeSensorDetected=NO;
/****************************************************************************
Function
InitRightTapeSensor
Parameters
none
Returns
none
Description
This
function initializes the ports for right Tape Sensor on tape detection
****************************************************************************/
void
InitRightTapeSensor(void)
{
PTT=PTT&BIT6LO;//clear
port
DDRT=DDRT&BIT6LO;
//set port t6 as an input for right tape sensor
RightTapeSensorDetected = NO;
}
/****************************************************************************
Function
RightTapeOn
Parameters
none
Returns
unsigned char YES or NO when right tape
sensor has detected tape
Description
****************************************************************************/
unsigned
char RightTapeOn(void)
{
static unsigned char Event = NO;
printf("checking right\r\n");
RightTapeSensorDetected=NO;
if((PTT & BIT6HI)!= 0)//if state is lo
{
RightTapeSensorDetected =
NO;
}
if((PTT & BIT6HI) == 0)//if
state is hi
{
RightTapeSensorDetected =
YES;
printf("right tape
detected \r\n");
}
Event = RightTapeSensorDetected;
return Event;
}
#ifndef
FRONT_BUMP_SENSOR_H
#define
FRONT_BUMP_SENSOR_H
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: FrontBumpSensor.h
Description:
Front Bump Sensor header file
************************************************************************/
void
InitFrontBumpSensor(void);
#endif
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: FrontBumpSensor.c
Description:
Front Bump Sensor code
************************************************************************/
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
#include
"EventNames.h"
#include
"WallDetectorModule.h"
#include
"TimerModule.h"
#include
"FrontBumpSensor.h"
/*Function
Prototypes
void
InitWallDetectorSensor(void);
*/
/****************************************************************************
Function
InitWallDetectorSensor
Parameters
none
Returns
none
Description
This function initializes the ports for wall
detector Sensor
****************************************************************************/
void
InitFrontBumpSensor(void)
{
ADS12_Init("IOOOOOOO");
}
#ifndef
REAR_BEACON_MODULE_H_
#define
REAR_BEACON_MODULE_H_
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: RearBeaconModule.h
Description:
Rear beacon module header file
************************************************************************/
void
InitRearBeacon(void);
void
DeInitRearBeacon(void);
unsigned
char CheckForThirty(void);
#endif
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: RearBeaconModule.c
Description:
Rear beacon module code
************************************************************************/
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
#include
"EventNames.h"
#include
"MotorModule.h"
#include
"RearBeaconModule.h"
/*Function
Prototypes
void
InitRearBeacon(void);
void
DeInitRearBeacon(void);
unsigned
char CheckForThirty(void);
*/
/*Definitions*/
#define
RISING_EDGE 1
#define
FALLING_EDGE 0
/*Module
Level Variables*/
static
unsigned int DC_Period = 0;
static
unsigned int HI_Time = 0;
static
unsigned char DutyCycle;
static
unsigned char Correct30Duty;
static
unsigned char Correct30DutyCount=0;
/****************************************************************************
Function
InitRearBeacon
Parameters
none
Returns
none
Description
This function will initialize the necessary
Timer channel and interrupts
to be able to detect a front beacon.
Notes
****************************************************************************/
void
InitRearBeacon(void)
{
Correct30DutyCount=0;
Correct30Duty=NO;
printf("init rear beacon\r\n");
TIM0_TIE |= _S12_C5I; // enable local timer interrupt for front
beacon
TIM0_TCTL3 |= (_S12_EDG5A |
_S12_EDG5B); // set to capture on both
//rising and falling edges
TIM0_TFLG1 = _S12_C5F; // clear interrupt flag for front beacon
EnableInterrupts;
}
/****************************************************************************
Function
DeInitFrontBeacon
Parameters
none
Returns
none
Description
This function turn off the input capture
for the beacon so we no longer
detect the 50% beacon unless re-initialized
Notes
****************************************************************************/
void
DeInitRearBeacon(void)
{
Correct30DutyCount=0;
Correct30Duty=NO;
TIM0_TFLG1 = _S12_C5F; // clear the flag
TIM0_TIE &= ~ _S12_C5I; // disable the
local timer interrupt for front //beacon
}
/****************************************************************************
Function
CheckForThirty(void)
Parameters
none
Returns
none
Description
This function returns Yes if front beacon
is 50% and No if front beacon is
not 50%.
Notes
****************************************************************************/
unsigned
char CheckForThirty(void)
{
/*DEBUG
CorrectDuty=YES;
*/
//printf("inside check for 30
function\r\n");
if(Correct30DutyCount>10)
{
Correct30Duty=YES;
}
else
{
Correct30Duty=NO;
}
return Correct30Duty;
}
/****************************************************************************
Function
CaptureRearBeaconDuty
Parameters
none
Returns
none
Description
Interrupt response routine to an input
capture which triggers on any edge.
Calculates duty cycle after first 3 edges
have been captured
Notes
First 2 iterations do not calculate a valid
duty cycle.
****************************************************************************/
void
interrupt _Vec_tim0ch5 CaptureRearBeaconDuty(void)
{
static unsigned int EdgeCount = 0;
unsigned int Current_LO = 0;
static unsigned int Last_LO = 0;
unsigned int Current_HI = 0;
static unsigned int Last_HI = 0;
//printf("i\r\n");
if((PTT & BIT1HI) != 0) // rising
edge detected
{
Current_HI = TIM0_TC5;
DC_Period = Current_HI - Last_HI;
HI_Time = Last_LO - Last_HI;
Last_HI = Current_HI;
}
if((PTT & BIT1HI) == FALLING_EDGE) // falling edge detected
{
Current_LO
= TIM0_TC5;
DC_Period
= Current_LO - Last_LO;
HI_Time
= Current_LO - Last_HI;
Last_LO
= Current_LO;
}
if (DC_Period > 220)// if DC frequency
detected is greater than frequency //of
beacon
{
DC_Period = 0;
EdgeCount = 0;
}
if(HI_Time > DC_Period)// if long time
passes between last hi and last lo
{ //
or if long time passes between current lo and last hi
HI_Time = 0;
EdgeCount = 0;
}
if (EdgeCount < 3)// make sure we don't
get garbage data on the first 2 edges detected
{
DutyCycle
= 0;
EdgeCount
++;
}
else
{
//calculate
duty cycle,
DutyCycle
= ((unsigned int)HI_Time*100)/DC_Period;
}
if(DutyCycle > 100) // make sure it is a valid duty cycle
{
DutyCycle = 0;
}
if(DC_Period == 0) // no duty cycle if DC_Period is 0
{
DutyCycle = 0;
}
/*Check to see if Duty Cycle falls within
appropriate bounds for 30 percent*/
if ((DutyCycle <
37)&&(DutyCycle > 10))
{
Correct30DutyCount++;
} else
{
Correct30DutyCount=0;
}
TIM0_TFLG1 = _S12_C5F; // clear interrupt
flag
}
#ifndef
WALL_DETECTOR_MODULE_H_
#define
WALL_DETECTOR_MODULE_H_
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: WallDetectorModule.h
Description:
wall detector module header file
************************************************************************/
/*function
prototypes*/
void
InitWallDetectorSensor(void);
unsigned
char BlackWallDetected(void);
#endif
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: WallDetectorModule.c
Description:
wall detector module code
************************************************************************/
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
#include
"EventNames.h"
#include
"WallDetectorModule.h"
#include
"TimerModule.h"
/*Function
Prototypes
void
InitWallDetectorSensor(void);
unsigned
char BlackWallDetected(void);
*/
#define
DEBOUNCE_TIME 3000
/*Module
Level Variables*/
unsigned
char HitWall;
/****************************************************************************
Function
InitWallDetectorSensor
Parameters
none
Returns
none
Description
This function initializes the ports for ball
detector Sensor on tape detection
****************************************************************************/
void
InitWallDetectorSensor(void)
{
DDRP=DDRP&BIT2LO; //Set port P2 as an
input for the ball detector circuit
printf("DDRP input output status
%x\r\n", DDRP);
//PTP=PTP&BIT2LO;
HitWall=NO;
}
/****************************************************************************
Function
WallDetected
Parameters
none
Returns
none
Description
This function checks to see if a ball is in
the sorter
****************************************************************************/
unsigned
int WallDetected(void)
{
static unsigned int LastWallDetectTime=0;
unsigned int CurrentWallDetectTime;
if((PTP & BIT2HI)!=0)
{
CurrentWallDetectTime = TIM0_TCNT;
printf("Current Wall Detect
Time: %u\r\n", CurrentWallDetectTime);
printf("LastWallDetect Time :
%u\r\n", LastWallDetectTime);
if((CurrentWallDetectTime-LastWallDetectTime)>DEBOUNCE_TIME)
{
//printf("Ball
detected \r\n");
HitWall=YES;
}
else
{
HitWall=NO;
}
}
else
{
//printf("Ball NOT detected
\r\n");
HitWall=NO;
}
LastWallDetectTime=CurrentWallDetectTime;
return HitWall;
}
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: MotorModule.c
Description:
Motor Drive Module
************************************************************************/
#ifndef
MOTORMODULE_H_
#define
MOTORMODULE_H_
#define
FORWARD 1
#define
BACKWARD 0
#define
TICKS 150
#define
STOP 0
#define
CW 1
#define
CCW 0
#define
LEFT_MOTOR_STRAIGHT_DUTY 20 //THIS IS
THE RIGHT MOTOR (re-wired)
#define
RIGHT_MOTOR_STRAIGHT_DUTY 19//THIS IS THE LEFT MOTOR (re-wired)
#define
ROTATE_DUTY 12
void
InitializePWM(void);
void
SetMotors(unsigned char PWM_DCL, unsigned char PWM_DCR, unsigned char DirL,
unsigned char DirR);
void
DriveStraightForward(void);
void
StopDriving(void);
void
DifferentialDriveForward(unsigned char LeftDuty, unsigned char RightDuty);
void
Rotate(unsigned char Direction, unsigned char Duty);
#endif
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: MotorModule.c
Description:
Motor Drive Module
************************************************************************/
/*************************
#included libraries *************************/
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
#include
"MotorModule.h"
/************************************************************************
Function
InitializePWM()
Inputs:
None
Returns:
None
Description:
This function initializes necessary ports and settings to
use
the PWM subsystem of the E128.
************************************************************************/
void
InitializePWM(void)
{
//Initialize Port P4 and P5 as motor direction
outputs
// P4 is Left Motor Direction
// P5 is Right motor Direction
PTP &= (BIT4LO & BIT5LO); // clear
data in Port P bit 4 and 5
//Right motor direction control
DDRP|=BIT5HI;
//Left motor direction control
DDRP|=BIT4HI;
//Enable PWM on channel 2 and 3
// Channel 2 is Left motor PWM
//Channel 3 is Right motor PWM
PWME=PWME|(_S12_PWME2 | _S12_PWME3);
//Choose prescale setting (/16)
PWMPRCLK |= (_S12_PCKB2);
printf("prescale value %x\r\n",
PWMPRCLK);
//PWMPRCLK &= ~ (_S12_PCKA1);
//Choose to use scaled clock
PWMCLK = PWMCLK|(_S12_PCLK2|_S12_PCLK3);
printf("scaled clock selection
%x\r\n", PWMCLK);
//Set post scale clock scaler (/100)
/(50*2), 50 in binary = 110010
PWMSCLB = (BIT1HI|BIT4HI|BIT5HI);
printf("postscale value %x\r\n",
PWMSCLB);
// PWMSCLA &= ~(BIT0HI|BIT2HI|BIT3HI);
//Write PWM to port U2 (for left Motor) and
U3 (for right motor)
MODRR=MODRR|(_S12_MODRR2 | _S12_MODRR3);
//Choose period (set to 100 Hz)
PWMPER2=150; // 150 ticks per period on left motor
PWMPER3=150; // 150 ticks per period on
right motor
printf("Init PWM\r\n");
}
/************************************************************************
Function
SetMotors
Inputs:
unsigned char PWM_DCL, unsigned char PWM_DCR
Returns:
None
Description:
This function sets the left and right motors with the
assigned
duty cycle.
************************************************************************/
void
SetMotors(unsigned char PWM_DCL, unsigned char PWM_DCR, unsigned char DirL,
unsigned char DirR)
{
// using U2 for left motor
PWMDTY2 = (((unsigned
int)PWM_DCL*TICKS)/100);
//left motor direction
if(DirL==FORWARD)
{
PWMPOL &= ~_S12_PPOL2; // set active LO
PTP|=BIT4HI;
}
else
{
PWMPOL |= _S12_PPOL2; // set active HI
PTP&=BIT4LO;
}
//using U3 for right motor
PWMDTY3 = (((unsigned
int)PWM_DCR*TICKS)/100);
//right motor direction
if(DirR==FORWARD)
{
PWMPOL = PWMPOL &
~(_S12_PPOL3); // set active LO
PTP|=BIT5HI;
}
else
{
PWMPOL |= _S12_PPOL3; // set active HI
PTP&=BIT5LO;
}
}
/**********************
Second Level Drive Functions *******************/
/************************************************************************
Function
DriveStraightForward()
Inputs:
None
Returns:
None
Description:
Drives the motors at individual duty cycles that correspond
to straight line motion. requires call to InitializePWM();
************************************************************************/
void
DriveStraightForward(void)
{
SetMotors(LEFT_MOTOR_STRAIGHT_DUTY,
RIGHT_MOTOR_STRAIGHT_DUTY, FORWARD, FORWARD);
/*need to tune these #defines with
the actual robot loaded with an
approximation of the actual weight*/
}
/************************************************************************
Function
StopDriving()
Inputs:
None
Returns:
None
Description:
Stops both motors so that the robot stops moving
************************************************************************/
void
StopDriving(void)
{
SetMotors(STOP, STOP, FORWARD,
FORWARD);
/*need to tune these #defines with
the actual robot loaded with an
approximation of the actual weight*/
}
/************************************************************************
Function
DifferentialDriveForward(unsigned char LeftDuty, unsigned char RightDuty)
Inputs:
left and right duty cycles (1-100)
Returns:
None
Description:
Drives the motors at individual duty cycles in forward motion.
requires call to InitializePWM();
************************************************************************/
void
DifferentialDriveForward(unsigned char LeftDuty, unsigned char RightDuty)
{
SetMotors(LeftDuty, RightDuty,
FORWARD, FORWARD);
}
/************************************************************************
Function
Rotate(unsigned char Direction, unsigned char Duty)
Inputs:
Direction (CW or CCW), and Duty
Returns:
None
Description:
Rotates around the center axis by driving one motor in each direction
requires call to InitializePWM();
************************************************************************/
void
Rotate(unsigned char Direction, unsigned char Duty)
{
if (Direction == CW){
SetMotors(Duty, Duty, FORWARD, BACKWARD);
}
if (Direction == CCW){
SetMotors(Duty, Duty, BACKWARD, FORWARD);
}
}
#ifndef
TIMERMODULE_H_
#define
TIMERMODULE_H_
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: TimerModule.h
Description:
Timer Module header file
************************************************************************/
/*Timer
Channel and Overflow Init Routines*/
void
InitTimerOverflow(void);
void
InitTimer1(void);
/*Timer
Init Routines*/
void
InitGameTimer(void);
void
InitRotateTimer(void);
void
DeInitRotateTimer(void);
void
InitPushTimer(void);
void
InitRetractTimer(void);
void
InitInBetweenBallSetTimer(void);
void
InitDumpTimer(void);
/*Timer
DeInit Routines*/
void
DeInitPushTimer(void);
void
DeInitRetractTimer(void);
void
DeInitInBetweenBallSetTimer(void);
void
DeInitDumpTimer(void);
/*Timer
Expired Check Routines*/
unsigned
char GameOver(void);
unsigned
char BlindRotateTimerExpired(void);
unsigned
char PushTimerExpired(void);
unsigned
char RetractTimerExpired(void);
unsigned
char InBetweenBallSetTimerExpired(void);
unsigned
int TotalTOF(void);
unsigned
char SensorCheckTimerExpired(void);
unsigned
char DumpTimerExpired(void);
#endif
/************************************************************************
ME218B
Project
Date:
Winter Quarter 2008
Christine
Tower, Nathan Parkhill, Stephanie Lue
File
Name: TimerModule.c
Description:
Timer Module code
************************************************************************/
#include
<stdio.h>
#include
<timers12.h>
#include
<hidef.h>
#include
<mc9s12e128.h>
#include
<S12E128bits.h>
#include
<bitdefs.h>
#include
<string.h>
#include
"ADS12.h"
#include
"S12evec.h"
#include
"EventNames.h"
/*Function
Prototypes
void
InitTimerOverflow(void);
void
InitTimer1(void)
void
InitGameTimer(void);
void
InitRotateTimer(void);
void
DeInitRotateTimer(void);
void
InitPushTimer(void);
void
InitRetractTimer(void);
void
InitInBetweenBallSetTimer(void);
void
DeInitPushTimer(void);
void
DeInitRetractTimer(void);
void
DeInitInBetweenBallSetTimer(void);
unsigned
char GameOver(void);
unsigned
char BlindRotateTimerExpired(void);
unsigned
char PushTimerExpired(void);
unsigned
char RetractTimerExpired(void);
unsigned
char InBetweenBallSetTimerExpired(void);
unsigned
char TotalTOF(void);
*/
/*Definitions*/
#define
GAMEOVER_OVERFLOWS 350
#define
BLIND_ROTATE_OVERFLOWS 3
#define
PUSH_TIMER_OVERFLOWS 7
#define
RETRACT_TIMER_OVERFLOWS 2
#define
IN_BETWEEN_BALL_SET_TIMER_OVERFLOWS 9
#define
DUMP_TIMER_OVERFLOWS 7
/*Module
Level Variables*/
static
unsigned char GameTimeFlag;
static
unsigned char GameOverflowCount;
static
unsigned char BlindRotateOverflowCount;
static
unsigned char BlindRotateFlag;
static
unsigned char PushTimerFlag;
static
unsigned char PushTimerOverflowCount;
static
unsigned char RetractTimerFlag;
static
unsigned char RetractTimerOverflowCount;
static
unsigned char InBetweenBallSetTimerFlag;
static
unsigned char InBetweenBallSetTimerOverflowCount;
static
unsigned char DumpTimerFlag;
static
unsigned char DumpTimerOverflowCount;
static
unsigned char TotalOverFlows;
static
unsigned char Sensor_Check;
/****************************************************************************
Timer Channel and Overflow
Init Routines
****************************************************************************/
/****************************************************************************
Function
InitTimerOverflow
Parameters
none
Returns
none
Description
This function initializes the flag for timer
overflows and sets up timer 0
Notes
****************************************************************************/
void
InitTimerOverflow(void)
{
TIM0_TSCR1 = _S12_TEN;
// timer 0 enable for game clock
TIM0_TSCR2 = (_S12_PR2 |
_S12_PR1|_S12_PR0); // pre scale clock for tim0 //to /128. THIS CAN CHANGE
//
overflow period of 349 ms
TIM0_TSCR2 |= _S12_TOI; // enable local interrupt for overflow flag
TIM0_TFLG2 = _S12_TOF; // clear timer overflow flag
EnableInterrupts;
}
/****************************************************************************
Function
InitTimer1
Parameters
none
Returns
none
Description
This function initializes timer 1 for use as
a check timer for the tape sensors
****************************************************************************/
void
InitTimer1(void)
{
TIM1_TSCR1 = _S12_TEN; // enable timer 1
(this is enabled here because //this //is the first time using timer 1)
TIM1_TSCR2 = (_S12_PR2|_S12_PR0); // pre
scale clock for tim1 to /16. enabled //here because first time using timer 1
TIM1_TSCR2 &= ~(_S12_PR1);
TIM1_TSCR2 |= _S12_TOI; // enable local interrupt for overflow flag
TIM1_TFLG2
= _S12_TOF; // clear timer overflow
flag
Sensor_Check=NO;
EnableInterrupts;
}
/****************************************************************************
Timer Init
Routines
****************************************************************************/
/****************************************************************************
Function
InitGameTimer
Parameters
none
Returns
none
Description
This function sets the flag so that GameTime
overflows start incrementing
Notes
****************************************************************************/
void
InitGameTimer(void)
{
GameTimeFlag=YES;
}
/****************************************************************************
Function
InitRotateTimer
Parameters
none
Returns
none
Description
This function sets the flag so that
BlindRotateTime overflows start incrementing
Notes
****************************************************************************/
void
InitRotateTimer(void)
{
BlindRotateFlag=YES;
}
/****************************************************************************
Function
InitPushTimer
Parameters
none
Returns
none
Description
This function sets the flag so that push
timer overflows start incrementing
Notes
****************************************************************************/
void
InitPushTimer(void)
{
PushTimerOverflowCount=0;
PushTimerFlag=YES;
}
/****************************************************************************
Function
InitRetractTimer
Parameters
none
Returns
none
Description
This function sets the flag so that retract timer
overflows start incrementing
Notes
****************************************************************************/
void
InitRetractTimer(void)
{
RetractTimerOverflowCount=0;
RetractTimerFlag=YES;
}
/****************************************************************************
Function
InitInBetweenBallSetTimer
Parameters
none
Returns
none
Description
This function sets the flag so that in
between ball set timer overflows start incrementing
Notes
****************************************************************************/
void
InitInBetweenBallSetTimer(void)
{
InBetweenBallSetTimerOverflowCount=0;
InBetweenBallSetTimerFlag=YES;
}
/****************************************************************************
Function
InitDumpTimer
Parameters
none
Returns
none
Description
This function sets the flag so that in
between ball set timer overflows start incrementing
Notes
****************************************************************************/
void
InitDumpTimer(void)
{
DumpTimerOverflowCount=0;
DumpTimerFlag=YES;
}
/****************************************************************************
Timer DeInit
Routines
****************************************************************************/
/****************************************************************************
Function
DeInitRotateTimer
Parameters
none
Returns
none
Description
This function sets the flag so that
BlindRotateTime overflows will no longer increment
****************************************************************************/
void
DeInitRotateTimer(void)
{
BlindRotateFlag=NO;
BlindRotateOverflowCount=0;
}
/****************************************************************************
Function
DeInitPushTimer
Parameters
none
Returns
none
Description
This function sets the flag so that push
timer overflows will no longer increment
****************************************************************************/
void
DeInitPushTimer(void)
{
PushTimerOverflowCount=0;
PushTimerFlag=NO;
}
/****************************************************************************
Function
DeInitRetractTimer
Parameters
none
Returns
none
Description
This function sets the flag so that push
timer overflows will no longer increment
****************************************************************************/
void
DeInitRetractTimer(void)
{
RetractTimerOverflowCount=0;
RetractTimerFlag=NO;
}
/****************************************************************************
Function
DeInitRetractTimer
Parameters
none
Returns
none
Description
This function sets the flag so that push
timer overflows will no longer increment
****************************************************************************/
void
DeInitInBetweenBallSetTimer(void)
{
InBetweenBallSetTimerOverflowCount=0;
InBetweenBallSetTimerFlag=NO;
}
/****************************************************************************
Function
DeInitRetractTimer
Parameters
none
Returns
none
Description
This function sets the flag so that push timer
overflows will no longer increment
****************************************************************************/
void
DeInitDumpTimer(void)
{
DumpTimerOverflowCount=0;
DumpTimerFlag=NO;
}
/****************************************************************************
Timer Overflow Interrupt
Response Routine
****************************************************************************/
/****************************************************************************
Interrupt
Response
TrackTimer0Overflow
Parameters
none
Returns
none
Description
This ISR keeps track of specific timer
overflows
****************************************************************************/
void
interrupt _Vec_tim0ovf TrackTimer0Overflow(void)
{
static unsigned int GameTimerOverflows=0;
static unsigned int
BlindRotateTimerOverflows=0;
/*Check to see if InitGameTimer() has
been called*/
if(GameTimeFlag==YES)
{
GameTimerOverflows++;
}
/*Check to see if InitBlindRotate() has
been called*/
if(BlindRotateFlag==YES)
{
BlindRotateOverflowCount++;
}
/*Check to see if InitPushTimer() has
been called*/
if(PushTimerFlag==YES)
{
PushTimerOverflowCount++;
printf("Incrementing push timer
counter, value is %u\r\n", PushTimerOverflowCount);
}
/*Check to see if InitRetractTimer() has
been called*/
if(RetractTimerFlag==YES)
{
RetractTimerOverflowCount++;
}
/*Check to see if
InitInBetweenBallSetTimer() has been called*/
if(InBetweenBallSetTimerFlag==YES)
{
InBetweenBallSetTimerOverflowCount++;
}
/*Check to see if InitDumpTimer() has
been called*/
if(DumpTimerFlag==YES)
{
DumpTimerOverflowCount++;
}
GameOverflowCount=GameTimerOverflows;
//BlindRotateOverflowCount=BlindRotateTimerOverflows;
TIM0_TFLG2 = _S12_TOF; // clear timer overflow flag
}
/****************************************************************************
Interrupt Service Routine
SensorTOF
Parameters
none
Returns
none
Description
This function sets the flag so that Timer1
overflows start incrementing
and the output compare begins for use in
tape sensor checking
****************************************************************************/
void
interrupt _Vec_tim1ovf SensorTOF(void)
{
Sensor_Check=YES;
TIM1_TFLG2 = _S12_TOF; // clear timer overflow flag
}
/****************************************************************************
Timer Expired Check
Routines
****************************************************************************/
/****************************************************************************
Function
GameOver
Parameters
none
Returns
unsigned char YES or NO if GameOverflowCount
has reached the max amount
Description
****************************************************************************/
unsigned
char GameOver(void)
{
unsigned char Event = NO;
if(GameOverflowCount >=
GAMEOVER_OVERFLOWS)
{
Event = YES;
//printf("Game
Over\r\n");
}
else
{
Event = NO;
//printf("Game not yet
over\r\n");
}
return Event;
}
/****************************************************************************
Function
BlindRotateTimerExpired
Parameters
none
Returns
unsigned char YES or NO if
BlindRotateOverflowCount has reached the max amount
Description
****************************************************************************/
unsigned
char BlindRotateTimerExpired(void)
{
unsigned char Event = NO;
if(BlindRotateOverflowCount >=
BLIND_ROTATE_OVERFLOWS)
{
Event = YES;
DeInitRotateTimer();
printf("Blind Rotate
Over\r\n");
}
else
{
Event = NO;
}
return Event;
}
/****************************************************************************
Function
PushTimerExpired
Parameters
none
Returns
unsigned char YES or NO if
PushTimerOverflowCount has reached the max amount
Description
****************************************************************************/
unsigned
char PushTimerExpired(void)
{
unsigned char Event = NO;
if(PushTimerOverflowCount >=
PUSH_TIMER_OVERFLOWS)
{
Event = YES;
DeInitPushTimer();
printf("Push
Over\r\n");
}
else
{
Event = NO;
}
return Event;
}
/****************************************************************************
Function
RetractTimerExpired
Parameters
none
Returns
unsigned char YES or NO if
RetractTimerOverflowCount has reached the max amount
Description
****************************************************************************/
unsigned
char RetractTimerExpired(void)
{
unsigned char Event = NO;
if(RetractTimerOverflowCount >=
RETRACT_TIMER_OVERFLOWS)
{
Event = YES;
DeInitRetractTimer();
//printf("Push Over\r\n");
}
else
{
Event = NO;
}
return Event;
}
/****************************************************************************
Function
InBetweenBallSetTimerExpired
Parameters
none
Returns
unsigned char YES or NO if InBetweenBallSetTimerOverflowCount
has reached the max amount
Description
****************************************************************************/
unsigned
char InBetweenBallSetTimerExpired(void)
{
unsigned char Event = NO;
if(InBetweenBallSetTimerOverflowCount
>= IN_BETWEEN_BALL_SET_TIMER_OVERFLOWS)
{
Event = YES;
DeInitInBetweenBallSetTimer();
//printf("Push
Over\r\n");
}
else
{
Event = NO;
}
return Event;
}
/****************************************************************************
Function
TotalTOF
Parameters
none
Returns
unsigned int value for timer overflows
Description
****************************************************************************/
unsigned
int TotalTOF(void)
{
TotalOverFlows=GameOverflowCount;
return TotalOverFlows;
}
/****************************************************************************
Function
SensorCheckTimerExpired
Parameters
none
Returns
unsigned int value for timer overflows
Description
****************************************************************************/
unsigned
char SensorCheckTimerExpired(void)
{
unsigned char
Sensor_Check_Timer_Expired=NO;
if(Sensor_Check==YES)
{
Sensor_Check_Timer_Expired=YES;
}
else
{
Sensor_Check_Timer_Expired=NO;
}
Sensor_Check=NO;
return Sensor_Check_Timer_Expired;
}
/****************************************************************************
Function
DumpTimerExpired
Parameters
none
Returns
unsigned int value for timer overflows
Description
****************************************************************************/
unsigned
char DumpTimerExpired(void)
{
unsigned char Event = NO;
printf("dump timer overflow
is:%u\r\n",DumpTimerOverflowCount);
if(DumpTimerOverflowCount >=
DUMP_TIMER_OVERFLOWS)
{
Event = YES;
DeInitDumpTimer();
//printf("Push
Over\r\n");
}
else
{
Event = NO;
}
return Event;
}