pseudo code.docx -...

21

Click here to load reader

Upload: vokiet

Post on 16-Mar-2018

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

Pin diagram

NC NC NC NC

PE0 AD0 Wine Tilt PE0 AD0 Wine Tilt

PE1 AD1 Strobe PE1 AD1 Strobe

Wine CLK PT7 AD2 Ack Wine CLK PT7 AD2 Ack

Wine RCK PT6 AD3 Data1 Wine RCK PT6 AD3 Data1

Wine Data PT5 AD4 Data2 Wine Data PT5 AD4 Data2

BAT ID Detect PT4 AD5 Data3 Music CLK PT4 AD5 Data3

Servo BM PT3 AD6 Data4 Music RCK PT3 AD6 Data4

PT2 AD7 Music Data PT2 AD7

PT1 PM5 FB CLK Servo Big PT1 PM5 165 CLK

BatSignal PT0 PM4 FB RCK Servo CW PT0 PM4 165 SL

VM Wine FB PM0 PM3 FB Data VM Wine FB PM0 PM3 165 Data

VM BM Puppet PM1 PM2 BM Puppet in VM CW Puppet PM1 PM2 CW Puppet In

Gnd Gnd Gnd Gnd

Page 2: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

Batman Communication State MachineCOMM STATE MACHINE

switch along CurentStateif CurrentState is Idle (data receiving)

if EventType = RisingStrobe set correct input/outputs on ADS12read dataassert Ackset correct input/outputs on ADS12CurrentState = Wait4StrobeFall

if EventType = SendReqcheck guard that strobe is not asserted

assert strobestart timerset correct input/outputs on ADS12assert dataCurrentState = Wait4Ack

if strobe is assertedsave new dataCurrentState = Wait2Receive

if CurrentState = Wait4StrobeFallif EventType = FallingStrobe

set correct input/outputs on ADS12deassert AckCurrentState = Idle

if CurrentState = Wait2Receiveif EventType = RisingStrobeset correct input/outputs on ADS12read dataassert AckCurrentState = Wait2Send

if CurrentState = Wait2Sendstart timeset correct input/outputs on ADS12deassert Ackassert NewData

assert strobe set correct input/outputs on ADS12 CurrentState = Wait4Ack;if CurrentState = Wait4Ack: set correct input/outputs on ADS12 if EventType is timeout repost send req else if EventType is RisingAck deassert strobe

Page 3: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

deassert data

COMM EVENT CHECKERS

static unsigned char LastStrobeState = 0;static unsigned char LastAckState = 0;

boolean CheckStrobe(void)declare CurrentStrobeState;check for pin high and different from last time

if CurrentStrobeState not equal to LastStrobeState :event detected, post detected event if CurrentStrobeState == 1 post Rising Stobe event else post falling strobe event

LastStrobeState = CurrentStrobeState;

