Pseudo Code.docx



Pin diagram NCNCNCNCPE0AD0Wine TiltPE0AD0Wine TiltPE1AD1StrobePE1AD1StrobeWine CLKPT7AD2AckWine CLKPT7AD2AckWine RCKPT6AD3Data1Wine RCKPT6AD3Data1Wine DataPT5AD4Data2Wine DataPT5AD4Data2BAT ID DetectPT4AD5Data3Music CLKPT4AD5Data3Servo BMPT3AD6Data4Music RCKPT3AD6Data4PT2AD7Music DataPT2AD7PT1PM5FB CLKServo BigPT1PM5165 CLKBatSignalPT0PM4FB RCKServo CWPT0PM4165 SLVM Wine FBPM0PM3FB DataVM Wine FBPM0PM3165 DataVM BM PuppetPM1PM2BM Puppet inVM CW PuppetPM1PM2CW Puppet InGndGndGndGndBatman Communication State MachineCOMM STATE MACHINEswitch along CurentStateif CurrentState is Idle (data receiving)if EventType = RisingStrobe set correct input/outputs on ADS12read dataassert Ackset correct input/outputs on ADS12CurrentState = Wait4StrobeFallif EventType = SendReqcheck guard that strobe is not assertedassert strobestart timerset correct input/outputs on ADS12assert dataCurrentState = Wait4Ackif strobe is assertedsave new dataCurrentState = Wait2Receiveif CurrentState = Wait4StrobeFallif EventType = FallingStrobeset correct input/outputs on ADS12deassert AckCurrentState = Idleif CurrentState = Wait2Receiveif EventType = RisingStrobeset correct input/outputs on ADS12read dataassert AckCurrentState = Wait2Sendif 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 deassert dataCOMM 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 fallCatwoman Communication State MachineCOMM STATE MACHINEswitch along CurentStateif CurrentState is Idle (data receiving)if EventType = RisingStrobe set correct input/outputs on ADS12read dataassert Ackset correct input/outputs on ADS12CurrentState = Wait4StrobeFallif EventType = SendReqcheck guard that strobe is not assertedassert strobestart timerset correct input/outputs on ADS12assert dataCurrentState = Wait4Ackif strobe is assertedsave new dataCurrentState = Wait2Receiveif CurrentState = Wait4StrobeFallif EventType = FallingStrobeset correct input/outputs on ADS12deassert AckCurrentState = Idleif CurrentState = Wait2Receiveif EventType = RisingStrobeset correct input/outputs on ADS12read dataassert AckCurrentState = Wait2Sendif 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 deassert dataCOMM 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 fallBATID State machine Pseudo-code for the BATID moduleInitializeMorseElementsTakes 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 InitializeMorseElementsCheckMorseEventsTakes no parameters, returns True if an event was posted Static local: LastInputState; local RetrunVal = FalseGet CurrentState of the input lineIf the state of the Morse input line has changedIf the current state of the input line is highPostEvent RisingEdge with parameter CurrentTimeElse (current input state is low)PostEvent FallingEdge with parameter CurrentTimeEndifSet ReturnVal = TrueEndifSet LastInputState to CurrentStateReturn ReturnValMorseElementsSM (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: NextStateSet NextState to CurrentStateBased on the state of the CurrentState variable choose one of the following blocks of code:CurrentState is InitMorseElementsIf ThisEvent is EF_InitSet NextState to CalWaitForRiseEndif End InitMorseElements blockCurrentState is CalWaitForRiseIf ThisEvent is RisingEdgeSet TimeOfLastRise to ThisTimeSet NextState to CalWaitForFallEndif If ThisEvent is CalibrationCompleteSet NextState to EOC_WaitRiseEndif End CalWaitForRise blockCurrentState is CalWaitForFallIf ThisEvent is FallingEdgeSet TimeOfLastFall to ThisTimeSet NextState to CalWaitForRiseCall TestCalibrationEndIfEnd CalWaitForFall blockCurrentState is EOC_WaitRiseIf ThisEvent is RisingEdgeSet TimeOfLastRise to ThisTimeSet NextState to EOC_WaitFallCall CharacterizeSpace functionEndif If ThisEvent is ButtonDownSet NextState to CalWaitForRiseSet FirstDelta to 0Endif End EOC_WaitRise blockCurrentState is EOC_WaitFallIf ThisEvent is FallingEdgeSet TimeOfLastFall to ThisTimeSet NextState to EOC_WaitRiseEndIfIf ThisEvent is ButtonDownSet NextState to CalWaitForRiseSet FirstDelta to 0Endif If ThisEvent is EOCDetectedSet NextState to DecodeWaitFallEndif End EOC_WaitFall blockCurrentState is DecodeWaitRiseIf ThisEvent is RisingEdgeSet TimeOfLastRise to ThisTimeSet NextState to DecodeWaitFallCall CharacterizeSpace functionEndif If ThisEvent is ButtonDownSet NextState to CalWaitForRiseSet FirstDelta to 0Endif End DecodeWaitRise blockCurrentState is DecodeWaitFallIf ThisEvent is FallingEdgeSet TimeOfLastFall to ThisTimeSet NextState to DecodeWaitRiseCall CharacterizePulse functionEndif If ThisEvent is ButtonDownSet NextState to CalWaitForRiseSet FirstDelta to 0Endif End DecodeWaitFall blockCurrentState is StopDecodingIf ThisEvent is RestartDecodingSet NextState to CatWaitRiseSet CurrentState to NextStateIf ThisEvent is DecodedSet NextState to Stop DecodingReturn TrueEnd of MorseElementsSM TestCalibrationTakes no parameters, returns nothing. Local variable SecondDeltaIf calibration is just starting (FirstDelta is 0)Set FirstDelta to most recent pulse widthElseSet SecondDelta to most recent pulse widthIf (100 * FirstDelta / SecondDelta) less than or equal to 33Save FirstDelta as LengthOfDotPostEvent CalCompletedElseIf (100 * FirstDelta / Second Delta) greater than or equal to 300Save SecondDelta as LengthOfDotPostEvent CalCompletedElse (prepare for next pulse)SetFirstDelta to SecondDeltaEndIfEndIfReturn End of TestCalibrationCharacterizeSpaceTakes no parameters, returns nothing.Posts one of EOCDetected Event, EOWDetected Event, BadSpace EventLocal variable LastIntervalCalculate LastInterval as TimeOfLastRise ? TimeOfLastFallIf LastInterval OK for a Character SpacePostEvent EOCDetected EventElseIf LastInterval OK for Word SpacePostEvent EOWDetected EventElsePostEvent BadSpace EventEndIfEndIfReturnEnd of CharacterizeSpaceCharacterizePulseTakes no parameters, returns nothing.Posts one of DotDetectedEvent, DashDetectedEvent, BadPulseEvent,Local variable LastPulseWidthCalculate LastPulseWidth as TimeOfLastFall - TimeOfLastRiseIf LastPulseWidth OK for a dotPostEvent DotDetected EventElseIf LastPulseWidth OK for dashPostEvent DashDetected EventElsePostEvent BadPulse EventEndIfEndIfReturn End of CharacterizePulsePseudo-code for the Decode Morse module Data private to the module: MorseString, the arrays LegalChars and MorseCodeInitMorseDecodeTakes a priority number, returns True. Initialize the MyPriority variable with the passed in parameter.Call ClearMorseCharEnd of InitMorseCharMorseDecodeSM (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 otherwiseBased on the state of the ThisEvent variable choose one of the following blocks of code:If ThisEvent is DotDetected EventIf there is room for another Morse element in the internal representationAdd a Dot to the internal representationReturn TrueElseReturn FalseEnd if ThisEvent is DotDetected EventIf ThisEvent is DashDetected EventIf there is room for another Morse element in the internal representationAdd a Dash to the internal representationReturn TrueElseReturn FalseEnd if ThisEvent is DashDetected EventIf ThisEvent is EOCDetected EventIf call to DecodeMorse does not return an errorPrint to LCD the decoded characterCall ClearMorseCharReturn TrueElseReturn FalseEnd if is EOCDetected EventIf ThisEvent is EOWDetected EventIf call to DecodeMorse does not return an errorPrint to LCD the decoded characterPrint to the LCD a spaceCall ClearMorseCharReturn TrueElseReturn FalseEnd if ThisEvent is EOWDetected EventIf ThisEvent is ButtonDown Event (Added 11/3/11 JEC)Call ClearMorseCharReturn TrueEnd if ThisEvent is ButtonDown EventEnd DecodeMorseSMClearMorseCharTakes no parameters, returns nothingClear (empty) the MorseString variableEnd of ClearMorseCharDecodeMorseTakes no parameters, returns either a character or a symbolic value indicating failureFor every entry in the array MorseCodeIf MorseString is the same as current position in MorseCode return contents of current position in LegalCharsEndIfEndForKeep storing last 6 legal chars in an arrayIf last 6 legal chars = BATMANPost Event Stop Decoding to morseElemtents State Machinereturn ERROR, since we didn't find a matching string in MorseCodeOther Batman Event CheckersApart from communication and BatID, batman state machine had a puppet event checkerbmPinChecker - 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 = currentPM2stateCatwoman Event CheckerscwPinChecker - 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 outlastPM2state = currentPM2statecwShiftCheckerinitialize stuffpulse shift loadpulse clock 10 times and store the 10 button statesif button state 1 has changed and is equal to lowES_BM_MASK ONsetlastbutton1value to currentbutton1valueif button state 2 has changed and is equal to lowES_CW_MASK ONsetlastbutton2value to currentbutton2valueif button state 3 has changed and is equal to lowES_BM_PROPOSE with parameter 1setlastbutton3value to currentbutton3valueif button state 4 has changed and is equal to lowES_BM_PROPOSE with parameter 2setlastbutton4value to currentbutton4valueif button state 5 has changed and is equal to lowES_BM_PROPOSE with parameter 3setlastbutton5value to currentbutton5valueif button state 6 has changed and is equal to lowES_BM_PROPOSE with parameter 4setlastbutton6value to currentbutton6valueif button state 7 has changed and is equal to lowES_CW_DECIDE with parameter 1setlastbutton7value to currentbutton7valueif button state 8 has changed and is equal to lowES_CW_DECIDE with parameter 2setlastbutton8value to currentbutton8valueif button state 9 has changed and is equal to lowES_CW_DECIDE with parameter 3setlastbutton9value to currentbutton9valueif button state 10 has changed and is equal to lowES_CW_DECIDE with parameter 4setlastbutton10value to currentbutton10valueBatman State Machine Pseudocodedeclare static LastState variabledeclare CurrentState variableswitch through CurrentStatecase LOCKOUT:if ES_BATID_DETECTEDmake 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 catwomanbreakcase MASKWAITING:if COMM_MASKS_ONCurrentState = TALKINGstart Talk Timerstart Puppet TimerPlayer LED green for both batman and catwomanbreakcase TALKING:if Puppet timer runs outcheck 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 puppetselsechange performance LED to fastvibrate puppetsreset puppet timerif talk timer runs outcheck if performance was good for a specific amount of timegive good feedback to both batman and CWrotate servoselse give bad feedback to bothrotate servos in bad directionset Batman play LED green Catwoman play LED redcase BATMAN WINE POURif wine tilt eventcheck volume of wineset increase LEDs accordinglyif volume > glass sizewine spill eventif voume = glass size +- deltawine fill eventif wine fill eventrotate serovs positivefeedback LED batman positiveAudio feedbacksend wine fill comm to CWCurrentState = CW WINE FILLif wine spill eventrotate servo negativefeedback LED batman negativesend wine spill event to CWCurrentState = CW WINE FILLsame timer code as other statesbreak;case catwoman wine fillif wine fill eventrotate serovs positivefeedback LED batman positiveAudio feedbackCurrentState = Proposeif wine spill eventrotate servo negativefeedback LED batman negativeCurrentState = Proposesame timer code as other statesbreak;case PROPOSEif good proposecurrent state = decideelse current state = decidesame timer code as other statescase DECIDEcurrent state = game oversame timer code as other statescase Game overgame time outCatwoman State Machinedeclare static LastState variabledeclare CurrentState variableswitch through CurrentStatecase LOCKEDif received COMM_BATID_DETECTEDstart game timerif both masks are not oncurrentState = mask waitingelse currentState = Talkingcommunicate both masks on to batman state machinecase WAITING:if ES_CW_MASK_ON && ES_BM_MASK_ONsend COMM_MASKS_ONCurrentState = TALKINGcase TALKING:if ES_CW_TALK_FASTsend COMM_CW_TALK_FASTif ES_CW_TALK_SLOWsend COMM_CW_TALK_SLOWif received COMM_TALKING_PERF_GOODmove servo in good direc, give feedbackif received COMM_TALKING_PERF_BADmove servo in bad direc, give feedbackcase BM_WINE_POURING:if BM wine fill eventrotate serovs positivefeedback LED batman positiveAudio feedbackCurrentState = CW WINE pourif wine spill eventrotate servo negativefeedback LED batman negativeCurrentState = CW WINE FILLbreak;Case CW Wine pourif wine tilt eventcheck volume of wineset increase LEDs accordinglyif volume > glass sizewine spill eventif volume = glass size +- deltawine fill eventif wine fill eventrotate serovs positivefeedback LED batman positiveAudio feedbacksend wine fill comm to CMCurrentState = Proposeif wine spill eventrotate servo negativefeedback LED batman negativesend wine spill event to BMCurrentState = Proposebreak;case Proposeif good proposerotate catwoman servo positiveplay audiocommunicate good propose to batmancurrent state = decideif good proposerotate catwoman servo negativeplay audiocommunicate bad propose to batmancurrent state = decidecase DECIDEif acceptrotate big servo positiveplay accept audoif rejectrotate big servo negativeplay reject audiocurrent state = game overcase gameoverif event lockout received from batman state machinecurrent state = lockoutPseudo code for wine pourcurrentAccRead = 0static overallWine = 0delta wine = 0 firstAccReading = get initial accelerometer readingOn timeoutgetCurrentAccReading from PTIAD0deltaWine = Timer value * (CurrentAccReading -FirstAccReading)overallWine = overallWine + deltaWineif CurrentAccReaidng is above a cetain rangeWINE SPILL EVENTelse if overallWine is at a certain valueaccordingly increase the number of LEDSif overallwine = fill threshold && less than spill thresholdWINE FILL EVENTelse if overallwine is > spillThresholdWINESPILL EVENTpseudo 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 timerbatmanif rising edge, counter++if talking timer timeoutcheck value of counterset a value high speed or lowcounter = 0restart timerif batman and catwoman event value set variable if matchgreen LEDif batman > catwomanred fast LEDvibrateif batman < catwomanred slow LEDVibrate ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download