1 /****************************************************************************
  2  Header file for IR detector control software
  3  ****************************************************************************/
  4 
  5 #ifndef IR_DetectorControl_H
  6 #define IR_DetectorControl_H
  7 
  8 /****************************************************************************/
  9 
 10 // General
 11 void InitIR_DetectorControl( void );
 12 bool Check4IR_Pulse( void );
 13 unsigned char isBeaconDetected( void );
 14 
 15 typedef enum {
 16 	NO_BEACON = 0,
 17 	GOAL_BEACON,
 18 	OPPONENT_BEACON
 19 	} Beacon_t;
 20 
 21 #endif /* IR_DetectorControl_H */

  1 /****************************************************************************
  2  Module
  3    IR_DetectorControl.c
  4 
  5  Description
  6    This service provides the init function for the IR detector subsystem.
  7 
  8  Notes
  9 
 10  History
 11  When           Who     What/Why
 12  -------------- ---     --------
 13  2/24/14        whg      created for testing with empty methods
 14 ****************************************************************************/
 15 /*----------------------------- Include Files -----------------------------*/
 16 #include "ES_Configure.h"
 17 #include "ES_Framework.h"
 18 
 19 #include "IR_DetectorControl.h"		/* My header */
 20 #include "PinAndPortAssignments.h" 	/* For pin define names */
 21 	
 22 #include <s12evec.h>      			/* E128 interrupt vectors */
 23 #include "GameMasterHSM.h"			/* for Post to HSM */
 24 
 25 
 26 
 27 /*----------------------------- Module Defines ----------------------------*/
 28 /* defines for the IR Detector subsystem */
 29 #define GOAL_PERIOD 		480
 30 #define OPPONENT_PERIOD		800
 31 #define PERIOD_MARGIN		20
 32 #define CLOCK_SCALAR		64	
 33 
 34 /*---------------------------- Module Functions ---------------------------*/
 35 
 36 
 37 /*---------------------------- Module Variables ---------------------------*/
 38 /* Module variables for IR Detector subsystem */
 39 static unsigned int IR_Period_us = 0;
 40 static unsigned long uPeriod = 0;
 41 static unsigned int uOverFlows = 0;
 42 static unsigned long uLastTime = 0;
 43 static unsigned int Last_IR_Period_us = 0;
 44 
 45 /* Module variables for event tracking */
 46 static unsigned char BeaconStatus = NO_BEACON;
 47 static unsigned char LastBeaconStatus = NO_BEACON;
 48 
 49 
 50 /*------------------------------ Module Code ------------------------------*/
 51 /****************************************************************************
 52  Function
 53      InitShooterControl
 54 
 55  Parameters
 56     --
 57 
 58  Returns
 59      bool, false if error in initialization, true otherwise
 60 
 61  Description
 62      Inits pins/hardware necessary to run the IR detector subsystem
 63  Notes
 64 
 65  Author
 66      J. Edward Carryer, 01/16/12, 10:00
 67 ****************************************************************************/
 68 void InitIR_DetectorControl ( void )
 69 {
 70   // Set timer overflow interrupt and flag
 71   TIM1_TSCR2 |=  _S12_TOI;               // Enable timer overflow interrupt
 72   TIM1_TFLG2 =   _S12_TOF;               // Clear the timer overflow flag
 73 
 74   // Set up IR channel T5 for input capture
 75   // Channel 5 corresponds to T5 for TIM1 
 76   TIM1_TIOS =  ~_S12_IOS5;               // Set T5 as input capture  
 77   TIM1_TCTL3 = _S12_EDG5A;               // Set T5 to capture rising edges 
 78   TIM1_TFLG1 = _S12_C5F;                 // Clear the T5 flag
 79   TIM1_TIE =   _S12_C5I;                 // Set interrupt for channel 5
 80 
 81   // Enable interrupts
 82   // EnableInterrupts; - handled in general Init
 83   //puts("IR Detector System Initialized \r");    
 84 }
 85 
 86 /****************************************************************************
 87  Function
 88     isBeaconDetected
 89 
 90  Parameters
 91    --
 92 
 93  Returns
 94    Whether or not robot sees a beacon and which beacon
 95 
 96  Description
 97    Returns status of beacon sensor
 98 
 99 ****************************************************************************/
