For the complete documentation index, see llms.txt. This page is also available as Markdown.

Learn by Examples

In this section, you’ll learn how to use the python background library through practical examples. The Background library is used to create background tasks that run in parallel with the robot’s main program.

01 Basic API

In this example the background task is used to control the color of a LED light based on the robot state.

Key Points to Note:

  • The Background Task is developed as standard python script.

  • At the beginning of every background task, always call wait_boot_completed(). This ensures that the robot controller has completed its startup procedure before the task proceeds. This is particularly important when a background task starts automatically at startup, as the controller may launch it before initialization is complete.

  • Use functions set_digital_output(index, value) and get_digital_input(index) to control the robot digital I/O.

  • Use functions set_analog_output(index, value) and get_analog_input(index) to control the robot analog I/O.

  • Use function get_state() to get the current status of the robot.

  • Use functions get_program_state() and get_program_name() to get information of running robot program.

  • Use functions get_tcp_pose() and get_joint_pose() to get current robot position.

  • Allways use sleeps in loops to avoid high CPU load on robot controller.

02 Rule Scheduler

The previous example could also re-writted using the rule scheduler syntax.

Use when construction

when(condition).do(action).start()

to automatically and continually trigger action if condition is True.

Use on construction

on(condition).do(action).start()

to trigger action only on rising edge of condition. i.e. action is triggered just one time when condition becomes true.

03 Error Handling

Use Python’s standard try / except block for error handling. This is important because if an exception or error is raised outside the script, the background task will terminate and will not restart automatically.

Alternatively, you can use the @loop and @repeat annotations for error handling.

  • @loop continuously repeats a function. The function terminates if an exception is raised.

  • @repeat retries a function if an exception is raised, repeating execution until it succeeds.

So the previous example can be re-written also in this way:

04 Threads

You can use Python threads inside the background task:

threading.Thread(target=thread_function).start()

05 Interacting with the Robot Program

Modbus

To interact with the robot main program modbus general purpose registers can be used.

RPC - Remote Procedure Call

Also xmlrpc protocol can be used to call functions defined in the background task from the robot main program.

06 Interacting with external services

HTTP Client

The Background Task can be used to call external web services such as REST API.

A valid approach could be:

Main program — RPC —> Background Task — HTTP client —> REST API

HTTP Server

The Background task can also be used to expose HTTP REST API to external clients.

Sending E-mails

The background task can also be used for senging emails with SMTP protocol.

Last updated