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