Skip to content

Commit

Permalink
Add sleep action
Browse files Browse the repository at this point in the history
Signed-off-by: Gerson Fernando Budke <[email protected]>
  • Loading branch information
nandojve authored and otavio committed Aug 8, 2024
1 parent 3643c9e commit 7b5e157
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/zephyr/zephyrbt/zephyrbt.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ enum zephyrbt_child_status zephyrbt_action_always_success(struct zephyrbt_contex
struct zephyrbt_node *self);
enum zephyrbt_child_status zephyrbt_action_always_failure(struct zephyrbt_context *ctx,
struct zephyrbt_node *self);
enum zephyrbt_sleep_attributes {
ZEPHYRBT_SLEEP_ATTRIBUTE_MSEC,
};
enum zephyrbt_child_status zephyrbt_action_sleep(struct zephyrbt_context *ctx,
struct zephyrbt_node *self);
enum zephyrbt_child_status zephyrbt_action_sleep_init(struct zephyrbt_context *ctx,
struct zephyrbt_node *self);
enum zephyrbt_child_status zephyrbt_control_fallback(struct zephyrbt_context *ctx,
struct zephyrbt_node *self);
enum zephyrbt_child_status zephyrbt_control_sequence(struct zephyrbt_context *ctx,
Expand Down
3 changes: 3 additions & 0 deletions scripts/generate-zephyrbt-from-behaviourtreecpp-xml
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ def main(zephyrbt_filename, output_inc, output_data, output_stub,
stack, prio, user_file) -> None:
zephyrbt = Path(zephyrbt_filename).stem

sleep = [['msec', 'input_port', None, 'Wait the amount of milliseconds']]

parallel = [['failure_count', 'input_port', '1', 'number of children which need to fail to trigger a FAILURE'],
['success_count', 'input_port', '-1', 'number of children which need to succeed to trigger a SUCCESS']
]
Expand All @@ -439,6 +441,7 @@ def main(zephyrbt_filename, output_inc, output_data, output_stub,
# node_name, type, has_init, blackboard_attributes
builtin = [('always_success', 'action', False, []),
('always_failure', 'action', False, []),
('sleep', 'action', True, sleep),
('fallback', 'control', False, []),
('sequence', 'control', False, []),
('parallel', 'control', True, parallel),
Expand Down
1 change: 1 addition & 0 deletions subsys/zephyrbt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ zephyr_sources_ifdef(CONFIG_ZEPHYR_BEHAVIOUR_TREE
)

zephyr_sources_ifdef(CONFIG_ZEPHYR_BEHAVIOUR_TREE_NODE_CONTEXT
zephyrbt_action_sleep.c
zephyrbt_decorator_delay.c
zephyrbt_decorator_run_once.c
zephyrbt_decorator_timeout.c
Expand Down
63 changes: 63 additions & 0 deletions subsys/zephyrbt/zephyrbt_action_sleep.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2024 O.S. Systems Software LTDA.
* Copyright (c) 2024 Freedom Veiculos Eletricos
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/zephyrbt/zephyrbt.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(zephyrbt_action_sleep, CONFIG_ZEPHYR_BEHAVIOUR_TREE_LOG_LEVEL);

struct zephyrbt_action_sleep_context {
struct zephyrbt_blackboard_item *msec;
};

enum zephyrbt_child_status zephyrbt_action_sleep_init(struct zephyrbt_context *ctx,
struct zephyrbt_node *self)
{
#if defined(CONFIG_ZEPHYR_BEHAVIOUR_TREE_NODE_INFO)
LOG_DBG("init: %s", self->name);
#endif

struct zephyrbt_action_sleep_context *sleep;

sleep = k_malloc(sizeof(struct zephyrbt_action_sleep_context));
self->ctx = sleep;

if (sleep == NULL) {
LOG_ERR("Context can not be allocate.");
return ZEPHYRBT_CHILD_FAILURE_STATUS;
}

memset(sleep, 0, sizeof(struct zephyrbt_action_sleep_context));

sleep->msec = zephyrbt_search_blackboard(ctx, self->index, ZEPHYRBT_SLEEP_ATTRIBUTE_MSEC);

if (sleep->msec == NULL) {
LOG_ERR("Invalid sleep msec value.");
return ZEPHYRBT_CHILD_FAILURE_STATUS;
}

return ZEPHYRBT_CHILD_SUCCESS_STATUS;
}

enum zephyrbt_child_status zephyrbt_action_sleep(struct zephyrbt_context *ctx,
struct zephyrbt_node *self)
{
#if defined(CONFIG_ZEPHYR_BEHAVIOUR_TREE_NODE_INFO)
LOG_DBG("%s", self->name);
#endif

struct zephyrbt_action_sleep_context *sleep;
sleep = (struct zephyrbt_action_sleep_context *)self->ctx;

if (sleep == NULL) {
LOG_ERR("Undefined behaviour on zephyrbt_action_sleep.");
return ZEPHYRBT_CHILD_FAILURE_STATUS;
}

k_msleep((uintptr_t)sleep->msec->item);

return ZEPHYRBT_CHILD_SUCCESS_STATUS;
}

0 comments on commit 7b5e157

Please sign in to comment.