Skip to main content

bteng-ros2 — ROS 2 extension for BTEng

bteng-ros2

Python License Version bteng

ROS 2 base classes for the BTEng behavior tree engine.

A pure Python pip library. No ROS 2 workspace or colcon required.

Install

source /opt/ros/jazzy/setup.bash    # or kilted / rolling — Python 3.12+
pip install bteng-ros2

bteng is pulled in automatically. The pin is bteng>=0.3.2, which keeps this package on the same engine release it is developed and tested against. That is a support decision rather than a hard technical floor — the suite also passes against 0.3.1 — so treat older cores as untested, not as known-broken.

Importing without ROS 2

import bteng_ros2 succeeds on a machine with no rclpy installed, so a CLI can serve --help / --dry-run and a test suite can run off-robot:

Without rclpy Works?
import bteng_ros2, bteng_ros2.__version__ yes
Defining node classes: RosActionNode, RosServiceNode, RosConditionNode, RosStatefulActionNode, the four mixins yes
Constructing and ticking them against FakeRosNode yes
Building and validating a Tree of them yes
Subclassing RosBTExecutor / LifecycleBTExecutor yes
Constructing RosBTExecutor / LifecycleBTExecutor no — raises ImportError naming the missing rclpy symbol
Any call that reaches real ROS traffic (create_publisher on a real node, _init_action_client, …) no

The two executors are real rclpy nodes, so they cannot work without ROS; they fail loudly at construction rather than half-working. Check bteng_ros2.executor.RCLPY_AVAILABLE if your program needs to branch.

Retargetable endpoints

A subclass of RosActionNode, RosServiceNode or RosConditionNode gets its endpoint as an input port whose default is the class attribute — action_name, service_name, topic_name respectively. A tree can point one node at a second server without a subclass, and --namespace-style rewrites have something to rewrite:

<GoToWaypoint name="robot2" action_name="/robot2/navigate_to_pose" />
<GoToWaypoint name="dock"   action_name="{dock_action}" />

Say nothing and the class attribute is used. The port is appended by __init_subclass__, so subclasses that define their own provided_ports() — as most do — inherit it without calling super(). RosTopicMixin is not covered: a publisher's topic stays a class attribute.

Discovery

DDS discovery has not finished at the moment a client is created, so a node that asks service_is_ready() immediately gets "no" from a healthy stack. Every action and service node waits for its server, reporting RUNNING meanwhile, up to discovery_timeout (5.0 s; set 0.0 to require it on the first tick). The wait is spread across ticks, not blocking, so the rest of the tree keeps running.

Design

bteng-ros2 provides mixins and base classes — not a standalone node. Users inherit from them inside their own ROS 2 packages.

Mixins (combine freely)         Pre-combined bases (common patterns)
─────────────────────────       ───────────────────────────────────
RosNodeMixin                    RosActionNode
RosActionClientMixin            RosStatefulActionNode
RosServiceClientMixin           RosConditionNode
RosTopicMixin

Usage

Simple action node

from bteng_ros2 import RosActionNode
from nav2_msgs.action import NavigateToPose

class Navigate(RosActionNode):
    action_type = NavigateToPose
    action_name = "/navigate_to_pose"

    def make_goal(self):
        goal = NavigateToPose.Goal()
        goal.pose = self.blackboard.get("target_pose")
        return goal

    def on_success(self):
        self.blackboard.set("arrived", True)

Stateful node with multiple ROS capabilities

from bteng_ros2 import RosStatefulActionNode
from nav2_msgs.action import NavigateToPose
from std_msgs.msg import String

class NavigateAndPublish(RosStatefulActionNode):
    def on_start(self):
        self._pub = self.create_publisher(String, "/status", 10)
        self._init_action_client(NavigateToPose, "/navigate_to_pose")
        self.send_goal(self._build_goal())

    def on_running(self):
        self._pub.publish(String(data="navigating"))
        return self.action_status()

    def on_halted(self):
        self.cancel_goal()

Mix freely — combine only what you need

from bteng import StatefulActionNode, NodeStatus
from bteng_ros2 import RosActionClientMixin, RosTopicMixin

# Only action + topic, no service client overhead
class Navigate(RosActionClientMixin, RosTopicMixin, StatefulActionNode):
    ...

Condition from a topic

from bteng_ros2 import RosConditionNode
from sensor_msgs.msg import LaserScan

