1313
1414#include " behaviortree_cpp/controls/parallel_node.h"
1515
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 )
1825{
1926}
2027
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 ()
2243{
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+
2351 success_childred_num_ = 0 ;
2452 failure_childred_num_ = 0 ;
2553 // Vector size initialization. children_count_ could change at runtime if you edit the tree
@@ -35,9 +63,9 @@ BT::NodeStatus BT::ParallelNode::tick()
3563 switch (child_status)
3664 {
3765 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_ )
4169 {
4270 success_childred_num_ = 0 ;
4371 failure_childred_num_ = 0 ;
@@ -46,9 +74,9 @@ BT::NodeStatus BT::ParallelNode::tick()
4674 }
4775 break ;
4876 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_ )
5280 {
5381 success_childred_num_ = 0 ;
5482 failure_childred_num_ = 0 ;
@@ -66,19 +94,21 @@ BT::NodeStatus BT::ParallelNode::tick()
6694 return NodeStatus::RUNNING;
6795}
6896
69- void BT:: ParallelNode::halt ()
97+ void ParallelNode::halt ()
7098{
7199 success_childred_num_ = 0 ;
72100 failure_childred_num_ = 0 ;
73- BT:: ControlNode::halt ();
101+ ControlNode::halt ();
74102}
75103
76- unsigned int BT:: ParallelNode::thresholdM ()
104+ unsigned int ParallelNode::thresholdM ()
77105{
78- return threshold_M_ ;
106+ return threshold_ ;
79107}
80108
81- void BT:: ParallelNode::setThresholdM (unsigned int threshold_M)
109+ void ParallelNode::setThresholdM (unsigned int threshold_M)
82110{
83- threshold_M_ = threshold_M;
111+ threshold_ = threshold_M;
112+ }
113+
84114}
0 commit comments