-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrt_connect.cpp
217 lines (183 loc) · 7.08 KB
/
rrt_connect.cpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// #include <vector>
// #include <cmath>
// #include <limits>
// #include <random>
// #include <algorithm>
// #include <memory>
// #include "collision.h"
#include "rrt_connect.h"
RRTTree::RRTTree(const State &rootState, CollisionChecker &collisionChecker) : collisionChecker(collisionChecker) {
nodes.push_back(new TreeNode(rootState));
}
RRTTree::~RRTTree() {
for (auto *node : nodes) {
delete node;
}
}
TreeNode* RRTTree::nearest(const State &targetState) {
TreeNode *nearestNode = nullptr;
double minDistance = std::numeric_limits<double>::max();
for (auto *node : nodes) {
double dist = distance(node->state, targetState);
if (dist < minDistance) {
minDistance = dist;
nearestNode = node;
}
}
return nearestNode;
}
double RRTTree::distance(const State &a, const State &b) {
// Euclidean distance in joint space
double d0 = a.joint[0] - b.joint[0];
double d1 = a.joint[1] - b.joint[1];
double d2 = a.joint[2] - b.joint[2];
return sqrt(d0 * d0 + d1 * d1 + d2 * d2);
}
TreeNode *RRTTree::extend(const State &targetState) {
// Find the nearest node in the tree to the target state
TreeNode *nearestNode = nearest(targetState);
// Calculate the direction from the nearest node to the target state
State direction(
targetState.joint[0] - nearestNode->state.joint[0],
targetState.joint[1] - nearestNode->state.joint[1],
targetState.joint[2] - nearestNode->state.joint[2]
);
// Normalize the direction
double norm = sqrt(
direction.joint[0] * direction.joint[0] +
direction.joint[1] * direction.joint[1] +
direction.joint[2] * direction.joint[2]
);
State newState;
if (norm > stepSize) {
// Move from nearest node towards the target by the step size
double scale = stepSize / norm;
newState = State(
nearestNode->state.joint[0] + scale * direction.joint[0],
nearestNode->state.joint[1] + scale * direction.joint[1],
nearestNode->state.joint[2] + scale * direction.joint[2]
);
} else {
// Target state is within one step size, so set the new state to the target state
newState = targetState;
}
// Check if the path to the new state is collision-free
// TODO: Implement a trajectory collision checker
if (collisionChecker.isCollisionFree(newState)) {
// Create a new node and add it to the tree
TreeNode *newNode = new TreeNode(newState, nearestNode);
nodes.push_back(newNode);
return newNode;
}
// If the path is not collision-free, return nullptr
return nullptr;
}
TreeNode *RRTTree::connect(const State &targetState, bool &Connected) {
TreeNode *newNode = nullptr;
TreeNode *nearestNode = nearest(targetState);
while (true) {
State direction(
targetState.joint[0] - nearestNode->state.joint[0],
targetState.joint[1] - nearestNode->state.joint[1],
targetState.joint[2] - nearestNode->state.joint[2]
);
// Normalize the direction
double norm = sqrt(
direction.joint[0] * direction.joint[0] +
direction.joint[1] * direction.joint[1] +
direction.joint[2] * direction.joint[2]
);
if (norm < std::numeric_limits<double>::epsilon()) {
return newNode; // The target state is the same as the nearest node
}
State newState;
bool stepCloser = false;
if (norm > stepSize) {
// Move from nearest node towards the target by the step size
double scale = stepSize / norm;
newState = State(
nearestNode->state.joint[0] + scale * direction.joint[0],
nearestNode->state.joint[1] + scale * direction.joint[1],
nearestNode->state.joint[2] + scale * direction.joint[2]
);
stepCloser = true;
} else {
// Target state is within one step size, so set the new state to the target state
newState = targetState;
}
// Check if the path to the new state is collision-free
// // TODO: Implement a trajectory collision checker
if (collisionChecker.isCollisionFree(newState)) {
// Create a new node and add it to the tree
newNode = new TreeNode(newState, nearestNode);
nodes.push_back(newNode);
nearestNode = newNode;
// If a step was taken closer, continue, otherwise we've reached the target state
if (!stepCloser) {
Connected = true;
return newNode;
}
} else {
// If the path is not collision-free, return the last valid node
return newNode;
}
}
}
RRTConnectPlanner::RRTConnectPlanner(const State &start, const State &goal, CollisionChecker &collisionChecker): collisionChecker(collisionChecker){
// new (&treeA) RRTTree(start, collisionChecker);
// new (&treeB) RRTTree(goal, collisionChecker);
// exit(0);
treeA = new RRTTree(start, collisionChecker);
treeB = new RRTTree(goal, collisionChecker);
}
std::vector<State> RRTConnectPlanner::plan() {
int counter = 0;
bool reversed = false;
while (true) {
if(counter%1000==0){
cout<<"Iteration: "<<counter<<endl;
}
counter++;
State randState = randomState();
if (collisionChecker.isCollisionFree(randState)) {
TreeNode *newNodeA = treeA->extend(randState);
bool Connected = false;
if(newNodeA) treeB->connect(newNodeA->state, Connected);
if (Connected) {
std::vector<State> path = extractPath(newNodeA, treeB->nearest(newNodeA->state));
if(reversed) std::reverse(path.begin(), path.end());
return path;
}
std::swap(treeA, treeB);
reversed = !reversed;
}
}
}
State RRTConnectPlanner::randomState() {
// Generate a random state within the joint limits
State randomState;
randomState.joint[0] = dist[0](gen);
randomState.joint[1] = dist[1](gen);
randomState.joint[2] = dist[2](gen);
return randomState;
}
std::vector<State> RRTConnectPlanner::extractPath(TreeNode *endNodeA, TreeNode *endNodeB) {
std::vector<State> path;
// generate path in treeA
TreeNode *currentNode = endNodeA;
// Traverse from the end node back to the root node
while (currentNode != nullptr) {
path.push_back(currentNode->state); // Add the state to the path
currentNode = currentNode->parent; // Move to the parent node
}
// The path is currently from end to start, so reverse it to start from the start node
std::reverse(path.begin(), path.end());
// generate path in treeB
currentNode = endNodeB;
// Traverse from the end node back to the root node
while (currentNode != nullptr) {
path.push_back(currentNode->state); // Add the state to the path
currentNode = currentNode->parent; // Move to the parent node
}
return path;
}