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
101101 src/xml_parsing.cpp
102102
103103 src/actions/test_node.cpp
104+ src/actions/sleep_node.cpp
104105
105106 src/decorators/inverter_node.cpp
106107 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 1414#pragma once
1515
1616#include " behaviortree_cpp/action_node.h"
17- #include " behaviortree_cpp/decorators /timer_queue.h"
17+ #include " behaviortree_cpp/utils /timer_queue.h"
1818#include " behaviortree_cpp/scripting/script_parser.hpp"
1919
2020namespace BT
Original file line number Diff line number Diff line change 3838#include " behaviortree_cpp/actions/script_node.h"
3939#include " behaviortree_cpp/actions/set_blackboard_node.h"
4040#include " behaviortree_cpp/actions/test_node.h"
41+ #include " behaviortree_cpp/actions/sleep_node.h"
4142
4243#include " behaviortree_cpp/decorators/force_success_node.h"
4344#include " behaviortree_cpp/decorators/force_failure_node.h"
Original file line number Diff line number Diff line change 11#pragma once
22
33#include " behaviortree_cpp/decorator_node.h"
4+ #include " behaviortree_cpp/utils/timer_queue.h"
45#include < atomic>
5- #include " timer_queue.h"
66
77namespace BT
88{
Original file line number Diff line number Diff line change 11#pragma once
22
33#include " behaviortree_cpp/decorator_node.h"
4+ #include " behaviortree_cpp/utils/timer_queue.h"
45#include < atomic>
5- #include " behaviortree_cpp/decorators/timer_queue.h"
66
77namespace BT
88{
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()
6565 registerNodeType<AlwaysFailureNode>(" AlwaysFailure" );
6666 registerNodeType<ScriptNode>(" Script" );
6767 registerNodeType<SetBlackboard>(" SetBlackboard" );
68+ registerNodeType<SleepNode>(" Sleep" );
6869
6970 registerNodeType<SubTreeNode>(" SubTree" );
7071
You can’t perform that action at this time.
0 commit comments