t02_basic_ports.cpp
Go to the documentation of this file.
00001 #include "behaviortree_cpp/bt_factory.h"
00002 
00003 #include "dummy_nodes.h"
00004 #include "movebase_node.h"
00005 
00006 using namespace BT;
00007 
00028 // clang-format off
00029 static const char* xml_text = R"(
00030 
00031  <root main_tree_to_execute = "MainTree" >
00032 
00033      <BehaviorTree ID="MainTree">
00034         <Sequence name="root">
00035             <SaySomething     message="start thinking..." />
00036             <ThinkWhatToSay   text="{the_answer}"/>
00037             <SaySomething     message="{the_answer}" />
00038             <SaySomething2    message="SaySomething2 works too..." />
00039             <SaySomething2    message="{the_answer}" />
00040         </Sequence>
00041      </BehaviorTree>
00042 
00043  </root>
00044  )";
00045 // clang-format on
00046 
00047 
00048 class ThinkWhatToSay : public BT::SyncActionNode
00049 {
00050   public:
00051     ThinkWhatToSay(const std::string& name, const BT::NodeConfiguration& config)
00052       : BT::SyncActionNode(name, config)
00053     {
00054     }
00055 
00056     // This Action simply write a value in the port "text"
00057     BT::NodeStatus tick() override
00058     {
00059         setOutput("text", "The answer is 42" );
00060         return BT::NodeStatus::SUCCESS;
00061     }
00062 
00063     // A node having ports MUST implement this STATIC method
00064     static BT::PortsList providedPorts()
00065     {
00066         return { BT::OutputPort<std::string>("text") };
00067     }
00068 };
00069 
00070 
00071 int main()
00072 {
00073     using namespace DummyNodes;
00074 
00075     BehaviorTreeFactory factory;
00076 
00077     // The class SaySomething has a method called providedPorts() that define the INPUTS.
00078     // In this case, it requires an input called "message"
00079     factory.registerNodeType<SaySomething>("SaySomething");
00080 
00081 
00082     // Similarly to SaySomething, ThinkWhatToSay has an OUTPUT port called "text"
00083     // Both these ports are std::string, therefore they can connect to each other
00084     factory.registerNodeType<ThinkWhatToSay>("ThinkWhatToSay");
00085 
00086     // SimpleActionNodes can not define their own method providedPorts(), therefore
00087     // we have to pass the PortsList explicitly if we want the Action to use getInput()
00088     // or setOutput();
00089     PortsList say_something_ports = { InputPort<std::string>("message") };
00090     factory.registerSimpleAction("SaySomething2", SaySomethingSimple, say_something_ports );
00091 
00092     /* An INPUT can be either a string, for instance:
00093      *
00094      *     <SaySomething message="start thinking..." />
00095      *
00096      * or contain a "pointer" to a type erased entry in the Blackboard,
00097      * using this syntax: {name_of_entry}. Example:
00098      *
00099      *     <SaySomething message="{the_answer}" />
00100      */
00101 
00102     auto tree = factory.createTreeFromText(xml_text);
00103 
00104     tree.root_node->executeTick();
00105 
00106     /*  Expected output:
00107      *
00108         Robot says: start thinking...
00109         Robot says: The answer is 42
00110         Robot says: SaySomething2 works too...
00111         Robot says: The answer is 42
00112     *
00113     * The way we "connect" output ports to input ports is to "point" to the same
00114     * Blackboard entry.
00115     *
00116     * This means that ThinkSomething will write into the entry with key "the_answer";
00117     * SaySomething and SaySomething2 will read the message from the same entry.
00118     *
00119     */
00120     return 0;
00121 }
00122 
00123 


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 20:17:15