Skip to content

Commit 6c3680d

Browse files
committed
samples: zephyrbt: Add subtree_reuse sample
Add a sample that validates correct behavior tree traversal when a subtree is used multiple times. This reproduces the scenario from issue #28 where incorrect indices caused nodes to be skipped during execution. The behavior tree structure tests: Sequence -> SubTree(sub) -> A -> SubTree(sub) -> B Expected execution order: sub -> A -> sub -> B The test verifies that all nodes are evaluated in the correct order and that no nodes are skipped due to incorrect sibling indices. Signed-off-by: Gerson Fernando Budke <[email protected]>
1 parent d0c7dc6 commit 6c3680d

File tree

7 files changed

+212
-0
lines changed

7 files changed

+212
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) 2026 O.S. Systems Software LTDA.
2+
# Copyright (c) 2026 Freedom Veiculos Eletricos
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
cmake_minimum_required(VERSION 3.20.0)
6+
7+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
8+
include(${ZEPHYR_ZEPHYRBT_MODULE_DIR}/cmake/zephyrbt-from-behaviourtreecpp-xml.cmake)
9+
10+
project(zephyrbt_subtree_reuse)
11+
12+
zephyr_include_directories(
13+
${CMAKE_CURRENT_SOURCE_DIR}/include
14+
)
15+
16+
target_sources(app PRIVATE
17+
src/main.c
18+
)
19+
20+
zephyrbt_define_from_behaviourtreecpp_xml(app
21+
models/subtree_reuse.xml
22+
${CMAKE_BINARY_DIR}/include
23+
${CMAKE_BINARY_DIR}/src
24+
1024
25+
0
26+
yes
27+
)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
.. Copyright (c) 2026 O.S. Systems Software LTDA.
2+
.. Copyright (c) 2026 Freedom Veiculos Eletricos
3+
.. SPDX-License-Identifier: Apache-2.0
4+
.. _zephyrbt_subtree_reuse:
5+
6+
Zephyr Behaviour Tree - Subtree Reuse
7+
#####################################
8+
9+
Overview
10+
********
11+
12+
This sample validates the correct behavior tree traversal when a subtree is
13+
used multiple times. It reproduces the scenario from `GitHub issue #28`_ where
14+
incorrect indices caused nodes to be skipped during execution.
15+
16+
The behavior tree structure is:
17+
18+
.. code-block:: text
19+
20+
Sequence
21+
├── SubTree (btree_subtree) → sub
22+
├── A
23+
├── SubTree (btree_subtree) → sub
24+
└── B
25+
26+
The expected execution order is: sub → A → sub → B
27+
28+
Prior to the fix, the second instance of the subtree had incorrect sibling
29+
indices, causing node A to be skipped entirely.
30+
31+
.. _GitHub issue #28: https://github.com/OSSystems/ZephyrBT/issues/28
32+
33+
Building and Running
34+
********************
35+
36+
This application can be built and executed on ``qemu_cortex_m3`` as follows:
37+
38+
.. code-block:: console
39+
40+
west build -p -b qemu_cortex_m3 samples/subsys/zephyrbt/subtree_reuse -t run
41+
42+
To build for another board, change "qemu_cortex_m3" above to that board's name.
43+
44+
Sample Output
45+
=============
46+
47+
.. code-block:: console
48+
49+
*** Booting Zephyr OS ***
50+
D: zephyrbt_action_sub_init stub function
51+
D: zephyrbt_action_a_init stub function
52+
D: zephyrbt_action_sub_init stub function
53+
D: zephyrbt_action_b_init stub function
54+
D: tick
55+
D: eval sequence [control, 4]
56+
D: Deep: 1
57+
D: sequence [control, 4]
58+
D: eval sub [action, 3]
59+
D: Deep: 2
60+
D: zephyrbt_action_sub stub function
61+
D: eval a [action, 2]
62+
D: Deep: 2
63+
D: zephyrbt_action_a stub function
64+
D: eval sub [action, 1]
65+
D: Deep: 2
66+
D: zephyrbt_action_sub stub function
67+
D: eval b [action, 0]
68+
D: Deep: 2
69+
D: zephyrbt_action_b stub function
70+
71+
The key verification points are:
72+
73+
1. All four nodes (sub, a, sub, b) are evaluated in the correct order
74+
2. Node 'a' is not skipped (which was the bug symptom)
75+
3. Each subtree instance has a unique index (3 and 1)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright (c) 2026 O.S. Systems Software LTDA.
3+
* Copyright (c) 2026 Freedom Veiculos Eletricos
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#ifndef ZEPHYRBT_USER_H
9+
#define ZEPHYRBT_USER_H
10+
11+
#endif /* ZEPHYRBT_USER_H */
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<root BTCPP_format="4"
3+
main_tree_to_execute="btree_debug">
4+
<BehaviorTree ID="btree_debug">
5+
<Sequence>
6+
<SubTree ID="btree_subtree"
7+
_autoremap="true"/>
8+
<A/>
9+
<SubTree ID="btree_subtree"
10+
_autoremap="true"/>
11+
<B/>
12+
</Sequence>
13+
</BehaviorTree>
14+
15+
<BehaviorTree ID="btree_subtree">
16+
<sub/>
17+
</BehaviorTree>
18+
19+
<!-- Description of Node Models (used by Groot) -->
20+
<TreeNodesModel>
21+
<Action ID="A"
22+
editable="true"/>
23+
<Action ID="B"
24+
editable="true"/>
25+
<Action ID="sub"
26+
editable="true"/>
27+
</TreeNodesModel>
28+
29+
</root>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (c) 2026 O.S. Systems Software LTDA.
2+
# Copyright (c) 2026 Freedom Veiculos Eletricos
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
CONFIG_LOG=y
6+
CONFIG_LOG_MODE_MINIMAL=y
7+
8+
CONFIG_ZEPHYR_BEHAVIOUR_TREE=y
9+
CONFIG_ZEPHYR_BEHAVIOUR_TREE_DYNAMIC=y
10+
CONFIG_ZEPHYR_BEHAVIOUR_TREE_NODE_INFO=y
11+
CONFIG_ZEPHYR_BEHAVIOUR_TREE_NODE_INIT=y
12+
CONFIG_ZEPHYR_BEHAVIOUR_TREE_NODE_CONTEXT=y
13+
CONFIG_ZEPHYR_BEHAVIOUR_TREE_LOG_LEVEL_DBG=y
14+
15+
CONFIG_DYNAMIC_THREAD=y
16+
CONFIG_DYNAMIC_THREAD_POOL_SIZE=10
17+
CONFIG_THREAD_STACK_INFO=y
18+
19+
CONFIG_KERNEL_MEM_POOL=y
20+
CONFIG_HEAP_MEM_POOL_SIZE=8192
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright (c) 2026 O.S. Systems Software LTDA.
2+
# Copyright (c) 2026 Freedom Veiculos Eletricos
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
sample:
6+
name: Zephyr Behaviour Tree - Subtree Reuse (Issue #28)
7+
description: |
8+
This sample validates correct traversal when a subtree is used multiple
9+
times in the same behavior tree. It reproduces the scenario from issue #28
10+
where incorrect indices caused nodes to be skipped during execution.
11+
common:
12+
tags: zephyrbt
13+
tests:
14+
samples.zephyrbt.subtree_reuse:
15+
platform_allow:
16+
- qemu_cortex_m3
17+
- nucleo_f767zi
18+
harness: console
19+
harness_config:
20+
type: multi_line
21+
regex:
22+
- "D: tick"
23+
- "D: eval sequence \\[control, 4\\]"
24+
- "D: Deep: 1"
25+
- "D: sequence \\[control, 4\\]"
26+
- "D: eval sub \\[action, 3\\]"
27+
- "D: Deep: 2"
28+
- "D: zephyrbt_action_sub stub function"
29+
- "D: eval a \\[action, 2\\]"
30+
- "D: Deep: 2"
31+
- "D: zephyrbt_action_a stub function"
32+
- "D: eval sub \\[action, 1\\]"
33+
- "D: Deep: 2"
34+
- "D: zephyrbt_action_sub stub function"
35+
- "D: eval b \\[action, 0\\]"
36+
- "D: Deep: 2"
37+
- "D: zephyrbt_action_b stub function"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2026 O.S. Systems Software LTDA.
3+
* Copyright (c) 2026 Freedom Veiculos Eletricos
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include <zephyr/kernel.h>
9+
10+
int main(void)
11+
{
12+
return 0;
13+
}

0 commit comments

Comments
 (0)