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

Skip to content

Commit 0d54dcd

Browse files
committed
Update the documentation
1 parent a7a1635 commit 0d54dcd

File tree

4 files changed

+149
-7
lines changed

4 files changed

+149
-7
lines changed

docs/tutorial_B_node_parameters.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
NodeParameters are like arguments passed to a function.
44

55
They are a map of __key/value__ pairs (both strings) that are usually
6-
parsed from file.
6+
read from file.
77

88
To create a TreeNodes that accepts NodeParameters, you must follow these rules:
99

10-
- Use inheritance. NodeParameters are __not supported__ by *SimpleActionNodes* nor
11-
*SimpleConditionNodes*.
10+
- Inherit from either ActionNodeBase, ActionNode, ConditionNode or DecoratorNode.
1211

1312
- You must provide a constructor with the following signature:
1413

@@ -22,6 +21,9 @@ MyAction(const std::string& name, const BT::NodeParameters& params)
2221
static const BT::NodeParameters& requiredNodeParameters()
2322
```
2423

24+
Alternatively, since version 2.2, Simple Nodes can also support NodeParameters.
25+
Check the [tutorial 6](tutorial_G_legacy.md) for details.
26+
2527

2628
## Example: an Action requiring the parameter "message"
2729

docs/tutorial_G_legacy.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Wrap legacy code
2+
3+
In this tutorial we see how to deal with legacy code that was not meant to be used
4+
with BehaviorTree.CPP.
5+
6+
Let's start supposing that this is my class.
7+
8+
``` c++
9+
// This is my custom type.
10+
struct Point3D { double x,y,z; };
11+
12+
class MyLegacyMoveTo
13+
{
14+
public:
15+
bool go(Point3D goal)
16+
{
17+
printf("Going to: %f %f %f\n", goal.x, goal.y, goal.z);
18+
return true; // true means success in my legacy code
19+
}
20+
};
21+
```
22+
23+
We want to create an ActionNode called "MoveTo" that invokes the method __MyLegacyMoveTo::go()__.
24+
25+
The final goal is to be able to use this ActionNode in a tree like this one:
26+
27+
``` XML
28+
<root main_tree_to_execute = "MainTree" >
29+
<BehaviorTree ID="MainTree">
30+
<SequenceStar name="root">
31+
<MoveTo goal="-1;3;0.5" />
32+
<MoveTo goal="${myGoal}" />
33+
</SequenceStar>
34+
</BehaviorTree>
35+
</root>
36+
```
37+
38+
The first thing that we need to do is to allow our library to convert
39+
a NodeParameter (that is
40+
nothing more than a pair of strings representing key/value) into a Point3D.
41+
42+
As we did in a previous tutorial, we should implement a template specialization
43+
for __convertFromString__.
44+
45+
Our particular string representation of a Point3D consists in three semicolon-separated
46+
numbers, representing __x, y and z_.
47+
48+
49+
``` c++
50+
namespace BT
51+
{
52+
template <> Point3D convertFromString(const StringView& key)
53+
{
54+
// three real numbers separated by semicolons
55+
auto parts = BT::splitString(key, ';');
56+
if (parts.size() != 3)
57+
{
58+
throw std::runtime_error("invalid input)");
59+
}
60+
else
61+
{
62+
Point3D output;
63+
output.x = convertFromString<double>(parts[0]);
64+
output.y = convertFromString<double>(parts[1]);
65+
output.z = convertFromString<double>(parts[2]);
66+
return output;
67+
}
68+
}
69+
}
70+
```
71+
72+
Finally, we can use a __C++11 lambda__ (or, alternatively, __std::bind__) to wrap
73+
out method into a function with the right signature.
74+
75+
``` c++
76+
int main()
77+
{
78+
using namespace BT;
79+
80+
MyLegacyMoveTo move_to;
81+
82+
// Here we use a lambda that captures the reference of move_to
83+
auto MoveToWrapperWithLambda = [&move_to](TreeNode& parent_node) -> NodeStatus
84+
{
85+
Point3D goal;
86+
// thanks to paren_node, you can access easily the NodeParameters and the blackboard
87+
parent_node.getParam("goal", goal);
88+
89+
bool res = move_to.go( goal );
90+
// convert bool to NodeStatus
91+
return res ? NodeStatus::SUCCESS : NodeStatus::FAILURE;
92+
};
93+
94+
BehaviorTreeFactory factory;
95+
factory.registerSimpleAction("MoveTo", MoveToWrapperWithLambda);
96+
97+
auto blackboard = Blackboard::create<BlackboardLocal>();
98+
auto tree = buildTreeFromText(factory, xml_text, blackboard);
99+
100+
// We set the entry "myGoal" in the blackboard.
101+
Point3D my_goal = {3,4,5};
102+
blackboard->set("myGoal", my_goal);
103+
104+
NodeStatus status = NodeStatus::RUNNING;
105+
while (status == NodeStatus::RUNNING)
106+
{
107+
status = tree.root_node->executeTick();
108+
}
109+
return 0;
110+
}
111+
112+
/* Expected output:
113+
114+
Going to: -1.000000 3.000000 0.500000
115+
Going to: 3.000000 4.000000 5.000000
116+
117+
The first MoveTo read the parameter from the string "-1;3;0.5"
118+
whilst the second from the blackboard, that contains a copy of the Point3D my_goal.
119+
120+
*/
121+
```
122+
123+
124+
The functor we are passing to __SimpleActionNode__ requires the following signature:
125+
126+
BT::NodeStatus myFunction(BT::TreeNode& parent)
127+
128+
As a consequence, we can access a NodeParameter by
129+
130+
parent.getParam()
131+
132+
or even set/get an entry of the Blackboard using
133+
134+
parent.blackboard()
135+
136+
137+
138+
139+

examples/t06_wrap_legacy.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class MyLegacyMoveTo
3939
};
4040

4141
// Similarly to the previous tutorials, we need to implement this parsing method,
42-
// deplaring a specialization of BT::convertFromString
42+
// providing a specialization of BT::convertFromString
4343
namespace BT
4444
{
4545
template <> Point3D convertFromString(const StringView& key)
@@ -84,7 +84,7 @@ int main()
8484
};
8585

8686
BehaviorTreeFactory factory;
87-
// Regoister the lambda with BehaviorTreeFactory::registerSimpleAction
87+
// Register the lambda with BehaviorTreeFactory::registerSimpleAction
8888
factory.registerSimpleAction("MoveTo", MoveToWrapperWithLambda);
8989

9090
auto blackboard = Blackboard::create<BlackboardLocal>();

mkdocs.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ theme:
1111
feature:
1212
tabs: true
1313
palette:
14-
primary: blue
15-
accent: indigo
14+
primary: blue grey
15+
accent: green
1616
font:
1717
text: Ubuntu
1818
code: Roboto Mono
@@ -43,4 +43,5 @@ pages:
4343
- "Tutorial 4: Subtrees": tutorial_D_subtrees.md
4444
- "Tutorial 5: Plugins": tutorial_E_plugins.md
4545
- "Tutorial 6: Loggers": tutorial_F_loggers.md
46+
- "Tutorial 7: Legacy code": tutorial_G_legacy.md
4647

0 commit comments

Comments
 (0)