13
13
14
14
#include " behaviortree_cpp/controls/parallel_node.h"
15
15
16
- BT::ParallelNode::ParallelNode (const std::string& name, int threshold_M)
17
- : ControlNode::ControlNode(name, NodeParameters()), threshold_M_(threshold_M)
16
+ namespace BT
17
+ {
18
+
19
+ constexpr const char * ParallelNode::THRESHOLD_KEY;
20
+
21
+ ParallelNode::ParallelNode (const std::string& name, int threshold)
22
+ : ControlNode::ControlNode(name, {{THRESHOLD_KEY, std::to_string (threshold)}}),
23
+ threshold_ (threshold),
24
+ refresh_parameter_ (false )
18
25
{
19
26
}
20
27
21
- BT::NodeStatus BT::ParallelNode::tick ()
28
+ ParallelNode::ParallelNode (const std::string &name,
29
+ const NodeParameters ¶ms)
30
+ : ControlNode::ControlNode(name, params),
31
+ threshold_ (1 ),
32
+ refresh_parameter_(false )
33
+ {
34
+ auto param = getParam<unsigned >( THRESHOLD_KEY );
35
+ if ( !param )
36
+ {
37
+ throw std::runtime_error (" Missing parameter [threshold] in ParallelNode" );
38
+ }
39
+ refresh_parameter_ = isBlackboardPattern ( params.begin ()->second );
40
+ }
41
+
42
+ NodeStatus ParallelNode::tick ()
22
43
{
44
+ if ( refresh_parameter_ )
45
+ {
46
+ // Read it at every tick. Since it points to the blackboard,
47
+ // it may change dynamically
48
+ getParam (THRESHOLD_KEY, threshold_);
49
+ }
50
+
23
51
success_childred_num_ = 0 ;
24
52
failure_childred_num_ = 0 ;
25
53
// Vector size initialization. children_count_ could change at runtime if you edit the tree
@@ -35,9 +63,9 @@ BT::NodeStatus BT::ParallelNode::tick()
35
63
switch (child_status)
36
64
{
37
65
case NodeStatus::SUCCESS:
38
- child_node->setStatus (
39
- NodeStatus::IDLE); // the child goes in idle if it has returned success.
40
- if (++success_childred_num_ == threshold_M_ )
66
+ child_node->setStatus (NodeStatus::IDLE);
67
+ // the child goes in idle if it has returned success.
68
+ if (++success_childred_num_ == threshold_ )
41
69
{
42
70
success_childred_num_ = 0 ;
43
71
failure_childred_num_ = 0 ;
@@ -46,9 +74,9 @@ BT::NodeStatus BT::ParallelNode::tick()
46
74
}
47
75
break ;
48
76
case NodeStatus::FAILURE:
49
- child_node->setStatus (
50
- NodeStatus::IDLE); // the child goes in idle if it has returned failure.
51
- if (++failure_childred_num_ > children_count - threshold_M_ )
77
+ child_node->setStatus (NodeStatus::IDLE);
78
+ // the child goes in idle if it has returned failure.
79
+ if (++failure_childred_num_ > children_count - threshold_ )
52
80
{
53
81
success_childred_num_ = 0 ;
54
82
failure_childred_num_ = 0 ;
@@ -66,19 +94,21 @@ BT::NodeStatus BT::ParallelNode::tick()
66
94
return NodeStatus::RUNNING;
67
95
}
68
96
69
- void BT:: ParallelNode::halt ()
97
+ void ParallelNode::halt ()
70
98
{
71
99
success_childred_num_ = 0 ;
72
100
failure_childred_num_ = 0 ;
73
- BT:: ControlNode::halt ();
101
+ ControlNode::halt ();
74
102
}
75
103
76
- unsigned int BT:: ParallelNode::thresholdM ()
104
+ unsigned int ParallelNode::thresholdM ()
77
105
{
78
- return threshold_M_ ;
106
+ return threshold_ ;
79
107
}
80
108
81
- void BT:: ParallelNode::setThresholdM (unsigned int threshold_M)
109
+ void ParallelNode::setThresholdM (unsigned int threshold_M)
82
110
{
83
- threshold_M_ = threshold_M;
111
+ threshold_ = threshold_M;
112
+ }
113
+
84
114
}
0 commit comments