timeout_node.cpp
Go to the documentation of this file.
00001 /*  Copyright (C) 2018 Davide Faconti -  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 #include "behaviortree_cpp/decorators/timeout_node.h"
00013 
00014 namespace BT
00015 {
00016 TimeoutNode::TimeoutNode(const std::string& name, unsigned milliseconds)
00017   : DecoratorNode(name, {}), child_halted_(false), msec_(milliseconds),
00018     read_parameter_from_blackboard_(false)
00019 {
00020     setRegistrationName("Timeout");
00021 }
00022 
00023 TimeoutNode::TimeoutNode(const std::string& name, const BT::NodeParameters& params)
00024   : DecoratorNode(name, params), child_halted_(false), msec_(0)
00025 {
00026     read_parameter_from_blackboard_ = isBlackboardPattern( params.at("msec") );
00027     if(!read_parameter_from_blackboard_)
00028     {
00029         if( !getParam("msec", msec_) )
00030         {
00031             throw std::runtime_error("Missing parameter [msec] in TimeoutNode");
00032         }
00033     }
00034 }
00035 
00036 NodeStatus TimeoutNode::tick()
00037 {
00038     if( read_parameter_from_blackboard_ )
00039     {
00040         if( !getParam("msec", msec_) )
00041         {
00042             throw std::runtime_error("Missing parameter [msec] in TimeoutNode");
00043         }
00044     }
00045 
00046     if (status() == NodeStatus::IDLE)
00047     {
00048         setStatus(NodeStatus::RUNNING);
00049         child_halted_ = false;
00050 
00051         if (msec_ > 0)
00052         {
00053             timer_id_ = timer().add(std::chrono::milliseconds(msec_), [this](bool aborted) {
00054                 if (!aborted && child()->status() == NodeStatus::RUNNING)
00055                 {
00056                     child()->halt();
00057                     child_halted_ = true;
00058                 }
00059             });
00060         }
00061     }
00062 
00063     if (child_halted_)
00064     {
00065         setStatus(NodeStatus::FAILURE);
00066     }
00067     else
00068     {
00069         auto child_status = child()->executeTick();
00070         if (child_status != NodeStatus::RUNNING)
00071         {
00072             timer().cancel(timer_id_);
00073         }
00074         setStatus(child_status);
00075     }
00076 
00077     return status();
00078 }
00079 
00080 }


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:10