Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 4

/****************************************************************************

Module
MasterMachine.c
Description
Top level state machine for robot project
****************************************************************************/
/*----------------------------- 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 "ES_Configure.h"
#include "ES_Framework.h"
#include "DRS.h"
#include "MasterMachine.h"
#include "ActionMachine.h"
#include "DriveMotorService.h"
#include "InputCapture.h"
#include "TargetTracking.h"
#include "BallShooting.h"
#include "IRSensor.h"
#include "SendCommand.h"
#include "LED.h"
#include
#include
#include
#include
#include
#include
#include
#include

"inc/hw_memmap.h"
"inc/hw_gpio.h"
"inc/hw_ssi.h"
"inc/hw_sysctl.h"
"inc/hw_types.h"
"inc/hw_nvic.h"
"bitdefs.h"
"ADMulti.h"

/*----------------------------- Module Defines ----------------------------*/


#define BitsPerNibble 4
/*---------------------------- Module Functions ---------------------------*/
/*---------------------------- Module Variables ---------------------------*/
// with the introduction of Gen2, we need a module level Priority var as well
static uint8_t MyPriority;
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
InitMasterSM
Parameters
uint8_t : the priorty of this service
Returns
boolean, False if error in initialization, True otherwise
Description
****************************************************************************/
bool InitMaster( uint8_t Priority )
{
ES_Event ThisEvent;
MyPriority = Priority;

// save our priority

ThisEvent.EventType = ES_ENTRY;
MyPriority = Priority;
//Initialize hardware
InitDriveMotorService();
InitTargetTrackingControl();
InitDRS();
InitBallShooting();
InitLED();
ADC_MultiInit(2);
//Read value from potentiometer
uint32_t PotReading[2];
ADC_MultiRead(PotReading);
uint8_t n = 0;
//Kart 1
if(PotReading[1] > 2400) {
n = 1;
}
//kart 2
else if(PotReading[1] > 1300) {
n = 2;
}
//kart 3
else {
n = 3;
}
printf("Kart number is: %d\r\n",n);
//write kart number
SetKartNumber(n);
// start state machine
StartMaster( ThisEvent );
printf("InitMaster Complete\r\n");
return true;
}
/****************************************************************************
Function
PostMasterSM
Parameters
ES_Event ThisEvent , the event to post to the queue
Returns
boolean False if the post operation failed, True otherwise
Description
Posts an event to this state machine's queue
****************************************************************************/
bool PostMaster( ES_Event ThisEvent )
{
return ES_PostToService( MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunMasterSM
Parameters

ES_Event: the event to process


Returns
ES_Event: an event to return
Description
the run function for the top level state machine
****************************************************************************/
ES_Event RunMaster( ES_Event CurrentEvent )
{
ES_Event ReturnEvent = { ES_NO_EVENT, 0 }; // assume no error
//
//
//
//

If current state is state one


Execute During function for state one. ES_ENTRY & ES_EXIT are
processed here allow the lowere level state machines to re-map
or consume the event

CurrentEvent = DuringMaster(CurrentEvent);
// No events to process
// in the absence of an error the top level state machine should
// always return ES_NO_EVENT, which we initialized at the top of func
return(ReturnEvent);
}
/****************************************************************************
Function
StartMasterSM
Parameters
ES_Event CurrentEvent
Returns
nothing
Description
Does any required initialization for this state machine
Notes
Author
J. Edward Carryer, 02/06/12, 22:15
****************************************************************************/
void StartMaster( ES_Event CurrentEvent )
{
//call run master
RunMaster(CurrentEvent);
return;
}
/***************************************************************************
private functions
***************************************************************************/
static ES_Event DuringMaster( ES_Event Event)
{
ES_Event ReturnEvent = Event; // assme no re-mapping or comsumption
// process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
if ( (Event.EventType == ES_ENTRY) ||
(Event.EventType == ES_ENTRY_HISTORY) )
{
// implement any entry actions required for this state machine

printf("HIGHEST SM STATE: Master\r\n");


// after that start any lower level machines that run in this state
// repeat the StartxxxSM() functions for concurrent state machines
// on the lower level
StartAction(Event);
StartDRS(Event);
}
else if ( Event.EventType == ES_EXIT )
{
// on exit, give the lower levels a chance to clean up first
RunAction(Event);
RunDRS(Event);
// repeat for any concurrently running state machines
// now do any local exit functionality

}else
// do the 'during' function for this state
{
// run any lower level state machine
ReturnEvent = RunAction(Event);
ReturnEvent = RunDRS(Event);
}
// return either Event, if you don't want to allow the lower level machine
// to remap the current event, or ReturnEvent if you do want to allow it.
return(ReturnEvent);

You might also like