Created
December 1, 2015 03:33
-
-
Save michaellin/8214b902123def1bf44a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BucketService.c | |
State Machine list of States: InitializeBucketService, Ready2Play, BucketPlaying | |
module variables | |
MyPriority | |
CurrentState | |
dropServosList is a list of PWM channels connected to servos | |
activeServoList is a 4 bit list of the servos currently active. Initialize list with all servos inactive. | |
dripServosPos is a list of positions of each servo | |
dripRateList is a list of drip rates for stepping the servos | |
numActiveServos is the number of active servos | |
WhichColumn is used to indicate which servo has reached the bottom and needs to be reset | |
BucketSensorState is the state read from a coin sensor | |
PipeDown is a flag to indicate if we are in flag down mode | |
DripRate is the drip rate of all servos at any time | |
ServoSetoffRate is the set off rate of all servos at any time | |
BucketServiceInit | |
takes in priority number and returns boolean | |
Set MyPriority | |
call Bucket_HWInit to initialize all PWM channels and GPIOs | |
set currentState to be at InitializeBucketService | |
post ES_INIT to bucket service | |
PostBucketService | |
takes in ES_Event to be posted to queue and returns boolean | |
Post event to my priority queue | |
RunBucketService | |
takes in ES_Event which we need to respond to and return ES_Event indicating any errors | |
Set ReturnEvent to No_event | |
Declare ES_Event Event2PostAudio, Event2PostUncleEd, Event2PostPipe that might be used to post to those service | |
PipeDown is a module boolean variable set to true if pipe is currently down | |
switch case CurrentState: | |
InitializeBucketService: | |
If Event is ES_INIT: | |
set current state to be Ready2Play | |
Ready2Play: | |
If Event is StartButton pressed: | |
currentState = Playing | |
start DRIP_SETOFF_TIMER with SetOffRate interval | |
start Drip_Speed_Timer with DripRate as interval | |
Playing: | |
If Event is Timeout and Timer == DRIP_TIMER: | |
Call function StepAllServos from activeServoList | |
Start DripTimer with DripRate again | |
Else if Event is Timeout and Timer == DRIP_SETOFF_TIMER: | |
RandomFreeColumn = select random servo that is not in activeServoList. This should set off servo, LED on and put servo as unavailable | |
Start DripTimer with DripRate again | |
Else if Event is DripServoAtBucket: | |
WhichColumn = Event parameter (1,2,3 or 4) get which drip servo was at bucket | |
BucketSensorState = SampleBucketSensor(WhichColumn) to see whether bucket caught the water drop | |
if BucketSensorState caught the water drop and we are not in PipeDown: | |
call postAudioService ES_PLAY_TUNE with success sound | |
call ResetServo(WhichColumn) to put it back at the beginning, LED off and | |
make it available | |
Else if Event is DripServoAtBottom: | |
WhichColumn = Event parameter get which drip servo was at bottom | |
call postUncleEdService ES_LOST_POINT to update the LCD display | |
call postAudioService ES_PLAY_TUNE with lost sound to give feedback to user | |
call ResetServo(WhichColumn) to put it back at the beginning and LED off and | |
make it available | |
Else if Event is Timeout and Timer == PIPE_TIMER: | |
PipeDown = true | |
call SetOffAllDripServo | |
Else if Event is PipeRecovered: | |
PipeDown = false | |
call ResetAllServos | |
Else if Event is GameOver: | |
call ResetAllServos to put them back at their beginning position | |
CurrentState = Ready2Play to be ready for the next start button press | |
put PipeDown to be false just in case game ends during pipeDown | |
Else if Event Wrench2Goal: | |
get the speed at which Wrench player is performing from EventParam | |
Given this speed set the dripRate. The slower the wrench player the faster the water drops fall and viceversa. | |
return ReturnEvent | |
Bucket_HWInit | |
Initialize PWM pins for group 0 and 2 (PB6, PB7, PE4, PE5) to 50 Hz used for drip servo control | |
Set all the pulse widths to be at the initial position of the drip servo | |
Enable PE2, PE3, PC6, PC7 to be input for coin sensor reading | |
enable PD3, PD6, PD7 and PE1 to be output for the drip Servo LEDs | |
ResetAllServos call ResetServo for each servo | |
SetOffDripServo | |
takes nothing and return nothing | |
call ResetAllServos | |
set activeServoList as 0x0f to indicate all servos are running | |
for each LED | |
call TurnOnLED | |
endfor | |
StepAllServos | |
takes nothing and returns nothing | |
for each servo | |
if masked activeServoList with corresponding mask is true | |
increment the position by STEP_SIZE | |
if current servo position is at Bucket | |
post DripServoAtBucket to Bucket service with servo number as EventParam | |
endif | |
if current servo position is at Bottom | |
post DripServoAtBottom to Bucket service with servo number as EventParam | |
else | |
Set the PWM pulse width to the new Servo Position | |
endif | |
endif | |
endfor | |
ResetServo | |
takes uint8 indicating which servo to reset return nothing | |
flip the bit in activeServoList to make it inactive | |
set dripServoPos for corresponding servo to Initial position | |
set the pulse width of the PWM channel to Initial position | |
call TurnOffLED with the corresponding argument | |
decrement numActiveServos | |
RandServoList | |
call randNum = rand()%numAvailableServos to randomly select an available servo | |
initialize temp variable counter to 0 | |
for each dripServo | |
mask activeServoList to see if corresponding servo is active | |
if servo is not active and counter is at randNum | |
set this servo as active | |
turn on the corresponding LED | |
increment the numActiveServos by 1 | |
else if servo is not active but counter is not at randNum | |
increment counter by 1 | |
endif | |
endfor | |
TurnOnLED | |
take in number of LED to turn on and returns nothing | |
given the number in the argument it writes HIGH to the corresponding TIVA pin (LED 1-> PD3, LED 2-> PD6, 3-> PD7, 4-> PE1) | |
TurnOnLED | |
take in number of LED to turn off and returns nothing | |
given the number in the argument it writes LOW to the corresponding TIVA pin (LED 1-> PD3, LED 2-> PD6, 3-> PD7, 4-> PE1) | |
SampleBucketSensor | |
take in number of Coin Sensor to sample and returns boolean | |
given the number in the argument it reads the state of the corresponding TIVA pin (whichIR 1-> PE2, whichIR 2-> PE3, whichIR 3-> PC6, whichIR 4-> PC7) and puts the reading into temp variable ‘input’ | |
return ‘input’ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment