Watchdog-Script

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > Parallel-Robot Operations >

Watchdog-Script

Watchdog

Previous Top Next


MiniRobotLanguage (MRL)

 

Watchdog-Script

Use this Script to prevent hanging Scripts-

 

 

Intention

 

Watchdog Functionality:

This script acts as a watchdog, ensuring the main script stays within the communicated time frame.

Use this template in your scripts where a watchdog is needed to monitor the main script's processing.

 

Lets take this case. We have a Dictate Script that calls WHISPER from Open AI.

Sometimes WHISPER is just busy and not responding.

To prevent the Script from "waiting forever" (which it would not do anyway)

we can embed the functionality in a "20 Seconds Watchdog".

 

If the Script does not return  within the specified 20 Seconds, the Main Script is killed.

And the Watchdog will end itself automatically with the Main Script

 

'-----------------------------------------------------------

' Dictate

'-----------------------------------------------------------

:But_Dictate

%SetWatchdog 20

GSB.HiColor

GSB.Dictate

GSB.LoButton

%ClrWatchdog

RET.

 

The code below is ready to be used.

Call the Subprogram that starts the Watchdog-Robot.

Then use:

' Set the Watchdog to 20 Seconds

%SetWatchdog 20

 

' After using the Watchdog switch it off

%ClrWatchdog

 

The Watchdog will automatically end itself when the Main Script ends.

 

'**********************************************

' PRR./PRE./PRV. - Watchdog

' This script demonstrates a simple watchdog timer

' using the $$TIM variable to monitor the main script.

'

' Usage:

' - Set $$TIM to a non-zero value to start the watchdog.

' - The watchdog monitors the elapsed time.

' - If $$TIM is zero, the watchdog is off.

' - If the elapsed time exceeds $$TIM, an action is taken.

'**********************************************

:Watchdog_Start

MOD.New|Watchdog

' Variables Initialization

VAR.$$MWH=#cbw#

' Default State is off

VAR.$$TIM=0

'-----------------------------------------------------------

' Parallel Robot Routine

