This service is one of the three action services. It gets activated by the Master Service and when activated it waits for the presence of the user's hand. When this happens, it starts to pet the user (by rotating the DC motor), then it sends back an event to confirm that the petting has been completed. After a certain time, if the user did not place his hand, the Master Service will disable the Paw Moving Service.
# Constants
Create_constant TAIL_SPEED_WAITING as 100 # out of 255
Create_constant TAIl_SPEED_PETTING as 180 # out of 255
Create_constant DC_CHAN as 5 # PWM channel
Create_constant PAWMOVING_SPEED as 30 # out of 100
Create_constant DURATION_MIN_TO_COMPLETE as 5000 # milliseconds
# Variables
Declare CurrentState as State
No specific functions for this service.
Function InitPawMovingService:
Set CurrentState to InitPawMovingState
# DC Motor
Call PWMSetup_BasicConfig(5)
Call PWMSetup_MapChannelToOutputPin(DC_CHAN, PAWMOVING_DC)
Call PWMOperate_SetDutyOnChannel(0, DC_CHAN)
Call PWMSetup_AssignChannelToTimer(DC_CHAN, _Timer3_)
# Post the initial transition event
Post_to PawMovingService ES_INIT
Endfunction
Function RunPawMovingService(event):
Instantiate NextState to CurrentState
Switch CurrentState:
case InitPawMovingState:
If type of event is ES_INIT
Set NextState to InactivePawMoving
Call PWMOperate_SetDutyOnChannel(0, DC_CHAN)
Endif
case InactivePawMoving:
If type of event is ES_PAWMOVING_ACTIVATE:
Set NextState to WaitingForHand
Endif
case WaitingForHand:{
If type of event is ES_PAWMOVING_BEAM_BROKEN:
Set NextState to ActivelyPetting
Call PWMOperate_SetDutyOnChannel(PAWMOVING_SPEED, DC_CHAN)
Stop_timer PawmovingCounterTimer
Start_timer PawmovingCounterTimer with duration DURATION_MIN_TO_COMPLETE
Call PlayAudio(AUDIO_PURR)
Call DisplayAnim(ANIM_HAPPY)
Call SetTailSpeed(TAIl_SPEED_PETTING)
# Not inactive
Call SetMasterActive()
Endif
case ActivelyPetting:
If type of event is ES_TIMEOUT and parameter is PawmovingCounterTimer:
Post_to MasterService ES_PAWMOVING_COMPLETED
Call PWMOperate_SetDutyOnChannel(0, DC_CHAN)
Set NextState to InactivePawMoving
ElseIf type of event is ES_FAIL:
Set NextState to InactivePawMoving
CallPWMOperate_SetDutyOnChannel(0, DC_CHAN)
ElseIf type of event is ES_PAWMOVING_BEAM_RESTORED:
Set NextState to WaitingForHand
Call SetTailSpeed(TAIL_SPEED_WAITING)
Call PlayAudio(AUDIO_ANGRY)
Call PWMOperate_SetDutyOnChannel(0, DC_CHAN)
# Not inactive
Call SetMasterActive()
Endif
Endswitch
Set CurrentState to NextState
Endfunction