-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathdecorator_retry_node.cpp
More file actions
183 lines (149 loc) · 6.36 KB
/
Copy pathdecorator_retry_node.cpp
File metadata and controls
183 lines (149 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <decorator_retry_node.h>
BT::DecoratorRetryNode::DecoratorRetryNode(std::string name, unsigned int NTries) : ControlNode::ControlNode(name)
{
// thread_ start
NTries_ = NTries;
thread_ = boost::thread(&DecoratorRetryNode::Exec, this);
}
BT::DecoratorRetryNode::~DecoratorRetryNode() {}
void BT::DecoratorRetryNode::Exec()
{
int i;
// Waiting for the first tick to come
tick_engine.wait();
// Vector size initialization
N_of_children_ = children_nodes_.size();
// Simulating a tick for myself
tick_engine.tick();
while(true)
{
// Waiting for a tick to come
tick_engine.wait();
if(ReadState() == BT::EXIT)
{
// The behavior tree is going to be destroied
return;
}
// Checking if i was halted
if (ReadState() != BT::HALTED)
{
// If not, the children can be ticked
std::cout << get_name() << " ticked, ticking children..." << std::endl;
TryIndx_ = 0;
// For each child:
//for (i = 0; i<M; i++)
{
if (children_nodes_[0]->get_type() == BT::ACTION_NODE)
{
// 1) if it's an action:
// 1.1) read its state;
ReturnStatus ActionState = children_nodes_[0]->ReadState();
if (ActionState == BT::IDLE)
{
// 1.2) if it's "Idle":
// 1.2.1) ticking it;
children_nodes_[0]->tick_engine.tick();
// 1.2.2) retrive its state as soon as it is available;
children_states_[0] = children_nodes_[0]->GetNodeState();
}
else if (ActionState == BT::RUNNING)
{
// 1.3) if it's "Running":
// 1.3.1) saving "Running"
children_states_[0] = BT::RUNNING;
}
else
{
// 1.4) if it's "Success" of "Failure" (it can't be "Halted"!):
// 1.2.1) ticking it;
children_nodes_[0]->tick_engine.tick();
// 1.2.2) saving the read state;
children_states_[0] = ActionState;
}
}
else
{
// 2) if it's not an action:
// 2.1) ticking it;
children_nodes_[0]->tick_engine.tick();
// 2.2) retrive its state as soon as it is available;
children_states_[0] = children_nodes_[0]->GetNodeState();
}
// 3) if the child state is not a success:
if(children_states_[0] == BT::SUCCESS)
{
SetNodeState(BT::SUCCESS);
// 4.2) resetting the state;
WriteState(BT::IDLE);
std::cout << get_name() << " returning " << BT::SUCCESS << "!" << std::endl;
}
else
{
if(children_states_[0] == BT::FAILURE)
{
children_nodes_[0]->ResetColorState();
TryIndx_++;
}
if(children_states_[0] == BT::FAILURE && TryIndx_ < NTries_)
{
// 3.1) the node state is equal to running since I am rerunning the child
SetNodeState(BT::RUNNING);
// 3.2) state reset;
WriteState(BT::IDLE);
}
else
{
SetNodeState(children_states_[0]);
// 3.2) state reset;
WriteState(BT::IDLE);
std::cout << get_name() << " returning " << children_states_[0] << "!" << std::endl;
}
}
}
}
else
{
// If it was halted, all the "busy" children must be halted too
std::cout << get_name() << " halted! Halting all the children..." << std::endl;
if (children_nodes_[0]->get_type() != BT::ACTION_NODE && children_states_[0] == BT::RUNNING)
{
// if the control node was running:
// halting it;
children_nodes_[0]->Halt();
// sync with it (it's waiting on the semaphore);
children_nodes_[0]->tick_engine.tick();
std::cout << get_name() << " halting child " << "!" << std::endl;
}
else if (children_nodes_[0]->get_type() == BT::ACTION_NODE && children_nodes_[0]->ReadState() == BT::RUNNING)
{
std::cout << get_name() << " trying halting child " << "..." << std::endl;
// if it's a action node that hasn't finished its job:
// trying to halt it:
if (children_nodes_[0]->Halt() == false)
{
// this means that, before this node could set its child state
// to "Halted", the child had already written the action outcome;
// sync with him ignoring its state;
children_nodes_[0]->tick_engine.tick();
std::cout << get_name() << " halting of child " << " failed!" << std::endl;
}
std::cout << get_name() << " halting of child " << " succedeed!" << std::endl;
}
else if (children_nodes_[0]->get_type() == BT::ACTION_NODE && children_nodes_[0]->ReadState() != BT::IDLE)
{
// if it's a action node that has finished its job:
// ticking it without saving its returning state;
children_nodes_[0]->tick_engine.tick();
}
// updating its vector cell
children_states_[0] = BT::IDLE;
// Resetting the node state
WriteState(BT::IDLE);
}
}
}
int BT::DecoratorRetryNode::DrawType()
{
// Lock acquistion
return BT::DECORATOR;
}