PRR.0

  MOD.Enter|Watchdog

  ' Print robot information

  'PRT.I am (Parallel-)Robot-Nr. #pri#.

  'PRT.I am Son #pri# of $$MWH.

 

  :Loop

    ' Wait for timer value to be set

  WFV.$$TIM|3

  JIZ.$$TIM|off

 

    ' Record the start datetime

  $$STA=#dtime#

 

    ' Main watchdog loop

  :WatchdogLoop

  'PRT.Watchloop B: $$TIM   

  PAU.1

  JIZ.$$TIM|off

 

  ' Calculate the elapsed time      

  CAL.$$CUT=(#dtime#-$$STA)

 'PRT.Test: $$CUT>$$TIM

  ' Check if the elapsed time exceeds the timer value    

  IVV.$$CUT>$$TIM

    JIZ.$$TIM|off

    ' Print the elapsed time and the timer value

    MBT.Watchdog-Timeout: $$CUT>$$TIM

 

    ' Get and execute the command

    GDF.hp|$$MWH|$$PID

    CPR.$$PID

  EIF.

 

  ' Check if the timer is reset to zero (watchdog off)

      ' Repeat the watchdog loop

  JMP.WatchdogLoop

 

    ' Off condition handling

  :off

      ' Pause for 1 second

  PAU.1

 

      ' Jump back to the beginning of the loop

  JMP.Loop

 

  END.

 

  ' Set return value for the main program

PRE.$$WDG|0

MOD.Copy to Last|$$WDG

'-----------------------------------------------------------

MOD.Return

RET.

 

: %SetWatchdog 1

MOD.Enter|Watchdog

VIN.$$TIM=§§§01

PRV.$$WDG|$$TIM

MOD.Return

END%

 

: %ClrWatchdog

MOD.Enter|Watchdog

VIN.$$TIM=0

PRV.$$WDG|$$TIM

MOD.Return

END%

 

 

 


 

Sample 1.

 

'**********************************************

' PRR./PRE./PRV. - Sample 1

' This script demonstrates a simple watchdog timer

' using the $$TIM variable to monitor the main script.

'

' Usage:

' - Set $$TIM to a non-zero value to start the watchdog.

' - The watchdog monitors the elapsed time.

' - If $$TIM is zero, the watchdog is switched off.

' - If the elapsed time exceeds $$TIM, The parent Robot is killed.

'**********************************************

' Variables Initialization

VAR.$$MWH=#cbw#

 

'-----------------------------------------------------------

' Parallel Robot Routine

PRR.0

  ' Print robot information

  PRT.I am (Parallel-)Robot-Nr. #pri#.

  PRT.I am Son #pri# of $$MWH.

 

  :Loop

    ' Wait for timer value to be set

  WFV.$$TIM|3

  JIZ.$$TIM|off

 

    ' Record the start datetime

  $$STA=#dtime#

 

    ' Main watchdog loop

  :WatchdogLoop

      ' Pause for a short period

  PAU.1

 

      ' Calculate the elapsed time

  CAL.$$CUT=(#dtime#-$$STA)

  PRT.Test: $$CUT>$$TIM

      ' Check if the elapsed time exceeds the timer value

  IVV.$$CUT>$$TIM

        ' Print the elapsed time and the timer value

    PRT.Timeout: $$CUT>$$TIM

 

        ' Get and execute the command

    GDF.hp|$$MWH|$$PID

    CPR.$$PID

  EIF.

 

  ' Check if the timer is reset to zero (watchdog off)

  JIZ.$$TIM|off

  PRT.Here: $$CUT

      ' Repeat the watchdog loop

  JMP.WatchdogLoop

 

    ' Off condition handling

  :off

      ' Pause for 1 second

  PAU.1

 

      ' Jump back to the beginning of the loop

  JMP.Loop

 

  END.

 

  ' Set return value for the main program

PRE.$$RET|0

 

'-----------------------------------------------------------

' Main Program

PRT.KOM-H: $$RET

PAU.5

PRT.I have #prc# running parallel robots and PlugIns.

PAU.1

 

' Loop through and initialize parallel robots

FOR.$$MYL|1|300

  ' Set watchdog timer to a non-zero value to start

  VAN.$$TIM=2

  PRV.$$RET|$$TIM

 

  ' Pause for 30 seconds

  PAU.5

NEX.

MBX.!

 

END.

 

'**********************************************

' Instructions for Using the Watchdog Script

'**********************************************

' 1. Define the watchdog script as shown above.

' 2. Ensure the main script communicates with the watchdog:

'    a. Set $$TIM to a non-zero value to start the watchdog.

'    b. The watchdog will monitor the elapsed time.

' 3. The watchdog will execute a command if the elapsed 

'    time exceeds $$TIM and then reset $$TIM to zero.

' 4. Use the `GDF.hp|$$MWH|$$PID` and `CPR.$$PID` commands 

'    to define the action taken when the timer is exceeded.

' 5. Integrate this watchdog logic into your main script to 

'    ensure it stays within the required processing time.

'**********************************************

 

Sample 2.

 

'**********************************************

' PRR./PRE./PRV. - Sample 2

' This script demonstrates how a Parallel-Robot

' starts with all variables as they were at the 

' starting time of the initiating robot.

' It can also use all subroutines in this file.

'

' Watchdog Functionality:

' This script acts as a watchdog, ensuring the main

' script stays within the communicated time frame.

' Use this template in your scripts where a watchdog 

' is needed to monitor the main script's processing.

'**********************************************

 

'**********************************************

' Variables Initialization

VAR.$$INP=0

VAR.$$MWH=#cbw#

'-----------------------------------------------------------

' Parallel Robot Routine

PRR.0

  ' Print robot information

  PRT.I am (Parallel-)Robot-Nr. #pri#.

  PRT.I am Son #pri# of $$MWH.

 

  ' Initialize watchdog status

  $$WDS=0

 

  :Loop

    ' Check if watchdog is off and jump to :off if true

    JIV.$$WDS=0|off

 

    ' Print watchdog status

    PRT.WD-Switch set to: $$WDS

 

    ' Record the start datetime

    $$STA=#dtime#

 

    ' Wait for timer value for up to 1 second

    WFV.$$TIM|1

    JIZ.$$TIM|off

 

    ' Pause for 1 second

    PAU.1

 

    ' Calculate the elapsed time

    $$CUT=#dsince#

 

    ' Check if the elapsed time exceeds the timer value

    IVV.$$CUT>$$TIM

      ' Print the elapsed time and the timer value

      PRT.$$CUT>$$TIM

 

      ' Get the PID of the starting Robot and Kill the Process

      GDF.hp|$$MWH|$$PID

      CPR.$$PID

    EIF.

 

    ' Off condition handling

    :off

      ' Pause for 2 seconds

      PAU.2

 

      ' Jump back to the beginning of the loop

      JMP.Loop

 

  END.

 

  ' Set return value for the main program

  PRE.$$RET|0

 

'-----------------------------------------------------------

' Main Program

PRT.KOM-H: $$RET

PAU.5

PRT.I have #prc# running parallel robots and PlugIns.

PAU.1

 

' Loop through and initialize parallel robots

FOR.$$MYL|1|300

  ' Set watchdog status to on

  VAN.$$WDS=1

  PRV.$$RET|$$WDS

  

  ' Set timer value

  VAN.$$TIM=2

  PRV.$$RET|$$TIM

 

  ' Pause for 30 seconds

  PAU.30

NEX.

 

' End the parallel robot

VAN.$$INP=1

PRV.$$RET|$$INP

PAU.2

 

' Dump memory and mailbox

DMP.

MBX.!

 

END.

 

'**********************************************

' Instructions for Using the Watchdog Script

'**********************************************

' 1. Define the watchdog script as shown above.

' 2. Ensure the main script communicates with the watchdog:

'    a. Set the watchdog status using `VAN.$$WDS=1` to turn it on.

'    b. Set the timer value using `VAN.$$TIM=<time_in_seconds>`.

' 3. The watchdog will monitor the main script's processing time.

' 4. If the main script exceeds the specified time (`$$TIM`), 

'    the watchdog will execute the specified commands.

' 5. Use the `GDF.hp|$$MWH|$$PID` and `CPR.$$PID` commands 

'    to define the action taken when the timer is exceeded.

' 6. The watchdog script runs in parallel, so ensure proper 

'    synchronization between the main script and the watchdog.

' 7. Use `VAN.$$INP=1` to end the parallel robot when done.

' 8. Integrate this watchdog logic into your main script to 

'    ensure it stays within the required processing time.

'**********************************************