Skip to content

Commit

Permalink
renamed examples to match website
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed Apr 26, 2024
1 parent d72b319 commit e4adbb7
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 290 deletions.
34 changes: 19 additions & 15 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,33 @@ CompileExample("t07_load_multiple_xml")
CompileExample("t08_additional_node_args")
CompileExample("t09_scripting")
CompileExample("t10_observer")
CompileExample("t11_replace_rules")

if(BTCPP_GROOT_INTERFACE AND BTCPP_SQLITE_LOGGING)
CompileExample("t12_groot_howto")
CompileExample("generate_log")
CompileExample("t11_groot_howto")
endif()

CompileExample("t12_default_ports")
CompileExample("t13_access_by_ref")
CompileExample("t14_subtree_model")
CompileExample("t15_nodes_mocking")
CompileExample("t16_sqlite_log")
CompileExample("t17_blackboard_backup")
CompileExample("t18_waypoints")

CompileExample("ex01_wrap_legacy")
CompileExample("ex02_runtime_ports")
CompileExample("ex04_waypoints")
CompileExample("ex05_subtree_model")
CompileExample("ex06_access_by_ptr")
CompileExample("ex07_blackboard_backup")
CompileExample("ex08_sqlite_log")

CompileExample("t13_plugin_executor")

############ Create plugin for tutorial 13 ##########
############ Create plugin and executor in folder plugin_example ##########

# library must be SHARED
add_library(t13_plugin_action SHARED t13_plugin_action.cpp )
add_library(test_plugin_action SHARED plugin_example/plugin_action.cpp )
# you must set the definition BT_PLUGIN_EXPORT
target_compile_definitions(t13_plugin_action PRIVATE BT_PLUGIN_EXPORT )
# remove the "lib" prefix. Name of the file will be t13_plugin_action.so
set_target_properties(t13_plugin_action PROPERTIES PREFIX "")
target_compile_definitions(test_plugin_action PRIVATE BT_PLUGIN_EXPORT )
# remove the "lib" prefix. Name of the file will be test_plugin_action.so
set_target_properties(test_plugin_action PROPERTIES PREFIX "")
# link dependencies as usual
target_link_libraries(t13_plugin_action ${BTCPP_LIBRARY} )
target_link_libraries(test_plugin_action ${BTCPP_LIBRARY} )

add_executable(test_plugin_executor plugin_example/plugin_executor.cpp )
target_link_libraries(test_plugin_executor ${BTCPP_LIBRARY})
82 changes: 0 additions & 82 deletions examples/broken_sequence.cpp

This file was deleted.

48 changes: 0 additions & 48 deletions examples/generate_log.cpp

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "t13_custom_type.hpp"
#include "custom_type.hpp"
#include "behaviortree_cpp/bt_factory.h"

class PrintVector : public BT::SyncActionNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main(int argc, char** argv)
using namespace BT;
BehaviorTreeFactory factory;

std::string plugin_path = "t13_plugin_action.so";
std::string plugin_path = "test_plugin_action.so";

// if you don't want to use the hardcoded path, pass it as an argument
if(argc == 2)
Expand Down
File renamed without changes.
130 changes: 130 additions & 0 deletions examples/t12_default_ports.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include "behaviortree_cpp/bt_factory.h"
#include "behaviortree_cpp/json_export.h"

/**
* The goal of this tutorial is to show all the possible ways
* that we can define the default value of a port, i.e. the
* value that it should have if not specified in the XML
* */

// Custom type. to make things more interesting
struct Point2D
{
int x = 0;
int y = 0;
bool operator==(const Point2D& p) const
{
return x == p.x && y == p.y;
}
bool operator!=(const Point2D& p) const
{
return !(*this == p);
}
};

// Allow bi-directional convertion to JSON
BT_JSON_CONVERTER(Point2D, point)
{
add_field("x", &point.x);
add_field("y", &point.y);
}

// We can extend the traditional BT::convertFromString<Point2D>()
// to support the JSON format too (see port with name "pointE")
template <>
[[nodiscard]] Point2D BT::convertFromString<Point2D>(StringView str)
{
if(StartWith(str, "json:"))
{
str.remove_prefix(5);
return convertFromJSON<Point2D>(str);
}
const auto parts = BT::splitString(str, ',');
if(parts.size() != 2)
{
throw BT::RuntimeError("invalid input)");
}
int x = convertFromString<int>(parts[0]);
int y = convertFromString<int>(parts[1]);
return { x, y };
}

//-----------------------------------------------
using namespace BT;

class NodeWithDefaultPoints : public SyncActionNode
{
public:
NodeWithDefaultPoints(const std::string& name, const NodeConfig& config)
: SyncActionNode(name, config)
{}

NodeStatus tick() override
{
// Let0s check if all the portas have the expected value
Point2D pointA, pointB, pointC, pointD, pointE, input;

if(!getInput("pointA", pointA) || pointA != Point2D{ 1, 2 })
{
throw std::runtime_error("failed pointA");
}
if(!getInput("pointB", pointB) || pointB != Point2D{ 3, 4 })
{
throw std::runtime_error("failed pointB");
}
if(!getInput("pointC", pointC) || pointC != Point2D{ 5, 6 })
{
throw std::runtime_error("failed pointC");
}
if(!getInput("pointD", pointD) || pointD != Point2D{ 7, 8 })
{
throw std::runtime_error("failed pointD");
}
if(!getInput("pointE", pointE) || pointE != Point2D{ 9, 10 })
{
throw std::runtime_error("failed pointE");
}
if(!getInput("input", input) || input != Point2D{ -1, -2 })
{
throw std::runtime_error("failed input");
}
return NodeStatus::SUCCESS;
}

static PortsList providedPorts()
{
return { BT::InputPort<Point2D>("input", "no default value"),
BT::InputPort<Point2D>("pointA", Point2D{ 1, 2 }, "default value is [1,2]"),
BT::InputPort<Point2D>("pointB", "{point}",
"default value inside blackboard {point}"),
BT::InputPort<Point2D>("pointC", "5,6", "default value is [5,6]"),
BT::InputPort<Point2D>("pointD", "{=}",
"default value inside blackboard {pointD}"),
BT::InputPort<Point2D>("pointE", R"(json:{"x":9,"y":10})",
"default value is [9,10]") };
}
};

int main()
{
std::string xml_txt = R"(
<root BTCPP_format="4" >
<BehaviorTree>
<NodeWithDefaultPoints input="-1,-2"/>
</BehaviorTree>
</root>)";

JsonExporter::get().addConverter<Point2D>();

BehaviorTreeFactory factory;
factory.registerNodeType<NodeWithDefaultPoints>("NodeWithDefaultPoints");
auto tree = factory.createTreeFromText(xml_txt);

tree.subtrees.front()->blackboard->set<Point2D>("point", Point2D{ 3, 4 });
tree.subtrees.front()->blackboard->set<Point2D>("pointD", Point2D{ 7, 8 });

BT::NodeStatus status = tree.tickOnce();
std::cout << "Result: " << toStr(status) << std::endl;

return 0;
}
Loading

0 comments on commit e4adbb7

Please sign in to comment.