4
4
#include " behaviortree_cpp/loggers/groot2_publisher.h"
5
5
#include " behaviortree_cpp/loggers/bt_sqlite_logger.h"
6
6
#include " behaviortree_cpp/xml_parsing.h"
7
+ #include " behaviortree_cpp/json_export.h"
7
8
8
9
/* * We are using the same example in Tutorial 5,
9
10
* But this time we also show how to connect
10
11
*/
11
12
13
+ // A custom structuree that I want to visualize in Groot2
14
+ struct Position2D {
15
+ double x;
16
+ double y;
17
+ };
18
+
19
+ // Allows Position2D to be visualized in Groot2
20
+ // You still need BT::RegisterJsonDefinition<Position2D>(PositionToJson)
21
+ void PositionToJson (nlohmann::json& j, const Position2D& p)
22
+ {
23
+ j[" x" ] = p.x ;
24
+ j[" y" ] = p.y ;
25
+ }
26
+
27
+ // Simple Action that updates an instance of Position2D in the blackboard
28
+ class UpdatePosition : public BT ::SyncActionNode
29
+ {
30
+ public:
31
+ UpdatePosition (const std::string& name, const BT::NodeConfig& config):
32
+ BT::SyncActionNode (name, config) {}
33
+
34
+ BT::NodeStatus tick () override {
35
+ _pos.x += 0.2 ;
36
+ _pos.y += 0.1 ;
37
+ setOutput (" pos" , _pos);
38
+ return BT::NodeStatus::SUCCESS;
39
+ }
40
+
41
+ static BT::PortsList providedPorts ()
42
+ {
43
+ return {BT::OutputPort<Position2D>(" pos" )};
44
+ }
45
+ private:
46
+ Position2D _pos = {0 , 0 };
47
+ };
48
+
12
49
// clang-format off
13
50
14
51
static const char * xml_text = R"(
@@ -17,6 +54,7 @@ static const char* xml_text = R"(
17
54
<BehaviorTree ID="MainTree">
18
55
<Sequence>
19
56
<Script code="door_open:=false" />
57
+ <UpdatePosition pos="{pos_2D}" />
20
58
<Fallback>
21
59
<Inverter>
22
60
<IsDoorClosed/>
@@ -46,27 +84,33 @@ int main()
46
84
{
47
85
BT::BehaviorTreeFactory factory;
48
86
87
+ // Nodes registration, as usual
49
88
CrossDoor cross_door;
50
89
cross_door.registerNodes (factory);
90
+ factory.registerNodeType <UpdatePosition>(" UpdatePosition" );
51
91
52
92
// Groot2 editor requires a model of your registered Nodes.
53
93
// You don't need to write that by hand, it can be automatically
54
94
// generated using the following command.
55
95
std::string xml_models = BT::writeTreeNodesModelXML (factory);
56
96
57
97
factory.registerBehaviorTreeFromText (xml_text);
58
- auto tree = factory.createTree (" MainTree" );
59
98
99
+ // Add this to allow Groot2 to visualize your custom type
100
+ BT::RegisterJsonDefinition<Position2D>(PositionToJson);
101
+
102
+ auto tree = factory.createTree (" MainTree" );
60
103
61
104
std::cout << " ----------- XML file ----------\n "
62
105
<< BT::WriteTreeToXML (tree, false , false )
63
106
<< " --------------------------------\n " ;
64
107
65
108
// Connect the Groot2Publisher. This will allow Groot2 to
66
109
// get the tree and poll status updates.
67
- BT::Groot2Publisher publisher (tree);
110
+ const unsigned port = 1667 ;
111
+ BT::Groot2Publisher publisher (tree, port);
68
112
69
- // Add also two logger which save the transitions into a file.
113
+ // Add two more loggers, to save the transitions into a file.
70
114
// Both formats are compatible with Groot2
71
115
72
116
// Lightweight serialization
@@ -83,5 +127,7 @@ int main()
83
127
std::this_thread::sleep_for (std::chrono::milliseconds (2000 ));
84
128
}
85
129
130
+ BT::JsonExporter::get ().addConverter <Position2D>();
131
+
86
132
return 0 ;
87
133
}
0 commit comments