Você está na página 1de 5

FlagService

Defines
uint32_t FLAG_UPDATE_INTERVAL - time in ms at which to adjust flag position
InitFlagService
Takes a priority number, returns True.
Initialize the MyPriority variable with the passed in parameter.
End of InitFlagService
RunFlagService
static local variable CurrentPosition // percentage of total servo motion
local variable newPosition
Based on the state of the ThisEvent variable choose one of the following blocks of code:
If ThisEvent is FlagStart Event
Call Servos_SetServo(flag servo, 0%)
Start MainTimer at 60s
Start FlagTimer at FLAG_UPDATE_INTERVAL
End if ThisEvent is FlagStart Event
If ThisEvent is FlagWin Event (Added 11/3/11 JEC)
CurrentPosition = 100;
Call Servos_SetServo(flag servo, 100%)
End if ThisEvent is FlagWin Event
if ThisEvent is FlagTimeout
newPosition = CurrentPosition + FLAG_UPDATE_INTERVAL/60,000
CurrentPosition = newPosition
Call Servos_SetServo(flag servo, newPosition)
End if ThisEvent is FlagTimeout Event
if ThisEvent is AbortButtonDown
CurrentPosition = 0;
Call Servos_SetServo(flag servo, 0%)
End if ThisEvent is AbortButtonDown Event
End of RunFlag
JoySequenceSM
Defines
uint32_t JOY_UPDATE_INTERVAL - time in ms at which to change display
InitJoySequenceSM
Takes a priority number, returns True.
Initialize the MyPriority variable with the passed in parameter.
Set CurrentState to be InitJoyState
Post Event ES_Init to JoySequenceSM queue (this service)
End of InitJoyService

RunJoySequenceSM
Returns ES_NO_EVENT
Local Variables: NextState
Set NextState to CurrentState
Based on the state of the CurrentState variable choose one of the following blocks of code:
CurrentState is InitJoySequenceSM
If ThisEvent is ES_Init
Set NextState to JoyWaiting
Endif
End InitJoySequenceSM block
CurrentState is JoyWaiting
If ThisEvent is BeginJoySequence
Call ShiftRegister_AllLEDsOn()
Start JoyTimer for JOY_UPDATE_INTERVAL
Set NextState to JoyState1
Endif
End Waiting block
CurrentState is JoyState1
If ThisEvent is JoySequenceTimeout
Call ShiftRegister_AllLEDsOff()
Start JoyTimer for JOY_UPDATE_INTERVAL
Set NextState to JoyState2
Endif
If ThisEvent is AbortButtonDown or JoyTimeout
NextState = Waiting
EndIf
End JoyState1 block
CurrentState is JoyState2
If ThisEvent is JoySequenceTimeout
Call ShiftRegister_AllLEDsOn()
Start JoyTimer for JOY_UPDATE_INTERVAL
Set NextState to JoyState1
Endif
If ThisEvent is AbortButtonDown or JoyTimeout
NextState = Waiting
EndIf
End JoyState2 block
End of RunJoySequenceSM
SharkPosition Service
Data private to the module: (static) CurrentPosition
Defines

SHARK_UPDATE_TIME - interval in ms at which to update shark position


LEFT_MAX_POSITION - values in terms of servo function inputs
RIGHT_MAX_POSITION
VELOCITY_MAX - fastest speed we want the shark to rotate, units of percent of total range / ms
VELOCITY_MAX_PER_UPDATE = VELOCITY_MAX / SHARK_UPDATE_TIME - fastest speed shark can
rotate in units of percent of total range / update
ZERO_THRESHOLD - offset value to the boogie board analog input under which the shark will stay at its current
position
InitSharkPosition
Takes a priority number, returns True.
Initialize the MyPriority variable with the passed in parameter.
End of InitSharkPosition
RunSharkPosition (implements the service for SharkPosition)
Returns ES_NO_EVENT if No Error detected, ES_ERROR otherwise
local var ReturnValue initialized to ES_NO_EVENT
if QueryDDM_SM returns something other than SharkON or SharkOFF, then return ES_NO_EVENT
Based on the state of the ThisEvent variable choose one of the following blocks of code:
If ThisEvent is SharkTimeout Event
Call UpdateSharkPosition()
End if ThisEvent is SharkTimeout Event
return ReturnValue
End RunSharkPosition
Module Functions:
void UpdateSharkPosition( void )
// measure the input signal from the boogie boards and translate into a velocity. Then, calculate the next
position of the shark using theta = d_theta/dt * dt (theta is the servo position, d_theta/dt is the velocity according to
the analog input, dt is the SharkTimer interval)
// from analog library, the ADC will return values between 0 and 4095 corresponding to 0-3.3V
local variables uint32_t board1input, board2input,
signed int32 velocity, netInput
static local variable CurrentPosition // as a fraction of total range
read board input pin 1 and store to board1input
read board input pin 2 and store to board2input
netInput = board1input + board2input - 4095// will be some integer from -4095 to 4095
if netInput > 0 and netInput < ZERO_THRESHOLD then return
if netInput < 0 and -1 * netInput < ZERO_THRESHOLD then return
velocity = netInput / 4095 * VELOCITY_MAX_PER_UPDATE

