/**************************************************************************** Module Angels_FieldStatusMachine.c Revision 1.0.1 Description This is a file for implementing flat state machines under the Gen2 Events and Services Framework. Notes History When Who What/Why -------------- --- -------- 01/15/12 11:12 jec revisions for Gen2 framework 11/07/11 11:26 jec made the queue static 10/30/11 17:59 jec fixed references to CurrentEvent in RunMotorSM() 10/23/11 18:20 jec began conversion from SMTMotor.c (02/20/07 rev) ****************************************************************************/ /**************************************************************************** Function RunFieldStatusSM Takes an event to process Returns an event to return ****************************************************************************/ Depending on CurrentState: If the FSR is in the initial state move to the first request state If the FSR is in the first request state ... and the SPI timer has run out, ... and the transmit register is empty query the FSR start a 2ms SPI timer move to the state where garbage is coming in If the FSR is in the state where it's reading garbage ... and the SPI timer has run out, ... and the SPI is checking the flag receive in the garbage hexadecimal move to the second request state start a 2ms SPI timer. If the FSR is in the second request state, ... and the SPI timer has run out, ... and the transmit register is empty clear the SPI register start a 50ms SPI timer. move to the state where the SPI is receiving info from the FSR. If the SPI is reading in information from the FSR ... and the SPI timer has run out, ... and the SPI is checking the flag desposit the received hexadecimal in the register ... and both request don't return garbage depending on the item being queried to the FSR looking for number of balls on the field if the number of balls isn't zero and the last number of balls is zero post that the game has started refresh the number of balls variable looking for the number of balls in bin 1 write number of balls to bin 1 looking for the number of balls in bin 2 write number of balls to bin 2 looking for the number of balls in bin 3 write number of balls to bin 3 looking for the number of balls in bin 4 write number of balls to bin 4 looking for wall angle refresh the incoming wall angle value calculate wall angle else we got garbage start a 2ms SPI timer loop the query for each piece of information move to the first request state End of RunFieldStatusSM /**************************************************************************** Function initSPI Takes nothing Returns nothing ****************************************************************************/ Set the baud rate to 64 Initialize the SPI clock Enable the Master in SPICR1 Take control of the SPI pins Set Most Significant Bit First Set up SPI Clock Control Set the SS pin to auto control Make sure we're using only one master (set no fault mode) End of initSPI /**************************************************************************** Function Bin1Score Takes nothing Returns unsigned int ****************************************************************************/ Returns the number of balls in Bin 1 End of Bin1Score /**************************************************************************** Function Bin2Score Takes nothing Returns unsigned int ****************************************************************************/ Returns the number of balls in Bin 2 End of Bin2Score /**************************************************************************** Function Bin3Score Takes nothing Returns unsigned int ****************************************************************************/ Returns the number of balls in Bin 3 End of Bin3Score /**************************************************************************** Function Bin4Score Takes nothing Returns unsigned int ****************************************************************************/ Returns the number of balls in Bin 4 End of Bin4Score /**************************************************************************** Function WallAngleReport Takes nothing Returns unsigned int ****************************************************************************/ Returns the wall angle of the field End of WallAngleReport /**************************************************************************** Function WallVelocityReport Takes nothing Returns int ****************************************************************************/ Calculate the differnce from the last wall angle If the current wall angle is greater than 345 Calculate the current wall angle to give a postive value 0-360 If the current wall angle is less than 345 Calculate the current wall angle to give a postive value 0-360 Return the current wall angle End of WallVelocityReport /**************************************************************************** Function isBinOnSide Takes two unsigned int Returns true or false ****************************************************************************/ Depending on the bin For Bin 1 If the Wall angle is less than C or greater than H the bin is on the red side If the wall angle is greater than D or less than G the bin is on the blue side Otherwise it's in a grey area For Bin 2 If the Wall angle is less than E or greater than B the bin is on the red side If the wall angle is greater than F or less than A the bin is on the blue side Otherwise it's in a grey area For Bin 3 If the Wall angle is less than G or greater than D the bin is on the red side If the wall angle is greater than H or less than C the bin is on the blue side Otherwise it's in a grey area For Bin 4 If the Wall angle is less than A or greater than F the bin is on the red side If the wall angle is greater than B or less than E the bin is on the blue side Otherwise it's in a grey area Return True Return False End of isBinOnSide