Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 709c729

Browse files
author
Davide Faconti
committed
experimental: add priorityFallbackNode
1 parent fcd0d1e commit 709c729

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ list(APPEND BT_SOURCE
9393
src/controls/sequence_star_node.cpp
9494
src/controls/fallback_node.cpp
9595
src/controls/fallback_star_node.cpp
96+
src/controls/priority_fallback.cpp
9697

9798
src/loggers/bt_cout_logger.cpp
9899
src/loggers/bt_file_logger.cpp

include/behaviortree_cpp/behavior_tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "behaviortree_cpp/controls/sequence_star_node.h"
2222
#include "behaviortree_cpp/controls/fallback_star_node.h"
23+
#include "behaviortree_cpp/controls/priority_fallback.h"
2324

2425
#include "behaviortree_cpp/action_node.h"
2526
#include "behaviortree_cpp/condition_node.h"
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* Copyright (C) 2018 Davide Faconti - All Rights Reserved
2+
*
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
4+
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
5+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
10+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11+
*/
12+
13+
#ifndef PRIORITY_FALLBACK_NODE_H
14+
#define PRIORITY_FALLBACK_NODE_H
15+
16+
#include "behaviortree_cpp/control_node.h"
17+
18+
namespace BT
19+
{
20+
21+
class PriorityFallbackNode : public ControlNode
22+
{
23+
public:
24+
PriorityFallbackNode(const std::string& name, const NodeParameters& params);
25+
26+
static const NodeParameters& requiredNodeParameters()
27+
{
28+
static NodeParameters params = {{CHILDREN_PRIORITY, ""}};
29+
return params;
30+
}
31+
32+
private:
33+
virtual BT::NodeStatus tick() override;
34+
35+
std::vector<int> children_order_;
36+
bool refresh_parameter_;
37+
static constexpr const char* CHILDREN_PRIORITY = "children_priority";
38+
};
39+
40+
}
41+
42+
#endif

src/bt_factory.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ BehaviorTreeFactory::BehaviorTreeFactory()
2121
registerNodeType<FallbackStarNode>("FallbackStar");
2222
registerNodeType<SequenceNode>("Sequence");
2323
registerNodeType<SequenceStarNode>("SequenceStar");
24+
registerNodeType<PriorityFallbackNode>("PrioritySequence");
2425

2526
registerNodeType<InverterNode>("Inverter");
2627
registerNodeType<RetryNode>("RetryUntilSuccesful");

src/controls/priority_fallback.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* Copyright (C) 2018 Davide Faconti - All Rights Reserved
2+
*
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
4+
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
5+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
10+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11+
*/
12+
13+
#include "behaviortree_cpp/controls/priority_fallback.h"
14+
15+
namespace BT
16+
{
17+
18+
constexpr const char* PriorityFallbackNode::CHILDREN_PRIORITY;
19+
20+
PriorityFallbackNode::PriorityFallbackNode(const std::string &name,
21+
const NodeParameters &params)
22+
: ControlNode(name, params)
23+
, refresh_parameter_(false)
24+
{
25+
if( !getParam(CHILDREN_PRIORITY, children_order_) )
26+
{
27+
throw std::runtime_error("Missing parameter [children_priority]"
28+
" in PrioritySequenceNode");
29+
}
30+
refresh_parameter_ = isBlackboardPattern( params.at(CHILDREN_PRIORITY) );
31+
}
32+
33+
NodeStatus PriorityFallbackNode::tick()
34+
{
35+
const unsigned children_count = children_nodes_.size();
36+
37+
if( refresh_parameter_ )
38+
{
39+
if( !getParam(CHILDREN_PRIORITY, children_order_) )
40+
{
41+
throw std::runtime_error("Problem parsing vector [children_priority] in PrioritySequenceNode");
42+
}
43+
}
44+
if( children_order_.size() != children_count)
45+
{
46+
throw std::runtime_error("Size mismatch in [children_priority], at PrioritySequenceNode");
47+
}
48+
49+
setStatus(NodeStatus::RUNNING);
50+
51+
for (unsigned i = 0; i < children_count; i++)
52+
{
53+
int index = children_order_[i];
54+
if( index < 0)
55+
{
56+
continue;
57+
}
58+
TreeNode* child_node = children_nodes_[index];
59+
const NodeStatus child_status = child_node->executeTick();
60+
61+
switch (child_status)
62+
{
63+
case NodeStatus::RUNNING:
64+
{
65+
return child_status;
66+
}
67+
case NodeStatus::SUCCESS:
68+
{
69+
for (unsigned t = 0; t <= i; t++)
70+
{
71+
int idx = children_order_[t];
72+
children_nodes_[ idx ]->setStatus(NodeStatus::IDLE);
73+
}
74+
for (unsigned t = i+1; t < children_count; t++)
75+
{
76+
int idx = children_order_[t];
77+
if (children_nodes_[idx]->status() == NodeStatus::RUNNING)
78+
{
79+
children_nodes_[idx]->halt();
80+
}
81+
}
82+
return child_status;
83+
}
84+
case NodeStatus::FAILURE:
85+
{
86+
// continue;
87+
}
88+
break;
89+
90+
case NodeStatus::IDLE:
91+
{
92+
throw std::runtime_error("This is not supposed to happen");
93+
}
94+
} // end switch
95+
} // end for loop
96+
97+
for (auto& ch : children_nodes_)
98+
{
99+
ch->setStatus(NodeStatus::IDLE);
100+
}
101+
return NodeStatus::FAILURE;
102+
}
103+
104+
}

0 commit comments

Comments
 (0)