Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 950fef2

Browse files
committed
Introducing SyncActionNode that is more self explaining and less ambiguous
1 parent ef52505 commit 950fef2

14 files changed

+98
-105
lines changed

docs/getting_started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ a long calculation.
3434
To create a custom TreeNode, you should inherit from the proper class.
3535

3636
For instance, to create your own synchronous Action, you should inherit from the
37-
class __ActionNodeBase__.
37+
class __SyncActionNode__.
3838

3939
Alternatively, we provided a mechanism to create a TreeNode passing a
4040
__function pointer__ to a wrapper (dependency injection).

docs/tutorial_A_create_trees.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,18 @@ You can find the source code here: **sample_nodes/dummy_nodes.h**.
1515
The default (and recommended) way to create a TreeNode is by inheritance.
1616

1717
``` c++
18-
// Example of custom ActionNodeBase (synchronous Action)
19-
class ApproachObject: public BT::ActionNodeBase
18+
// Example of custom SyncActionNode (synchronous Action)
19+
class ApproachObject: public BT::SyncActionNode
2020
{
2121
public:
2222
ApproachObject(const std::string& name):
23-
BT::ActionNodeBase(name) {}
23+
BT::SyncActionNode(name) {}
2424

2525
// You must override this virtual function
2626
BT::NodeStatus tick() override
2727
{
28-
std::cout << "ApproachObject: " << this->name() << std::endl;
29-
return BT::NodeStatus::SUCCESS;
30-
}
31-
32-
// You must override this virtual function
33-
virtual void halt() override
34-
{
35-
// Do nothing. This is used by asynchronous nodes only.
28+
std::cout << "ApproachObject: " << this->name() << std::endl;
29+
return BT::NodeStatus::SUCCESS;
3630
}
3731
};
3832
```

docs/tutorial_B_node_parameters.md

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ read from file.
77

88
To create a TreeNodes that accepts NodeParameters, you must follow these rules:
99

10-
- Inherit from either ActionNodeBase, ActionNode, ConditionNode or DecoratorNode.
10+
- Inherit from either ActionNode, ConditionNode or DecoratorNode.
1111

1212
- You must provide a constructor with the following signature:
1313

@@ -27,7 +27,7 @@ Check the [tutorial 6](tutorial_G_legacy.md) for details.
2727

2828
## Example: an Action requiring the parameter "message"
2929

30-
`SaySomething` is a simple synchronous ActionNodeBase which will print the
30+
`SaySomething` is a simple SyncActionNode which will print the
3131
string passed in the NodeParameter called "message".
3232

3333
Please note:
@@ -41,12 +41,12 @@ Please note:
4141
`tick()` method.
4242

4343
``` c++ hl_lines="5 9 18"
44-
class SaySomething: public ActionNodeBase
44+
class SaySomething: public SyncActionNode
4545
{
4646
public:
4747
// There must be a constructor with this signature
4848
SaySomething(const std::string& name, const NodeParameters& params):
49-
ActionNodeBase(name, params) {}
49+
SyncActionNode(name, params) {}
5050

5151
// It is mandatory to define this static method.
5252
static const NodeParameters& requiredNodeParameters()
@@ -57,20 +57,18 @@ public:
5757

5858
virtual NodeStatus tick() override
5959
{
60-
std::string msg;
61-
if( getParam("message", msg) == false )
62-
{
63-
// if getParam failed, use the default value
64-
msg = requiredNodeParameters().at("message");
65-
}
66-
std::cout << "Robot says: " << msg << std::endl;
67-
return BT::NodeStatus::SUCCESS;
68-
}
69-
virtual void halt() override {}
60+
std::string msg;
61+
if( getParam("message", msg) == false )
62+
{
63+
// if getParam failed, use the default value
64+
msg = requiredNodeParameters().at("message");
65+
}
66+
std::cout << "Robot says: " << msg << std::endl;
67+
return BT::NodeStatus::SUCCESS;
68+
}
7069
};
7170
```
7271

73-
7472
## Example: conversion to user defined C++ types
7573

7674
In the next example we have a user defined type `Pose2D`.
@@ -146,36 +144,36 @@ public:
146144
return params;
147145
}
148146

