Skip to content

Add thread yield capability #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.. Copyright (c) 2024 O.S. Systems Software LTDA.
.. Copyright (c) 2024 Freedom Veiculos Eletricos
.. Copyright (c) 2024-2025 O.S. Systems Software LTDA.
.. Copyright (c) 2024-2025 Freedom Veiculos Eletricos
.. SPDX-License-Identifier: Apache-2.0

.. _zephyr_behaviour_tree_module:
Expand All @@ -14,7 +14,7 @@ Preparation

The first step is prepare the development machine. The developer must follow the
steps at `Zephyr RTOS getting started`_. The recommendation is test using the
``native_posix`` board. In this case, the last step from procedure should try
``qemu_cortex_m3`` board. In this case, the last step from procedure should try
using this command:

.. code-block:: console
Expand All @@ -23,7 +23,7 @@ using this command:

.. code-block:: console

west build -b native_posix samples/hello_world -t run
west build -b qemu_cortex_m3 samples/hello_world -t run

The second step is add your credentials to github. This will add necessary
permissions to clone using ssh and send changes. The github documentation can
Expand Down
28 changes: 20 additions & 8 deletions cmake/zephyrbt-from-behaviourtreecpp-xml.cmake
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Copyright (c) 2024 O.S. Systems Software LTDA.
# Copyright (c) 2024 Freedom Veiculos Eletricos
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
# SPDX-License-Identifier: Apache-2.0

include(CheckIncludeFile)

