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

Skip to content

Commit 4a8cf33

Browse files
Added an option to repeat the previous selection in ManualSelector
1 parent bfb8681 commit 4a8cf33

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

examples/t12_ncurses_manual_selector.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@
33

44
using namespace BT;
55

6+
/* Try also
7+
* <ManualSelector repeat_last_selection="1">
8+
* to see the difference.
9+
*/
10+
611
// clang-format off
712
static const char* xml_text = R"(
813
<root main_tree_to_execute = "MainTree" >
914
<BehaviorTree ID="MainTree">
10-
<ManualSelector name="root">
11-
<SaySomething name="Option1" message="Option1" />
12-
<SaySomething name="Option2" message="Option2" />
13-
<SaySomething name="Option3" message="Option3" />
14-
<SaySomething name="Option4" message="Option4" />
15-
<ManualSelector name="YouChoose" />
16-
</ManualSelector>
15+
<Repeat num_cycles="3">
16+
<ManualSelector repeat_last_selection="0">
17+
<SaySomething name="Option1" message="Option1" />
18+
<SaySomething name="Option2" message="Option2" />
19+
<SaySomething name="Option3" message="Option3" />
20+
<SaySomething name="Option4" message="Option4" />
21+
<ManualSelector name="YouChoose" />
22+
</ManualSelector>
23+
</Repeat>
1724
</BehaviorTree>
1825
</root>
1926
)";

include/behaviortree_cpp_v3/controls/manual_node.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,25 @@ namespace BT
2323
class ManualSelectorNode : public ControlNode
2424
{
2525
public:
26-
ManualSelectorNode(const std::string& name);
26+
ManualSelectorNode(const std::string& name, const NodeConfiguration& config);
2727

2828
virtual ~ManualSelectorNode() override = default;
2929

3030
virtual void halt() override;
3131

32+
static PortsList providedPorts()
33+
{
34+
return { InputPort<bool>(REPEAT_LAST_SELECTION, false,
35+
"If true, execute again the same child that was selected the last time") };
36+
}
37+
3238
private:
3339

40+
static constexpr const char* REPEAT_LAST_SELECTION = "repeat_last_selection";
41+
3442
virtual BT::NodeStatus tick() override;
3543
int running_child_idx_;
44+
int previously_executed_idx_;
3645

3746
enum NumericarStatus{
3847
NUM_SUCCESS = 253,

src/controls/manual_node.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ namespace BT
1919
{
2020

2121

22-
ManualSelectorNode::ManualSelectorNode(const std::string& name)
23-
: ControlNode::ControlNode(name, {} )
22+
ManualSelectorNode::ManualSelectorNode(const std::string& name, const NodeConfiguration& config)
23+
: ControlNode::ControlNode(name, config )
2424
, running_child_idx_(-1)
25+
, previously_executed_idx_(-1)
2526
{
2627
setRegistrationID("ManualSelector");
2728
}
@@ -45,22 +46,32 @@ NodeStatus ManualSelectorNode::tick()
4546
return selectStatus();
4647
}
4748

48-
setStatus(NodeStatus::RUNNING);
49+
bool repeat_last = false;
50+
getInput(REPEAT_LAST_SELECTION, repeat_last);
4951

50-
unsigned idx = selectChild();
52+
int idx = 0;
5153

52-
if( idx == NUM_SUCCESS ){
53-
return NodeStatus::SUCCESS;
54-
}
55-
if( idx == NUM_FAILURE ){
56-
return NodeStatus::FAILURE;
54+
if( repeat_last && previously_executed_idx_ >= 0)
55+
{
56+
idx = previously_executed_idx_;
5757
}
58-
if( idx == NUM_RUNNING ){
59-
return NodeStatus::RUNNING;
58+
else{
59+
setStatus(NodeStatus::RUNNING);
60+
idx = selectChild();
61+
previously_executed_idx_ = idx;
62+
63+
if( idx == NUM_SUCCESS ){
64+
return NodeStatus::SUCCESS;
65+
}
66+
if( idx == NUM_FAILURE ){
67+
return NodeStatus::FAILURE;
68+
}
69+
if( idx == NUM_RUNNING ){
70+
return NodeStatus::RUNNING;
71+
}
6072
}
6173

6274
NodeStatus ret = children_nodes_[idx]->executeTick();
63-
6475
if(ret == NodeStatus::RUNNING)
6576
{
6677
running_child_idx_ = idx;

0 commit comments

Comments
 (0)