runURScript

abstract fun runURScript(script: String, cmdTimeout: Long? = null, onChange: (URScriptState) -> Unit? = null, onFinished: (URScriptState) -> Unit? = null): URScriptState

Executes a custom URScript on the cobot.

The script is sent to the UR controller and executed asynchronously. Optional callbacks allow tracking state changes and detecting when execution finishes.

The provided script must contain only the body of the program. Do not wrap it in a def … end block — the API automatically generates and wraps the script inside its own function context before sending it to the robot.

Execution errors can be signaled to the URScriptState with the URScript function send_event("error","title","message").

Return

URScriptState representing the current URScript execution state, which is continuously updated.

Parameters

script

The URScript body to execute (without a def wrapper).

cmdTimeout

Maximum time in milliseconds to wait for the command to complete. If this timeout is exceeded, the command is considered failed and the URScriptState will no longer be updated.

onChange

⚠️ Experimental: Optional callback invoked whenever the URScriptState changes during execution. This API is experimental and may change or be removed in future versions.

onFinished

optional callback that is invoked once when the URScriptState is final. The state is final when runningState assumes the following value: RunningState.END, RunningState.TIMEOUT, RunningState.CANCELED

Samples

import com.wolfscowl.ur_client.UR
import com.wolfscowl.ur_client.examples.Examples.ur
import com.wolfscowl.ur_client.interfaces.state.await
import com.wolfscowl.ur_client.interfaces.state.awaitBlocking
import com.wolfscowl.ur_client.interfaces.state.awaitBlockingUntil
import com.wolfscowl.ur_client.interfaces.state.awaitUntil
import com.wolfscowl.ur_client.model.element.JointPosition
import com.wolfscowl.ur_client.model.element.Pose
import com.wolfscowl.ur_client.model.element.RunningState
import com.wolfscowl.ur_client.model.element.Vec3
import com.wolfscowl.ur_client.model.robot_state.mode.RobotMode
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import kotlin.system.exitProcess

fun main() { 
   //sampleStart 
   val state = ur.runURScript(
    script = """
    def base_loop():
      i = 0
      delta = 0.4
      while (i < 3):
        movej([-1.57 + delta, -1.57, 0.00, -3.14, -1.57, -1.57], a=1.4, v=1.05, t=0, r=0)
        movej([-1.57, -1.57, 0.00, -3.14, -1.57, -1.57], a=1.4, v=1.05, t=0, r=0)
        i = i + 1
      end
    end
    
    send_event("error", "Test Error", "This is just a test ;)")
    
    base_loop()
    """.trimIndent(),
    cmdTimeout = 20000,
    onChange = null,
    onFinished = { state -> /* Do something */
    }
).await() // await for the finale state
println(state) 
   //sampleEnd
}