fallback_node.cpp
Go to the documentation of this file.
00001 /* Copyright (C) 2015-2017 Michele Colledanchise - All Rights Reserved
00002 *
00003 *   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
00004 *   to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
00005 *   and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
00006 *   The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00007 *
00008 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00009 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00010 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00011 */
00012 
00013 #include <fallback_node.h>
00014 #include <string>
00015 
00016 BT::FallbackNode::FallbackNode(std::string name) : ControlNode::ControlNode(name) {}
00017 
00018 BT::FallbackNode::~FallbackNode() {}
00019 
00020 BT::ReturnStatus BT::FallbackNode::Tick()
00021 {
00022     {
00023         // gets the number of children. The number could change if, at runtime, one edits the tree.
00024         N_of_children_ = children_nodes_.size();
00025 
00026         // Routing the ticks according to the fallback node's logic:
00027 
00028         for (unsigned int i = 0; i < N_of_children_; i++)
00029         {
00030             /*      Ticking an action is different from ticking a condition. An action executed some portion of code in another thread.
00031                     We want this thread detached so we can cancel its execution (when the action no longer receive ticks).
00032                     Hence we cannot just call the method Tick() from the action as doing so will block the execution of the tree.
00033                     For this reason if a child of this node is an action, then we send the tick using the tick engine. Otherwise we call the method Tick() and wait for the response.
00034             */
00035             if (children_nodes_[i]->get_type() == BT::ACTION_NODE)
00036             {
00037                 // 1) If the child i is an action, read its state.
00038                 child_i_status_ = children_nodes_[i]->get_status();
00039 
00040                 if (child_i_status_ == BT::IDLE || child_i_status_ == BT::HALTED)
00041                 {
00042                     // 1.1) If the action status is not running, the sequence node sends a tick to it.
00043                     DEBUG_STDOUT(get_name() << "NEEDS TO TICK " << children_nodes_[i]->get_name());
00044                     children_nodes_[i]->tick_engine.Tick();
00045 
00046                     // waits for the tick to arrive to the child
00047                     do
00048                     {
00049                         child_i_status_ = children_nodes_[i]->get_status();
00050                         std::this_thread::sleep_for(std::chrono::milliseconds(10));
00051                     }
00052                     while (child_i_status_ != BT::RUNNING && child_i_status_ != BT::SUCCESS
00053                            && child_i_status_ != BT::FAILURE);
00054                 }
00055             }
00056             else
00057             {
00058                 // 2) if it's not an action:
00059                 // Send the tick and wait for the response;
00060                 child_i_status_ = children_nodes_[i]->Tick();
00061             }
00062             // Ponderate on which status to send to the parent
00063             if (child_i_status_ != BT::FAILURE)
00064             {
00065                 if (child_i_status_ == BT::SUCCESS)
00066                 {
00067                     children_nodes_[i]->set_status(BT::IDLE);  // the child goes in idle if it has returned success.
00068                 }
00069                 // If the  child status is not failure, halt the next children and return the status to your parent.
00070                 DEBUG_STDOUT(get_name() << " is HALTING children from " << (i+1));
00071                 HaltChildren(i+1);
00072                 set_status(child_i_status_);
00073                 return child_i_status_;
00074             }
00075             else
00076             {
00077                 // the child returned failure.
00078                 children_nodes_[i]->set_status(BT::IDLE);
00079                 if (i == N_of_children_ - 1)
00080                 {
00081                     // If the  child status is failure, and it is the last child to be ticked,
00082                     // then the sequence has failed.
00083                     set_status(BT::FAILURE);
00084                     return BT::FAILURE;
00085                 }
00086             }
00087         }
00088     }
00089     return BT::EXIT;
00090 }
00091 
00092 int BT::FallbackNode::DrawType()
00093 {
00094     // Lock acquistion
00095 
00096     return BT::SELECTOR;
00097 }


behavior_tree_core
Author(s): Michele Colledanchise
autogenerated on Sun Sep 10 2017 02:31:49