Module BallSortingService.c Description This is a module for sorting and counting the balls recieved into the desired bin Notes Private definitions SERVO_CENTER SERVO_RECYCLE SERVO_LANDFILL MAX_RECYCLE MAX_LANDFILL RecycleCount LandfillCount CurrentColor GameActive HoldBallRecycle FullRecycle FullLandfill /*---------------------------- Module Functions ---------------------------*/ /* prototypes for private functions for this service.They should be functions relevant to the behavior of this service */ static uint8_t CategorizeColor(void); bool InitBallSortingService(uint8_t Priority) //Sets priority //Sets servomotor frequency to 50Hz, sets servos to starting position //Reset ball counters //Set servo to neutral position //Set next state to InitBSState // post the initial transition event ES_Event_t RunBallSortingService(ES_Event_t ThisEvent) // we will force the ball sorting service to always sort, no matter if // the game has started or not //Reads current State //If Current State is InitBSState //If recieved event is ES_INIT //Reset ball counters //Set servo to neutral position //Set state for NoBall //If Current State is NoBall //If recieved event is BALL_AT_COLOR_SENSOR and GameActive is false //Set Current State to NoBall //If recieved event is BALL_AT_COLOR_SENSOR, GameActive is true, EventParam is 1, and FullRecycle is false //Recieved Ball is recycling, store in bin 1 //Set Servo to recycling direction //Increase Recycle count //Check if Recycle Count has gotten to the max value //Recycle Count is equal to MAX_RECYCLE //Set FullRecycle flag to true //Post RECYCLE_FULL event to Gameplay SM //Start ColorServoTimer for 500ms //Set next state to Emptying //If recieved event is BALL_AT_COLOR_SENSOR, GameActive is true, EventParam is 0, and FullLandfill is false //Recieved Ball is landfill, store in bin 2 //Set Servo to landfill direction //Increase Landfill count //Check if Landfill Count has gotten to the max value //Landfill Count is equal to MAX_LANDFILL //Set FullLandfill flag to true //Post TRASH_FULL event to Gameplay SM //Start ColorServoTimer for 500ms //Set next state to Emptying //If recieved event is BALL_AT_COLOR_SENSOR, GameActive is true, EventParam is 1, and FullRecycle is true //Set HoldBallRecycle to true //Set next state as WaitingForBall //If recieved event is BALL_AT_COLOR_SENSOR, GameActive is true, EventParam is 0, and FullTrash is true //Set HoldBallRecycle to false //Set next state as WaitingForBall //If recieved event is GAME_OVER //Return Servo to normal position //Reset ball counters //Set FullLandfill and FullRecycle flags to false //Return to no ball state //If recieved event is RECYCLE_EMPTY and GameActive is true //Reset recycling ball counter //Set FullRecycle flag to false //If recieved event is TRASH_EMPTY and GameActive is true //Reset landfill ball counter //Set FullLandfill flag to false //If Current State is WaitingForBall //If recieved event is GAME_OVER //Return Servo to normal position //Reset ball counters //Set FullLandfill and FullRecycle flags to false //Return to no ball state //If recieved event is RECYCLE_EMPTY and HoldBallRecycle is false //Set RecycleCount to 0 //Set FullRecycle flag to false //If recieved event is RECYCLE_EMPTY and HoldBallRecycle is true //Reset recycle ball Counter //Reset FullRecycle flag //Set servo to recycling position //Add 1 to RecycleCount //Start 500 ms ColorServoTimer //Set next state to Emptying //If recieved event is TRASH_EMPTY and HoldBallRecycle is true //Set LandfillCount to 0 //Set FullLandfill flag to false //If recieved event is TRASH_EMPTY and HoldBallRecycle is false //Reset landfill ball Counter //Reset FullLandfill flag //Set servo to landfill position //Add 1 to LandfillCount //Start 500 ms ColorServoTimer //Set next state to Emptying //If Current State is WaitingForBall //If recieved event is ES_TIMEOUT and EventParam is ColorServoTimer //Return Servo to normal position //Return to no ball state //If recieved event is TRASH_EMPTY //Set LandfillCount to 0 //Set FullLandfill flag to false //If recieved event is RECYCLE_EMPTY //Set RecycleCount to 0 //Set FullRecycle flag to false //If recieved event is GAME_OVER //Return Servo to normal position //Reset ball counters //Set FullLandfill and FullRecycle flags to false //Return to no ball state //return ReturnEvent bool QueryNoBall(void) //If CurrentState is NoBall //Return true //Else //Return false