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 00014 #include <actions/action_test_node.h> 00015 #include <string> 00016 00017 00018 BT::ActionTestNode::ActionTestNode(std::string name) : ActionNode::ActionNode(name) 00019 { 00020 type_ = BT::ACTION_NODE; 00021 boolean_value_ = true; 00022 time_ = 3; 00023 thread_ = std::thread(&ActionTestNode::WaitForTick, this); 00024 } 00025 00026 BT::ActionTestNode::~ActionTestNode() {} 00027 00028 void BT::ActionTestNode::WaitForTick() 00029 { 00030 while (true) 00031 { 00032 // Waiting for the first tick to come 00033 DEBUG_STDOUT(get_name() << " WAIT FOR TICK"); 00034 00035 tick_engine.Wait(); 00036 DEBUG_STDOUT(get_name() << " TICK RECEIVED"); 00037 00038 // Running state 00039 set_status(BT::RUNNING); 00040 // Perform action... 00041 int i = 0; 00042 while (get_status() != BT::HALTED && i++ < time_) 00043 { 00044 DEBUG_STDOUT(" Action " << get_name() << "running! Thread id:" << std::this_thread::get_id()); 00045 std::this_thread::sleep_for(std::chrono::seconds(1)); 00046 } 00047 if (get_status() != BT::HALTED) 00048 { 00049 if (boolean_value_) 00050 { 00051 set_status(BT::SUCCESS); 00052 DEBUG_STDOUT(" Action " << get_name() << " Done!"); 00053 } 00054 else 00055 { 00056 set_status(BT::FAILURE); 00057 DEBUG_STDOUT(" Action " << get_name() << " FAILED!"); 00058 } 00059 } 00060 } 00061 } 00062 00063 void BT::ActionTestNode::Halt() 00064 { 00065 set_status(BT::HALTED); 00066 DEBUG_STDOUT("HALTED state set!"); 00067 } 00068 00069 00070 void BT::ActionTestNode::set_time(int time) 00071 { 00072 time_ = time; 00073 } 00074 00075 00076 00077 void BT::ActionTestNode::set_boolean_value(bool boolean_value) 00078 { 00079 boolean_value_ = boolean_value; 00080 } 00081 00082