-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
152 lines (109 loc) · 3.61 KB
/
utils.py
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
import numpy as np
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 6))
# Bresenhams Line Generation Algorithm
# ref: https://www.geeksforgeeks.org/bresenhams-line-generation-algorithm/
def bresenham(x1, y1, x2, y2, w, h):
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
dx = abs(x2 - x1)
dy = abs(y2 - y1)
steep = 0
if dx <= dy:
steep = 1
x1, y1 = y1, x1
x2, y2 = y2, x2
dx, dy = dy, dx
pk = 2 * dy - dx
loc = []
for _ in range(0, dx + 1):
if (x1 < 0 or y1 < 0) or (steep == 0 and (x1 >= h or y1 >= w)) or (steep == 1 and (x1 >= w or y1 >= h)):
break
if steep == 0:
loc.append([x1, y1])
else:
loc.append([y1, x1])
if x1 < x2:
x1 = x1 + 1
else:
x1 = x1 - 1
if (pk < 0):
if steep == 0:
pk = pk + 2 * dy
else:
pk = pk + 2 * dy
else:
if y1 < y2:
y1 = y1 + 1
else:
y1 = y1 - 1
pk = pk + 2 * dy - 2 * dx
return loc
def wrapAngle(radian):
radian = radian - 2 * np.pi * np.floor((radian + np.pi) / (2 * np.pi))
return radian
def degree2radian(degree):
return degree / 180 * np.pi
def prob2logodds(prob):
return np.log(prob / (1 - prob + 1e-15))
def logodds2prob(logodds):
return 1 - 1 / (1 + np.exp(logodds) + 1e-15)
def normalDistribution(mean, variance):
return np.exp(-(np.power(mean, 2) / variance / 2.0) / np.sqrt(2.0 * np.pi * variance))
def create_rotation_matrix(theta):
R = np.array([
[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]
])
R_inv = np.linalg.inv(R)
return R, R_inv
def absolute2relative(position, states):
x, y, theta = states
pose = np.array([x, y])
R, R_inv = create_rotation_matrix(theta)
position = position - pose
position = np.array(position) @ R_inv.T
return position
def relative2absolute(position, states):
x, y, theta = states
pose = np.array([x, y])
R, R_inv = create_rotation_matrix(theta)
position = np.array(position) @ R.T
position = position + pose
return position
def visualize(robot, particles, best_particle, radar_list, step, title, output_path, visualize=False):
ax1.clear()
ax2.clear()
fig.suptitle("{}\n\n number of particles:{}, step:{}".format(title, len(particles), step + 1))
ax1.set_title("Estimated by Particles")
ax2.set_title("Ground Truth")
ax1.axis("off")
ax2.axis("off")
grid_size = best_particle.grid_size
ax1.set_xlim(0, grid_size[1])
ax1.set_ylim(0, grid_size[0])
grid_size = robot.grid_size
ax2.set_xlim(0, grid_size[1])
ax2.set_ylim(0, grid_size[0])
# draw map
world_map = 1 - best_particle.grid
ax1.imshow(world_map, cmap='gray')
world_map = 1 - robot.grid
ax2.imshow(world_map, cmap='gray')
# draw radar beams
for (x, y) in radar_list:
ax2.plot(x, y, "yo", markersize=1)
# draw tragectory
true_path = np.array(robot.trajectory)
ax2.plot(true_path[:, 0], true_path[:, 1], "b")
estimated_path = np.array(best_particle.trajectory)
ax1.plot(estimated_path[:, 0], estimated_path[:, 1], "g")
# draw particles position
for p in particles:
ax1.plot(p.x, p.y, "go", markersize=1)
# draw robot position
ax2.plot(robot.x, robot.y, "bo")
if step % 10 == 0:
plt.savefig('{}_{}.png'.format(output_path, step), bbox_inches='tight')
if visualize:
plt.draw()
plt.pause(0.01)