Pseudo-Code for the PLAYER's Servo Signal Generator PIC
//*** Pseudo-code for new SPI byte event checker ***//
This code checks to see if a new byte has been received by the SPI communications hardware
Read buffer full flag BF
If BF is set
Post a New_SPI_Byte event to the SPI slave service
Reset the communications timeout timer
//*** Pseudo-code for communications timeout event checker ***//
This code checks to see if the communications link has failed (no messages received within a specified timeout interval).
If the Timer 2 timeout flag (set via an Interrupt Service Routine) is set
Clear the timeout flag
If the number of consecutive timeouts is greater than a specified interval
Post an event that will return the PLAYER to safe mode (this indicates a complete communications failure and all motors should be set to their default positions/zero throttle).
Else if the number of consecutive timeouts is greater than a specified interval (shorter than above)
Post an event that will return the PLAYER to idle mode (this indicates that there has been a break in the transmission of a multibyte message, but a communications failure with the XBee Master PIC has not yet occurred).
//*** Pseudo-code for initialization of SPI communications service ***//
This code initializes the PIC for SPI communications in slave mode so that it can receive commands from the XBee Master PIC. The motor control/servo PIC does not send data back to the XBee Master PIC.
Configure Timer 2 to handle byte timeout (return to idle) and communication failure (return to safe mode) events.
Set T2CKPS in T2CON register for divide by 16 execution clock pre-scaler
Set TOUTPS in T2CON register for divide by 16 flag-setting post-scaler
Enable Timer 2 (TMR2ON)
Clear interrupt flags (TMR2IF)
Enable interrupts for Timer 2 (TMR2IE)
Enable interrupts for peripherals (PEIE)
Configure SPI communications peripheral
Configure SPI for Mode 3 operation (CKE and CKP)
Clear SPI interrupt flag (SSPIF)
Clear SPI buffer full flag by reading SPI buffer (SSPBUF)
Configure SPI for slave mode operation, require slave select line to be held low (SSPM)
Enable SPI system (SSPEN)
Set slave select port to be a digital input
Set serial data out port to be a digital input since this PIC will not return data
Set shift clock port as a digital input since this device is a slave
Set serial data in port to be a digital input
Configure the whimsy function control port (foam dart launcher) to be a digital output
Globally enable interrupts
//*** Pseudo-code for the SPI communications run service ***//
This code runs the SPI communications service in slave mode so that the motor control/servo PIC can receive commands from the XBee Master PIC. The motor control/servo PIC does not send data back to the XBee Master PIC.
Enter initialization state
Set all servo motors to default position and set thrust motor throttle to zero
Enter safe mode state
Set all servo motors to default position and set thrust motor throttle to zero
If a new SPI byte is received, decode the command header byte and transition states based on the result
In idle mode
If a new SPI byte is received, decode the command header byte and transition states based on the result
If the go to safe mode command is received, transition to safe mode
While waiting for control bytes (the command header was decoded to indicate the start of a multibyte command message)
If a new SPI byte is received
Sequentially store the SPI data as throttle, steering, auxiliary, whimsy, translation, and energy display commands.
Increment the byte counter (keeps track of which byte is being received)
If the byte counter is less than the total number of bytes in a control message, remain in this state
If the byte counter is equal to the total number of bytes in a control message, return to the idle state (the transmission of a control message is complete)
If Timer 2 has timed out, return to idle state (communication has been temporarily interrupted)
If the go to safe mode command is received, transition to safe mode
While waiting for energy status (the command header was decoded to indicate that the next byte will contain the energy status of the PLAYER)
If a new SPI byte is received
Decode the energy display command and return to idle state (this is a one-byte message)
If Timer 2 has timed out, return to idle state (communication has been temporarily interrupted)
If the go to safe mode command is received, transition to safe mode
//*** Pseudo-code for the decode command header function ***//
This code decodes bytes received while the PLAYER is in its idle state and checks to see if the bytes match any of the specified header bytes (designate the start of control or energy display messages).
If the header byte is a control header, transition to the wait for control bytes state
If the header is a display command header, transition to the wait for energy status state
//*** Pseudo-code for the decode throttle command function ***//
This code decodes the signed char throttle control byte and scales it to an appropriate servo signal pulse width that will be sent to the DC brushless thrust motor’s electronic speed controller.
If the throttle command is <= 0, set throttle to minimum value (reverse thrust is not possible on this PLAYER)
Else scale the throttle value
If scaled value is less than the minimum allowable throttle, set the throttle to the minimum allowable value.
Else if scaled value is greater than the maximum allowable throttle, set the throttle to the maximum allowable value.
Update the throttle servo signal port to output the new pulse width.
//*** Pseudo-code for the decode steering command function ***//
This code decodes the signed char steering control byte and scales it to an appropriate servo signal pulse width that will be sent to the steering servo.
If the steering command is less than a lower threshold, set steering to minimum value
Else if the steering command is greater than an upper threshold, set steering to a maximum value
Else scale the steering value
If scaled value is less than the minimum allowable steering, set the steering to the minimum allowable value.
Else if scaled value is greater than the maximum allowable steering, set the steering to the maximum allowable value.
Update the steering servo signal port to output the new pulse width.
//*** Pseudo-code for the decode auxilliary command function ***//
This code decodes the unsigned char auxiliary control byte that contains information about kicking and braking
If the kicking bytes are greater than a defined threshold and the energy level of the PLAYER is not empty, update the kicking servo to its kick-deployed position.
Else update the kicking servo to its kicking-arm-retracted position.
Braking is handled by a different PIC
//*** Pseudo-code for the decode auxilliary command function ***//
This code decodes the unsigned char auxiliary control byte that contains information about kicking and braking
If the kicking bytes are greater than a defined threshold and the energy level of the PLAYER is not empty, update the kicking servo to its kick-deployed position.
Else update the kicking servo to its kicking-arm-retracted position.
Braking is handled by a different PIC
//*** Pseudo-code for the decode whimsy command function ***//
This code decodes the unsigned char whimsy control byte that contains information about the PLAYER’s whimsical function (foam dart launcher).
If the whimsy value is greater than a defined threshold, set the foam dart firing port high.
Else set the foam dart firing port low.
//*** Pseudo-code for the decode energy display command function ***//
This code decodes the unsigned char energy display command and outputs the transmitted energy level to a visual display.
If the display command is <= 7 (0 – 7 is the valid range of energy levels)
Update the module-level static variable that other pieces of the code read to check the energy level of the PLAYER.
Scale the value for an appropriate pointer arrow servo position.
If scaled value is less than the minimum allowable pointing position, set the pointing servo to the minimum allowable value.
Else if scaled value is greater than the maximum allowable pointing position, set the pointing servo to the maximum allowable value.
Update the energy display pointing arrow servo signal port to output the new pulse width.
Also update a series of LEDs on the board for visual debugging purposes.
//*** Pseudo-code for initialization of the servo signal generation service ***//
This code initializes the PIC for generating servo signals out of multiple ports while only using a single timer and interrupt.
Scale default servo signal pulse widths for each channel (defined in ms) to the corresponding Timer 1 tick counts.
For each enabled servo channel, set the corresponding port to be a digital output initialized as low.
Configure Timer 1 to time up to five servo channels
Configure the system clock to use an external oscillator (SCS in OSCCON register)
Configure clock scaler to divide system clock by 8 (T1CON)
Configure CCP1 system for output compare mode, to generate a software interrupt, and to not affect the CCP1 pin (CCP1CON)
Initialize the output compare register (CCPR)
Clear interrupt flags (CCP1IF)
Enable interrupts for CCP1 systems (CCP1IE in PIE1 register)
Enable interrupts for peripherals (PEIE in INTCON register)
Globally enable interrupts (GIE in INTCON register)
//*** Pseudo-code for updating the pulse width on a particular servo channel ***//
This code updates the module-level static variable that stores the pulse width that should be outputted on a given servo channel. This function is a public function that can be called by other services on the PIC. This code simply checks to ensure that the desired pulse width is a valid servo signal pulse width value (and forces the input to the minimum or maximum value if necessary).
//*** Pseudo-code for servo signal generator interrupt service routine ***//
This code handles the periodic generation of servo signals on five output ports. There is no run function for the servo control service.
If the CCP1 system (Timer 1) interrupt flag is set
Clear the flag (CCP1IF)
Sequentially pulse the servo channels by stepping through an array each time the interrupt fires (and resetting the index after reaching the end). Each index corresponds to an active servo channel. After setting a servo channel high, Timer 1 is set for the given channel’s pulse width. When Timer 1 next times out, the previous servo port will be set low and the next servo channel will be set high. After all of the servo channels have been pulsed, an idle period will be set to ensure that the total period for pulsing is 20 ms.
This code checks to see if a new byte has been received by the SPI communications hardware
Read buffer full flag BF
If BF is set
Post a New_SPI_Byte event to the SPI slave service
Reset the communications timeout timer
//*** Pseudo-code for communications timeout event checker ***//
This code checks to see if the communications link has failed (no messages received within a specified timeout interval).
If the Timer 2 timeout flag (set via an Interrupt Service Routine) is set
Clear the timeout flag
If the number of consecutive timeouts is greater than a specified interval
Post an event that will return the PLAYER to safe mode (this indicates a complete communications failure and all motors should be set to their default positions/zero throttle).
Else if the number of consecutive timeouts is greater than a specified interval (shorter than above)
Post an event that will return the PLAYER to idle mode (this indicates that there has been a break in the transmission of a multibyte message, but a communications failure with the XBee Master PIC has not yet occurred).
//*** Pseudo-code for initialization of SPI communications service ***//
This code initializes the PIC for SPI communications in slave mode so that it can receive commands from the XBee Master PIC. The motor control/servo PIC does not send data back to the XBee Master PIC.
Configure Timer 2 to handle byte timeout (return to idle) and communication failure (return to safe mode) events.
Set T2CKPS in T2CON register for divide by 16 execution clock pre-scaler
Set TOUTPS in T2CON register for divide by 16 flag-setting post-scaler
Enable Timer 2 (TMR2ON)
Clear interrupt flags (TMR2IF)
Enable interrupts for Timer 2 (TMR2IE)
Enable interrupts for peripherals (PEIE)
Configure SPI communications peripheral
Configure SPI for Mode 3 operation (CKE and CKP)
Clear SPI interrupt flag (SSPIF)
Clear SPI buffer full flag by reading SPI buffer (SSPBUF)
Configure SPI for slave mode operation, require slave select line to be held low (SSPM)
Enable SPI system (SSPEN)
Set slave select port to be a digital input
Set serial data out port to be a digital input since this PIC will not return data
Set shift clock port as a digital input since this device is a slave
Set serial data in port to be a digital input
Configure the whimsy function control port (foam dart launcher) to be a digital output
Globally enable interrupts
//*** Pseudo-code for the SPI communications run service ***//
This code runs the SPI communications service in slave mode so that the motor control/servo PIC can receive commands from the XBee Master PIC. The motor control/servo PIC does not send data back to the XBee Master PIC.
Enter initialization state
Set all servo motors to default position and set thrust motor throttle to zero
Enter safe mode state
Set all servo motors to default position and set thrust motor throttle to zero
If a new SPI byte is received, decode the command header byte and transition states based on the result
In idle mode
If a new SPI byte is received, decode the command header byte and transition states based on the result
If the go to safe mode command is received, transition to safe mode
While waiting for control bytes (the command header was decoded to indicate the start of a multibyte command message)
If a new SPI byte is received
Sequentially store the SPI data as throttle, steering, auxiliary, whimsy, translation, and energy display commands.
Increment the byte counter (keeps track of which byte is being received)
If the byte counter is less than the total number of bytes in a control message, remain in this state
If the byte counter is equal to the total number of bytes in a control message, return to the idle state (the transmission of a control message is complete)
If Timer 2 has timed out, return to idle state (communication has been temporarily interrupted)
If the go to safe mode command is received, transition to safe mode
While waiting for energy status (the command header was decoded to indicate that the next byte will contain the energy status of the PLAYER)
If a new SPI byte is received
Decode the energy display command and return to idle state (this is a one-byte message)
If Timer 2 has timed out, return to idle state (communication has been temporarily interrupted)
If the go to safe mode command is received, transition to safe mode
//*** Pseudo-code for the decode command header function ***//
This code decodes bytes received while the PLAYER is in its idle state and checks to see if the bytes match any of the specified header bytes (designate the start of control or energy display messages).
If the header byte is a control header, transition to the wait for control bytes state
If the header is a display command header, transition to the wait for energy status state
//*** Pseudo-code for the decode throttle command function ***//
This code decodes the signed char throttle control byte and scales it to an appropriate servo signal pulse width that will be sent to the DC brushless thrust motor’s electronic speed controller.
If the throttle command is <= 0, set throttle to minimum value (reverse thrust is not possible on this PLAYER)
Else scale the throttle value
If scaled value is less than the minimum allowable throttle, set the throttle to the minimum allowable value.
Else if scaled value is greater than the maximum allowable throttle, set the throttle to the maximum allowable value.
Update the throttle servo signal port to output the new pulse width.
//*** Pseudo-code for the decode steering command function ***//
This code decodes the signed char steering control byte and scales it to an appropriate servo signal pulse width that will be sent to the steering servo.
If the steering command is less than a lower threshold, set steering to minimum value
Else if the steering command is greater than an upper threshold, set steering to a maximum value
Else scale the steering value
If scaled value is less than the minimum allowable steering, set the steering to the minimum allowable value.
Else if scaled value is greater than the maximum allowable steering, set the steering to the maximum allowable value.
Update the steering servo signal port to output the new pulse width.
//*** Pseudo-code for the decode auxilliary command function ***//
This code decodes the unsigned char auxiliary control byte that contains information about kicking and braking
If the kicking bytes are greater than a defined threshold and the energy level of the PLAYER is not empty, update the kicking servo to its kick-deployed position.
Else update the kicking servo to its kicking-arm-retracted position.
Braking is handled by a different PIC
//*** Pseudo-code for the decode auxilliary command function ***//
This code decodes the unsigned char auxiliary control byte that contains information about kicking and braking
If the kicking bytes are greater than a defined threshold and the energy level of the PLAYER is not empty, update the kicking servo to its kick-deployed position.
Else update the kicking servo to its kicking-arm-retracted position.
Braking is handled by a different PIC
//*** Pseudo-code for the decode whimsy command function ***//
This code decodes the unsigned char whimsy control byte that contains information about the PLAYER’s whimsical function (foam dart launcher).
If the whimsy value is greater than a defined threshold, set the foam dart firing port high.
Else set the foam dart firing port low.
//*** Pseudo-code for the decode energy display command function ***//
This code decodes the unsigned char energy display command and outputs the transmitted energy level to a visual display.
If the display command is <= 7 (0 – 7 is the valid range of energy levels)
Update the module-level static variable that other pieces of the code read to check the energy level of the PLAYER.
Scale the value for an appropriate pointer arrow servo position.
If scaled value is less than the minimum allowable pointing position, set the pointing servo to the minimum allowable value.
Else if scaled value is greater than the maximum allowable pointing position, set the pointing servo to the maximum allowable value.
Update the energy display pointing arrow servo signal port to output the new pulse width.
Also update a series of LEDs on the board for visual debugging purposes.
//*** Pseudo-code for initialization of the servo signal generation service ***//
This code initializes the PIC for generating servo signals out of multiple ports while only using a single timer and interrupt.
Scale default servo signal pulse widths for each channel (defined in ms) to the corresponding Timer 1 tick counts.
For each enabled servo channel, set the corresponding port to be a digital output initialized as low.
Configure Timer 1 to time up to five servo channels
Configure the system clock to use an external oscillator (SCS in OSCCON register)
Configure clock scaler to divide system clock by 8 (T1CON)
Configure CCP1 system for output compare mode, to generate a software interrupt, and to not affect the CCP1 pin (CCP1CON)
Initialize the output compare register (CCPR)
Clear interrupt flags (CCP1IF)
Enable interrupts for CCP1 systems (CCP1IE in PIE1 register)
Enable interrupts for peripherals (PEIE in INTCON register)
Globally enable interrupts (GIE in INTCON register)
//*** Pseudo-code for updating the pulse width on a particular servo channel ***//
This code updates the module-level static variable that stores the pulse width that should be outputted on a given servo channel. This function is a public function that can be called by other services on the PIC. This code simply checks to ensure that the desired pulse width is a valid servo signal pulse width value (and forces the input to the minimum or maximum value if necessary).
//*** Pseudo-code for servo signal generator interrupt service routine ***//
This code handles the periodic generation of servo signals on five output ports. There is no run function for the servo control service.
If the CCP1 system (Timer 1) interrupt flag is set
Clear the flag (CCP1IF)
Sequentially pulse the servo channels by stepping through an array each time the interrupt fires (and resetting the index after reaching the end). Each index corresponds to an active servo channel. After setting a servo channel high, Timer 1 is set for the given channel’s pulse width. When Timer 1 next times out, the previous servo port will be set low and the next servo channel will be set high. After all of the servo channels have been pulsed, an idle period will be set to ensure that the total period for pulsing is 20 ms.
Pseudo-Code for the PLAYER's LiFKIM Control PIC
//*** LiFKIM Controller PIC Pseudo-Code ***//
The project specifications required that this code be written in assembly language for the PIC16F190. The purpose of this code is to communicate with the LiFKIM (Lift Fan Kontroller and Impact Monitor) via UART/SCI and the XBee Master PIC via SPI.
Configure PIC to boot with watch-dog timer disabled, master clear disabled, and oscillator control to drive a high-speed oscillator.
Define memory locations for all required variables (place variables associated with the interrupt service routine in the special file registers so that they can be accessed from any data bank).
Define the interrupt service routine:
Push:
Store contents of working register
Store contents of status register
Store contents of program counter
ISR Body:
Identify source of the interrupt
If transmit register empty flag (TXIF) is set, send the next queued message to the LiFKIM
If the SPI buffer is filled with a new message, clear the flag (SSPIF) and decode the message to see if it should be forwarded to the LiFKIM
POP:
Restore program counter
Restore status register
Restore working register
Main routine initialization:
Configure all ports to be digital
Configure TX and SDO ports as outputs (all ports are initialized as inputs by default)
Configure PIC to use external oscillator for its execution clock (SCS in OSCCON)
Configure UART for 9600 baud, 8-bit data, no parity
Enable UART (SPEN)
Enable receiver and transmitter
Configure SPI system for Mode 3 operation in slave mode
Enable SPI system (SSPEN)
Clear SPI interrupt flag (SSPIF)
Enable SPI interrupts (SSPIE in PIE1 register)
Enable peripheral interrupts (PEIE in INTCON register)
Initialize variables
Globally enable interrupts
Event checker to see if a new message has been received from the LiFKIM on the UART:
If RCIF flag is set, call the Receive UART Message function
Receive UART message function:
Decode the LiFKIM message per the LiFKIM communications specifications
If the message matches the specified LiFKIM communications format, accept the message as valid and continue decoding of the message
Decode function for valid UART messages:
Move decoded message into SPI buffer for forwarding to the XBee Master PIC
If the message corresponds to an energy status update, update a local display of LEDs for debugging purposes
Activate LiFKIM function:
Encode the activation command per the LiFKIM communication specifications
Move the encoded command into the UART transmit message storage variable
Enable UART transmit interrupt
The project specifications required that this code be written in assembly language for the PIC16F190. The purpose of this code is to communicate with the LiFKIM (Lift Fan Kontroller and Impact Monitor) via UART/SCI and the XBee Master PIC via SPI.
Configure PIC to boot with watch-dog timer disabled, master clear disabled, and oscillator control to drive a high-speed oscillator.
Define memory locations for all required variables (place variables associated with the interrupt service routine in the special file registers so that they can be accessed from any data bank).
Define the interrupt service routine:
Push:
Store contents of working register
Store contents of status register
Store contents of program counter
ISR Body:
Identify source of the interrupt
If transmit register empty flag (TXIF) is set, send the next queued message to the LiFKIM
If the SPI buffer is filled with a new message, clear the flag (SSPIF) and decode the message to see if it should be forwarded to the LiFKIM
POP:
Restore program counter
Restore status register
Restore working register
Main routine initialization:
Configure all ports to be digital
Configure TX and SDO ports as outputs (all ports are initialized as inputs by default)
Configure PIC to use external oscillator for its execution clock (SCS in OSCCON)
Configure UART for 9600 baud, 8-bit data, no parity
Enable UART (SPEN)
Enable receiver and transmitter
Configure SPI system for Mode 3 operation in slave mode
Enable SPI system (SSPEN)
Clear SPI interrupt flag (SSPIF)
Enable SPI interrupts (SSPIE in PIE1 register)
Enable peripheral interrupts (PEIE in INTCON register)
Initialize variables
Globally enable interrupts
Event checker to see if a new message has been received from the LiFKIM on the UART:
If RCIF flag is set, call the Receive UART Message function
Receive UART message function:
Decode the LiFKIM message per the LiFKIM communications specifications
If the message matches the specified LiFKIM communications format, accept the message as valid and continue decoding of the message
Decode function for valid UART messages:
Move decoded message into SPI buffer for forwarding to the XBee Master PIC
If the message corresponds to an energy status update, update a local display of LEDs for debugging purposes
Activate LiFKIM function:
Encode the activation command per the LiFKIM communication specifications
Move the encoded command into the UART transmit message storage variable
Enable UART transmit interrupt
Pseudo-Code for the PLAYER's XBee Master PIC
Player.c
Wait to link:
Check data from xbee
If got pair request message, check Jersey number.
If Jnumber is correct, then check color. Else back to wait to link.
If color is correct, send pair success message to coach and go to paired state. Else send color wrong message back.
Paired:
Check event from Xbee.
0x04 for RESET: back to Wait to link state.
0x02 for TAG_OUT: send TagInfo to LiFKIM PIC.
0x07 for TAG_DETECTED: send TagInfo to LiFKIM PIC and go to Tag detected state and start Detected timer.
0x03 for REQ_PAIR: If Jnumber is correct, then check color. If color is correct, send already paired message to coach
0x01 for CTRL: send control data to servo PIC and reset heartbeat timer
Heartbeat timer timeout: go back to Wait to link state.
Status timer timeout: send status message to the coach.
Tag detected:
Check event from Xbee.
0x04 for RESET: back to Wait to link state.
0x02 for TAG_OUT: send TagInfo to LiFKIM PIC.
0x03 for REQ_PAIR: If Jnumber is correct, then check color. If color is correct, send already paired message to coach
0x01 for CTRL: send control data to servo PIC and reset heartbeat timer
Heartbeat timer timeout: go back to Wait to link state.
Status timer timeout: send status message to the coach.
Detected timer timeout: go back to paired state.
Wait to link:
Check data from xbee
If got pair request message, check Jersey number.
If Jnumber is correct, then check color. Else back to wait to link.
If color is correct, send pair success message to coach and go to paired state. Else send color wrong message back.
Paired:
Check event from Xbee.
0x04 for RESET: back to Wait to link state.
0x02 for TAG_OUT: send TagInfo to LiFKIM PIC.
0x07 for TAG_DETECTED: send TagInfo to LiFKIM PIC and go to Tag detected state and start Detected timer.
0x03 for REQ_PAIR: If Jnumber is correct, then check color. If color is correct, send already paired message to coach
0x01 for CTRL: send control data to servo PIC and reset heartbeat timer
Heartbeat timer timeout: go back to Wait to link state.
Status timer timeout: send status message to the coach.
Tag detected:
Check event from Xbee.
0x04 for RESET: back to Wait to link state.
0x02 for TAG_OUT: send TagInfo to LiFKIM PIC.
0x03 for REQ_PAIR: If Jnumber is correct, then check color. If color is correct, send already paired message to coach
0x01 for CTRL: send control data to servo PIC and reset heartbeat timer
Heartbeat timer timeout: go back to Wait to link state.
Status timer timeout: send status message to the coach.
Detected timer timeout: go back to paired state.
Pseudo-Code for the COACH
EventChecker.c
bool Receive_Check(void)
{
Set ReturnVal to false;
if((SCI1SR1 & _S12_RDRF) == _S12_RDRF)
{
Clear RDRF;
Read SCI1DRL and assign it to RX_Message;
Post Reveived with RX_Message to SCI_Receive;
Set ReturnVal to true;
}
return RetrunVal;
}
bool PairButton_Check(void)
{
Set ReturnVal to false;
if(T3 is high)
{
Post PairButtonPress to Coach;
Set ReturnVal to true;
}
return RetrunVal;
}
bool TagOut_Check(void)
{
Set ReturnVal to false;
set col and Senddata to 0;
if(T6 is high)
{
Read from AD0 and modify it to 0-3 and assign it to Pos;
if(T7 is high) set col to be 1;
Left shift Pos for 1 bit and OR it with col and assign it to Senddata;
Post Tagout with Senddata to Coach;
Set ReturnVal to true;
}
return RetrunVal;
}
bool Reset_Check(void)
{
Set ReturnVal to false;
if(T2 is high)
{
Post Reset to Coach;
Set ReturnVal to true;
}
return RetrunVal;
}
Coach.c
bool Init_Coach ( uint8_t Priority )
Set MyPriority to Priority;
Set CurrentState to IDLE;
Initialize values for Data, SPD, DIR, AUX, WHIM and TRANS;
Initialize AD0-AD3 to be A/D port and AD4-AD7 to be input;
Set ThisEvent.EventType to be ES_INIT;
ES_Event Run_Coach( ES_Event ThisEvent )
Set ReturnEvent.EventType to be ES_NO_EVENT;
switch (CurrentState)
case IDLE:
if (ThisEvent.EventType == PairButtonPress)
{
Clear all LEDs by setting all U ports to be 0;
Read from AD3, divide it by 4 and assign it to TeamN;
According to TeamN we set RID to be 1-13;
Test if AD7 is high, if yes, set Bit 4 of Data to be high;
Post event SendPairReq with Data as parameter to SCI_Send;
Set Data to be 0x00;
Set CurrentState to be WAIT_PAIR;
Start WaitPair timer;
}
case WAIT_PAIR:
if(ThisEvent.EventType == PAIR_RESP)
{
if ((Query_SCI_Receive (3) & BIT1HI) == BIT1HI)
{
Set CurrentState to IDLE;
Set U2 to be high;
}
else if ((Query_SCI_Receive (3) & BIT2HI) == BIT2HI)
{
Set CurrentState to IDLE;
Set U1 to be high;
}
else if ((Query_SCI_Receive(3) & BIT0HI) == BIT0HI)
{
Set AddMSB to be Query_SCI_Receive(0);
Set AddLSB to be Query_SCI_Receive(1);
Set U0 to be high;
Set CurrentState to PAIRED;
Start HeartBeat timer;
Start SendControl timer;
}
else if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam == WaitPair_TIMER))
{
Set CurrentState to IDLE;
}
}
case PAIRED:
if (ThisEvent.EventType == GetHeartBeat)
{
Set HeartBeat Timer;
}
else if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam == HeartBeat_TIMER))
{
Set CurrentState to IDLE;
Write_LED(0);
Put off all LEDs by setting all U ports to be 0;
}
else if (ThisEvent.EventType == TaggedOut)
{
Set CurrentState to IDLE;
Write_LED(0);
Put off all LEDs by setting all U ports to be 0;
}
else if ((ThisEvent.EventType == TagOut) && (Query_SendState() == Assembling))
{
Post SendTAGOUT to SCI_SEND;
}
else if ((ThisEvent.EventType == Reset) && (Query_SendState() == Assembling))
{
Post SendReset to SCI_SEND;
Set CurrentState to IDLE;
Write_LED(0);
Put off all LEDs by setting all U ports to be 0;
}
else if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam == SendControl_TIMER))
{
Set AUX to be 0x00;
Set WHIM to be 0x00;
Read from AD1 and modify it as the Speed value and set it to SPD;
if (SPD<=10) set AUX to be 0xFF;
Read from AD2 and modify it as the Direction value and set it to DIR;
According to the return value of Query_Ultrason() set KICK to be 0x00 or 0xFF;
Left shift AUX by 4 bits and OR it with KICK;
if (U7 is high) set WHIM to 0xFF;
Set TRANS to 0x00;
Post SendCtrl to SCI_Send;
Set SendControl timer;
}
bool Receive_Check(void)
{
Set ReturnVal to false;
if((SCI1SR1 & _S12_RDRF) == _S12_RDRF)
{
Clear RDRF;
Read SCI1DRL and assign it to RX_Message;
Post Reveived with RX_Message to SCI_Receive;
Set ReturnVal to true;
}
return RetrunVal;
}
bool PairButton_Check(void)
{
Set ReturnVal to false;
if(T3 is high)
{
Post PairButtonPress to Coach;
Set ReturnVal to true;
}
return RetrunVal;
}
bool TagOut_Check(void)
{
Set ReturnVal to false;
set col and Senddata to 0;
if(T6 is high)
{
Read from AD0 and modify it to 0-3 and assign it to Pos;
if(T7 is high) set col to be 1;
Left shift Pos for 1 bit and OR it with col and assign it to Senddata;
Post Tagout with Senddata to Coach;
Set ReturnVal to true;
}
return RetrunVal;
}
bool Reset_Check(void)
{
Set ReturnVal to false;
if(T2 is high)
{
Post Reset to Coach;
Set ReturnVal to true;
}
return RetrunVal;
}
Coach.c
bool Init_Coach ( uint8_t Priority )
Set MyPriority to Priority;
Set CurrentState to IDLE;
Initialize values for Data, SPD, DIR, AUX, WHIM and TRANS;
Initialize AD0-AD3 to be A/D port and AD4-AD7 to be input;
Set ThisEvent.EventType to be ES_INIT;
ES_Event Run_Coach( ES_Event ThisEvent )
Set ReturnEvent.EventType to be ES_NO_EVENT;
switch (CurrentState)
case IDLE:
if (ThisEvent.EventType == PairButtonPress)
{
Clear all LEDs by setting all U ports to be 0;
Read from AD3, divide it by 4 and assign it to TeamN;
According to TeamN we set RID to be 1-13;
Test if AD7 is high, if yes, set Bit 4 of Data to be high;
Post event SendPairReq with Data as parameter to SCI_Send;
Set Data to be 0x00;
Set CurrentState to be WAIT_PAIR;
Start WaitPair timer;
}
case WAIT_PAIR:
if(ThisEvent.EventType == PAIR_RESP)
{
if ((Query_SCI_Receive (3) & BIT1HI) == BIT1HI)
{
Set CurrentState to IDLE;
Set U2 to be high;
}
else if ((Query_SCI_Receive (3) & BIT2HI) == BIT2HI)
{
Set CurrentState to IDLE;
Set U1 to be high;
}
else if ((Query_SCI_Receive(3) & BIT0HI) == BIT0HI)
{
Set AddMSB to be Query_SCI_Receive(0);
Set AddLSB to be Query_SCI_Receive(1);
Set U0 to be high;
Set CurrentState to PAIRED;
Start HeartBeat timer;
Start SendControl timer;
}
else if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam == WaitPair_TIMER))
{
Set CurrentState to IDLE;
}
}
case PAIRED:
if (ThisEvent.EventType == GetHeartBeat)
{
Set HeartBeat Timer;
}
else if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam == HeartBeat_TIMER))
{
Set CurrentState to IDLE;
Write_LED(0);
Put off all LEDs by setting all U ports to be 0;
}
else if (ThisEvent.EventType == TaggedOut)
{
Set CurrentState to IDLE;
Write_LED(0);
Put off all LEDs by setting all U ports to be 0;
}
else if ((ThisEvent.EventType == TagOut) && (Query_SendState() == Assembling))
{
Post SendTAGOUT to SCI_SEND;
}
else if ((ThisEvent.EventType == Reset) && (Query_SendState() == Assembling))
{
Post SendReset to SCI_SEND;
Set CurrentState to IDLE;
Write_LED(0);
Put off all LEDs by setting all U ports to be 0;
}
else if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam == SendControl_TIMER))
{
Set AUX to be 0x00;
Set WHIM to be 0x00;
Read from AD1 and modify it as the Speed value and set it to SPD;
if (SPD<=10) set AUX to be 0xFF;
Read from AD2 and modify it as the Direction value and set it to DIR;
According to the return value of Query_Ultrason() set KICK to be 0x00 or 0xFF;
Left shift AUX by 4 bits and OR it with KICK;
if (U7 is high) set WHIM to 0xFF;
Set TRANS to 0x00;
Post SendCtrl to SCI_Send;
Set SendControl timer;
}