Writing a HelloWorld ActionNode

Overview

This tutorial shows the classical HelloWorld example. It is the simplest possible starting point to learn careBT.

Create the HelloWorldAction ActionNode

Create a file named helloworld.py with following content. Or use the provided file: helloworld.py

 1from carebt import ActionNode
 2from carebt import NodeStatus
 3
 4
 5class HelloWorldAction(ActionNode):
 6    """The `HelloWorldAction` example node.
 7
 8    The `HelloWorldAction` provides a classical Hello World example.
 9    It demonstrates a simple implementation of a careBT `ActionNode`.
10
11    When running the `HelloWorldAction`,
12    'HelloWorldAction: Hello World !!!' is printed on standard output.
13    """
14
15    def __init__(self, bt_runner):
16        super().__init__(bt_runner)
17
18    def on_tick(self) -> None:
19        print('HelloWorldAction: Hello World !!!')
20        self.set_status(NodeStatus.SUCCESS)

The code explained

The first two statements are the includes for the ActionNode and the NodeStatus.

from carebt import ActionNode
from carebt import NodeStatus

The HelloWorldAction node is implemented as a Python class which inherits from ActionNode.

class HelloWorldAction(ActionNode):

The class definition is followed by the Docstring documentation of the node.

    """The `HelloWorldAction` example node.

    The `HelloWorldAction` provides a classical Hello World example.
    It demonstrates a simple implementation of a careBT `ActionNode`.

    When running the `HelloWorldAction`,
    'HelloWorldAction: Hello World !!!' is printed on standard output.
    """

The constructor (__init__) of the HelloWorldAction needs to call the constructor (super().__init__) of the ActionNode and passes the bt_runner as an argument. This is required by the careBT execution engine to work properly. The constructor is also the place to register the input/output parameters of the node and will be explained in further tutorials.

    def __init__(self, bt_runner):
        super().__init__(bt_runner)

In the on_tick function the code has to be placed which is executed every time the node is ticked. In this example this is a simple print statemant which shows the text “HelloWorldAction: Hello World !!!” on the standard output. The following line puts the node into the status SUCCESS, which indicates that the execution of the node has completed and the execution was succesful. Thus, the HelloWorldAction node is not ticked again.

    def on_tick(self) -> None:
        print('HelloWorldAction: Hello World !!!')
        self.set_status(NodeStatus.SUCCESS)

Run the example

Start the Python interpreter and run the HelloWorldAction node:

>>> import carebt
>>> from carebt.examples.helloworld import HelloWorldAction
>>> bt_runner = carebt.BehaviorTreeRunner()
>>> bt_runner.run(HelloWorldAction)
HelloWorldAction: Hello World !!!

Or run it with log-level set to INFO:

>>> import carebt
>>> from carebt import LogLevel
>>> from carebt.examples.helloworld import HelloWorldAction
>>> bt_runner = carebt.BehaviorTreeRunner()
>>> bt_runner.get_logger().set_log_level(LogLevel.INFO)
>>> bt_runner.run(HelloWorldAction)
2022-03-05 16:30:11 INFO creating HelloWorldAction
HelloWorldAction: Hello World !!!
2022-03-05 16:30:11 INFO ---------------------------------------------------
2022-03-05 16:30:11 INFO bt execution finished
2022-03-05 16:30:11 INFO status:  NodeStatus.SUCCESS
2022-03-05 16:30:11 INFO contingency-message:
2022-03-05 16:30:11 INFO ---------------------------------------------------