149-
virtual NodeStatus tick() override
150-
{
151-
Pose2D goal;
147+
virtual NodeStatus tick() override
148+
{
149+
Pose2D goal;
152150
if( getParam<Pose2D>("goal", goal) == false )
153151
{
154152
auto default_goal = requiredNodeParameters().at("goal");
155153
goal = BT::convertFromString<Pose2D>( default_goal_value );
156154
}
157155
158-
printf("[ MoveBase: STARTED ]. goal: x=%.f y=%.1f theta=%.2f\n",
159-
goal.x, goal.y, goal.theta);
156+
printf("[ MoveBase: STARTED ]. goal: x=%.f y=%.1f theta=%.2f\n",
157+
goal.x, goal.y, goal.theta);
160158

161-
halt_requested_ = false;
159+
halt_requested_ = false;
162160
163-
int count = 0;
164-
// "compute" for 250 milliseconds or until halt_requested_
165-
while( !halt_requested_ && count++ < 25)
166-
{
167-
SleepMilliseconds(10);
168-
}
169-
170-
std::cout << "[ MoveBase: FINISHED ]" << std::endl;
171-
return halt_requested_ ? NodeStatus::FAILURE :
172-
NodeStatus::SUCCESS;
173-
}
174-
175-
virtual void halt() override
176-
{
177-
halt_requested_ = true;
178-
}
161+
int count = 0;
162+
// "compute" for 250 milliseconds or until halt_requested_
163+
while( !halt_requested_ && count++ < 25)
164+
{
165+
SleepMilliseconds(10);
166+
}
167+
168+
std::cout << "[ MoveBase: FINISHED ]" << std::endl;
169+
return halt_requested_ ? NodeStatus::FAILURE :
170+
NodeStatus::SUCCESS;
171+
}
172+
173+
virtual void halt() override
174+
{
175+
halt_requested_ = true;
176+
}
179177
private:
180178
bool halt_requested_;
181179
};

gtest/gtest_blackboard.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
using namespace BT;
2020