boolean CheckAck(void) { declare CurrentAckState CurrentAckState = (PTIAD & BIT2HI) if CurrentAckState not equal to LastAckState if CurrentAckState == 1 post RisingAck event else do nothing, Ack fall

Page 4: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

Catwoman Communication State MachineCOMM STATE MACHINE

switch along CurentStateif CurrentState is Idle (data receiving)

if EventType = RisingStrobe set correct input/outputs on ADS12read dataassert Ackset correct input/outputs on ADS12CurrentState = Wait4StrobeFall

if EventType = SendReqcheck guard that strobe is not asserted

assert strobestart timerset correct input/outputs on ADS12assert dataCurrentState = Wait4Ack

if strobe is assertedsave new dataCurrentState = Wait2Receive

if CurrentState = Wait4StrobeFallif EventType = FallingStrobe

set correct input/outputs on ADS12deassert AckCurrentState = Idle

if CurrentState = Wait2Receiveif EventType = RisingStrobeset correct input/outputs on ADS12read dataassert AckCurrentState = Wait2Send

if CurrentState = Wait2Sendstart timeset correct input/outputs on ADS12deassert Ackassert NewData

assert strobe set correct input/outputs on ADS12 CurrentState = Wait4Ack;if CurrentState = Wait4Ack: set correct input/outputs on ADS12 if EventType is timeout repost send req else if EventType is RisingAck deassert strobe

Page 5: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

deassert data

COMM EVENT CHECKERS

static unsigned char LastStrobeState = 0;static unsigned char LastAckState = 0;

boolean CheckStrobe(void)declare CurrentStrobeState;check for pin high and different from last time

if CurrentStrobeState not equal to LastStrobeState :event detected, post detected event if CurrentStrobeState == 1 post Rising Stobe event else post falling strobe event

LastStrobeState = CurrentStrobeState;

boolean CheckAck(void) { declare CurrentAckState CurrentAckState = (PTIAD & BIT2HI) if CurrentAckState not equal to LastAckState if CurrentAckState == 1 post RisingAck event else do nothing, Ack fall

Page 6: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

BATID State machine

Pseudo-code for the BATID module

InitializeMorseElementsTakes a priority number, returns True.

Initialize the MyPriority variable with the passed in parameter.Initialize the port line to receive Morse codeSet CurrentState to be InitMorseElementsSet FirstDelta to 0End of InitializeMorseElements

CheckMorseEventsTakes no parameters, returns True if an event was posted Static local: LastInputState; local RetrunVal = False

Get CurrentState of the input lineIf the state of the Morse input line has changed

If the current state of the input line is highPostEvent RisingEdge with parameter CurrentTime

Else (current input state is low)PostEvent FallingEdge with parameter CurrentTime

EndifSet ReturnVal = True

EndifSet LastInputState to CurrentStateReturn ReturnVal

MorseElementsSM (implements the state machine for Morse Elements)The EventType field of ThisEvent will be one of: RisingEdge, FallingEdge, CalibrationCompleted, EOCDetected, EOWDetected, ButtonDown. The parameter field of the ThisEvent will be the time that the event occurred.Returns True

Local Variables: NextState

Set NextState to CurrentStateBased on the state of the CurrentState variable choose one of the following blocks of code:

CurrentState is InitMorseElementsIf ThisEvent is EF_Init

Set NextState to CalWaitForRiseEndif

End InitMorseElements block

CurrentState is CalWaitForRiseIf ThisEvent is RisingEdge

Set TimeOfLastRise to ThisTime

Page 7: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

Set NextState to CalWaitForFallEndif If ThisEvent is CalibrationComplete

Set NextState to EOC_WaitRiseEndif

End CalWaitForRise block

CurrentState is CalWaitForFallIf ThisEvent is FallingEdge

Set TimeOfLastFall to ThisTimeSet NextState to CalWaitForRiseCall TestCalibrationEndIf

End CalWaitForFall block

CurrentState is EOC_WaitRiseIf ThisEvent is RisingEdge

Set TimeOfLastRise to ThisTimeSet NextState to EOC_WaitFallCall CharacterizeSpace function

Endif If ThisEvent is ButtonDown

Set NextState to CalWaitForRiseSet FirstDelta to 0

Endif End EOC_WaitRise block

CurrentState is EOC_WaitFallIf ThisEvent is FallingEdge

Set TimeOfLastFall to ThisTimeSet NextState to EOC_WaitRiseEndIf

If ThisEvent is ButtonDownSet NextState to CalWaitForRiseSet FirstDelta to 0

Endif If ThisEvent is EOCDetected

Set NextState to DecodeWaitFallEndif

End EOC_WaitFall block

CurrentState is DecodeWaitRiseIf ThisEvent is RisingEdge

Set TimeOfLastRise to ThisTimeSet NextState to DecodeWaitFallCall CharacterizeSpace function

Endif If ThisEvent is ButtonDown

Set NextState to CalWaitForRise

Page 8: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

Set FirstDelta to 0Endif

End DecodeWaitRise block

CurrentState is DecodeWaitFallIf ThisEvent is FallingEdge

Set TimeOfLastFall to ThisTimeSet NextState to DecodeWaitRiseCall CharacterizePulse function

Endif If ThisEvent is ButtonDown

Set NextState to CalWaitForRiseSet FirstDelta to 0

Endif End DecodeWaitFall block

CurrentState is StopDecodingIf ThisEvent is RestartDecoding

Set NextState to CatWaitRise

Set CurrentState to NextState

If ThisEvent is DecodedSet NextState to Stop Decoding

Return TrueEnd of MorseElementsSM

TestCalibrationTakes no parameters, returns nothing.

Local variable SecondDelta

If calibration is just starting (FirstDelta is 0)Set FirstDelta to most recent pulse width

ElseSet SecondDelta to most recent pulse widthIf (100 * FirstDelta / SecondDelta) less than or equal to 33

Save FirstDelta as LengthOfDotPostEvent CalCompleted

ElseIf (100 * FirstDelta / Second Delta) greater than or equal to 300Save SecondDelta as LengthOfDotPostEvent CalCompleted

Else (prepare for next pulse)SetFirstDelta to SecondDelta

Page 9: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

EndIfEndIf

Return End of TestCalibration

CharacterizeSpaceTakes no parameters, returns nothing.Posts one of EOCDetected Event, EOWDetected Event, BadSpace Event

Local variable LastInterval

Calculate LastInterval as TimeOfLastRise TimeOfLastFall�If LastInterval OK for a Character Space

PostEvent EOCDetected EventElse

If LastInterval OK for Word SpacePostEvent EOWDetected Event

ElsePostEvent BadSpace Event

EndIfEndIfReturnEnd of CharacterizeSpace

CharacterizePulseTakes no parameters, returns nothing.Posts one of DotDetectedEvent, DashDetectedEvent, BadPulseEvent,

Local variable LastPulseWidth

Calculate LastPulseWidth as TimeOfLastFall - TimeOfLastRiseIf LastPulseWidth OK for a dot

PostEvent DotDetected EventElse

If LastPulseWidth OK for dashPostEvent DashDetected Event

ElsePostEvent BadPulse Event

EndIfEndIfReturn End of CharacterizePulse

Page 10: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

Pseudo-code for the Decode Morse module Data private to the module: MorseString, the arrays LegalChars and MorseCode

InitMorseDecodeTakes a priority number, returns True.

Initialize the MyPriority variable with the passed in parameter.Call ClearMorseCharEnd of InitMorseChar

MorseDecodeSM (implements the state machine for Morse Decode, only 1 state)The EventType field of ThisEvent will be one of: DotDetectedEvent, DashDetectedEvent, EOCDetected, EOWDetected, ButtonDown.Returns True if No Error detected, False otherwise

Based on the state of the ThisEvent variable choose one of the following blocks of code:If ThisEvent is DotDetected Event

If there is room for another Morse element in the internal representationAdd a Dot to the internal representationReturn True

ElseReturn False

End if ThisEvent is DotDetected Event

If ThisEvent is DashDetected EventIf there is room for another Morse element in the internal representation

Add a Dash to the internal representationReturn True

ElseReturn False

End if ThisEvent is DashDetected Event

If ThisEvent is EOCDetected EventIf call to DecodeMorse does not return an error

Print to LCD the decoded characterCall ClearMorseCharReturn True

ElseReturn False

End if is EOCDetected Event

If ThisEvent is EOWDetected EventIf call to DecodeMorse does not return an error

Print to LCD the decoded characterPrint to the LCD a spaceCall ClearMorseCharReturn True

Else

Page 11: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

Return FalseEnd if ThisEvent is EOWDetected Event

If ThisEvent is ButtonDown Event (Added 11/3/11 JEC)Call ClearMorseCharReturn True

End if ThisEvent is ButtonDown Event

End DecodeMorseSM

ClearMorseCharTakes no parameters, returns nothing

Clear (empty) the MorseString variableEnd of ClearMorseChar

DecodeMorseTakes no parameters, returns either a character or a symbolic value indicating failure

For every entry in the array MorseCodeIf MorseString is the same as current position in MorseCode

return contents of current position in LegalCharsEndIf

EndForKeep storing last 6 legal chars in an arrayIf last 6 legal chars = BATMAN

Post Event Stop Decoding to morseElemtents State Machine

return ERROR, since we didn't find a matching string in MorseCode

Page 12: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

Other Batman Event Checkers

Apart from communication and BatID, batman state machine had a puppet event checker

bmPinChecker - bat puppetif current state is talking and

if current PM2 state is not equal to last PM2 state and current PM2 state is highincrement a counter (make get and set function to access counter from state machine)

Note - This counter is cleared every time timer for puppet runs outlastPM2state = currentPM2state

Catwoman Event Checkers

cwPinChecker - cw puppetif current state is talking and

if current PM2 state is not equal to last PM2 state and current PM2 state is highincrement a counter (make get and set function to access counter from state machine)

Note - This counter is cleared every time timer for puppet runs out

lastPM2state = currentPM2state

cwShiftCheckerinitialize stuff

pulse shift loadpulse clock 10 times and store the 10 button states

if button state 1 has changed and is equal to lowES_BM_MASK ON

setlastbutton1value to currentbutton1value

if button state 2 has changed and is equal to lowES_CW_MASK ON

setlastbutton2value to currentbutton2value

if button state 3 has changed and is equal to lowES_BM_PROPOSE with parameter 1

setlastbutton3value to currentbutton3value

if button state 4 has changed and is equal to lowES_BM_PROPOSE with parameter 2

setlastbutton4value to currentbutton4value

if button state 5 has changed and is equal to lowES_BM_PROPOSE with parameter 3

Page 13: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

setlastbutton5value to currentbutton5value

if button state 6 has changed and is equal to lowES_BM_PROPOSE with parameter 4

setlastbutton6value to currentbutton6value

if button state 7 has changed and is equal to lowES_CW_DECIDE with parameter 1

setlastbutton7value to currentbutton7value

if button state 8 has changed and is equal to lowES_CW_DECIDE with parameter 2

setlastbutton8value to currentbutton8value

if button state 9 has changed and is equal to lowES_CW_DECIDE with parameter 3

setlastbutton9value to currentbutton9value

if button state 10 has changed and is equal to lowES_CW_DECIDE with parameter 4

setlastbutton10value to currentbutton10value

Batman State Machine Pseudocodedeclare static LastState variabledeclare CurrentState variableswitch through CurrentState

case LOCKOUT:if ES_BATID_DETECTED

make cool design with wine LEDs / otherwise indicate that game as startedmake sure servo positions from last game have been resetstart GameTimerbegin BatSignalsend COMM_BATID_DETECTEDif both masks are not on

CurrentState = MASKWAITINGelse

CurrentState =TALKINGstart Talk Timerstart Puppet TimerPlayer LED green for both batman and catwoman

break

case MASKWAITING:if COMM_MASKS_ONCurrentState = TALKING

Page 14: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

start Talk Timerstart Puppet TimerPlayer LED green for both batman and catwoman

break

case TALKING:if Puppet timer runs out

check if catwoman Communicated speed == bat puppet speedchange Performance LED status to goodelse if catwoman comm speed > bat puppet speed (using counter in

event checkers)change performance LED to slowvibrate puppets

elsechange performance LED to fastvibrate puppets

reset puppet timer

if talk timer runs outcheck if performance was good for a specific amount of time

give good feedback to both batman and CWrotate servos

else give bad feedback to bothrotate servos in bad direction

set Batman play LED green Catwoman play LED red

case BATMAN WINE POURif wine tilt event

check volume of wineset increase LEDs accordinglyif volume > glass size

wine spill eventif voume = glass size +- delta

wine fill eventif wine fill event

rotate serovs positivefeedback LED batman positiveAudio feedbacksend wine fill comm to CWCurrentState = CW WINE FILL

if wine spill eventrotate servo negativefeedback LED batman negativesend wine spill event to CWCurrentState = CW WINE FILL

same timer code as other states

Page 15: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

break;

case catwoman wine fillif wine fill event

rotate serovs positivefeedback LED batman positiveAudio feedbackCurrentState = Propose

if wine spill eventrotate servo negativefeedback LED batman negativeCurrentState = Propose

same timer code as other statesbreak;

case PROPOSEif good propose

current state = decideelse

current state = decidesame timer code as other states

case DECIDEcurrent state = game oversame timer code as other states

case Game overgame time out

Page 16: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

Catwoman State Machine

declare static LastState variabledeclare CurrentState variableswitch through CurrentState

case LOCKEDif received COMM_BATID_DETECTEDstart game timerif both masks are not on

currentState = mask waitingelse

currentState = Talkingcommunicate both masks on to batman state machine

case WAITING:if ES_CW_MASK_ON && ES_BM_MASK_ON

send COMM_MASKS_ONCurrentState = TALKING

case TALKING:if ES_CW_TALK_FAST

send COMM_CW_TALK_FASTif ES_CW_TALK_SLOW

send COMM_CW_TALK_SLOWif received COMM_TALKING_PERF_GOOD

move servo in good direc, give feedbackif received COMM_TALKING_PERF_BAD

move servo in bad direc, give feedback

case BM_WINE_POURING:if BM wine fill event

rotate serovs positivefeedback LED batman positiveAudio feedbackCurrentState = CW WINE pour

if wine spill eventrotate servo negativefeedback LED batman negativeCurrentState = CW WINE FILL

break;

Case CW Wine pourif wine tilt event

check volume of wine

Page 17: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

set increase LEDs accordinglyif volume > glass size

wine spill eventif volume = glass size +- delta

wine fill eventif wine fill event

rotate serovs positivefeedback LED batman positiveAudio feedbacksend wine fill comm to CMCurrentState = Propose

if wine spill eventrotate servo negativefeedback LED batman negativesend wine spill event to BMCurrentState = Propose

break;

case Proposeif good propose

rotate catwoman servo positiveplay audiocommunicate good propose to batmancurrent state = decide

if good proposerotate catwoman servo negativeplay audiocommunicate bad propose to batmancurrent state = decide

case DECIDEif accept

rotate big servo positiveplay accept audo

if rejectrotate big servo negativeplay reject audio

current state = game over

case gameoverif event lockout received from batman state machine

current state = lockout

Page 18: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

Pseudo code for wine pourcurrentAccRead = 0static overallWine = 0delta wine = 0 firstAccReading = get initial accelerometer reading

On timeoutgetCurrentAccReading from PTIAD0deltaWine = Timer value * (CurrentAccReading -FirstAccReading)overallWine = overallWine + deltaWine

if CurrentAccReaidng is above a cetain rangeWINE SPILL EVENT

else if overallWine is at a certain valueaccordingly increase the number of LEDS

if overallwine = fill threshold && less than spill thresholdWINE FILL EVENT

else if overallwine is > spillThresholdWINESPILL EVENT

pseudo code for puppet talkcatwomanif rising edge, counter++

if talking timer timeoutcheck value of counterif high - send event to batman talk speed highelse low - send event to batman talk speed lowcounter = 0restart timer

batmanif rising edge, counter++

if talking timer timeoutcheck value of counter

set a value high speed or lowcounter = 0restart timer

if batman and catwoman event value set variable if match

Page 19: Pseudo Code.docx - me218batdate.weebly.comme218batdate.weebly.com/uploads/1/5/4/0/15409644/ps…  · Web viewBatman Communication State ... TimeOfLastFallIf LastInterval OK for a

green LEDif batman > catwoman

red fast LEDvibrate

if batman < catwomanred slow LEDVibrate