Skip to content

Commit 1f1f63d

Browse files
committed
Fixed Astar, needs to be tested with moving robot in simulator first
1 parent e782693 commit 1f1f63d

15 files changed

+220
-233
lines changed

ros_ws/src/navigation/CMakeLists.txt

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ cmake_minimum_required(VERSION 2.8.3)
22
project(navigation)
33

44
set (NAVSOURCES src/euclidiandistancehueristic.cpp
5-
src/PathFinderTutorials.cpp)
5+
src/navigation_node.cpp src/navigation.cpp src/minheap.cpp include/navigation/astar.hpp include/navigation/minheap.hpp include/navigation/updatepriorityqueue.hpp include/navigation/astar.h
6+
include/navigation/euclidiandistancehueristic.h include/navigation/minheap.h include/navigation/navigation.h include/navigation/typewkey.h
7+
include/navigation/updatepriorityqueue.h)
8+
9+
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g")
610

711

812

@@ -11,6 +15,7 @@ find_package(catkin REQUIRED COMPONENTS
1115
roscpp
1216
sensor_msgs
1317
std_msgs
18+
nav_msgs
1419
)
1520

1621

@@ -21,7 +26,7 @@ find_package(catkin REQUIRED COMPONENTS
2126
catkin_package(
2227
INCLUDE_DIRS include
2328
LIBRARIES navigation
24-
CATKIN_DEPENDS geometry_msgs roscpp sensor_msgs std_msgs
29+
CATKIN_DEPENDS geometry_msgs roscpp sensor_msgs std_msgs nav_msgs
2530
DEPENDS system_lib
2631
)
2732

ros_ws/src/navigation/include/navigation/astar.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
#ifndef ASTAR_H_
1010
#define ASTAR_H_
1111

12+
#include <nav_msgs/OccupancyGrid.h>
1213
#include "updatepriorityqueue.h"
1314
#include "typewkey.h"
15+
#include <vector>
1416

1517
template<typename typekey, typename Heuristic>
16-
std::vector<std::pair<size_t, size_t>> Astar(occupancyGrid[],
18+
std::vector<std::pair<size_t, size_t>> Astar(nav_msgs::OccupancyGrid o_grid,
1719
TypeWkey<typekey> start, TypeWkey<typekey> goal, const Heuristic& heuristic);
1820
template<typename typekey>
1921
const std::vector<std::pair<size_t, size_t>>& reconstruct_path(
2022
std::vector<std::pair<size_t, size_t>>& moves,
21-
const unordered_map<TypeWkey<typekey>, TypeWkey<typekey> >& parents,
23+
const unordered_map<typekey, typekey >& parents,
2224
const TypeWkey<typekey>& current, size_t width, size_t hieght);
2325

2426
#include "astar.hpp"

ros_ws/src/navigation/include/navigation/astar.hpp

+37-23
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88

99

1010
template<typename typekey, typename Heuristic>
11-
std::vector<std::pair<size_t, size_t>> Astar(occupancyGrid[],
11+
std::vector<std::pair<size_t, size_t>> Astar(nav_msgs::OccupancyGrid o_grid,
1212
TypeWkey<typekey> start, TypeWkey<typekey> goal, const Heuristic& heuristic)
1313
{
14+
std::cout << "Starting Astar" << std::endl;
1415
//set of moves to be returned
1516
std::vector<std::pair<size_t, size_t>> moves;
1617
//occupancy grid width
17-
size_t width = occupancyGrid.width();
18+
size_t width = o_grid.info.width;
1819
//occupancy grid height
19-
size_t height = occupancyGrid.height();
20+
size_t height = o_grid.info.height;
2021
//scale cost of a single cardinal move
2122
const double scale = 1;
2223
//path cost for start
@@ -26,9 +27,9 @@ std::vector<std::pair<size_t, size_t>> Astar(occupancyGrid[],
2627
//open priority queue, with removal and updating
2728
UpdatePriorityQueue<TypeWkey<typekey>, size_t> openPQ;
2829
//closed set map, maps the key for the type and the f cost for that type
29-
unordered_map<TypeWkey<typekey>, f_cost> closedSet;
30+
unordered_map<typekey, f_cost> closedSet;
3031
//map that maps parent with given key index
31-
unordered_map<TypeWkey<typekey>, TypeWkey<typekey> > parents;
32+
unordered_map<typekey, typekey > parents;
3233
// parents.insert(std::make_pair(start, start)); //parents[start] = undefined, we set the parent as itself
3334
//inserts the first item
3435
openPQ.insert(start, start_g, start_h); //openQ.add(start, f_score[start])
@@ -39,24 +40,30 @@ std::vector<std::pair<size_t, size_t>> Astar(occupancyGrid[],
3940
TypeWkey<typekey> v;
4041
//set of adjacent moves to u
4142
std::vector<std::pair<TypeWkey<typekey>, g_cost> > adj_moves;
42-
43+
//std::cout << "HOLA" <<std::endl;
4344
//while the priority queue is not empty
4445
while (openPQ.size())
4546
{
47+
//std::cout << "While Priority Queue is Not Empty, PriorityQueue size " << openPQ.size() << std::endl;
4648
//total score, g_score + h_score for u
4749
f_cost top_score = openPQ.getTotalVal(openPQ.top());
50+
// std::cout << "we got the top score " << top_score << endl;
4851
//poping off u from the priority queue
4952
u = openPQ.pop(); // Source node in first case
53+
// std::cout << "popped " << top_score << endl;
5054
if (u == goal)
5155
{
5256
//break and return current move list;
53-
return reconstruct_path(moves, parents, u, width, height);
57+
return reconstruct_path<typekey>(moves, parents, u, width, height);
5458
}
59+
//std::cout << "not goal yet " << std::endl;
5560
//if it isn't the goal we move the position to the closed set
56-
closedSet.insert(std::make_pair(u, top_score));
61+
closedSet.insert(std::make_pair(u.getKey(), top_score));
5762

63+
// std::cout << "Inserting done " << std::endl;
5864
//clear previous adjacent moves
5965
adj_moves.clear();
66+
// std::cout << "clearing moves " << std::endl;
6067
//NORTH
6168
typekey temp_key = u.getKey();
6269
//Cardinal direction keys
@@ -76,94 +83,101 @@ std::vector<std::pair<size_t, size_t>> Astar(occupancyGrid[],
7683
//north lookup
7784
if (temp_key >= width)
7885
{
79-
adj_moves.push_back(TypeWkey<typekey>(north_key), sdist);
86+
adj_moves.push_back(std::make_pair(TypeWkey<typekey>(north_key), sdist));
8087
//north west lookup
8188
if (temp_key % width > 0)
8289
{
83-
adj_moves.push_back(TypeWkey<typekey>(north_w_key), ddist);
90+
adj_moves.push_back(std::make_pair(TypeWkey<typekey>(north_w_key), ddist));
8491
}
8592
//north east lookup
8693
if (temp_key % width < (width - 1))
8794
{
88-
adj_moves.push_back(TypeWkey<typekey>(north_e_key), ddist);
95+
adj_moves.push_back(std::make_pair(TypeWkey<typekey>(north_e_key), ddist));
8996
}
9097
}
9198
//south
9299
if (temp_key <= (width - 1) * (height))
93100
{
94-
adj_moves.push_back(TypeWkey<typekey>(south_key), sdist);
101+
adj_moves.push_back(std::make_pair(TypeWkey<typekey>(south_key), sdist));
95102
//south west lookup
96103
if (temp_key % width > 0)
97104
{
98-
adj_moves.push_back(TypeWkey<typekey>(south_w_key), ddist);
105+
adj_moves.push_back(std::make_pair(TypeWkey<typekey>(south_w_key), ddist));
99106
}
100107
//south east lookup
101108
if (temp_key % width < (width - 1))
102109
{
103-
adj_moves.push_back(TypeWkey<typekey>(south_e_key), ddist);
110+
adj_moves.push_back(std::make_pair(TypeWkey<typekey>(south_e_key), ddist));
104111
}
105112
}
106113

107114
// west lookup
108115
if (temp_key % width > 0)
109116
{
110-
adj_moves.push_back(TypeWkey<typekey>(west_key), sdist);
117+
adj_moves.push_back(std::make_pair(TypeWkey<typekey>(west_key), sdist));
111118
}
112119
// east lookup
113120
if (temp_key % width < (width - 1))
114121
{
115-
adj_moves.push_back(TypeWkey<typekey>(east_key), sdist);
122+
adj_moves.push_back(std::make_pair(TypeWkey<typekey>(east_key), sdist));
116123
}
117124

125+
//std::cout << "Done generating Adjacency Moves" << std::endl;
118126
//going through all possible adjacent moves
119127
for (size_t i = 0; i < adj_moves.size(); i++) // where v is still in PQ.
120128
{
121129
//adjacent move
122130
v = adj_moves[i].first;
131+
//std::cout << "Going through adjacent moves " << i << std::endl;
123132
//if it is inside the closed set we can ignore it. Otherwise continue
124133
if (closedSet.find(v.getKey()) == closedSet.end())
125134
{
126135
//if the probability of blockage in the probability grid is below 2% (1%, 0%, -1 (not explored)%)
127-
if (occupancyGrid[v.getkey()] < 2)
136+
if (o_grid.data[v.getKey()] < 2)
128137
{
129138
//path cost of v
130139
g_cost path_cost = adj_moves[i].second; // determined by edge length and weight
131140
//heuristic value of v
132-
h_cost h_val = hueristic(v.getKey(), goal);
141+
h_cost h_val = heuristic(v.getKey(), goal);
133142

134143
//if it wasn't already in the openset
135144
if (!openPQ.find(v))
136145
{
137146
//we insert it
147+
parents.insert(std::make_pair(v.getKey(), u.getKey()));
138148
openPQ.insert(v, path_cost, h_val);
139149
}
140150
//if it was already in the openset, and we found that this path is shorter
141151
//than the one already stored
142152
else if (path_cost < openPQ.getCostVal(v)) // A shorter path to v has been found
143153
{
144154
//inserting parent (where v is the child and u is the parent)
145-
parents.insert(v, u);
155+
parents.insert(std::make_pair(v.getKey(), u.getKey()));
146156
//update the value
147157
openPQ.update(v, path_cost, h_val);
148158
}
149159
}
150160
}
151161
}
162+
//std::cout << "Done assessing adjacent " << std::endl;
152163
}
153164
return moves;
154165
}
155166

156167
template<typename typekey>
157168
const std::vector<std::pair<size_t, size_t>>& reconstruct_path(
158169
std::vector<std::pair<size_t, size_t>>& moves,
159-
const unordered_map<TypeWkey<typekey>, TypeWkey<typekey> >& parents,
170+
const unordered_map<typekey ,typekey >& parents,
160171
const TypeWkey<typekey>& current, size_t width, size_t hieght)
161172
{
173+
TypeWkey<typekey> path_current = current;
174+
std::cout << "finding parents" << std::endl;
162175
do
163176
{
164-
moves.push_back(current.getKey() % width, (current.getKey() / width));
165-
current = parents.at(current.getKey());
166-
} while (parents.find(current) != parents.end());
177+
178+
moves.push_back(std::make_pair(path_current.getKey() % width, (path_current.getKey() / width)));
179+
path_current = parents.at(path_current.getKey());
180+
} while (parents.find(path_current.getKey()) != parents.end());
167181
return moves;
168182
}
169183

ros_ws/src/navigation/include/navigation/euclidiandistancehueristic.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#define EUCLIDIANDISTANCEHUERISTIC_H_
1111

1212
#include "updatepriorityqueue.h"
13+
#include "typewkey.h"
1314
#include <math.h>
15+
#include <cstdlib>
1416

1517
class EuclidianDistanceHueristic
1618
{
@@ -26,7 +28,6 @@ class EuclidianDistanceHueristic
2628
}
2729
h_cost operator ()(const TypeWkey<size_t>& start,
2830
const TypeWkey<size_t>& goal) const;
29-
virtual ~EuclidianDistanceHueristic();
3031
};
3132

3233
#endif /* EUCLIDIANDISTANCEHUERISTIC_H_ */

ros_ws/src/navigation/include/navigation/minheap.h

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <utility>
1313
#include <iostream>
14+
#include <unordered_map>
1415
using namespace std;
1516

1617
template<typename T, typename comp_T>

ros_ws/src/navigation/include/navigation/minheap.hpp

+4-15
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ size_t MinHeap<T, comp_T>::heapifyUp(size_t i, unordered_map<T, size_t>& m_id)
100100
//std::cout << "swapping at i = " << i << " and j = " << j << std::endl;
101101
//std::cout << m_heap[i] <<" , " << m_heap[j] << std::endl;
102102
std::swap(m_heap[i], m_heap[j]);
103+
//std::cout << "i " << std::endl;
104+
//std::cout << m_id.at(m_heap[i]) << std::endl;
105+
//std::cout << "j " << std::endl;
106+
//std::cout << m_id.at(m_heap[j]) << std::endl;
103107
std::swap(m_id.at(m_heap[i]), m_id.at(m_heap[j]));
104108
//std::cout << m_heap[i] <<" , " << m_heap[j] << std::endl;
105109
i = j;
@@ -417,18 +421,3 @@ ostream& operator <<(ostream& os, const MinHeap<T, comp_T>& rhs)
417421
return os;
418422
}
419423

420-
size_t parent(size_t i)
421-
{
422-
423-
return (i ? (i - 1) : i) / 2;
424-
}
425-
426-
size_t leftChild(size_t i)
427-
{
428-
return (i * 2) + 1;
429-
}
430-
431-
size_t rightChild(size_t i)
432-
{
433-
return (i * 2) + 2;
434-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @file navigation.h
3+
* @author shae, CS5201 Section A
4+
* @date Apr 19, 2016
5+
* @brief Description:
6+
* @details Details:
7+
*/
8+
9+
#ifndef NAVIGATION_INCLUDE_NAVIGATION_NAVIGATION_H_
10+
#define NAVIGATION_INCLUDE_NAVIGATION_NAVIGATION_H_
11+
#include <ros/ros.h>
12+
#include <nav_msgs/OccupancyGrid.h>
13+
#include "astar.h"
14+
#include "euclidiandistancehueristic.h"
15+
16+
class Navigation
17+
{
18+
private:
19+
ros::NodeHandle nh;
20+
ros::Subscriber omap_sub;
21+
nav_msgs::OccupancyGrid m_occupancy_grid;
22+
23+
void oMapCallback(const nav_msgs::OccupancyGrid::ConstPtr& occupancy_grid);
24+
public:
25+
Navigation();
26+
void update();
27+
};
28+
29+
30+
#endif /* NAVIGATION_INCLUDE_NAVIGATION_NAVIGATION_H_ */

ros_ws/src/navigation/include/navigation/typewkey.h

+14
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,24 @@ struct TypeWkey
4040
return *this;
4141
}
4242

43+
4344
int getKey() const
4445
{
4546
return m_data;
4647
}
4748
};
4849

50+
template <typename T>
51+
bool operator == (const TypeWkey<T>& lhs, const TypeWkey<T>& rhs)
52+
{
53+
return lhs.getKey() == rhs.getKey();
54+
}
55+
56+
template <typename T>
57+
bool operator != (const TypeWkey<T>& lhs, const TypeWkey<T>& rhs)
58+
{
59+
return !(lhs == rhs);
60+
}
61+
62+
4963
#endif /* TYPEWKEY_H_ */

ros_ws/src/navigation/include/navigation/updatepriorityqueue.hpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ void UpdatePriorityQueue<T, K>::insert(const T& item, const g_cost& cost,
6767

6868
m_h.insert(make_pair(key, h));
6969
//(*m_h.find(key)).second = h;
70-
70+
m_id.insert(make_pair(key, size() - 1));
7171
size_t heap_index = m_min_heap.insert(key, m_id);
72-
m_id.insert(make_pair(key, heap_index));
72+
m_id[key] = heap_index;
7373
//cout << "H" << hueristic << endl;
7474
//cout << "m_g" << m_g[key] << endl;
7575
//cout << "m_g" << (*m_g.find(key)).second << endl;
@@ -193,7 +193,7 @@ template<typename T, typename K>
193193
bool UpdatePriorityQueue<T, K>::find(const T& item) const
194194
{
195195
K key = item.getKey();
196-
std::unordered_map<K, size_t>::const_iterator got = m_id.find(key);
196+
typename std::unordered_map<K, size_t>::const_iterator got = m_id.find(key);
197197

198198
return (got != m_id.end()) ? true : false;
199199
}
@@ -239,20 +239,20 @@ bool UpdatePriorityQueue<T, K>::valid_heap()
239239
K key_i = m_min_heap[i];
240240
K key_j = m_min_heap[j];
241241

242-
std::cout << m_min_heap.useCompareObject(j, i) << std::endl;
243-
std::cout << "comparing i position " << i << " with j position " << j << std::endl;
244-
std::cout << "comparing i key " << m_min_heap[i] << " with j key"
245-
<< m_min_heap[j] << std::endl;
246-
std::cout << "comparing h[i] " << m_h.at(key_i) << " with h[j] " << m_h.at(key_j)
247-
<< std::endl;
248-
std::cout << "comparing g[i] " << m_g.at(key_i) << " with g[j] " << m_g.at(key_j)
249-
<< std::endl;
250-
std::cout << "comparing f[i] " << m_g.at(key_i) + m_h.at(key_i) << " with f[j] "
251-
<< m_g.at(key_j) + m_h.at(key_j) << std::endl;
242+
//std::cout << m_min_heap.useCompareObject(j, i) << std::endl;
243+
//std::cout << "comparing i position " << i << " with j position " << j << std::endl;
244+
//std::cout << "comparing i key " << m_min_heap[i] << " with j key"
245+
// << m_min_heap[j] << std::endl;
246+
//std::cout << "comparing h[i] " << m_h.at(key_i) << " with h[j] " << m_h.at(key_j)
247+
// << std::endl;
248+
//std::cout << "comparing g[i] " << m_g.at(key_i) << " with g[j] " << m_g.at(key_j)
249+
// << std::endl;
250+
//std::cout << "comparing f[i] " << m_g.at(key_i) + m_h.at(key_i) << " with f[j] "
251+
// << m_g.at(key_j) + m_h.at(key_j) << std::endl;
252252
for (size_t k = 0; k < size(); k++)
253253
{
254254
K key_k = m_min_heap[k];
255-
std::cout << m_min_heap[k] << " : " << m_g.at(key_k) + m_h.at(key_k) << std::endl;
255+
//std::cout << m_min_heap[k] << " : " << m_g.at(key_k) + m_h.at(key_k) << std::endl;
256256
}
257257
return false;
258258
}

0 commit comments

Comments
 (0)