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  : ThreadedAction(name, {}), success_count_(0), failure_count_(0)
19 {
20  expected_result_ = NodeStatus::SUCCESS;
21  time_ = deadline_ms;
22  tick_count_ = 0;
23 }
24 
26 {
27  using std::chrono::high_resolution_clock;
28  tick_count_++;
29 
30  auto initial_time = high_resolution_clock::now();
31 
32  // we simulate an asynchronous action that takes an amount of time equal to time_
33  while(!isHaltRequested() && high_resolution_clock::now() < initial_time + time_)
34  {
35  std::this_thread::sleep_for(std::chrono::milliseconds(1));
36  }
37 
38  // check if we exited the while() loop because of the flag stop_loop_
39  if(isHaltRequested())
40  {
41  return NodeStatus::IDLE;
42  }
43 
44  if(expected_result_ == NodeStatus::SUCCESS)
45  {
46  success_count_++;
47  }
48  else if(expected_result_ == NodeStatus::FAILURE)
49  {
50  failure_count_++;
51  }
52 
53  return expected_result_;
54 }
55 
57 {
58  // do more cleanup here if necessary
60 }
61 
63 {
64  time_ = time;
65 }
66 
68 {
69  expected_result_ = res;
70 }
71 
72 //----------------------------------------------
73 
74 BT::SyncActionTest::SyncActionTest(const std::string& name) : SyncActionNode(name, {})
75 {
76  tick_count_ = 0;
77  expected_result_ = NodeStatus::SUCCESS;
78 }
79 
81 {
82  tick_count_++;
83  return expected_result_;
84 }
85 
87 {
88  expected_result_ = res;
89 }
BT::SyncActionTest::SyncActionTest
SyncActionTest(const std::string &name)
Definition: action_test_node.cpp:74
BT::Duration
std::chrono::high_resolution_clock::duration Duration
Definition: basic_types.h:628
BT::AsyncActionTest::setExpectedResult
void setExpectedResult(NodeStatus res)
Definition: action_test_node.cpp:67
BT::NodeStatus::FAILURE
@ FAILURE
BT::SyncActionTest::setExpectedResult
void setExpectedResult(NodeStatus res)
Definition: action_test_node.cpp:86
BT::AsyncActionTest::setTime
void setTime(BT::Duration time)
Definition: action_test_node.cpp:62
BT::SyncActionTest::tick
BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: action_test_node.cpp:80
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::ThreadedAction::halt
virtual void halt() override
Definition: action_node.cpp:233
action_test_node.h
BT::NodeStatus::IDLE
@ IDLE
BT::AsyncActionTest::AsyncActionTest
AsyncActionTest(const std::string &name, BT::Duration deadline_ms=std::chrono::milliseconds(100))
Definition: action_test_node.cpp:17
BT::ThreadedAction
The ThreadedAction executes the tick in a different thread.
Definition: action_node.h:116
BT::AsyncActionTest::halt
virtual void halt() override
Definition: action_test_node.cpp:56
BT::SyncActionNode
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn't require a...
Definition: action_node.h:52
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33
BT::AsyncActionTest::tick
BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: action_test_node.cpp:25


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07