There are two sub sections of communications for the PIC processors.  First, is the SPI communications protocol.

SPI

Highslide JS
Click to Enlarge

TOWRP SPI Communications
The SPI system has the Master PIC connected to two slave motor driver PICs.  Velocity commands are sent from the Master PIC to each of the Slave PICs. 

Pseudocode

Initialize SPI
    Turn of collision detect bit
    No Receive overflow bit
    SPI mode
    Idle state for clock is a low level
    SPI Master Mode, clock Fosc/64
    Input data sampled at middle of data output time
    Data transmitted rising edge of SCK
    Send a test byte

Transmit_SPI (Precondition of TX_SPI_DATA must contain the SPI byte to be sent)
    Loop until the last byte is finished transmitting
    Put the data to be sent in the SPI buffer

XBee

The second communications protocol on the TOWRP is the XBee communications. Although more information of the XBee communications protocol can be found in the state machine subsection, a receiver state machine had to be created that deciphers the incoming messages from a COACH.

Highslide JS
Click to Enlarge

TOWRP Received Message State Machine

Pseudocode

HANDLE_MSG
    Save the received data
    Goto the current state in SWITCH_DECISION

SWITCH_DECISION
    Find out what state to go to and go to it

STATE_PACKET_START
    If the message is a 7E
        Set the current state to STATE_LENGTH_MSB
    Else
        Do nothing

STATE_LENGTH_MSB
    If the message is 0
        Do Nothing
    Else
        Set the current state to STATE_LENGTH_LSB

STATE_LENGTH_LSB
    Set i =Length
    Set j = length
    If the length is > 7
        Get rid of the message
    Else
        Set the current state to STATE_GET_DATA

STATE_GET_DATA
    Get the memory location of the packet array
    Find out where in the array to put the new item
    Get the current data
    Store it in the array
    Decrement our current I placement
    If we are at the end of i
        Set the current state to STATE_CHECK_SUM

STATE_CHECK_SUM
    Store the length of LSB into a temp value
    Read every array value and sum them up
    0xFF-SumOfValues
    If the checksums are the same
        Increment the state
    Else
        Reset the state machine

EVALUATE_DATA
    Check to see if the data is a received data
    If it is a message from another XBee, Handle it
    If it is a status message, handle it
    Else, Do nothing