|
| 1 | +#include "behaviortree_cpp_v3/bt_factory.h" |
| 2 | +#include "behaviortree_cpp_v3/actions/pop_from_queue.hpp" |
| 3 | +#include "behaviortree_cpp_v3/decorators/consume_queue.h" |
| 4 | +#include <list> |
| 5 | + |
| 6 | +using namespace BT; |
| 7 | + |
| 8 | +/* |
| 9 | + * In this example we will show how a common design pattern could be implemented. |
| 10 | + * We want to iterate through the elements of a queue, for instance a list of waypoints. |
| 11 | + * |
| 12 | + * Two ways to create a "loop" are presented, one using the actions "QueueSize" and "PopFromQueue" |
| 13 | + * and the other using the decorator "ConsumeQueue". |
| 14 | + */ |
| 15 | + |
| 16 | +struct Pose2D |
| 17 | +{ |
| 18 | + double x, y, theta; |
| 19 | +}; |
| 20 | + |
| 21 | + |
| 22 | +/** |
| 23 | + * @brief Dummy action that generates a list of poses. |
| 24 | + */ |
| 25 | +class GenerateWaypoints : public SyncActionNode |
| 26 | +{ |
| 27 | + public: |
| 28 | + GenerateWaypoints(const std::string& name, const NodeConfiguration& config) |
| 29 | + : SyncActionNode(name, config) |
| 30 | + {} |
| 31 | + |
| 32 | + NodeStatus tick() override |
| 33 | + { |
| 34 | + auto queue = std::make_shared< ProtectedQueue<Pose2D> >(); |
| 35 | + for(int i=0; i<10; i++) |
| 36 | + { |
| 37 | + queue->items.push_back( Pose2D{ double(i), double(i), 0} ); |
| 38 | + } |
| 39 | + setOutput("waypoints", queue); |
| 40 | + return NodeStatus::SUCCESS; |
| 41 | + } |
| 42 | + |
| 43 | + static PortsList providedPorts() |
| 44 | + { |
| 45 | + return { OutputPort< std::shared_ptr<ProtectedQueue<Pose2D> > >("waypoints") }; |
| 46 | + } |
| 47 | +}; |
| 48 | +//-------------------------------------------------------------- |
| 49 | +class UseWaypointQueue : public AsyncActionNode |
| 50 | +{ |
| 51 | + public: |
| 52 | + UseWaypointQueue(const std::string& name, const NodeConfiguration& config) |
| 53 | + : AsyncActionNode(name, config) |
| 54 | + { } |
| 55 | + |
| 56 | + NodeStatus tick() override |
| 57 | + { |
| 58 | + std::shared_ptr<ProtectedQueue<Pose2D>> queue; |
| 59 | + if( getInput("waypoints", queue) && queue ) |
| 60 | + { |
| 61 | + Pose2D wp; |
| 62 | + { |
| 63 | + // Since we are using reference semantic (the queue is wrapped in |
| 64 | + // a shared_ptr) to modify the queue inside the blackboard, |
| 65 | + // we are effectively bypassing the thread safety of the BB. |
| 66 | + // This is the reason why we need to use a mutex explicitly. |
| 67 | + std::unique_lock<std::mutex> lk(queue->mtx); |
| 68 | + |
| 69 | + auto& waypoints = queue->items; |
| 70 | + if( waypoints.empty() ) |
| 71 | + { |
| 72 | + return NodeStatus::FAILURE; |
| 73 | + } |
| 74 | + wp = waypoints.front(); |
| 75 | + waypoints.pop_front(); |
| 76 | + |
| 77 | + } // end mutex lock |
| 78 | + |
| 79 | + std::this_thread::sleep_for( std::chrono::milliseconds(100) ); |
| 80 | + std::cout << "Using waypoint: " << wp.x << "/" << wp.y << std::endl; |
| 81 | + |
| 82 | + return NodeStatus::SUCCESS; |
| 83 | + } |
| 84 | + else{ |
| 85 | + return NodeStatus::FAILURE; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + static PortsList providedPorts() |
| 90 | + { |
| 91 | + return { InputPort< std::shared_ptr< ProtectedQueue<Pose2D>> >("waypoints") }; |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | + |
| 96 | +/** |
| 97 | + * @brief Simple Action that uses the output of PopFromQueue<Pose2D> or ConsumeQueue<Pose2D> |
| 98 | + */ |
| 99 | +class UseWaypoint : public AsyncActionNode |
| 100 | +{ |
| 101 | + public: |
| 102 | + UseWaypoint(const std::string& name, const NodeConfiguration& config) |
| 103 | + : AsyncActionNode(name, config) |
| 104 | + { } |
| 105 | + |
| 106 | + NodeStatus tick() override |
| 107 | + { |
| 108 | + Pose2D wp; |
| 109 | + if( getInput("waypoint", wp) ) |
| 110 | + { |
| 111 | + std::this_thread::sleep_for( std::chrono::milliseconds(100) ); |
| 112 | + std::cout << "Using waypoint: " << wp.x << "/" << wp.y << std::endl; |
| 113 | + return NodeStatus::SUCCESS; |
| 114 | + } |
| 115 | + else{ |
| 116 | + return NodeStatus::FAILURE; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + static PortsList providedPorts() |
| 121 | + { |
| 122 | + return { InputPort<Pose2D>("waypoint") }; |
| 123 | + } |
| 124 | +}; |
| 125 | + |
| 126 | + |
| 127 | +// clang-format off |
| 128 | + |
| 129 | +static const char* xml_implicit = R"( |
| 130 | + <root main_tree_to_execute = "TreeImplicit" > |
| 131 | + <BehaviorTree ID="TreeImplicit"> |
| 132 | + <Sequence> |
| 133 | + <GenerateWaypoints waypoints="{waypoints}" /> |
| 134 | + <KeepRunningUntilFailure> |
| 135 | + <UseWaypointQueue waypoints="{waypoints}" /> |
| 136 | + </KeepRunningUntilFailure> |
| 137 | + </Sequence> |
| 138 | + </BehaviorTree> |
| 139 | + </root> |
| 140 | + )"; |
| 141 | + |
| 142 | + |
| 143 | +static const char* xml_A = R"( |
| 144 | + <root main_tree_to_execute = "TreeA" > |
| 145 | + <BehaviorTree ID="TreeA"> |
| 146 | + <Sequence> |
| 147 | + <GenerateWaypoints waypoints="{waypoints}" /> |
| 148 | + <QueueSize queue="{waypoints}" size="{wp_size}" /> |
| 149 | + <Repeat num_cycles="{wp_size}" > |
| 150 | + <Sequence> |
| 151 | + <PopFromQueue queue="{waypoints}" popped_item="{wp}" /> |
| 152 | + <UseWaypoint waypoint="{wp}" /> |
| 153 | + </Sequence> |
| 154 | + </Repeat> |
| 155 | + </Sequence> |
| 156 | + </BehaviorTree> |
| 157 | + </root> |
| 158 | + )"; |
| 159 | + |
| 160 | +static const char* xml_B = R"( |
| 161 | + <root main_tree_to_execute = "TreeB" > |
| 162 | + <BehaviorTree ID="TreeB"> |
| 163 | + <Sequence> |
| 164 | + <GenerateWaypoints waypoints="{waypoints}" /> |
| 165 | + <ConsumeQueue queue="{waypoints}" popped_item="{wp}"> |
| 166 | + <UseWaypoint waypoint="{wp}" /> |
| 167 | + </ConsumeQueue> |
| 168 | + </Sequence> |
| 169 | + </BehaviorTree> |
| 170 | + </root> |
| 171 | + )"; |
| 172 | + |
| 173 | +// clang-format on |
| 174 | + |
| 175 | +int main() |
| 176 | +{ |
| 177 | + BehaviorTreeFactory factory; |
| 178 | + |
| 179 | + factory.registerNodeType<PopFromQueue<Pose2D>>("PopFromQueue"); |
| 180 | + factory.registerNodeType<QueueSize<Pose2D>>("QueueSize"); |
| 181 | + factory.registerNodeType<ConsumeQueue<Pose2D>>("ConsumeQueue"); |
| 182 | + |
| 183 | + factory.registerNodeType<UseWaypoint>("UseWaypoint"); |
| 184 | + factory.registerNodeType<UseWaypointQueue>("UseWaypointQueue"); |
| 185 | + factory.registerNodeType<GenerateWaypoints>("GenerateWaypoints"); |
| 186 | + |
| 187 | + |
| 188 | + for(const auto& xml_text : {xml_implicit, xml_A, xml_B} ) |
| 189 | + { |
| 190 | + auto tree = factory.createTreeFromText(xml_text); |
| 191 | + while( tree.tickRoot() == NodeStatus::RUNNING ) |
| 192 | + { |
| 193 | + tree.sleep( std::chrono::milliseconds(10) ); |
| 194 | + } |
| 195 | + std::cout << "--------------" << std::endl; |
| 196 | + } |
| 197 | + |
| 198 | + return 0; |
| 199 | +} |
| 200 | + |
| 201 | + |
0 commit comments