Skip to content

Commit 0a08f78

Browse files
committed
add a specific tutorial for plugins
1 parent d8c5436 commit 0a08f78

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

examples/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,15 @@ endif()
3636
CompileExample("ex01_wrap_legacy")
3737
CompileExample("ex02_runtime_ports")
3838
CompileExample("ex04_waypoints")
39+
40+
CompileExample("t13_plugin_executor")
41+
42+
############ Create plugin for tutorial 13 ##########
43+
# library must be SHARED
44+
add_library(t13_plugin_action SHARED t13_plugin_action.cpp )
45+
# you must set the definition BT_PLUGIN_EXPORT
46+
target_compile_definitions(t13_plugin_action PRIVATE BT_PLUGIN_EXPORT )
47+
# remove the "lib" prefix. Name of the file will be t13_plugin_action.so
48+
set_target_properties(t13_plugin_action PROPERTIES PREFIX "")
49+
# link dependencies as usual
50+
target_link_libraries(t13_plugin_action ${BTCPP_LIBRARY} )

examples/t13_custom_type.hpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#pragma once
2+
3+
#include "behaviortree_cpp/behavior_tree.h"
4+
#include "behaviortree_cpp/json_export.h"
5+
6+
// my custom type
7+
struct Vector4D
8+
{
9+
double w;
10+
double x;
11+
double y;
12+
double z;
13+
};
14+
15+
// add this just in case, if it is necessary to register it with
16+
// Groot2 publisher.
17+
// You will need to add `RegisterJsonDefinition<Vector4D>(ToJson);` in you main
18+
inline void ToJson(nlohmann::json& dest, const Vector4D& pose)
19+
{
20+
dest["w"] = pose.w;
21+
dest["x"] = pose.x;
22+
dest["y"] = pose.y;
23+
dest["z"] = pose.z;
24+
}
25+
26+
namespace BT {
27+
28+
template <> inline
29+
Vector4D convertFromString(StringView key)
30+
{
31+
const auto parts = BT::splitString(key, ',');
32+
if (parts.size() != 4)
33+
{
34+
throw BT::RuntimeError("invalid input)");
35+
}
36+
37+
Vector4D output;
38+
output.w = convertFromString<double>(parts[0]);
39+
output.x = convertFromString<double>(parts[1]);
40+
output.y = convertFromString<double>(parts[2]);
41+
output.z = convertFromString<double>(parts[3]);
42+
return output;
43+
}
44+
45+
}

examples/t13_plugin_action.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "t13_custom_type.hpp"
2+
#include "behaviortree_cpp/bt_factory.h"
3+
4+
class ShowVector : public BT::SyncActionNode
5+
{
6+
public:
7+
ShowVector(const std::string& name, const BT::NodeConfig& config)
8+
: BT::SyncActionNode(name, config)
9+
{
10+
}
11+
12+
BT::NodeStatus tick() override
13+
{
14+
const auto v = getInput<Vector4D>("value").value();
15+
printf("x:%f y:%f z:%f w:%f\n", v.x, v.y, v.z, v.w);
16+
return BT::NodeStatus::SUCCESS;
17+
}
18+
19+
// It is mandatory to define this static method.
20+
static BT::PortsList providedPorts()
21+
{
22+
return{ BT::InputPort<Vector4D>("value") };
23+
}
24+
};
25+
26+
// Function used to register ShowVector automatically, when
27+
// loading the plugin.
28+
// Remember that is mandatory to add to toy CMakeLists.txtx file
29+
// this:
30+
// target_compile_definitions(<target_name> PRIVATE BT_PLUGIN_EXPORT)
31+
//
32+
BT_REGISTER_NODES(factory)
33+
{
34+
factory.registerNodeType<ShowVector>("ShowVector");
35+
}

examples/t13_plugin_executor.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "behaviortree_cpp/bt_factory.h"
2+
#include "t13_custom_type.hpp"
3+
4+
static const char* xml_text = R"(
5+
6+
// clang-format off
7+
<root BTCPP_format="4" main_tree_to_execute="MainTree" >
8+
<BehaviorTree ID="MainTree">
9+
<Sequence>
10+
<Script code="vect:='1,2,3,4'"/>
11+
<ShowVector value="{vect}"/>;
12+
<SubTree ID="MySub" v4="{vect}"/>
13+
</Sequence>
14+
</BehaviorTree>
15+
16+
<BehaviorTree ID="MySub">
17+
<ShowVector value="{v4}"/>;
18+
</BehaviorTree>
19+
</root>
20+
)";
21+
// clang-format on
22+
23+
int main(int argc, char** argv)
24+
{
25+
using namespace BT;
26+
BehaviorTreeFactory factory;
27+
factory.registerFromPlugin("t13_plugin_action.so");
28+
29+
// Not mandatory, since we don't have a Groot2 publisher
30+
RegisterJsonDefinition<Vector4D>(ToJson);
31+
32+
auto tree = factory.createTreeFromText(xml_text);
33+
tree.tickWhileRunning();
34+
35+
return 0;
36+
}
37+

0 commit comments

Comments
 (0)