Skip to content

Commit e9b2c0b

Browse files
committed
Add thread yield capability
Current the Behaviour Tree does not yield. This could lead to side effects like starvation and lost of interrupts on critical systems. This add the capability to the user define if engine should perform a k_yield() to give opportunity to other threads with the same priority to run. This do not affect the time slice configuration and both can be used. The time slice can be useful when a very long action may block tree walk. To enable time slice make sure that behaviour tree priority is greater than TIMESLICE_PRIORITY and TIMESLICING=y. The best walkthru performace can be achieve when ZEPHYR_BEHAVIOUR_TREE_ALLOW_YIELD option is disabled. Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
1 parent 92397db commit e9b2c0b

File tree

12 files changed

+95
-37
lines changed

12 files changed

+95
-37
lines changed

cmake/zephyrbt-from-behaviourtreecpp-xml.cmake

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
# Copyright (c) 2024 O.S. Systems Software LTDA.
2-
# Copyright (c) 2024 Freedom Veiculos Eletricos
1+
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
2+
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
33
# SPDX-License-Identifier: Apache-2.0
44

55
include(CheckIncludeFile)
66

77
function(zephyrbt_define_from_behaviourtreecpp_xml
8-
target # The current target used to add the generated files
9-
input_file # The behaviour tree input file
10-
output_inc # Output directory of the generated header
11-
output_src # Output directory of the generated sources
12-
stack_size # The amount of RAM used to run the BT
13-
thread_prio # The Thread Priority
8+
target # The current target used to add the generated files
9+
input_file # The behaviour tree input file
10+
output_inc # Output directory of the generated header
11+
output_src # Output directory of the generated sources
12+
stack_size # The amount of RAM used to run the BT
13+
thread_prio # The Thread Priority
14+
thread_yield # The Thread yield option [ True or False ]
1415
)
1516
get_filename_component(zephyrbt_name ${input_file} NAME_WE [CACHE])
1617
get_filename_component(input_file ${input_file} ABSOLUTE)
@@ -31,6 +32,12 @@ function(zephyrbt_define_from_behaviourtreecpp_xml
3132
set(zephyrbt_user_include_file "")
3233
endif()
3334

35+
if(${thread_yield})
36+
set(zephyrbt_thread_yield "-y True")
37+
else()
38+
set(zephyrbt_thread_yield "")
39+
endif()
40+
3441
add_custom_command(
3542
OUTPUT ${zephyrbt_inc_file}
3643
${zephyrbt_data_file}
@@ -44,6 +51,7 @@ function(zephyrbt_define_from_behaviourtreecpp_xml
4451
-ot ${zephyrbt_stub_file}
4552
-s ${stack_size}
4653
-p ${thread_prio}
54+
${zephyrbt_thread_yield}
4755
${zephyrbt_user_include_file}
4856
)
4957

include/zephyr/zephyrbt/zephyrbt.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (c) 2024 O.S. Systems Software LTDA.
3-
* Copyright (c) 2024 Freedom Veiculos Eletricos
2+
* Copyright (c) 2024-2025 O.S. Systems Software LTDA.
3+
* Copyright (c) 2024-2025 Freedom Veiculos Eletricos
44
*
55
* SPDX-License-Identifier: Apache-2.0
66
*/
@@ -89,6 +89,9 @@ struct zephyrbt_context {
8989
const int stack_size;
9090
const int thread_prio;
9191
k_tid_t tid;
92+
#ifdef CONFIG_ZEPHYR_BEHAVIOUR_TREE_ALLOW_YIELD
93+
bool thread_yield;
94+
#endif
9295
#endif
9396
};
9497

@@ -102,10 +105,12 @@ struct zephyrbt_context {
102105
* @param _nodes The Behaviour Tree nodes structure.
103106
* @param _stack_size Thread stack size.
104107
* @param _thread_prio Thread priority.
108+
* @param _thread_yield Thread should yield. Default is True.
105109
* @param _user_data User defined data. Default is NULL.
106110
* @param _blackboard The Behaviour Tree blackboard structure.
107111
*/
108-
#define ZEPHYRBT_DEFINE(_name, _nodes, _stack_size, _thread_prio, _user_data, _blackboard) \
112+
#define ZEPHYRBT_DEFINE(_name, _nodes, _stack_size, _thread_prio, \
113+
_thread_yield, _user_data, _blackboard) \
109114
STRUCT_SECTION_ITERABLE(zephyrbt_context, _name) = { \
110115
IF_ENABLED(CONFIG_ZEPHYR_BEHAVIOUR_TREE_NODE_INFO, ( \
111116
.name = #_name, \
@@ -116,7 +121,10 @@ struct zephyrbt_context {
116121
) \
117122
IF_ENABLED(CONFIG_ZEPHYR_BEHAVIOUR_TREE_DYNAMIC, ( \
118123
.stack_size = _stack_size, \
119-
.thread_prio = _thread_prio, ) \
124+
.thread_prio = _thread_prio, \
125+
IF_ENABLED(CONFIG_ZEPHYR_BEHAVIOUR_TREE_ALLOW_YIELD, ( \
126+
.thread_yield = _thread_yield, ) \
127+
)) \
120128
) \
121129
.node = _nodes, \
122130
.nodes = ARRAY_SIZE(_nodes), \

samples/subsys/zephyrbt/dynamic/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Copyright (c) 2024 O.S. Systems Software LTDA.
2-
# Copyright (c) 2024 Freedom Veiculos Eletricos
1+
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
2+
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
33
# SPDX-License-Identifier: Apache-2.0
44

55
cmake_minimum_required(VERSION 3.20.0)
@@ -23,6 +23,7 @@ zephyrbt_define_from_behaviourtreecpp_xml(app
2323
${CMAKE_BINARY_DIR}/src
2424
1024
2525
0
26+
yes
2627
)
2728

2829
zephyrbt_define_from_behaviourtreecpp_xml(app
@@ -31,4 +32,5 @@ zephyrbt_define_from_behaviourtreecpp_xml(app
3132
${CMAKE_BINARY_DIR}/src
3233
1024
3334
0
35+
yes
3436
)

samples/subsys/zephyrbt/tutorial/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Copyright (c) 2024 O.S. Systems Software LTDA.
2-
# Copyright (c) 2024 Freedom Veiculos Eletricos
1+
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
2+
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
33
# SPDX-License-Identifier: Apache-2.0
44

55
cmake_minimum_required(VERSION 3.20.0)
@@ -23,4 +23,5 @@ zephyrbt_define_from_behaviourtreecpp_xml(app
2323
${CMAKE_BINARY_DIR}/src
2424
1024
2525
0
26+
yes
2627
)

samples/subsys/zephyrbt/tutorial/lessons/lesson-1/resources/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Copyright (c) 2024 O.S. Systems Software LTDA.
2-
# Copyright (c) 2024 Freedom Veiculos Eletricos
1+
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
2+
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
33
# SPDX-License-Identifier: Apache-2.0
44

55
cmake_minimum_required(VERSION 3.20.0)
@@ -24,4 +24,5 @@ zephyrbt_define_from_behaviourtreecpp_xml(app
2424
${CMAKE_BINARY_DIR}/src
2525
1024
2626
0
27+
yes
2728
)

samples/subsys/zephyrbt/tutorial/lessons/lesson-2/resources/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Copyright (c) 2024 O.S. Systems Software LTDA.
2-
# Copyright (c) 2024 Freedom Veiculos Eletricos
1+
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
2+
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
33
# SPDX-License-Identifier: Apache-2.0
44

55
cmake_minimum_required(VERSION 3.20.0)
@@ -24,4 +24,5 @@ zephyrbt_define_from_behaviourtreecpp_xml(app
2424
${CMAKE_BINARY_DIR}/src
2525
1024
2626
0
27+
yes
2728
)

samples/subsys/zephyrbt/tutorial/lessons/lesson-3/resources/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Copyright (c) 2024 O.S. Systems Software LTDA.
2-
# Copyright (c) 2024 Freedom Veiculos Eletricos
1+
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
2+
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
33
# SPDX-License-Identifier: Apache-2.0
44

55
cmake_minimum_required(VERSION 3.20.0)
@@ -25,4 +25,5 @@ zephyrbt_define_from_behaviourtreecpp_xml(app
2525
${CMAKE_BINARY_DIR}/src
2626
1024
2727
0
28+
yes
2829
)

samples/subsys/zephyrbt/tutorial/lessons/lesson-4/resources/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Copyright (c) 2024 O.S. Systems Software LTDA.
2-
# Copyright (c) 2024 Freedom Veiculos Eletricos
1+
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
2+
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
33
# SPDX-License-Identifier: Apache-2.0
44

55
cmake_minimum_required(VERSION 3.20.0)
@@ -26,4 +26,5 @@ zephyrbt_define_from_behaviourtreecpp_xml(app
2626
${CMAKE_BINARY_DIR}/src
2727
1024
2828
0
29+
yes
2930
)

samples/subsys/zephyrbt/tutorial/lessons/lesson-5/resources/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Copyright (c) 2024 O.S. Systems Software LTDA.
2-
# Copyright (c) 2024 Freedom Veiculos Eletricos
1+
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
2+
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
33
# SPDX-License-Identifier: Apache-2.0
44

55
cmake_minimum_required(VERSION 3.20.0)
@@ -27,4 +27,5 @@ zephyrbt_define_from_behaviourtreecpp_xml(app
2727
${CMAKE_BINARY_DIR}/src
2828
1024
2929
0
30+
yes
3031
)

scripts/generate-zephyrbt-from-behaviourtreecpp-xml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
2-
# Copyright (c) 2024 O.S. Systems Software LTDA.
3-
# Copyright (c) 2024 Freedom Veiculos Eletricos
2+
# Copyright (c) 2024-2025 O.S. Systems Software LTDA.
3+
# Copyright (c) 2024-2025 Freedom Veiculos Eletricos
44
# SPDX-License-Identifier: Apache-2.0
55

66
"""
@@ -214,7 +214,7 @@ def write_zephyrbt_blackboard_table(f, nodes, builtin, bt, blackboard, name):
214214

215215

216216
def generate_files(filename, output_inc, output_data, output_stub, builtin,
217-
nodes, bt, blackboard, stack, prio, user_file):
217+
nodes, bt, blackboard, stack, prio, thread_yield, user_file):
218218
with open(output_inc, "w") as f:
219219
f.write(HEADER)
220220
f.write(f"\n#ifndef {filename.upper()}_H")
@@ -235,7 +235,7 @@ def generate_files(filename, output_inc, output_data, output_stub, builtin,
235235
write_zephyrbt_nodes_table(f, nodes, builtin, bt, node_data_name)
236236
write_zephyrbt_blackboard_table(f, nodes, builtin, bt, blackboard, node_blackboard_name)
237237
f.write(f"\nZEPHYRBT_DEFINE({filename}, {filename}_nodes, ")
238-
f.write(f"{stack}, {prio}, NULL, {filename}_blackboard);\n")
238+
f.write(f"{stack}, {prio}, {'true' if thread_yield else 'false'}, NULL, {filename}_blackboard);\n")
239239

240240
with open(output_stub, "w") as f:
241241
node_data_name = f"{filename}_nodes"
@@ -479,7 +479,7 @@ def clean_files(filename, dir):
479479

480480

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

485485
sleep = [['msec', 'input_port', None, 'Wait the amount of milliseconds']]
@@ -529,10 +529,11 @@ def main(zephyrbt_filename, output_inc, output_data, output_stub,
529529
print("-- Behaviour Tree: ", zephyrbt)
530530
print("-- Stack Size: ", stack)
531531
print("-- Thread Priority:", prio)
532+
print("-- Thread Yield: ", thread_yield)
532533

533534
nodes, bt, blackboard = build_bt_set(builtin, root)
534535
generate_files(zephyrbt, output_inc, output_data, output_stub, builtin,
535-
nodes, bt, blackboard, stack, prio, user_file)
536+
nodes, bt, blackboard, stack, prio, thread_yield, user_file)
536537

537538

538539
if __name__ == "__main__":
@@ -579,14 +580,22 @@ if __name__ == "__main__":
579580
default=0,
580581
help="The Thread Priority",
581582
)
583+
parser.add_argument(
584+
"-y",
585+
"--thread_yield",
586+
type=bool,
587+
default=False,
588+
help="The Thread yield",
589+
)
582590
parser.add_argument(
583591
"-u",
584592
"--user_file",
585593
type=bool,
586594
default=False,
587595
help="Add user include file into code generation",
588596
)
597+
589598
args = parser.parse_args()
590599

591600
main(args.input, args.outinc, args.outdata, args.outstub,
592-
args.stack, args.prio, args.user_file)
601+
args.stack, args.prio, args.thread_yield, args.user_file)

0 commit comments

Comments
 (0)