This sample validates the correct behavior tree traversal when a subtree is used multiple times. It reproduces the scenario from GitHub issue #28 where incorrect indices caused nodes to be skipped during execution.
The behavior tree structure is:
Sequence
├── SubTree (btree_subtree) → sub
├── A
├── SubTree (btree_subtree) → sub
└── B
The expected execution order is: sub → A → sub → B
Prior to the fix, the second instance of the subtree had incorrect sibling indices, causing node A to be skipped entirely.
This application can be built and executed on qemu_cortex_m3 as follows:
west build -p -b qemu_cortex_m3 samples/subsys/zephyrbt/subtree_reuse -t runTo build for another board, change "qemu_cortex_m3" above to that board's name.
*** Booting Zephyr OS ***
D: zephyrbt_action_sub_init stub function
D: zephyrbt_action_a_init stub function
D: zephyrbt_action_sub_init stub function
D: zephyrbt_action_b_init stub function
D: tick
D: eval sequence [control, 4]
D: Deep: 1
D: sequence [control, 4]
D: eval sub [action, 3]
D: Deep: 2
D: zephyrbt_action_sub stub function
D: eval a [action, 2]
D: Deep: 2
D: zephyrbt_action_a stub function
D: eval sub [action, 1]
D: Deep: 2
D: zephyrbt_action_sub stub function
D: eval b [action, 0]
D: Deep: 2
D: zephyrbt_action_b stub functionThe key verification points are:
- All four nodes (sub, a, sub, b) are evaluated in the correct order
- Node 'a' is not skipped (which was the bug symptom)
- Each subtree instance has a unique index (3 and 1)