forked from ideasxiang/r2auto_nav
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
173 lines (152 loc) · 4.75 KB
/
Copy pathutils.py
File metadata and controls
173 lines (152 loc) · 4.75 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
import rospy
import random
from probabilistic_road_map import *
#from astar import *
from visualization_msgs.msg import Marker
from geometry_msgs.msg import Point
from scipy.interpolate import Rbf
from math import isnan, ceil
import numpy as np
import matplotlib.pyplot as plt
def getPoints(myMap, initialPos, goalPos):
initialPosXInMap = int((initialPos[0] - myMap.originX) * 1/myMap.resolution)
initialPosYInMap = int((initialPos[1] - myMap.originY) * 1/myMap.resolution)
initialPosInMap = [initialPosXInMap, initialPosYInMap]
goalPosXInMap = int((goalPos[0] - myMap.originX) * 1/myMap.resolution)
goalPosYInMap = int((goalPos[1] - myMap.originY) * 1/myMap.resolution)
goalPosInMap = [goalPosXInMap, goalPosYInMap]
#Note: Map x-axis increases from bottom to top and y-axis increases from left to right
#Need to align map to coordinates by rotating it 90 degrees clockwise and then flipping it
#upside down
myGrid = np.flipud(np.rot90(np.array(myMap.grid)))
myGridOrg = np.copy(myGrid)
myGridOrg[initialPosXInMap,initialPosYInMap] = 150
myGridOrg[goalPosXInMap,goalPosYInMap] = 50
sx = initialPosXInMap
sy = initialPosYInMap
gx = goalPosXInMap
gy = goalPosYInMap
robot_size = 1
ox = list(map(int, np.where(myGrid == 100.0)[0].tolist()))
oy = list(map(int, np.where(myGrid == 100.0)[1].tolist()))
if sx in ox and sy in oy:
rospy.loginfo("REMOVING ROBOT POSITION FROM OBSTACLE LIST")
ox.remove(sx)
oy.remove(sy)
attempts = 0
while attempts < 10:
rx, ry = PRM_planning(sx, sy, gx, gy, ox, oy, robot_size)
candidates = [list(pos) for pos in zip(rx, ry)]
if not candidates:
attempts += 1
rospy.loginfo("COULDN'T FIND ANY PATH AFTER " + str(attempts) + " ATTEMPTS! TRYING AGAIN")
else: break
rospy.sleep(0.01)
if not candidates:
rospy.loginfo("COULDN'T FIND ANY PATH!")
return []
else:
#rospy.loginfo("ASTAR COMPLETE!")
rospy.loginfo("PRM COMPLETE!")
for cand in candidates:
myGridOrg[int(cand[0]),int(cand[1])] = 200
myGridOrg = np.rot90(np.flipud(myGridOrg), axes=(1,0))
fig = plt.figure(figsize=(6, 3.2))
ax = fig.add_subplot(111)
ax.set_title('colorMap')
plt.imshow(myGridOrg)
ax.set_aspect('equal')
cax = fig.add_axes([0.12, 0.1, 0.78, 0.8])
cax.get_xaxis().set_visible(False)
cax.get_yaxis().set_visible(False)
cax.patch.set_alpha(0)
cax.set_frame_on(False)
plt.colorbar(orientation='vertical')
plt.show()
xVals = []
yVals = []
#convert back to simulation coordinates
for candidate in candidates:
xVals.append(int(candidate[0]) * myMap.resolution + myMap.originX)
yVals.append(int(candidate[1]) * myMap.resolution + myMap.originY)
realPts = [list(pos) for pos in zip(xVals, yVals)]
return realPts[::-1]
z = np.polyfit(np.array(xVals), np.array(yVals), 5)
p = np.poly1d(z)
if xVals[0] < xVals[-1]:
xNewVals = np.arange(xVals[0], xVals[-1], 0.3)
else:
xNewVals = np.arange(xVals[0], xVals[-1], -0.3)
np.append(xNewVals, xVals[-1])
yNewVals = []
for xNewVal in xNewVals:
yNewVals.append(p(xNewVal))
realPts = [list(pos) for pos in zip(xNewVals, yNewVals)]
rospy.sleep(0.01)
return realPts[::-1]
def createPoint(pt, markerID):
marker = Marker()
marker.header.frame_id = "/map"
marker.type = marker.SPHERE
marker.action = marker.ADD
marker.ns = "robot"
marker.header.stamp = rospy.get_rostime()
marker.id = markerID
# marker scale
marker.scale.x = 0.1
marker.scale.y = 0.1
marker.scale.z = 0.1
# marker color
marker.color.a = 1.0
marker.color.r = 0.0
marker.color.g = 1.0
marker.color.b = 1.0
# marker orientaiton
marker.pose.orientation.x = 0.0
marker.pose.orientation.y = 0.0
marker.pose.orientation.z = 0.0
marker.pose.orientation.w = 1.0
# marker position
marker.pose.position.x = pt[0]
marker.pose.position.y = pt[1]
marker.pose.position.z = 0.0
return marker
def createLine(pt1, pt2, markerID):
marker = Marker()
marker.header.frame_id = "/map"
marker.type = marker.LINE_STRIP
marker.action = marker.ADD
marker.id = markerID
# marker scale
marker.scale.x = 0.03
marker.scale.y = 0.03
marker.scale.z = 0.03
# marker color
marker.color.a = 1.0
marker.color.r = 1.0
marker.color.g = 1.0
marker.color.b = 0.0
# marker orientaiton
marker.pose.orientation.x = 0.0
marker.pose.orientation.y = 0.0
marker.pose.orientation.z = 0.0
marker.pose.orientation.w = 1.0
# marker position
marker.pose.position.x = 0.0
marker.pose.position.y = 0.0
marker.pose.position.z = 0.0
# marker line points
marker.points = []
# first point
first_line_point = Point()
first_line_point.x = pt1[0]
first_line_point.y = pt1[1]
first_line_point.z = 0.0
marker.points.append(first_line_point)
# second point
second_line_point = Point()
second_line_point.x = pt2[0]
second_line_point.y = pt2[1]
second_line_point.z = 0.0
marker.points.append(second_line_point)
return marker