Sunteți pe pagina 1din 4

Pseudo-code for the DDM state machine module (a service that implements a state machine)

Data private to the module:


Defines:
AUDIO_CHECK_TIME - time between audio test events, in ms
AUDIO_THRESHOLD - ADC return value that both mics must be producing for a successful audio test (using volts
requires float values, which is probably possible but we can just ignore it for now)
REQUIRED_SINGING_TIME - time that heroes are required to sing (in ms) to complete singing stage
DURATIONMAX = REQUIRED_SINGING_TIME / AUDIO_CHECK_TIME - number of audio check events
required to pass singing stage
NUM_BOMBS - number of mines to destroy
Module Variables:
static int BombsLeft = NUM_BOMBS
static int Duration = 0
static int TargetToHit = 1;
static bool TargetStatus[NUM_BOMBS]
InitDDM_SM
Takes a priority number, returns True.
Initialize the MyPriority variable with the passed in parameter.
//Hardware stuff
Set up boogie board analog IN ports
Set up microphone analog IN ports
Call init functions for shift registers,
//Shift registers control target LEDs, acquired LED for when target is on, 5V output line, laser //diode
Call init functions for Servos
Set up digital ins for fire button, abort button
Set up digital ins for IR sensors in targets
Set CurrentState to be InitDDMState
Post Event ES_Init to DDM_SM queue (this service)
End of InitializeDDM_SM
RunDDM_SM (implements the state machine)
The EventType field of ThisEvent will be one of the following: ES_Init, AudioTimeout, MainTimeout,
AbortButtonDown, FireButtonDown, LaserOnDetector, LaserOffDetector, JoyTimeout. The parameter field of the
ThisEvent will be the time that the event occurred.
Returns ES_NO_EVENT

Local Variables: NextState


Static Local Variables: Duration, CurrentTarget
Set NextState to CurrentState
Based on the state of the CurrentState variable choose one of the following blocks of code:
CurrentState is InitDDM_SM
If ThisEvent is ES_Init

Start Audio Timer for duration AUDIO_CHECK_TIME


Call ShiftRegister_Reset(); (in shift register module- resets the
LEDs/laser/external line)
Set NextState to Fear
Endif
End InitDDM_SM block
CurrentState is Fear
If ThisEvent is AudioTimeout
if (areAudioPinsAboveThreshold())
Set NextState to Singing
Start AudioTimer for duration AUDIO_CHECK_TIME
Call UpdateIndicatorPosition
Duration++
Send FlagStart event to FlagService
else
Set NextState to CurrentState
Start AudioTimer for duration AUDIO_CHECK_TIME
Endif
Endif
If ThisEvent is MainTimeout or AbortButtonDown
FULL_RESET
EndIf
End Fear block
CurrentState is Singing
If ThisEvent is AudioTimeout
If areAudioPinsAboveThreshold() is false
Set Duration to 0
Call Servos_SetServo(indicator servo, 0%)
Restart audio timer for duration AUDIO_CHECK_TIME
Set NextState to CurrentState
Else If areAudioPinsAboveThreshold() is true
If Duration is DURATIONMAX
Set NextState to SharkOFF // shark is not on target by default
Call Servos_SetServo(indicator servo, 100%)
Light up bomb LEDs
Toggle Shark LED
Else If Duration is less than DURATIONMAX
Increase Duration by 1
Call Servos_SetServo(indicator servo, up by (1/Duration)%)
Restart audio timer for duration AUDIO_CHECK_TIME
Set NextState to CurrentState
Else
Error
EndIf
Else
Error
EndIf
EndIf
If ThisEvent is MainTimeout or AbortButtonDown
FULL_RESET
EndIf
End Singing block

CurrentState is SharkOFF
If ThisEvent is FireButtonDown
Set NextState to CurrentState
Endif
If ThisEvent is LaserOnDetector
Set CurrentTarget to ThisEvent.EventParam (EventParam is the target #)
Call ShiftRegister_TargetAcquiredLED(true)
Set NextState to SharkON
Endif
If ThisEvent is MainTimeout or Abort
FULL_RESET
EndIf
End SharkOFF block
CurrentState is SharkON
If ThisEvent is FireButtonDown
If BombsLeft is 1
Set NextState to Joy
Call ShiftRegister_TargetAcquiredLED(false)
Call ShiftRegister_ToggleTargetLEDs(CurrentTarget)
Post BeginJoySequence event to JoySequenceSM
Call ShiftRegister_ExternalLine5V(true)
Post FlagWin event to FlagService
Start Joy timer
Reset TargetToHit to 1
Else If BombsLeft is greater than 0
Set NextState to SharkOFF
Call ShiftRegister_TargetAcquiredLED(false)
Call ShiftRegister_ToggleTargetLEDs(CurrentTarget) //
switch red->grn
// Set Bomb LEDs to win & new target
Decrease BombsLeft by 1
Set TargetStatus of the target that was hit to false
Else
Error
EndIf
If ThisEvent is LaserOffDetector
Call ShiftRegister_TargetAcquiredLED(false)
Set NextState to SharkOFF
Endif
If ThisEvent is MainTimeout or AbortButtonDown
FULL_RESET
EndIf
End SharkON block
CurrentState is Joy
If ThisEvent is JoyTimeout or AbortButtonDown
FULL_RESET
EndIf
End Joy block
Set CurrentState to NextState
Return ES_NO_EVENT
End of RunDDM_SM

// Module Functions
boolean areAudioPinsAboveThreshold( void )
local variables: pin1val, pin2val
// get values from ADC_InSeq3() - will be integers from 0 to 4095
read from mic input pin 1 and store analog value to pin1val
read from mic input pin 2 and store analog value to pin2val
if pin1val > AUDIO_THRESHOLD and pin2val > AUDIO_THRESHOLD
return true
else
return false
end function
void UpdateIndicatorPosition( void )
newPosition = Duration / DURATIONMAX
Call Servos_SetServo(indicator servo, newPosition)
end UpdateIndicator Position
void FULL_RESET(void)
Call Servos_SetServo(indicator servo, 0%)
Call Servos_SetServo(shark servo, start position)
Call ShiftRegister_Reset()
Duration = 0
Bombs = NUM_BOMBS
end FULL_RESET
DDM_SM_state_t QueryDDM_SM()
return CurrentState
end QueryDDM_SM

S-ar putea să vă placă și