From e4adbb716a3186ec403cd87b8bf861b8c7e3d0fd Mon Sep 17 00:00:00 2001 From: Davide Faconti Date: Fri, 26 Apr 2024 15:30:48 +0200 Subject: [PATCH] renamed examples to match website --- examples/CMakeLists.txt | 34 +++-- examples/broken_sequence.cpp | 82 ---------- examples/generate_log.cpp | 48 ------ .../custom_type.hpp} | 0 .../plugin_action.cpp} | 2 +- .../plugin_executor.cpp} | 2 +- ...12_groot_howto.cpp => t11_groot_howto.cpp} | 0 examples/t12_default_ports.cpp | 130 ++++++++++++++++ examples/t12_groot_howto.cpp.orig | 141 ------------------ ...ccess_by_ptr.cpp => t13_access_by_ref.cpp} | 0 ...ubtree_model.cpp => t14_subtree_model.cpp} | 0 ...eplace_rules.cpp => t15_nodes_mocking.cpp} | 0 ...ex08_sqlite_log.cpp => t16_sqlite_log.cpp} | 2 - ...d_backup.cpp => t17_blackboard_backup.cpp} | 0 .../{ex04_waypoints.cpp => t18_waypoints.cpp} | 0 15 files changed, 151 insertions(+), 290 deletions(-) delete mode 100644 examples/broken_sequence.cpp delete mode 100644 examples/generate_log.cpp rename examples/{t13_custom_type.hpp => plugin_example/custom_type.hpp} (100%) rename examples/{t13_plugin_action.cpp => plugin_example/plugin_action.cpp} (96%) rename examples/{t13_plugin_executor.cpp => plugin_example/plugin_executor.cpp} (95%) rename examples/{t12_groot_howto.cpp => t11_groot_howto.cpp} (100%) create mode 100644 examples/t12_default_ports.cpp delete mode 100644 examples/t12_groot_howto.cpp.orig rename examples/{ex06_access_by_ptr.cpp => t13_access_by_ref.cpp} (100%) rename examples/{ex05_subtree_model.cpp => t14_subtree_model.cpp} (100%) rename examples/{t11_replace_rules.cpp => t15_nodes_mocking.cpp} (100%) rename examples/{ex08_sqlite_log.cpp => t16_sqlite_log.cpp} (98%) rename examples/{ex07_blackboard_backup.cpp => t17_blackboard_backup.cpp} (100%) rename examples/{ex04_waypoints.cpp => t18_waypoints.cpp} (100%) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index e200c5612..a668d7e85 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -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}) diff --git a/examples/broken_sequence.cpp b/examples/broken_sequence.cpp deleted file mode 100644 index 47c34b7ce..000000000 --- a/examples/broken_sequence.cpp +++ /dev/null @@ -1,82 +0,0 @@ -#include "Blackboard/blackboard_local.h" -#include "behaviortree_cpp/behavior_tree.h" -#include "behaviortree_cpp/bt_factory.h" - -using namespace BT; - -NodeStatus SayHello() -{ - printf("hello\n"); - return NodeStatus::SUCCESS; -} - -class ActionTestNode : public ActionNode -{ -public: - ActionTestNode(const std::string& name) : ActionNode(name) - {} - - NodeStatus tick() override - { - time_ = 5; - stop_loop_ = false; - int i = 0; - while(!stop_loop_ && i++ < time_) - { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - } - return NodeStatus::SUCCESS; - } - - virtual void halt() override - { - stop_loop_ = true; - } - -private: - int time_; - std::atomic_bool stop_loop_ = false; -}; - -int main() -{ - BT::SequenceNode root("root"); - BT::SimpleActionNode action1("say_hello", std::bind(SayHello)); - ActionTestNode action2("async_action"); - - root.addChild(&action1); - root.addChild(&action2); - - int count = 0; - - NodeStatus status = NodeStatus::RUNNING; - - while(status == NodeStatus::RUNNING) - { - status = root.executeTick(); - - std::cout << count++ << " : " << root.status() << " / " << action1.status() << " / " - << action2.status() << std::endl; - - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - } - - return 0; -} -// Output -/* - -hello -0 : RUNNING / SUCCESS / RUNNING -hello -1 : RUNNING / SUCCESS / RUNNING -hello -2 : RUNNING / SUCCESS / RUNNING -hello -3 : RUNNING / SUCCESS / RUNNING -hello -4 : RUNNING / SUCCESS / RUNNING -hello -5 : SUCCESS / IDLE / IDLE - -*/ diff --git a/examples/generate_log.cpp b/examples/generate_log.cpp deleted file mode 100644 index 1932cd8c8..000000000 --- a/examples/generate_log.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "behaviortree_cpp/bt_factory.h" -#include "behaviortree_cpp/loggers/groot2_publisher.h" -#include "behaviortree_cpp/loggers/bt_file_logger_v2.h" -#include "behaviortree_cpp/loggers/bt_sqlite_logger.h" -#include "behaviortree_cpp/loggers/bt_cout_logger.h" - -// clang-format on - -int main(int argc, char** argv) -{ - if(argc < 2 || argc > 3) - { - std::cout << "Provide a XML file as first argument. " - "Second argument might be the name of the tree to instantiate." - << std::endl; - return 1; - } - const std::string file = argv[1]; - - BT::BehaviorTreeFactory factory; - - BT::Tree tree; - - if(argc == 3) - { - factory.registerBehaviorTreeFromFile(file); - tree = factory.createTree(argv[2]); - } - else - { - tree = factory.createTreeFromFile(file); - } - - BT::StdCoutLogger cout_logger(tree); - BT::Groot2Publisher publisher(tree); - BT::FileLogger2 file_logger(tree, "./generated_log.btlog"); - BT::SqliteLogger sqlite_logger(tree, "./generated_log.db3"); - - // helper function to print the tree - BT::printTreeRecursively(tree.rootNode()); - - std::cout << "\nTree will run indefinitively. Press CTRL-C to stop\n"; - - while(true) - { - tree.tickWhileRunning(); - } -} diff --git a/examples/t13_custom_type.hpp b/examples/plugin_example/custom_type.hpp similarity index 100% rename from examples/t13_custom_type.hpp rename to examples/plugin_example/custom_type.hpp diff --git a/examples/t13_plugin_action.cpp b/examples/plugin_example/plugin_action.cpp similarity index 96% rename from examples/t13_plugin_action.cpp rename to examples/plugin_example/plugin_action.cpp index cd05b2677..ba2f2e285 100644 --- a/examples/t13_plugin_action.cpp +++ b/examples/plugin_example/plugin_action.cpp @@ -1,4 +1,4 @@ -#include "t13_custom_type.hpp" +#include "custom_type.hpp" #include "behaviortree_cpp/bt_factory.h" class PrintVector : public BT::SyncActionNode diff --git a/examples/t13_plugin_executor.cpp b/examples/plugin_example/plugin_executor.cpp similarity index 95% rename from examples/t13_plugin_executor.cpp rename to examples/plugin_example/plugin_executor.cpp index 7eb29b25c..5862db864 100644 --- a/examples/t13_plugin_executor.cpp +++ b/examples/plugin_example/plugin_executor.cpp @@ -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) diff --git a/examples/t12_groot_howto.cpp b/examples/t11_groot_howto.cpp similarity index 100% rename from examples/t12_groot_howto.cpp rename to examples/t11_groot_howto.cpp diff --git a/examples/t12_default_ports.cpp b/examples/t12_default_ports.cpp new file mode 100644 index 000000000..8fa866a93 --- /dev/null +++ b/examples/t12_default_ports.cpp @@ -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() +// to support the JSON format too (see port with name "pointE") +template <> +[[nodiscard]] Point2D BT::convertFromString(StringView str) +{ + if(StartWith(str, "json:")) + { + str.remove_prefix(5); + return convertFromJSON(str); + } + const auto parts = BT::splitString(str, ','); + if(parts.size() != 2) + { + throw BT::RuntimeError("invalid input)"); + } + int x = convertFromString(parts[0]); + int y = convertFromString(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("input", "no default value"), + BT::InputPort("pointA", Point2D{ 1, 2 }, "default value is [1,2]"), + BT::InputPort("pointB", "{point}", + "default value inside blackboard {point}"), + BT::InputPort("pointC", "5,6", "default value is [5,6]"), + BT::InputPort("pointD", "{=}", + "default value inside blackboard {pointD}"), + BT::InputPort("pointE", R"(json:{"x":9,"y":10})", + "default value is [9,10]") }; + } +}; + +int main() +{ + std::string xml_txt = R"( + + + + + )"; + + JsonExporter::get().addConverter(); + + BehaviorTreeFactory factory; + factory.registerNodeType("NodeWithDefaultPoints"); + auto tree = factory.createTreeFromText(xml_txt); + + tree.subtrees.front()->blackboard->set("point", Point2D{ 3, 4 }); + tree.subtrees.front()->blackboard->set("pointD", Point2D{ 7, 8 }); + + BT::NodeStatus status = tree.tickOnce(); + std::cout << "Result: " << toStr(status) << std::endl; + + return 0; +} diff --git a/examples/t12_groot_howto.cpp.orig b/examples/t12_groot_howto.cpp.orig deleted file mode 100644 index dfea32863..000000000 --- a/examples/t12_groot_howto.cpp.orig +++ /dev/null @@ -1,141 +0,0 @@ -#include "behaviortree_cpp/loggers/bt_file_logger_v2.h" -#include "crossdoor_nodes.h" -#include "behaviortree_cpp/bt_factory.h" -#include "behaviortree_cpp/loggers/groot2_publisher.h" -#include "behaviortree_cpp/loggers/bt_sqlite_logger.h" -#include "behaviortree_cpp/xml_parsing.h" -#include "behaviortree_cpp/json_export.h" - -/** We are using the same example in Tutorial 5, - * But this time we also show how to connect - */ - -<<<<<<< HEAD -// A custom structuree that I want to visualize in Groot2 -======= -// A custom struct that I want to visualize in Groot2 ->>>>>>> 8f94d2c (json conversions) -struct Position2D -{ - double x; - double y; -}; - -// This macro will generate the code that is needed to convert -// the object to/from JSON. -// You still need to call BT::RegisterJsonDefinition() -// in main() -BT_JSON_CONVERTER(Position2D, pos) -{ - add_field("x", &pos.x); - add_field("y", &pos.y); -} - -// Simple Action that updates an instance of Position2D in the blackboard -class UpdatePosition : public BT::SyncActionNode -{ -public: - UpdatePosition(const std::string& name, const BT::NodeConfig& config) - : BT::SyncActionNode(name, config) - {} - - BT::NodeStatus tick() override - { - _pos.x += 0.2; - _pos.y += 0.1; - setOutput("pos", _pos); - return BT::NodeStatus::SUCCESS; - } - - static BT::PortsList providedPorts() - { - return { BT::OutputPort("pos") }; - } - -private: - Position2D _pos = { 0, 0 }; -}; - -// clang-format off - -static const char* xml_text = R"( - - - - -