/**************************************************************************** Module TemplateService.c Revision 1.0.1 Description This is a template file for implementing a simple service under the Gen2 Events and Services Framework. Notes History When Who What/Why -------------- --- -------- 01/16/12 09:58 jec began conversion from TemplateFSM.c ****************************************************************************/ /*----------------------------- 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 "TapeDebounce.h" #include "ADS12.h" #include "TopHSM.h" #include "MotorService.h" #define DebounceTime 50 /*----------------------------- Module Defines ----------------------------*/ #define ADCENTRAL 1 #define ADLATERAL 0 /*---------------------------- Module Functions ---------------------------*/ /* prototypes for private functions for this service.They should be functions relevant to the behavior of this service */ /*---------------------------- Module Variables ---------------------------*/ // with the introduction of Gen2, we need a module level Priority variable static uint8_t MyPriority; static bool LatMessageSent; static bool CenMessageSent; /*------------------------------ Module Code ------------------------------*/ /**************************************************************************** Function InitTemplateService Parameters uint8_t : the priorty of this service Returns bool, false if error in initialization, true otherwise Description Saves away the priority, and does any other required initialization for this service Notes Author J. Edward Carryer, 01/16/12, 10:00 ****************************************************************************/ bool InitTapeDebounce( uint8_t Priority ) { ES_Event ThisEvent; /*ES_Timer_InitTimer(CENTRAL_DEBOUNCE_TIMER, DebounceTime); ES_Timer_InitTimer(LATERAL_DEBOUNCE_TIMER, DebounceTime); LateralState = DEBOUNCINGLATERAL; CentralState = DEBOUNCINGCENTRAL; */ MyPriority = Priority; CenMessageSent = false; LatMessageSent = false; /******************************************** in here you write your initialization code *******************************************/ // post the initial transition event ThisEvent.EventType = ES_INIT; if (ES_PostToService( MyPriority, ThisEvent) == true) { return true; }else { return false; } } /**************************************************************************** Function PostTemplateService Parameters EF_Event ThisEvent ,the event to post to the queue Returns bool false if the Enqueue operation failed, true otherwise Description Posts an event to this state machine's queue Notes Author J. Edward Carryer, 10/23/11, 19:25 ****************************************************************************/ bool PostTapeDebounce( ES_Event ThisEvent ) { return ES_PostToService( MyPriority, ThisEvent); } /**************************************************************************** Function RunTemplateService Parameters ES_Event : the event to process Returns ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise Description add your description here Notes Author J. Edward Carryer, 01/15/12, 15:23 ****************************************************************************/ ES_Event RunTapeDebounce( ES_Event ThisEvent ) { ES_Event ReturnEvent; ReturnEvent.EventType = ES_NO_EVENT; // assume no errors /* if ((ThisEvent.EventType==ES_TIMEOUT) && (ThisEvent.EventParam==CENTRAL_DEBOUNCE_TIMER)) { if (CentralState == DEBOUNCINGCENTRAL) { CentralState = READY2SAMPLECENTRAL; ES_Timer_InitTimer(CENTRAL_DEBOUNCE_TIMER, DebounceTime); } else if (CentralState ==READY2SAMPLECENTRAL) { CentralState = WAITINGCENTRAL; } } else if((ThisEvent.EventType==ES_TIMEOUT) && (ThisEvent.EventParam== LATERAL_DEBOUNCE_TIMER) ) { if (LateralState == DEBOUNCINGLATERAL) { LateralState = READY2SAMPLELATERAL; ES_Timer_InitTimer(LATERAL_DEBOUNCE_TIMER, DebounceTime); } else if (LateralState ==READY2SAMPLELATERAL) { LateralState = WAITINGLATERAL; } } */ return ReturnEvent; } bool CentralTapeQuery(void) { static short ADValue; static uint8_t CurrentPinState; ES_Event ThisEvent; static uint8_t LastPinState = 0; bool ReturnVal = false; ADValue = ADS12_ReadADPin(ADCENTRAL); CurrentPinState = (ADValue > blackMin); if ((CurrentPinState == true) && (CurrentPinState != LastPinState) ) { ThisEvent.EventType = ES_CENTRAL_TAPE; ThisEvent.EventParam = ADValue; // converts ADValue to 0-255 range PostMasterSM(ThisEvent); ReturnVal = true; //printf("Central Tape\n"); } LastPinState = CurrentPinState; // update the state for next time return ReturnVal; } /*static short ADValue; static bool CurrentPinState; ES_Event ThisEvent; bool ReturnVal = false; ADValue = ADS12_ReadADPin(ADCENTRAL); CurrentPinState = (ADValue > blackMin); if ((CurrentPinState == false) && (CenMessageSent == true) ) { CenMessageSent = false; } else if ((CentralState == WAITINGCENTRAL) && (CurrentPinState == true) && (CenMessageSent == false)) { printf("InitialRead"); CentralState = DEBOUNCINGCENTRAL; ES_Timer_InitTimer(CENTRAL_DEBOUNCE_TIMER, DebounceTime); } else if ( (CentralState == READY2SAMPLECENTRAL) && (CenMessageSent == false)) { ThisEvent.EventType = ES_CENTRAL_TAPE; ThisEvent.EventParam = ADValue; // converts ADValue to 0-255 range PostMasterSM(ThisEvent); ReturnVal = true; CenMessageSent = true; } return ReturnVal; */ bool LateralTapeQuery(void) { static short ADValue; static uint8_t CurrentPinState; ES_Event ThisEvent; static uint8_t LastPinState = 0; bool ReturnVal = false; ADValue = ADS12_ReadADPin(ADLATERAL); //printf("%d\n", ADValue); CurrentPinState = (ADValue > blackMin); if ((CurrentPinState == true) && (CurrentPinState != LastPinState)) { ThisEvent.EventType = ES_LATERAL_TAPE; ThisEvent.EventParam = ADValue; // converts ADValue to 0-255 range PostMasterSM(ThisEvent); ReturnVal = true; //printf("Lateral Tape\n"); } LastPinState = CurrentPinState; // update the state for next time return ReturnVal; } /*static short ADValue; static bool CurrentPinState; ES_Event ThisEvent; bool ReturnVal = false; ADValue = ADS12_ReadADPin(ADLATERAL); CurrentPinState = (ADValue > blackMin); if ((CurrentPinState == false) && (LatMessageSent == true) ) { LatMessageSent = false; } else if ((LateralState == WAITINGLATERAL) && (CurrentPinState == true) && (LatMessageSent == false)) { LateralState = DEBOUNCINGLATERAL; ES_Timer_InitTimer(LATERAL_DEBOUNCE_TIMER, DebounceTime); } else if ( (LateralState == READY2SAMPLELATERAL) && (LatMessageSent == false)) { ThisEvent.EventType = ES_LATERAL_TAPE; ThisEvent.EventParam = ADValue; // converts ADValue to 0-255 range //puts("Lateral tapedetected!"); PostMasterSM(ThisEvent); ReturnVal = true; LatMessageSent = true; } return ReturnVal; */ /*************************************************************************** private functions ***************************************************************************/ /*------------------------------- Footnotes -------------------------------*/ /*------------------------------ End of file ------------------------------*/