/src/behaviortreecpp/src/actions/sleep_node.cpp
Line | Count | Source |
1 | | #include "behaviortree_cpp/actions/sleep_node.h" |
2 | | |
3 | | namespace BT |
4 | | { |
5 | | |
6 | | SleepNode::SleepNode(const std::string& name, const NodeConfig& config) |
7 | 76 | : StatefulActionNode(name, config), timer_waiting_(false) |
8 | 76 | {} |
9 | | |
10 | | NodeStatus SleepNode::onStart() |
11 | 0 | { |
12 | 0 | unsigned msec = 0; |
13 | 0 | if(!getInput("msec", msec)) |
14 | 0 | { |
15 | 0 | throw RuntimeError("Missing parameter [msec] in SleepNode"); |
16 | 0 | } |
17 | | |
18 | 0 | if(msec <= 0) |
19 | 0 | { |
20 | 0 | return NodeStatus::SUCCESS; |
21 | 0 | } |
22 | | |
23 | 0 | setStatus(NodeStatus::RUNNING); |
24 | |
|
25 | 0 | timer_waiting_ = true; |
26 | |
|
27 | 0 | timer_id_ = timer_.add(std::chrono::milliseconds(msec), [this](bool aborted) { |
28 | 0 | const std::unique_lock<std::mutex> lk(delay_mutex_); |
29 | 0 | if(!aborted) |
30 | 0 | { |
31 | 0 | emitWakeUpSignal(); |
32 | 0 | } |
33 | 0 | timer_waiting_ = false; |
34 | 0 | }); |
35 | |
|
36 | 0 | return NodeStatus::RUNNING; |
37 | 0 | } |
38 | | |
39 | | NodeStatus SleepNode::onRunning() |
40 | 0 | { |
41 | 0 | return timer_waiting_ ? NodeStatus::RUNNING : NodeStatus::SUCCESS; |
42 | 0 | } |
43 | | |
44 | | void SleepNode::onHalted() |
45 | 0 | { |
46 | 0 | timer_waiting_ = false; |
47 | 0 | timer_.cancel(timer_id_); |
48 | 0 | } |
49 | | |
50 | | } // namespace BT |