File tree Expand file tree Collapse file tree 9 files changed +102
-3
lines changed Expand file tree Collapse file tree 9 files changed +102
-3
lines changed Original file line number Diff line number Diff line change @@ -101,6 +101,7 @@ list(APPEND BT_SOURCE
101
101
src/xml_parsing.cpp
102
102
103
103
src/actions/test_node.cpp
104
+ src/actions/sleep_node.cpp
104
105
105
106
src/decorators/inverter_node.cpp
106
107
src/decorators/repeat_node.cpp
Original file line number Diff line number Diff line change
1
+ #pragma once
2
+
3
+ #include " behaviortree_cpp/action_node.h"
4
+ #include " behaviortree_cpp/utils/timer_queue.h"
5
+ #include < atomic>
6
+
7
+ namespace BT
8
+ {
9
+ /* *
10
+ * @brief Sleep for a certain amount of time.
11
+ * Consider also using the decorator <Delay/>
12
+ *
13
+ * <Sleep msec="5000"/>
14
+ */
15
+ class SleepNode : public StatefulActionNode
16
+ {
17
+ public:
18
+
19
+ SleepNode (const std::string& name, const NodeConfig& config);
20
+
21
+ ~SleepNode () override
22
+ {
23
+ halt ();
24
+ }
25
+
26
+ NodeStatus onStart () override ;
27
+
28
+ NodeStatus onRunning () override ;
29
+
30
+ void onHalted () override ;
31
+
32
+ static PortsList providedPorts ()
33
+ {
34
+ return {InputPort<unsigned >(" msec" )};
35
+ }
36
+
37
+ private:
38
+ TimerQueue<> timer_;
39
+ uint64_t timer_id_;
40
+
41
+ bool timer_waiting_;
42
+ std::mutex delay_mutex_;
43
+ };
44
+
45
+ } // namespace BT
Original file line number Diff line number Diff line change 14
14
#pragma once
15
15
16
16
#include " behaviortree_cpp/action_node.h"
17
- #include " behaviortree_cpp/decorators /timer_queue.h"
17
+ #include " behaviortree_cpp/utils /timer_queue.h"
18
18
#include " behaviortree_cpp/scripting/script_parser.hpp"
19
19
20
20
namespace BT
Original file line number Diff line number Diff line change 38
38
#include " behaviortree_cpp/actions/script_node.h"
39
39
#include " behaviortree_cpp/actions/set_blackboard_node.h"
40
40
#include " behaviortree_cpp/actions/test_node.h"
41
+ #include " behaviortree_cpp/actions/sleep_node.h"
41
42
42
43
#include " behaviortree_cpp/decorators/force_success_node.h"
43
44
#include " behaviortree_cpp/decorators/force_failure_node.h"
Original file line number Diff line number Diff line change 1
1
#pragma once
2
2
3
3
#include " behaviortree_cpp/decorator_node.h"
4
+ #include " behaviortree_cpp/utils/timer_queue.h"
4
5
#include < atomic>
5
- #include " timer_queue.h"
6
6
7
7
namespace BT
8
8
{
Original file line number Diff line number Diff line change 1
1
#pragma once
2
2
3
3
#include " behaviortree_cpp/decorator_node.h"
4
+ #include " behaviortree_cpp/utils/timer_queue.h"
4
5
#include < atomic>
5
- #include " behaviortree_cpp/decorators/timer_queue.h"
6
6
7
7
namespace BT
8
8
{
File renamed without changes.
Original file line number Diff line number Diff line change
1
+ #include " behaviortree_cpp/actions/sleep_node.h"
2
+
3
+ namespace BT
4
+ {
5
+
6
+ SleepNode::SleepNode (const std::string& name,
7
+ const NodeConfig& config) :
8
+ StatefulActionNode (name, config),
9
+ timer_waiting_ (false )
10
+ {}
11
+
12
+ NodeStatus SleepNode::onStart ()
13
+ {
14
+ unsigned msec = 0 ;
15
+ if (!getInput (" msec" , msec))
16
+ {
17
+ throw RuntimeError (" Missing parameter [msec] in SleepNode" );
18
+ }
19
+
20
+ if (msec <= 0 ) {
21
+ return NodeStatus::SUCCESS;
22
+ }
23
+
24
+ setStatus (NodeStatus::RUNNING);
25
+
26
+ timer_waiting_ = true ;
27
+
28
+ timer_id_ = timer_.add (std::chrono::milliseconds (msec), [this ](bool aborted) {
29
+ std::unique_lock<std::mutex> lk (delay_mutex_);
30
+ if (!aborted)
31
+ {
32
+ emitWakeUpSignal ();
33
+ }
34
+ timer_waiting_ = false ;
35
+ });
36
+
37
+ return NodeStatus::RUNNING;
38
+ }
39
+
40
+ NodeStatus SleepNode::onRunning ()
41
+ {
42
+ return timer_waiting_ ? NodeStatus::RUNNING : NodeStatus::SUCCESS;
43
+ }
44
+
45
+ void SleepNode::onHalted ()
46
+ {
47
+ timer_waiting_ = false ;
48
+ timer_.cancel (timer_id_);
49
+ }
50
+
51
+ } // namespace BT
Original file line number Diff line number Diff line change @@ -65,6 +65,7 @@ BehaviorTreeFactory::BehaviorTreeFactory()
65
65
registerNodeType<AlwaysFailureNode>(" AlwaysFailure" );
66
66
registerNodeType<ScriptNode>(" Script" );
67
67
registerNodeType<SetBlackboard>(" SetBlackboard" );
68
+ registerNodeType<SleepNode>(" Sleep" );
68
69
69
70
registerNodeType<SubTreeNode>(" SubTree" );
70
71
You can’t perform that action at this time.
0 commit comments