100 unsigned char isBeaconDetected( void )
101 {
102 	return true;// BeaconStatus;
103 }
104 
105 /****************************************************************************
106  Function
107     Check4IR_Pulse
108 
109  Parameters
110    --
111 
112  Returns
113    Returns true if event seen
114 
115  Description
116    Event checker that looks for change in beacon status
117 
118 ****************************************************************************/
119 bool Check4IR_Pulse( void ) 
120 {
121 	ES_Event ThisEvent;
122 	bool ReturnValue = false;	// default no event
123   	unsigned int CurrentPeriod;    // (us)
124   	DisableInterrupts;
125   	CurrentPeriod = IR_Period_us;
126   	EnableInterrupts;
127 	
128 	// Only update period if it has changed
129   	if (Last_IR_Period_us != CurrentPeriod) 
130   	{
131   		Last_IR_Period_us = CurrentPeriod;
132   		if ( (CurrentPeriod > (GOAL_PERIOD - PERIOD_MARGIN)) &&
133        (CurrentPeriod < (GOAL_PERIOD + PERIOD_MARGIN)) )
134   		{
135 			BeaconStatus = GOAL_BEACON;
136   		}
137   		else if ( (CurrentPeriod > (OPPONENT_PERIOD - PERIOD_MARGIN)) &&
138         (CurrentPeriod < (OPPONENT_PERIOD + PERIOD_MARGIN)) )
139   		{
140 			BeaconStatus = OPPONENT_BEACON;	
141   		} else {
142 			BeaconStatus = NO_BEACON;
143 		}		
144   	}
145 	//printf("current period: %i \n\r", CurrentPeriod);
146 	
147 	
148 	// Only post event if beacon status has changed
149   	if (LastBeaconStatus != BeaconStatus) 
150   	{
151 		switch (BeaconStatus)
152 		{
153 			case OPPONENT_BEACON:
154 				ThisEvent.EventType = EC_OPPONENT_DETECTED;
155 				PostGameMasterHSMS(ThisEvent);
156 				ReturnValue = true; 
157 				break;
158 				
159 			case GOAL_BEACON:
160 				ThisEvent.EventType = EC_GOAL_DETECTED;
161 				PostGameMasterHSMS(ThisEvent);
162 				ReturnValue = true; 
163 				break;
164 				
165 			case NO_BEACON:
166 				ThisEvent.EventType = EC_BEACON_LOST;
167 				PostGameMasterHSMS(ThisEvent);
168 				ReturnValue = true;
169 				break;
170 		}
171 		LastBeaconStatus = BeaconStatus;
172 		// printf("current period and status: %i  -- %i \n\r", CurrentPeriod, BeaconStatus);
173 	}
174 	
175 	return ReturnValue;	
176 }
177 
178 /* interrupt response routines for the IR detector subsystem */
179 void interrupt _Vec_tim1ovf IR_TimerOverflow (void)
180 {
181    uOverFlows++;        // Increment overflow counter  
182    TIM1_TFLG2 = _S12_TOF;    // Clear overflow flag
183    
184    // Test Overflow Period with Scope
185    // if((PTT & BIT3HI) == BIT3HI) PTT &= BIT3LO;
186    // else PTT |= BIT3HI;
187 }
188 
189 void interrupt _Vec_tim1ch5 IR_Pulse (void)
190 {
191    unsigned long ThisTime;     
192    
193    // Account for rollover error
194    if( ((TIM1_TFLG2 & _S12_TOF) == _S12_TOF) && (TIM1_TC5 < 0x8000))
195    {
196       uOverFlows++;        // Increment overflow counter 
197       TIM1_TFLG2 = _S12_TOF;    // Clear overflow flag 
198    }
199    ThisTime = ((unsigned long)uOverFlows<<16) + TIM1_TC5; // Current encoder value
200    uPeriod = ThisTime - uLastTime;     // Calculate period
201    uLastTime = ThisTime;               // Reset last time
202    IR_Period_us = uPeriod * CLOCK_SCALAR / 24;
203    
204    TIM1_TFLG1 = _S12_C5F;    // Clear IC Flag
205 
206    // Test Encoder Interrupt Period with Scope
207    //if((PTT & BIT6HI) == BIT6HI) PTT &= BIT6LO;
208    //else PTT |= BIT6HI;
209 }