newPosition = CurrentPosition + velocity * SHARK_UPDATE_TIME


if newPosition >= RIGHT_MAX_POSITION then set newPosition = RIGHT_MAX_POSITION
if newPosition <= LEFT_MAX_POSITION then set newPosition = LEFT_MAX_POSITION
Call Servos_SetServo(Shark servo, newPosition)
end UpdateSharkPosition
void ResetSharkPosition ( void )
Call Servos_SetServo(Shark servo, 50%)
end ResetSharkPosition

AbortButton Service
local variable CurrentAbortState, LastAbortState
Defines
ABORT_DB_TIME- the debouncing time of the abort button
InitializeAbortButton
Takes a priority number, returns True.
Initialize the MyPriority variable with the passed in parameter.
Sample the button port pin and use it to initialize LastAbortButtonState
Set CurrentAbortState to be AbortDebouncing
Start abort_debounce timer (timer posts to AbortButtonDebounceSM)
End of InitializeAbortButton (return True)
RunAbortButtonDebounceSM (implements a 2-state state machine for debouncing timing)
The EventType field of ThisEvent will be one of: AbortButtonUp, AbortButtonDown, or ES_TIMEOUT
If CurrentAbortState is AbortDebouncing
If EventType is ES_TIMEOUT & parameter is abort debounce timer number
Set CurrentAbortState to Wait4AbortDB
Else if CurrentState is Wait4AbortDB
If EventType is AbortButtonUp
Start debounce timer
Set CurrentAbortState to DEBOUNCING
End if
If EventType is AbortButtonDown
Start abort_debounce timer
Set CurrentAbortState to DEBOUNCING
Post AbortButtonDown to DDM_SM
Post AbortButtonDown to SharkPositionService
Post AbortButtonDown to FlagService
Post AbortButtonDown to JoySequenceSM
End if
End Else
Return ES_NO_EVENT
End of RunAbortButtonDebounceSM
FireButton Service
local variable CurrentFireState, LastFireState
Defines
FIRE_DB_TIME- the debouncing time of the abort button

InitializeFireButton
Takes a priority number, returns True.
Initialize the MyPriority variable with the passed in parameter.
Sample the button port pin and use it to initialize LastFireButtonState
Set CurrentFireState to be FireDebouncing
Start fire_debounce timer (timer posts to FireButtonDebounceSM()
End of InitializeFireButton (return True)
RunFireButtonDebounceSM (implements a 2-state state machine for debouncing timing)
The EventType field of ThisEvent will be one of: FireButtonUp, FireButtonDown, or ES_TIMEOUT
If CurrentFireState is FireDebouncing
If EventType is ES_TIMEOUT & parameter is fire debounce timer number
Set CurrentFireState to Wait4FireDB
Else if CurrentFireState is Wait4FireDB
If EventType is FireButtonUp
Start debounce timer
Set CurrentFireState to DEBOUNCING
End if
If EventType is FireButtonDown
Start fire_debounce timer
Set CurrentFireState to DEBOUNCING
Post FireButtonDown to DDM_SM
Post FireButtonDown to SharkPositionService
End if
End Else
Return ES_NO_EVENT
End of RunFiretButtonDebounceSM

Você também pode gostar