class ObstacleDetected(RosConditionNode):
    topic_type = LaserScan
    topic_name = "/scan"

    def evaluate(self, msg: LaserScan) -> bool:
        return min(msg.ranges) < 0.5

Wire into your ROS 2 node

import rclpy
from bteng import SequenceNode, Tree, TreeMetadata, ExecutorConfig
from bteng_ros2 import RosBTExecutor

rclpy.init()

nav   = Navigate("nav")        # no ros_node needed at construction
check = ObstacleDetected("obs")
root  = SequenceNode("root", children=[check, nav])
tree  = Tree(TreeMetadata(id="robot"), root)

bt = RosBTExecutor(tree, ExecutorConfig(tick_interval=0.05))
# ↑ injects itself into nav and check automatically

status = bt.run(timeout=60.0)   # spins until the tree settles, then returns
bt.destroy_node()
rclpy.shutdown()

run() is the one-tree-then-exit form: it spins, returns the root's final NodeStatus, and on timeout halts the tree and returns FAILURE. For a long-lived node that keeps serving after the tree finishes, use rclpy.spin(bt) instead. Pass ros_node= to have the tree share an rclpy node you already own rather than the executor itself.

Lifecycle variant

from bteng_ros2 import LifecycleBTExecutor

class RobotBT(LifecycleBTExecutor):
    def build_tree(self) -> Tree:
        return Tree(TreeMetadata(id="robot"), ...)

Import from bteng_ros2.lifecycle:

from bteng_ros2.lifecycle import LifecycleBTExecutor

Testing without ROS 2

from bteng_ros2.testing import FakeRosNode

fake = FakeRosNode()
node = Navigate("nav", ros_node=fake)

# Inject a fake message into a subscription
fake.subscriptions["/scan"].inject(LaserScan(ranges=[1.0, 2.0]))

# Service clients: control readiness and when the response lands
fake = FakeRosNode(service_deferred=True)      # responses wait for resolve()
node = SetParam("svc", ros_node=fake)
node.on_start()                                 # RUNNING — call in flight
fake.service_clients["/set_param"].resolve(response)

See docs/testing.md.

Examples

Two runnable programs in examples/, trees defined in XML. Neither needs a ROS 2 install — both drive the real node classes against FakeRosNode:

python3 examples/01_robot_ready.py       # topic-driven conditions guarding actions
python3 examples/02_industrial_cell.py   # ReactiveSequence + e-stop, Retry, Timeout, service call

Documentation

Page Covers
Getting started Install, supported ROS 2 distributions, first node
Node types RosActionNode, RosServiceNode, RosConditionNode, RosStatefulActionNode
Mixins Mixin reference and free composition
Executors RosBTExecutor vs LifecycleBTExecutor
Testing Unit-testing nodes with no ROS 2 environment
Changelog What changed, and why

License

MIT — see LICENSE.

The third-party licence audit ships inside the package at bteng_ros2/licenses/THIRD_PARTY_LICENSES.md. pip install bteng-ros2 pulls exactly one package, bteng, which is MIT with no runtime dependencies.

Metadata

Release files for bteng-ros2 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bteng-ros2 0.3.0
File Size Uploaded Provenance Actions
bteng_ros2-0.3.0.tar.gz 30.5 kB Without provenance Details for bteng_ros2-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bteng-ros2 0.3.0
File Interpreter ABI Platform Actions
bteng_ros2-0.3.0-py3-none-any.whl Python 3 none any Details for bteng_ros2-0.3.0-py3-none-any.whl

Total release size: 65.7 kB

Release files / bteng_ros2-0.3.0.tar.gz

Download URL bteng_ros2-0.3.0.tar.gz
Size 30.5 kB
Tags Source
SHA-256 checksum
How to use checksums
766d360db8abb64a186e913c95b4a16d739852b54cb36669bdcb9c90864ba777
BLAKE2b-256 checksum
How to use checksums
62bfc7b3f28236e52a3cb8663d243dc13539ffc7f7e919e408ccef2110d14a8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / bteng_ros2-0.3.0-py3-none-any.whl

Download URL bteng_ros2-0.3.0-py3-none-any.whl
Size 35.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fb64cc7b0359a469588ab8d710e5482bec6c8ff708c602c4813b2ce10500f581
BLAKE2b-256 checksum
How to use checksums
8e95b4f7dfe1db8688e5d7fac9b0df0c56544b8e0f6ce3173fc494b304c01629
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for bteng-ros2 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page