forked from BehaviorTree/BehaviorTree.CPP
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97a434d
commit ca8d32a
Showing
19 changed files
with
1,776 additions
and
317 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "behaviortree_cpp/bt_factory.h" | ||
#include "dummy_nodes.h" | ||
|
||
using namespace BT; | ||
|
||
// clang-format off | ||
static const char* xml_tree = R"( | ||
<root BTCPP_format="4"> | ||
<BehaviorTree ID="MainTree"> | ||
<Sequence> | ||
<Script code="val_A:= 'john' "/> | ||
<Script code="val_B:= 42 "/> | ||
<SaySomething message="{val_A}" /> | ||
<SaySomething message="hello world" /> | ||
<SubTree ID="Sub" val="{val_A}" _autoremap="true" /> | ||
<SaySomething message="{reply}" /> | ||
</Sequence> | ||
</BehaviorTree> | ||
<BehaviorTree ID="Sub"> | ||
<Sequence> | ||
<SaySomething message="{val}" /> | ||
<SaySomething message="{val_B}" /> | ||
<Script code="reply:= 'done' "/> | ||
</Sequence> | ||
</BehaviorTree> | ||
</root> | ||
)"; | ||
|
||
// clang-format on | ||
|
||
int main() | ||
{ | ||
BehaviorTreeFactory factory; | ||
factory.registerNodeType<DummyNodes::SaySomething>("SaySomething"); | ||
factory.registerBehaviorTreeFromText(xml_tree); | ||
|
||
auto tree = factory.createTree("MainTree"); | ||
|
||
// We want to create a memory of the blackboard in memory. | ||
// This is conveninet when we want to reset its state to the | ||
// original one. | ||
// It is certainly more efficient than destroying and creating the tree again, | ||
// in many casess. | ||
|
||
const auto backup_before_tick = BlackboardBackup(tree); | ||
tree.tickWhileRunning(); | ||
|
||
// Restore the original status of the blackboard | ||
BlackboardRestore(backup_before_tick, tree); | ||
tree.tickWhileRunning(); | ||
|
||
// Alternatively, we may want to save he values of the element in the blackboard | ||
// to file, to restore them again. We use JSON serialization for that. | ||
nlohmann::json json_after_tick = ExportTreeToJSON(tree); | ||
|
||
// The JSOn object can be saved to file. See https://github.com/nlohmann/json | ||
// for details. For the time being, we just print it in the console | ||
|
||
std::cout << "--- blackboard serialized as JSON: ----\n" | ||
<< json_after_tick.dump(2) << std::endl; | ||
|
||
// We can restore the values of the blackboards using the JSON | ||
ImportTreeFromJSON(json_after_tick, tree); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
#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<Position2D>() | ||
// 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<Position2D>("pos") }; | ||
} | ||
|
||
private: | ||
Position2D _pos = { 0, 0 }; | ||
}; | ||
|
||
// clang-format off | ||
|
||
static const char* xml_text = R"( | ||
<root BTCPP_format="4"> | ||
|
||
<BehaviorTree ID="MainTree"> | ||
<Sequence> | ||
<Script code="door_open:=false" /> | ||
<UpdatePosition pos="{pos_2D}" /> | ||
<Fallback> | ||
<Inverter> | ||
<IsDoorClosed/> | ||
</Inverter> | ||
<SubTree ID="DoorClosed" _autoremap="true" door_open="{door_open}"/> | ||
</Fallback> | ||
<PassThroughDoor/> | ||
</Sequence> | ||
</BehaviorTree> | ||
|
||
<BehaviorTree ID="DoorClosed"> | ||
<Fallback name="tryOpen" _onSuccess="door_open:=true"> | ||
<OpenDoor/> | ||
<RetryUntilSuccessful num_attempts="5"> | ||
<PickLock/> | ||
</RetryUntilSuccessful> | ||
<SmashDoor/> | ||
</Fallback> | ||
</BehaviorTree> | ||
|
||
</root> | ||
)"; | ||
|
||
// clang-format on | ||
|
||
int main() | ||
{ | ||
BT::BehaviorTreeFactory factory; | ||
|
||
// Nodes registration, as usual | ||
CrossDoor cross_door; | ||
cross_door.registerNodes(factory); | ||
factory.registerNodeType<UpdatePosition>("UpdatePosition"); | ||
|
||
// Groot2 editor requires a model of your registered Nodes. | ||
// You don't need to write that by hand, it can be automatically | ||
// generated using the following command. | ||
std::string xml_models = BT::writeTreeNodesModelXML(factory); | ||
|
||
factory.registerBehaviorTreeFromText(xml_text); | ||
|
||
// Add this to allow Groot2 to visualize your custom type | ||
BT::RegisterJsonDefinition<Position2D>(); | ||
|
||
auto tree = factory.createTree("MainTree"); | ||
|
||
std::cout << "----------- XML file ----------\n" | ||
<< BT::WriteTreeToXML(tree, false, false) | ||
<< "--------------------------------\n"; | ||
|
||
// Connect the Groot2Publisher. This will allow Groot2 to | ||
// get the tree and poll status updates. | ||
const unsigned port = 1667; | ||
BT::Groot2Publisher publisher(tree, port); | ||
|
||
// Add two more loggers, to save the transitions into a file. | ||
// Both formats are compatible with Groot2 | ||
|
||
// Lightweight serialization | ||
BT::FileLogger2 logger2(tree, "t12_logger2.btlog"); | ||
// SQLite logger can save multiple sessions into the same database | ||
bool append_to_database = true; | ||
BT::SqliteLogger sqlite_logger(tree, "t12_sqlitelog.db3", append_to_database); | ||
|
||
while(1) | ||
{ | ||
std::cout << "Start" << std::endl; | ||
cross_door.reset(); | ||
tree.tickWhileRunning(); | ||
std::this_thread::sleep_for(std::chrono::milliseconds(2000)); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.