Skip to content

Commit 11774fc

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 <[email protected]>
1 parent 11a61a2 commit 11774fc

File tree

12 files changed

+107
-37
lines changed

12 files changed

+107
-37
lines changed

cmake/zephyrbt-from-behaviourtreecpp-xml.cmake

Lines changed: 20 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,16 @@ function(zephyrbt_define_from_behaviourtreecpp_xml
3132
set(zephyrbt_user_include_file "")
3233
endif()
3334

35+
if(${CONFIG_ZEPHYR_BEHAVIOUR_TREE_ALLOW_YIELD})
36+
if(${thread_yield})
37+
set(zephyrbt_thread_yield "-y 1")
38+
else()
39+
set(zephyrbt_thread_yield "-y 0")
40+
endif()
41+
else()
42+
set(zephyrbt_thread_yield "-y 2")
43+
endif()
44+
3445
add_custom_command(
3546
OUTPUT ${zephyrbt_inc_file}
3647
${zephyrbt_data_file}
@@ -44,6 +55,7 @@ function(zephyrbt_define_from_behaviourtreecpp_xml
4455
-ot ${zephyrbt_stub_file}
4556
-s ${stack_size}
4657
-p ${thread_prio}
58+
${zephyrbt_thread_yield}
4759
${zephyrbt_user_include_file}
4860
)
4961

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: 24 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 == 1 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']]
@@ -526,13 +526,22 @@ def main(zephyrbt_filename, output_inc, output_data, output_stub,
526526
print('Incompatible version')
527527
exit -1
528528

529+
match thread_yield:
530+
case 0:
531+
thread_yield_text = "False"
532+
case 1:
533+
thread_yield_text = "True"
534+
case 2:
535+
thread_yield_text = "Not available"
536+
529537
print("-- Behaviour Tree: ", zephyrbt)
530538
print("-- Stack Size: ", stack)
531539
print("-- Thread Priority:", prio)
540+
print("-- Thread Yield: ", thread_yield_text)
532541

533542
nodes, bt, blackboard = build_bt_set(builtin, root)
534543
generate_files(zephyrbt, output_inc, output_data, output_stub, builtin,
535-
nodes, bt, blackboard, stack, prio, user_file)
544+
nodes, bt, blackboard, stack, prio, thread_yield, user_file)
536545

537546

538547
if __name__ == "__main__":
@@ -579,14 +588,22 @@ if __name__ == "__main__":
579588
default=0,
580589
help="The Thread Priority",
581590
)
591+
parser.add_argument(
592+
"-y",
593+
"--thread_yield",
594+
type=int,
595+
default=0,
596+
help="The Thread yield",
597+
)
582598
parser.add_argument(
583599
"-u",
584600
"--user_file",
585601
type=bool,
586602
default=False,
587603
help="Add user include file into code generation",
588604
)
605+
589606
args = parser.parse_args()
590607

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

0 commit comments

Comments
 (0)