Trap Statement (User defined trigger)

Defines interrupts and what should happen when they occur.

With the Trap statement, you can jump to labels or call functions when the event occurs. Trap statement has 2 types as below:

  • 4 Traps that interrupts by the user defined input status
  • 7 Traps that interrupts by the system status

Trap with user defined trigger is explained here.

Syntax
Trap trapNumber, eventCondition GoTo label

Trap trapNumber, eventCondition Call funcname

Trap trapNumber, eventCondition Xqt funcname

Trap trapNumber

Parameters

trapNumber
Specify the trap number (an integer between 1 and 4), either as an expression or directly as a numeric value. (SPEL+ supports up to 4 active Trap interrupts at the same time.)
eventCondition
Specify input status used as a trigger.
[Event] Comparative operator ( =, <>, >=, >, <, <= ) [Integer expression]
The following functions and variables can be used in the Event.

  • Functions : Sw, In, InW, Oport, Out, OutW, MemSw, MemIn, MemInW, Ctr, GetRobotInsideBox, GetRobotInsidePlane, AIO_In, AIO_InW, AIO_Out, AIO_OutW, Hand_On, Hand_Off, SF_GetStatus

  • Variables : Byte, Int32, Integer, Long , Short, UByte, UInt32, UShort global preserve variable, Global variable, module variable

In addition, using the following operators you can specify multiple event conditions.

  • Operator: And, Or, Xor

  • (Example)

    Trap 1, Sw(5) = On Call, TrapFunc
    Trap 1, Sw(5) = On And Till(6) = Off, Call TrapFunc
    
    label
    The label where program execution is to be transferred when Trap condition is satisfied.
    funcname
    The function that is executed when Call or Xqt when the Trap condition is satisfied. The function with argument cannot be specified.

    Description
    A Trap executes interrupt processing which is specified by GoTo, Call, or Xqt when the specified condition is satisfied. The Trap condition must include at least one of the functions above.

    When variables are included in the Trap condition, their values are computed when setting the Trap condition. No use of variable is recommended. Otherwise, the condition may be an unintended condition.

    Once the interrupt process is executed, the Trap setting is cleared. If the same interrupt process is necessary, the Trap instruction must execute it again.

    To cancel a Trap setting simply execute the Trap instruction with only the trapNumber parameter.

    e.g. “Trap 3” cancels Trap #33.

    When the Function that executed Trap GoTo ends (or exit), the Trap Goto will be canceled automatically.

    When the declared task ends, Trap Call will be canceled.

    Trap Xqt will be canceled when all tasks have stopped.

    If GoTo is specified

    In the task set to Trap, the command being executed will be processed as described below. Then, control branches to the specified label.

    • Any arm motion will pause immediately
    • Waiting status by the Wait or Input commands will discontinue
    • All other commands will complete execution before control branches

    If Call is specified

    After executing the same process as GoTo described above, then control branches to the specified line number or label.

    Once the function ends, program execution returns to the next statement after the statement where program interruption occurred.

    Call statements cannot be used in the Trap processing function.

    When an error occurs in the trap process function, error handling with OnErr will be invalid and an error will occur.

    If Xqt is specified

    Program control executes the specified function as an interrupt processing task. In this case, the task which executes the Trap command will not wait for the Trap function to finish and will continue to execute.

    You cannot execute a task with an Xqt statement from an interrupt processing task.

    Notes


    • For EPSON RC+4.x user

      The Trap Call function of EPSON RC+ 4.x or before is replaced with Trap Xqt in Epson RC+ 8.0.

      The Trap GoSub function of EPSON RC+ 4.x or before is removed in Epson RC+ 8.0. Instead, use Trap Call.

    • To use a variables in the event condition expression

      • Available variables are Integer type (Byte, Int32, Integer, Long, Short, UByte, UInt32, UShort)
      • Array variables are not available
      • Local variables are not available
      • If variables value cannot satisfy the event condition for more than 0.01 seconds, the change in variables may not be retrieved.
      • Up to 64 can wait for variables in one system (including the ones used in the event condition expressions such as Wait). If it is over 64, an error occurs during the project build.
      • If you try to transfer a variable waiting for variables as a reference with Byref, an error occurs.
      • When a variable is included in the right side member of the event condition expression, the value is calculated when setting the Trap condition. We recommend not using variables in an integer expression to avoid making unintended conditions.

    See Also
    Call, GoTo, Xqt, SF_GetStatus

    Trap Statement Example
    [Example 1] Error process defined by User

    Sw(0) Input is regarded as an error input defined by user.

    Function Main
        Trap 1, Sw(0)= On GoTo EHandle  ' Defines Trap
        .
        .
        .
    EHandle:
        On 31   'Signal tower lights
        OpenCom #1
        Print #1, "Error is issued"
        CloseCom #1
    Fend
    

    [Example 2] Usage like multi-tasking

    Function Main
        Trap 2, MemSw(0) = On Or MemSw(1) = On Call Feeder
        .
        .
        .
    Fend
    .
    
    
    Function Feeder
      Select TRUE
        Case MemSw(0) = On
          MemOff 0
          On 2
        Case MemSw(1) = On
          MemOff 1
          On 3
      Send
    
      ' Re-arm the trap for next cycle
      Trap 2, MemSw(0) = On Or MemSw(1) = On Call Feeder
    Fend
    

    [Example 3] Using global variable as event condition

    Global Integer gi
    
    Function main
      Trap 1, gi = 5 GoTo THandle
      Xqt sub
      Wait 100
      Exit Function
    
    THandle:
      Print "IN Trap ", gi
    
    Fend
    
    Function sub
      For gi = 0 To 10
        Print gi
        Wait 0.5
      Next
    Fend