beacons Module

Files:

Interface:

Function: InitBeacons
Arguments: None.
Returns: None.
Description:Initializes the timers for each of the beacons.

Function: InitSide
Arguments: None.
Returns: None.
Description:Initializes the beacons based on which side we are on. If Pin 5 on the AD port is low, sets the side to 'R' and the own/opposing duty variables to 30%/70%. If Pin 6 on the AD port is low, sets the side to 'G' and the own/opposing duty variables to 70%/30%. If neither are low, then no side is selected.

Function: GetSide
Arguments: None.
Returns: CornerBeacons_t structure containing a character representing the side, the duty of the opponents side, and the duty of the friendly side.
Description:Returns the CornerBeacons structure for the current side.

Function: DetectFlash
Arguments: None.
Returns: Character representing TRUE when the flash is detected.
Description:Clears the flash-detected flag.

Function: BeaconDuty
Arguments: Character representing the Beacon Number
Returns: Character representing the detected duty of the referenced beacon.
Description:retrieves the detected duty of the referenced beacon.

Function: GetBeaconLevel
Arguments: Character representing the Beacon Pin Number
Returns: Short representing the value from 0 - 1023 representing the beacon intensity.
Description:Reads the referenced AD pin to determine the intensity of that beacon's signal.

Function: Orientation
Arguments: None.
Returns: Unsigned char representing the orientation of the robot.
Description:Returns the absolute orientation of the robot on the field: Facing 30, Facing 70, 70 left and 30 right, 30 left and 70 right.

Function: BeaconControlMove
Arguments: None.
Returns: None.
Description:Implements control law to head towards a beacon. The function takes the difference between two beacons(either both flag- or both orientation-dection beacons) and calculates the appropriate motor commands to minimize the difference.

Pseudo-Code:

 
void InitBeacons(void)
    StartTimer Top left, Top Right, Flag Left, and Flag right beacon timers to 1ms.
  
void InitSide(void)

    If Pin 5 on the AD port is low
        Set the side to 'R'
        Set own duty variables to 30%
        Set opposing duty variables to 70%
    If Pin 6 on the AD port is low
        Set the side to 'G'
        Set own duty variables to 70%
        Set opposing duty variables to 30%
    If neither are low	
     	Do nothing

CornerBeacons_t GetSide(void)
     Return corners structure

char DetectFlash(void)
    If TIM0_TFLG1 has been set
        Clear TIM0_TFLG1
        Return TRUE
    Else
        Return FALSE
    
char BeaconDuty(char BeaconNumber)
    If BeaconNumber is 4 
         Return Left Beacon's Duty Cycle
    Else If BeaconNumber is 5
         Return Right Beacon's Duty Cycle
    Else If BeaconNumber is 6 
         Return Left Flag Beacon's Duty Cycle
    Else If BeaconNumber is 7 
         Return Right Flag Beacon's Duty Cycle
        
short GetBeaconLevel(char BeaconPin)
    Return the AD value on the requested pin
    
unsigned char Orientation(void)
    If Front two beacons detect 70% duty
         Return "Facing 70" value
    Else If Front two beacons detect 30% duty
         Return "Facing 30" value
    Else If Left beacon detects 70% duty and Right beacon detects 30% duty
         Return 70LEFT_30RIGHT
    Else if Left beacon detects 30% duty and Right beacon detects 70% duty
         Return Return 30LEFT_70RIGHT
    Else
         Return NO_SIGNAL_DETECTED
        
void BeaconControlMove(void)
    If Beacon timers expire
     	Set that beacon's duty to 0
    If Beacon Control timers expire
     	Restart the timer
        
        Calculate difference in intensity between the Orientation beacons
        Calculate average intensity of the Orientation beacons
        Calculate appropriate motor correction commands based on PI control law
        
        Calculate difference in intensity between the Flag-detection beacons
        Calculate average intensity of the Flag-detection beacons
        Calculate appropriate motor correction commands based on bang-bang control


Interrupts:

void interrupt _Vec_tim1ch4 BeaconEdge4(void) void interrupt _Vec_tim1ch5 BeaconEdge5(void) Interrupt response routines for left and right Orientation beacons - input capture If rising edge detected Store timer Else Falling edge detected Store timer If timer overflow HighTime = ThisFallingEdge - ThisRisingEdge; Calculate Duty Cycle from HighTime Increment the appropriate counter depending on the recorded duty cycle Whichever counter gets to 40 first becomes the duty cycle Reset no-edge timer Clear the Interrupt Flag
void interrupt _Vec_tim1ch6 BeaconEdge6(void) void interrupt _Vec_tim1ch7 BeaconEdge7(void) Interrupt response routines for left and right flag beacons - input capture If rising edge detected Store timer Else Falling edge detected Store timer If timer overflow HighTime = ThisFallingEdge - ThisRisingEdge; Calculate Duty Cycle from HighTime Increment the appropriate counter depending on the recorded duty cycle Whichever counter gets to 40 first becomes the duty cycle Reset no-edge timer Clear the Interrupt Flag