Skip to content

Create a simple function block


  • Create a function block
  • Use the different views of the builder
  • Access the function input and create the function output
  • Use the created function block in a program
  • Log information to the ROS console.

1. Simple function block

  1. Go the the Program Overview.
  2. Select Create Basic Block in the drop-down menu on the upper right of the button New Program.
  3. Name the function block 'Raise by two'.
  4. The Builder for function blocks has two views, a Python and a JSON view.
  5. Switch between the views by using the buttons python-view-button and json-view-button in the menuband.
  6. The Python template consists of three functions:

    • def execute(input_parameters, output_parameters):
    • def error(input_parameters, output_parameters):
    • def abort():

    The only required parts are the execute() function and the python Shebang(!#/usr/bin/env python) in the first line.

  7. The execute() function is where the code of the function block is placed.

  8. The error() function is called when an error occurs(a python exception).
  9. The abort() function is called when the program is stopped by the user or aborted and should cleanup the block. (E.g. close open connections to allow a clean program termination.)
  10. Select Edit Information and change the icon to a fitting symbol and add a description: 'Adds two to the entered integer.'
  11. Under Input Parameters enter 'integer' as type and 'Input Value' as title. Leave the default value field empty.
  12. Under Output Parameters enter 'integer' as type and 'Output Value' as title. Leave the default value field empty.
  13. Leave Edit Information and enter the JSON view; here the "inputParameters" and "outputParameters" list have one element each, the parameters created in the last step. Under 'title' and 'dataType' the just entered values appear. The parameter which will change while running the code is the one named 'value'.
  14. Those parameters can be accessed in the Python code as:

    • input_parameters[x]['value']
    • output_parameters[x]['value']

    where x is the position in the list.

  15. To avoid problems with datatypes access the input parameter like this: int(input_parameters[0]['value'] ). This guarantees that the parameter value is of type integer.

  16. Change the execute function to this code:

    def execute(input_parameters, output_parameters):
        output_parameters[0]['value'] = int(input_parameters[0]['value'] ) + 2
        return output_parameters
  17. Test the function block by creating a program consisting of the new function block and a 'User Dialog' block.

  18. Set the function blocks input by selecting Set Parameter and link the output of the function block to the input of the 'User Dialog' block.
  19. Now the program can be executed and the message should display the number entered as input raised by two.

2. Display information in drag&bot console

Display information from a function block to the drag&bot console is done with rospy:

import rospy

def execute(input_parameters, output_parameters):
    rospy.loginfo("Input: " + str(input_parameters[0]['value']) )
    return output_parameters

3. Special JSON fields

3.1 Labels

Function blocks, which are provided by components, can contain additional labels (How to create a drag&bot component?). These can be defined by adding a "labels" field to the existing JSON structure. The labels contained there will be added when importing the function block.

    "labels": ["Example Label", "Gripper"],

Label example

Label example

Only works with component function blocks

Labels are only added if the function block is imported from a component. If the function block is imported by other means, even if there is an existing labels field, they will not be added.