Skip to content

Commit 81d75c1

Browse files
committed
fix astar and add dijkstra
1 parent ce9c84e commit 81d75c1

File tree

5 files changed

+307
-14
lines changed

5 files changed

+307
-14
lines changed

‎CMakeLists.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ target_link_libraries(pf ${OpenCV_LIBS} )
3535
#PathPlanning
3636
##############################
3737

38+
add_executable(dijkstra src/dijkstra.cpp)
39+
target_link_libraries(dijkstra ${OpenCV_LIBS} )
40+
3841
add_executable(astar src/a_star.cpp)
3942
target_link_libraries(astar ${OpenCV_LIBS} )
4043

File renamed without changes.

‎readme.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,23 @@ Find all the executable files in ***build/bin***.
6666
[Probabilistic Robotics](http://www.probabilistic-robotics.org/)
6767

6868
# Path Planning
69+
70+
## Dijkstra
71+
* blue point: the start point
72+
* red point: the goal point
73+
<img src="https://ram-lab.com/file/tailei/gif/dijkstra.gif" alt="dijkstra" width="400"/>
74+
6975
## A star
7076
* blue point: the start point
7177
* red point: the goal point
7278
<img src="https://ram-lab.com/file/tailei/gif/astar.gif" alt="astar" width="400"/>
7379

74-
7580
## RRT
7681
* red circle: the start point
7782
* blue circle: the goal point
7883
* black circle: obstacles
7984
<img src="https://ram-lab.com/file/tailei/gif/rrt.gif" alt="rrt" width="400"/>
8085

81-
8286
## Dynamic Window Approach
8387
* blue circle: the target point
8488
* red circle: the robot

‎src/a_star.cpp‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ class Node{
2020
public:
2121
int x;
2222
int y;
23-
float cost;
23+
float sum_cost;
2424
Node* p_node;
2525

26-
Node(int x_, int y_, float cost_, Node* p_node_=NULL):x(x_), y(y_),cost(cost_), p_node(p_node_){};
26+
Node(int x_, int y_, float sum_cost_=0, Node* p_node_=NULL):x(x_), y(y_), sum_cost(sum_cost_), p_node(p_node_){};
2727
};
2828

2929

@@ -91,8 +91,8 @@ bool verify_node(Node* node,
9191
}
9292

9393

94-
float calc_heristic(Node n1, Node n2, float w=1.0){
95-
return w * std::sqrt(std::pow(n1.x-n2.x, 2)+std::pow(n1.y-n2.y, 2));
94+
float calc_heristic(Node* n1, Node* n2, float w=1.0){
95+
return w * std::sqrt(std::pow(n1->x-n2->x, 2)+std::pow(n1->y-n2->y, 2));
9696
}
9797

9898
std::vector<Node> get_motion_model(){
@@ -160,7 +160,10 @@ void a_star_planning(float sx, float sy,
160160
cv::Scalar(0, 0, 255), -1);
161161

162162
std::vector<std::vector<int> > visit_map(xwidth, vector<int>(ywidth, 0));
163+
164+
std::vector<std::vector<float> > path_cost(xwidth, vector<float>(ywidth, std::numeric_limits<float>::max()));
163165

166+
path_cost[nstart->x][nstart->y] = 0;
164167

165168
std::vector<std::vector<int> > obmap = calc_obstacle_map(
166169
ox, oy,
@@ -170,7 +173,7 @@ void a_star_planning(float sx, float sy,
170173
bg, img_reso);
171174

172175
// NOTE: d_ary_heap should be a better choice here
173-
auto cmp = [](const Node* left, const Node* right){return left->cost > right->cost;};
176+
auto cmp = [](const Node* left, const Node* right){return left->sum_cost > right->sum_cost;};
174177
std::priority_queue<Node*, std::vector<Node*>, decltype(cmp)> pq(cmp);
175178

176179
pq.push(nstart);
@@ -189,7 +192,7 @@ void a_star_planning(float sx, float sy,
189192
}
190193

191194
if (node->x == ngoal->x && node->y==ngoal->y){
192-
ngoal->cost = node->cost;
195+
ngoal->sum_cost = node->sum_cost;
193196
ngoal->p_node = node;
194197
break;
195198
}
@@ -198,7 +201,7 @@ void a_star_planning(float sx, float sy,
198201
Node * new_node = new Node(
199202
node->x + motion[i].x,
200203
node->y + motion[i].y,
201-
node->cost + motion[i].cost,
204+
path_cost[node->x][node->y] + motion[i].sum_cost + calc_heristic(ngoal, node),
202205
node);
203206

204207
if (!verify_node(new_node, obmap, min_ox, max_ox, min_oy, max_oy)){
@@ -216,22 +219,25 @@ void a_star_planning(float sx, float sy,
216219
cv::Point((new_node->x+1)*img_reso, (new_node->y+1)*img_reso),
217220
cv::Scalar(0, 255, 0));
218221

219-
// std::string int_count = std::to_string(count);
220-
// cv::imwrite("./pngs/"+std::string(5-int_count.length(), '0').append(int_count)+".png", bg);
222+
std::string int_count = std::to_string(count);
223+
cv::imwrite("./pngs/"+std::string(5-int_count.length(), '0').append(int_count)+".png", bg);
221224
count++;
222225
cv::imshow("astar", bg);
223226
cv::waitKey(5);
224227

225-
pq.push(new_node);
228+
if (path_cost[node->x][node->y]+motion[i].sum_cost < path_cost[new_node->x][new_node->y]){
229+
path_cost[new_node->x][new_node->y]=path_cost[node->x][node->y]+motion[i].sum_cost;
230+
pq.push(new_node);
231+
}
226232
}
227233
}
228234

229235
calc_final_path(ngoal, reso, bg, img_reso);
230236
delete ngoal;
231237
delete nstart;
232238

233-
// std::string int_count = std::to_string(count);
234-
// cv::imwrite("./pngs/"+std::string(5-int_count.length(), '0').append(int_count)+".png", bg);
239+
std::string int_count = std::to_string(count);
240+
cv::imwrite("./pngs/"+std::string(5-int_count.length(), '0').append(int_count)+".png", bg);
235241
cv::imshow("astar", bg);
236242
cv::waitKey(5);
237243
};

0 commit comments

Comments
 (0)