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

Skip to content

Commit f6ed17e

Browse files
committed
experimental integration of Switch ControlNode
1 parent fcfa920 commit f6ed17e

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ list(APPEND BT_SOURCE
152152
src/controls/reactive_fallback.cpp
153153
src/controls/sequence_node.cpp
154154
src/controls/sequence_star_node.cpp
155+
src/controls/switch_node.cpp
155156

156157
src/loggers/bt_cout_logger.cpp
157158
src/loggers/bt_file_logger.cpp

include/behaviortree_cpp_v3/behavior_tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "behaviortree_cpp_v3/controls/fallback_node.h"
2121
#include "behaviortree_cpp_v3/controls/sequence_node.h"
2222
#include "behaviortree_cpp_v3/controls/sequence_star_node.h"
23+
#include "behaviortree_cpp_v3/controls/switch_node.h"
2324

2425
#include "behaviortree_cpp_v3/action_node.h"
2526
#include "behaviortree_cpp_v3/condition_node.h"
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* Copyright (C) 2019-2020 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 SWITCH_NODE_H
14+
#define SWITCH_NODE_H
15+
16+
#include "behaviortree_cpp_v3/control_node.h"
17+
18+
namespace BT
19+
{
20+
/**
21+
* @brief The SwitchNode
22+
*
23+
*/
24+
template <size_t NUM_CASES>
25+
class SwitchNode : public ControlNode
26+
{
27+
public:
28+
SwitchNode(const std::string& name, const BT::NodeConfiguration& config)
29+
: ControlNode::ControlNode(name, config ),
30+
running_child_(-1)
31+
{
32+
setRegistrationID("Switch");
33+
}
34+
35+
virtual ~SwitchNode() override = default;
36+
37+
void halt() override
38+
{
39+
running_child_ = -1;
40+
ControlNode::halt();
41+
}
42+
43+
static PortsList providedPorts()
44+
{
45+
PortsList ports;
46+
ports.insert( BT::InputPort<std::string>("variable") );
47+
for(unsigned i=0; i < NUM_CASES; i++)
48+
{
49+
char case_str[20];
50+
sprintf(case_str, "case_%d", i+1);
51+
ports.insert( BT::InputPort<std::string>(case_str) );
52+
}
53+
return ports;
54+
}
55+
56+
private:
57+
int running_child_;
58+
virtual BT::NodeStatus tick() override;
59+
};
60+
61+
template<size_t NUM_CASES> inline
62+
NodeStatus SwitchNode<NUM_CASES>::tick()
63+
{
64+
if( childrenCount() != NUM_CASES+1)
65+
{
66+
throw LogicError("Wrong number of children in SwitchNode; "
67+
"must be (num_cases + default)");
68+
}
69+
70+
std::string variable;
71+
std::string value;
72+
int child_index = NUM_CASES; // default index;
73+
74+
if (getInput("variable", variable)) // no variable? jump to default
75+
{
76+
// check each case until you find a match
77+
for (unsigned index = 0; index < NUM_CASES; ++index)
78+
{
79+
char case_str[20];
80+
sprintf(case_str, "case_%d", index+1);
81+
82+
if (getInput(case_str, value) && variable == value)
83+
{
84+
child_index = index;
85+
break;
86+
}
87+
}
88+
}
89+
90+
// if another one was running earlier, halt it
91+
if( running_child_ != -1 && running_child_ != child_index)
92+
{
93+
halt();
94+
}
95+
96+
NodeStatus ret = children_nodes_[child_index]->executeTick();
97+
if( ret == NodeStatus::RUNNING )
98+
{
99+
running_child_ = child_index;
100+
}
101+
else{
102+
running_child_ = -1;
103+
}
104+
105+
return ret;
106+
}
107+
108+
}
109+
110+
#endif // SWITCH_NODE_H

src/bt_factory.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ BehaviorTreeFactory::BehaviorTreeFactory()
4949
registerNodeType<BlackboardPreconditionNode<double>>("BlackboardCheckDouble");
5050
registerNodeType<BlackboardPreconditionNode<std::string>>("BlackboardCheckString");
5151

52+
registerNodeType<SwitchNode<2>>("Switch2");
53+
registerNodeType<SwitchNode<3>>("Switch3");
54+
registerNodeType<SwitchNode<4>>("Switch4");
55+
registerNodeType<SwitchNode<5>>("Switch5");
56+
registerNodeType<SwitchNode<6>>("Switch6");
57+
5258
for( const auto& it: builders_)
5359
{
5460
builtin_IDs_.insert( it.first );

src/controls/switch_node.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2+
* Copyright (C) 2018-2020 Davide Faconti, Eurecat - All Rights Reserved
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
5+
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
6+
* 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:
7+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
*
9+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10+
* 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,
11+
* 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.
12+
*/
13+
14+
#include "behaviortree_cpp_v3/controls/switch_node.h"
15+
#include "behaviortree_cpp_v3/action_node.h"

0 commit comments

Comments
 (0)