Collaborative Multitasking

Create Coroutine Task 3.0

task = async_task(func, arg1, arg2, ...)
  • Parameters
    • func Function name
    • args Arguments passed to the function
  • Returns
    • task Coroutine task. The task will not execute before calling the following 2 functions

Compatibility Note

Equivalent to the following syntax:

task = coroutine.create(function() return fun(args) end)

Wait for All Tasks to Complete 3.0

ret1, ret2, ... = wait_all(task1, task2, ...)
  • Parameters
    • tasks Coroutine tasks
  • Returns
    • rets Return values of the tasks

Wait for Any Task to Complete 3.0

ret, index = wait_any(task1, task2, ...)
  • Parameters
    • tasks Coroutine tasks
  • Returns
    • ret Return value of the completed task
    • index Index of the completed task

Example

disable_auto_sync()

function movement(param)
    print(param, 'starts running')
    movej({j1=0,j2=0,j3=0,j4=0,j5=0,j6=0}, 0, 0, 1, 0)
    sync() -- Wait for movement to complete
    return "Movement completed"
end

function gripper()
    wait(1000)
    set_claw(0, 100)
    return "Gripper completed"
end

ret1, ret2 = wait_all(async_task(movement, 'Thread one'), async_task(gripper))
print("Movement task return value: ", ret1)
print("Gripper task return value: ", ret2)
local task_timeout = async_task(wait, 1000) -- Create a task for timeout waiting
ret, index = wait_any(async_task(movement, 'Thread two'), task_timeout)
if index == 2
then -- task_timeout completed first
    print("Timeout")
else -- movement completed first
    print("Movement task return value: ", ret)
end