action_test_node.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2015-2017 Michele Colledanchise - All Rights Reserved
2  * Copyright (C) 2018-2019 Davide Faconti, Eurecat - All Rights Reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
6 * 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:
7 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10 * 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,
11 * 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.
12 */
13 
14 #include "action_test_node.h"
15 #include <string>
16 
17 BT::AsyncActionTest::AsyncActionTest(const std::string& name, BT::Duration deadline_ms) :
18  AsyncActionNode(name, {}),
19  success_count_(0),
21 {
23  time_ = deadline_ms;
24  tick_count_ = 0;
25 }
26 
28 {
29  using std::chrono::high_resolution_clock;
30  tick_count_++;
31 
32  auto initial_time = high_resolution_clock::now();
33 
34  // we simulate an asynchronous action that takes an amount of time equal to time_
35  while (!isHaltRequested() && high_resolution_clock::now() < initial_time + time_)
36  {
37  std::this_thread::sleep_for(std::chrono::milliseconds(1));
38  }
39 
40  // check if we exited the while(9 loop because of the flag stop_loop_
41  if( isHaltRequested() ){
42  return NodeStatus::IDLE;
43  }
44 
47  }
50  }
51 
52  return expected_result_;
53 }
54 
56 {
57  // do more cleanup here if necessary
59 }
60 
62 {
63  time_ = time;
64 }
65 
67 {
68  expected_result_ = res;
69 }
70 
71 //----------------------------------------------
72 
74  SyncActionNode(name, {})
75 {
76  tick_count_ = 0;
78 }
79 
81 {
82  tick_count_++;
83  return expected_result_;
84 }
85 
87 {
88  expected_result_ = res;
89 }
SyncActionTest(const std::string &name)
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:73
virtual void halt() override
bool isHaltRequested() const
Definition: action_node.h:123
AsyncActionTest(const std::string &name, BT::Duration deadline_ms=std::chrono::milliseconds(100))
The AsyncActionNode uses a different thread, where the action will be executed.
Definition: action_node.h:115
virtual void halt() override
void setExpectedResult(NodeStatus res)
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn&#39;t require a...
Definition: action_node.h:53
std::atomic< int > tick_count_
void setTime(BT::Duration time)
BT::NodeStatus tick() override
Method to be implemented by the user.
NodeStatus expected_result_
NodeStatus
Definition: basic_types.h:35
std::atomic< NodeStatus > expected_result_
void setExpectedResult(NodeStatus res)
BT::NodeStatus tick() override
Method to be implemented by the user.
std::chrono::high_resolution_clock::duration Duration
Definition: basic_types.h:340


behaviotree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Tue May 4 2021 02:56:24