function(zephyrbt_define_from_behaviourtreecpp_xml
target # The current target used to add the generated files
input_file # The behaviour tree input file
output_inc # Output directory of the generated header
output_src # Output directory of the generated sources
stack_size # The amount of RAM used to run the BT
thread_prio # The Thread Priority
target # The current target used to add the generated files
input_file # The behaviour tree input file
output_inc # Output directory of the generated header
output_src # Output directory of the generated sources
stack_size # The amount of RAM used to run the BT
thread_prio # The Thread Priority
thread_yield # The Thread yield option [ True or False ]
)
get_filename_component(zephyrbt_name ${input_file} NAME_WE [CACHE])
get_filename_component(input_file ${input_file} ABSOLUTE)
Expand All @@ -31,6 +32,16 @@ function(zephyrbt_define_from_behaviourtreecpp_xml
set(zephyrbt_user_include_file "")
endif()

if(${CONFIG_ZEPHYR_BEHAVIOUR_TREE_ALLOW_YIELD})
if(${thread_yield})
set(zephyrbt_thread_yield "-y 1")
else()
set(zephyrbt_thread_yield "-y 0")
endif()
else()
set(zephyrbt_thread_yield "-y 2")
endif()

add_custom_command(
OUTPUT ${zephyrbt_inc_file}
${zephyrbt_data_file}
Expand All @@ -44,6 +55,7 @@ function(zephyrbt_define_from_behaviourtreecpp_xml
-ot ${zephyrbt_stub_file}
-s ${stack_size}
-p ${thread_prio}
${zephyrbt_thread_yield}
${zephyrbt_user_include_file}
)

Expand Down
20 changes: 14 additions & 6 deletions include/zephyr/zephyrbt/zephyrbt.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2024 O.S. Systems Software LTDA.
* Copyright (c) 2024 Freedom Veiculos Eletricos
* Copyright (c) 2024-2025 O.S. Systems Software LTDA.
* Copyright (c) 2024-2025 Freedom Veiculos Eletricos
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -75,10 +75,10 @@ struct zephyrbt_blackboard_item {
struct zephyrbt_context {
#ifdef CONFIG_ZEPHYR_BEHAVIOUR_TREE_NODE_INFO
const char *name;
size_t deep;
uint32_t deep;
#endif
struct zephyrbt_node *node;
const size_t nodes;
const uint32_t nodes;
struct zephyrbt_blackboard_item *blackboard;
#ifdef CONFIG_ZEPHYR_BEHAVIOUR_TREE_USER_DATA
void *user_data;
Expand All @@ -89,6 +89,9 @@ struct zephyrbt_context {
const int stack_size;
const int thread_prio;
k_tid_t tid;
#ifdef CONFIG_ZEPHYR_BEHAVIOUR_TREE_ALLOW_YIELD
bool thread_yield;
#endif
#endif
};

Expand All @@ -102,10 +105,12 @@ struct zephyrbt_context {
* @param _nodes The Behaviour Tree nodes structure.
* @param _stack_size Thread stack size.
* @param _thread_prio Thread priority.
* @param _thread_yield Thread should yield. Default is True.
* @param _user_data User defined data. Default is NULL.
* @param _blackboard The Behaviour Tree blackboard structure.
*/
#define ZEPHYRBT_DEFINE(_name, _nodes, _stack_size, _thread_prio, _user_data, _blackboard) \
#define ZEPHYRBT_DEFINE(_name, _nodes, _stack_size, _thread_prio, \
_thread_yield, _user_data, _blackboard) \
STRUCT_SECTION_ITERABLE(zephyrbt_context, _name) = { \
IF_ENABLED(CONFIG_ZEPHYR_BEHAVIOUR_TREE_NODE_INFO, ( \
.name = #_name, \
Expand All @@ -116,7 +121,10 @@ struct zephyrbt_context {
) \
IF_ENABLED(CONFIG_ZEPHYR_BEHAVIOUR_TREE_DYNAMIC, ( \
.stack_size = _stack_size, \
.thread_prio = _thread_prio, ) \
.thread_prio = _thread_prio, \
IF_ENABLED(CONFIG_ZEPHYR_BEHAVIOUR_TREE_ALLOW_YIELD, ( \
.thread_yield = _thread_yield, ) \
)) \
) \
.node = _nodes, \
.nodes = ARRAY_SIZE(_nodes), \
Expand Down
6 changes: 4 additions & 2 deletions samples/subsys/zephyrbt/dynamic/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2024 O.S. Systems Software LTDA.
# Copyright (c) 2024 Freedom Veiculos Eletricos
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
Expand All @@ -23,6 +23,7 @@ zephyrbt_define_from_behaviourtreecpp_xml(app
${CMAKE_BINARY_DIR}/src
1024
0
yes
)

zephyrbt_define_from_behaviourtreecpp_xml(app
Expand All @@ -31,4 +32,5 @@ zephyrbt_define_from_behaviourtreecpp_xml(app
${CMAKE_BINARY_DIR}/src
1024
0
yes
)
6 changes: 3 additions & 3 deletions samples/subsys/zephyrbt/dynamic/sample.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2024 O.S. Systems Software LTDA.
# Copyright (c) 2024 Freedom Veiculos Eletricos
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
# SPDX-License-Identifier: Apache-2.0

sample:
Expand All @@ -9,7 +9,7 @@ common:
tests:
samples.zephyrbt.dynamic:
platform_allow:
- native_posix/native/64
- qemu_cortex_m3
- nucleo_f767zi
harness: console
harness_config:
Expand Down
6 changes: 3 additions & 3 deletions samples/subsys/zephyrbt/minimal/sample.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2024 O.S. Systems Software LTDA.
# Copyright (c) 2024 Freedom Veiculos Eletricos
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
# SPDX-License-Identifier: Apache-2.0

sample:
Expand All @@ -9,7 +9,7 @@ common:
tests:
samples.zephyrbt.minimal:
platform_allow:
- native_posix/native/64
- qemu_cortex_m3
- nucleo_f767zi
harness: console
harness_config:
Expand Down
5 changes: 3 additions & 2 deletions samples/subsys/zephyrbt/tutorial/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2024 O.S. Systems Software LTDA.
# Copyright (c) 2024 Freedom Veiculos Eletricos
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
Expand All @@ -23,4 +23,5 @@ zephyrbt_define_from_behaviourtreecpp_xml(app
${CMAKE_BINARY_DIR}/src
1024
0
yes
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2024 O.S. Systems Software LTDA.
# Copyright (c) 2024 Freedom Veiculos Eletricos
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
Expand All @@ -24,4 +24,5 @@ zephyrbt_define_from_behaviourtreecpp_xml(app
${CMAKE_BINARY_DIR}/src
1024
0
yes
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2024 O.S. Systems Software LTDA.
# Copyright (c) 2024 Freedom Veiculos Eletricos
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
Expand All @@ -24,4 +24,5 @@ zephyrbt_define_from_behaviourtreecpp_xml(app
${CMAKE_BINARY_DIR}/src
1024
0
yes
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2024 O.S. Systems Software LTDA.
# Copyright (c) 2024 Freedom Veiculos Eletricos
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
Expand All @@ -25,4 +25,5 @@ zephyrbt_define_from_behaviourtreecpp_xml(app
${CMAKE_BINARY_DIR}/src
1024
0
yes
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2024 O.S. Systems Software LTDA.
# Copyright (c) 2024 Freedom Veiculos Eletricos
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
Expand All @@ -26,4 +26,5 @@ zephyrbt_define_from_behaviourtreecpp_xml(app
${CMAKE_BINARY_DIR}/src
1024
0
yes
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2024 O.S. Systems Software LTDA.
# Copyright (c) 2024 Freedom Veiculos Eletricos
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
Expand Down Expand Up @@ -27,4 +27,5 @@ zephyrbt_define_from_behaviourtreecpp_xml(app
${CMAKE_BINARY_DIR}/src
1024
0
yes
)
33 changes: 25 additions & 8 deletions scripts/generate-zephyrbt-from-behaviourtreecpp-xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
# Copyright (c) 2024 O.S. Systems Software LTDA.
# Copyright (c) 2024 Freedom Veiculos Eletricos
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
# SPDX-License-Identifier: Apache-2.0

"""
Expand Down Expand Up @@ -214,7 +214,7 @@ def write_zephyrbt_blackboard_table(f, nodes, builtin, bt, blackboard, name):


def generate_files(filename, output_inc, output_data, output_stub, builtin,
nodes, bt, blackboard, stack, prio, user_file):
nodes, bt, blackboard, stack, prio, thread_yield, user_file):
with open(output_inc, "w") as f:
f.write(HEADER)
f.write(f"\n#ifndef {filename.upper()}_H")
Expand All @@ -235,7 +235,7 @@ def generate_files(filename, output_inc, output_data, output_stub, builtin,
write_zephyrbt_nodes_table(f, nodes, builtin, bt, node_data_name)
write_zephyrbt_blackboard_table(f, nodes, builtin, bt, blackboard, node_blackboard_name)
f.write(f"\nZEPHYRBT_DEFINE({filename}, {filename}_nodes, ")
f.write(f"{stack}, {prio}, NULL, {filename}_blackboard);\n")
f.write(f"{stack}, {prio}, {'true' if thread_yield == 1 else 'false'}, NULL, {filename}_blackboard);\n")

with open(output_stub, "w") as f:
node_data_name = f"{filename}_nodes"
Expand Down Expand Up @@ -313,7 +313,7 @@ def getSearchBuiltinNode(builtin, name):

def getChildIdx(idx_table, num, elem, root, mainroot):
child = None
if elem:
if len(elem):
for subelem in elem:
if subelem.tag == "SubTree":
subelem = resolveSubTree(subelem, mainroot)
Expand Down Expand Up @@ -479,7 +479,7 @@ def clean_files(filename, dir):


def main(zephyrbt_filename, output_inc, output_data, output_stub,
stack, prio, user_file) -> None:
stack, prio, thread_yield, user_file) -> None:
zephyrbt = Path(zephyrbt_filename).stem

sleep = [['msec', 'input_port', None, 'Wait the amount of milliseconds']]
Expand Down Expand Up @@ -526,13 +526,22 @@ def main(zephyrbt_filename, output_inc, output_data, output_stub,
print('Incompatible version')
exit -1

match thread_yield:
case 0:
thread_yield_text = "False"
case 1:
thread_yield_text = "True"
case 2:
thread_yield_text = "Not available"

print("-- Behaviour Tree: ", zephyrbt)
print("-- Stack Size: ", stack)
print("-- Thread Priority:", prio)
print("-- Thread Yield: ", thread_yield_text)

nodes, bt, blackboard = build_bt_set(builtin, root)
generate_files(zephyrbt, output_inc, output_data, output_stub, builtin,
nodes, bt, blackboard, stack, prio, user_file)
nodes, bt, blackboard, stack, prio, thread_yield, user_file)


if __name__ == "__main__":
Expand Down Expand Up @@ -579,14 +588,22 @@ if __name__ == "__main__":
default=0,
help="The Thread Priority",
)
parser.add_argument(
"-y",
"--thread_yield",
type=int,
default=0,
help="The Thread yield",
)
parser.add_argument(
"-u",
"--user_file",
type=bool,
default=False,
help="Add user include file into code generation",
)

args = parser.parse_args()

main(args.input, args.outinc, args.outdata, args.outstub,
args.stack, args.prio, args.user_file)
args.stack, args.prio, args.thread_yield, args.user_file)
Loading
Loading