21-
class InitTestNode: public ActionNodeBase
21+
class InitTestNode: public SyncActionNode
2222
{
2323
public:
2424
InitTestNode(bool try_to_access_bb, const std::string& name):
25-
ActionNodeBase(name),
25+
SyncActionNode(name),
2626
_value(0)
2727
{
2828
if( try_to_access_bb )
@@ -35,7 +35,6 @@ class InitTestNode: public ActionNodeBase
3535
void onInit() {
3636
blackboard()->get(KEY(), _value);
3737
}
38-
void halt() {}
3938

4039
NodeStatus tick()
4140
{

gtest/include/action_test_node.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@
55

66
namespace BT
77
{
8-
class SyncActionTest : public ActionNodeBase
8+
class SyncActionTest : public SyncActionNode
99
{
1010
public:
1111
SyncActionTest(const std::string& name);
1212

1313
BT::NodeStatus tick() override;
1414

15-
virtual void halt() override
16-
{
17-
}
18-
1915
void setBoolean(bool boolean_value);
2016

2117
int tickCount() const

gtest/navigation_test.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,32 +85,26 @@ class IsStuck: public ConditionNode, public TestNode
8585
}
8686
};
8787

88-
class BackUpAndSpin: public ActionNodeBase, public TestNode
88+
class BackUpAndSpin: public SyncActionNode, public TestNode
8989
{
9090
public:
91-
BackUpAndSpin(const std::string& name): ActionNodeBase(name), TestNode(name){}
91+
BackUpAndSpin(const std::string& name): SyncActionNode(name), TestNode(name){}
9292

9393
NodeStatus tick() override
9494
{
9595
return tickImpl();
9696
}
97-
void halt() override {
98-
std::cout << "BackUpAndSpin::halt" << std::endl;
99-
}
10097
};
10198

102-
class ComputePathToPose: public ActionNodeBase, public TestNode
99+
class ComputePathToPose: public SyncActionNode, public TestNode
103100
{
104101
public:
105-
ComputePathToPose(const std::string& name): ActionNodeBase(name), TestNode(name){}
102+
ComputePathToPose(const std::string& name): SyncActionNode(name), TestNode(name){}
106103

107104
NodeStatus tick() override
108105
{
109106
return tickImpl();
110107
}
111-
void halt() override {
112-
std::cout << "ComputePathToPose::halt" << std::endl;
113-
}
114108
};
115109

116110
class FollowPath: public CoroActionNode, public TestNode

gtest/src/action_test_node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void BT::AsyncActionTest::setBoolean(bool boolean_value)
6565

6666
//----------------------------------------------
6767

68-
BT::SyncActionTest::SyncActionTest(const std::string& name) : ActionNodeBase(name)
68+
BT::SyncActionTest::SyncActionTest(const std::string& name) : SyncActionNode(name)
6969
{
7070
tick_count_ = 0;
7171
boolean_value_ = true;

include/behaviortree_cpp/action_node.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace BT
3030
class ActionNodeBase : public LeafNode
3131
{
3232
public:
33-
// Constructor
33+
3434
ActionNodeBase(const std::string& name, const NodeParameters& parameters = NodeParameters());
3535
~ActionNodeBase() override = default;
3636

@@ -42,6 +42,26 @@ class ActionNodeBase : public LeafNode
4242
}
4343
};
4444

45+
/**
46+
* @brief The SyncActionNode is an helper derived class that
47+
* explicitly forbids the status RUNNING and doesn't require
48+
* an implementation of halt()
49+
*/
50+
class SyncActionNode : public ActionNodeBase
51+
{
52+
public:
53+
54+
SyncActionNode(const std::string& name, const NodeParameters& parameters = NodeParameters());
55+
~SyncActionNode() override = default;
56+
57+
virtual NodeStatus executeTick() override;
58+
59+
virtual void halt() override final // don't need to override this
60+
{
61+
setStatus(NodeStatus::IDLE);
62+
}
63+
};
64+
4565
/**
4666
* @brief The SimpleActionNode provides an easy to use ActionNode.
4767
* The user should simply provide a callback with this signature
@@ -90,7 +110,7 @@ class SimpleActionNode : public ActionNodeBase
90110
class AsyncActionNode : public ActionNodeBase
91111
{
92112
public:
93-
// Constructor
113+
94114
AsyncActionNode(const std::string& name, const NodeParameters& parameters = NodeParameters());
95115
virtual ~AsyncActionNode() override;
96116

@@ -123,7 +143,7 @@ class AsyncActionNode : public ActionNodeBase
123143
// For this reason, AsyncActionNode is a much better name.
124144

125145

126-
// The right class to use for synchronous Actions is ActionBase
146+
// The right class to use for synchronous Actions is SyncActionBase
127147
[[deprecated]]
128148
typedef AsyncActionNode ActionNode;
129149

include/behaviortree_cpp/actions/always_failure_node.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
namespace BT
1919
{
20-
class AlwaysFailure : public ActionNodeBase
20+
class AlwaysFailure : public SyncActionNode
2121
{
2222
public:
23-
AlwaysFailure(const std::string& name) : ActionNodeBase(name, NodeParameters())
23+
AlwaysFailure(const std::string& name) : SyncActionNode(name, NodeParameters())
2424
{
2525
}
2626

@@ -29,9 +29,6 @@ class AlwaysFailure : public ActionNodeBase
2929
{
3030
return NodeStatus::FAILURE;
3131
}
32-
virtual void halt() override
33-
{
34-
}
3532
};
3633
}
3734

include/behaviortree_cpp/actions/always_success_node.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
namespace BT
1919
{
20-
class AlwaysSuccess : public ActionNodeBase
20+
class AlwaysSuccess : public SyncActionNode
2121
{
2222
public:
23-
AlwaysSuccess(const std::string& name) : ActionNodeBase(name, NodeParameters())
23+
AlwaysSuccess(const std::string& name) : SyncActionNode(name, NodeParameters())
2424
{
2525
}
2626

@@ -29,9 +29,6 @@ class AlwaysSuccess : public ActionNodeBase
2929
{
3030
return NodeStatus::SUCCESS;
3131
}
32-
virtual void halt() override
33-
{
34-
}
3532
};
3633
}
3734

0 commit comments

Comments
 (0)