Create a simple function block¶
Summary
- 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¶
- Go the the Program Overview.
- Select in the drop-down menu on the upper right of the button New Program.
- Name the function block 'Raise by two'.
- The Builder for function blocks has two views, a Python and a JSON view.
- Switch between the views by using the buttons and in the menuband.
-
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. -
The
execute()
function is where the code of the function block is placed. - The
error()
function is called when an error occurs(a python exception). - 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.) - Select and change the icon to a fitting symbol and add a description: 'Adds two to the entered integer.'
- Under Input Parameters enter 'integer' as type and 'Input Value' as title. Leave the default value field empty.
- Under Output Parameters enter 'integer' as type and 'Output Value' as title. Leave the default value field empty.
- 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'. -
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.
-
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. -
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
-
Test the function block by creating a program consisting of the new function block and a 'User Dialog' block.
- Set the function blocks input by selecting and link the output of the function block to the input of the 'User Dialog' block.
- 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"],
...
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.