-
Notifications
You must be signed in to change notification settings - Fork 3
/
lslam.py
executable file
·190 lines (175 loc) · 6.86 KB
/
lslam.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
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
#!/usr/bin/python
# coding: UTF-8
from readbag import *
from gui import *
from costmap import *
import tf
import time
##########################
bagfile = 'h1.bag'
scan_topic = 'scan'
odom_topic = 'odom'
start_time = 0
end_time = 800
##########################
image_file_name = 'map1.pgm'
##########################
lidar_angle = 0.
lidar_x = 0.
lidar_y = 0.
##########################
w_x = 0.
w_y = 0.
w_a = 0.
##########################
size =(400,400)
original_point = (100,100)
resolution = 0.025
class LSLAM():
def __init__(self, raw_data, costmap, gui):
self.raw_data = raw_data
self.costmap = costmap
self.gui = gui
#tmp = tf.transformations.euler_matrix(0.0, 0.0, -0.63)
#tmp[0,3] = 0.15
tmp = tf.transformations.euler_matrix(0.0, 0.0, lidar_angle)
tmp[0,3] = lidar_x
tmp[1,3] = lidar_y
self.scan_base = np.matrix(tmp)
self.idx = 0
self.size = len(self.raw_data)
def run(self):
while self.idx < self.size:
time.sleep(0.03)
if self.gui.state == 1:
self.step()
self.idx += 1
elif self.gui.state == 2:
self.gui.state = 0
self.step()
self.idx += 1
elif self.gui.state == 3:
self.gui.state = 0
self.step()
self.idx -= 1
def step(self):
#for loopi in range(len(self.raw_data)):
#time.sleep(0.03)
scan, odom = self.raw_data[self.idx]
scan = np.matrix(scan)
odom = np.matrix(odom)
try:
last_odom = self.last_odom
self.last_odom = odom
except AttributeError:
self.last_odom = odom
self.pose = self.matrix_to_pose(odom)
return
#==================================
#Motion estimation
#==================================
# Calculate the difference between last_odom and odom
last_odom_inv = np.matrix(np.linalg.inv(last_odom))
odom_delta = last_odom_inv * odom
# Calculate the new pose
pose_matrix = self.pose_to_matrix(self.pose)
new_pose_matrix = pose_matrix * odom_delta
#new_pose_matrix = pose_matrix
new_pose = self.matrix_to_pose(new_pose_matrix)
#==================================
#Scan matching
#==================================
map_idx, scan_fix = self.get_scan_in_world_coord(scan, new_pose_matrix)
# Try to do scan matching
scan_delta = self.scan_matching(new_pose, map_idx, scan_fix)
# Update pose by scan matching
self.pose = new_pose + scan_delta
#==================================
#Map Update
#==================================
self.map_update(self.pose, map_idx)
# Set gui
self.costmap.prob_data[0,0] = 0
self.costmap.prob_data[0,1] = 1
x = int(self.pose[0])
y = int(self.pose[1])
gui.setdata(self.costmap.prob_data[y-40:y+41,x-40:x+41], self.costmap.prob_data, self.pose, map_idx)
def pose_to_matrix(self, pose):
pose_matrix = tf.transformations.euler_matrix(0.0, 0.0, pose[2])
pose_matrix[0,3] = (pose[0] - self.costmap.original_point[0])*self.costmap.resolution
pose_matrix[1,3] = (pose[1] - self.costmap.original_point[1])*self.costmap.resolution
return pose_matrix
def matrix_to_pose(self, odom):
al, be, ga = tf.transformations.euler_from_matrix(odom[0:3,0:3])
odompose = np.array([ [ odom[0,3], odom[1,3] ] ])
map_point = costmap.world_map(odompose)
return [map_point[0,0],map_point[0,1],ga]
def scan_matching(self, pose, map_idx, scan):
size = map_idx.shape[0]
sinRot = np.sin(pose[2])
cosRot = np.cos(pose[2])
H = np.matrix(np.zeros((3,3)))
dTr = np.matrix(np.zeros((3,1)))
for i in range(size):
transformedPointData = self.costmap.getMapValueWithDerivatives(map_idx[i,:])
curPoint = scan[i,:] / self.costmap.resolution
funVal = 1.0 - transformedPointData[0]
dTr[0] += transformedPointData[1] * funVal
dTr[1] += transformedPointData[2] * funVal
rotDeriv = ((-sinRot * curPoint[0,0] - cosRot * curPoint[0,1]) * transformedPointData[1] + (cosRot * curPoint[0,0] - sinRot * curPoint[0,1]) * transformedPointData[2])
dTr[2] += rotDeriv * funVal
H[0,0] += transformedPointData[1]*transformedPointData[1]
H[1,1] += transformedPointData[2]*transformedPointData[2]
H[2,2] += rotDeriv*rotDeriv
H[0,1] += transformedPointData[1] * transformedPointData[2]
H[0,2] += transformedPointData[1] * rotDeriv
H[1,2] += transformedPointData[2] * rotDeriv
H[0,0] += w_x
H[1,1] += w_y
H[2,2] += w_a
H[1,0] = H[0,1]
H[2,0] = H[0,2]
H[2,1] = H[1,2]
if H[0, 0] != 0.0 and H[1, 1] != 0.0 and H[2, 2] != 0.0:
daltaPose = np.linalg.inv(H) * dTr
return np.array([daltaPose[0,0]*self.costmap.resolution,daltaPose[1,0]*self.costmap.resolution,daltaPose[2,0]])
else:
return np.zeros( 3 )
def map_update(self, robotpose, map_idx):
for i in range(map_idx.shape[0]):
map_idx_i = map_idx[i,:]
map_idx_i_ = map_idx_i + 0.5
map_idx_i_int = map_idx_i_.astype(int)
use_gaussian = self.gui.checkbox_gaussian.isChecked()
costmap.updateCostMap(map_idx_i_int,0.9, use_gaussian)
costmap.updateLines(robotpose, map_idx_i,-0.1, use_gaussian)
def get_scan_in_world_coord(self, scan, odom):
scan_size = scan.shape[0]
scan_tmp = scan.transpose()
tmp = np.zeros((1, scan_size))
scan_tmp = np.vstack([scan_tmp, tmp])
tmp.fill(1)
scan_tmp = np.vstack([scan_tmp, tmp])
scan_tmp = np.matrix(scan_tmp)
scan_fix = self.scan_base*scan_tmp
world_idx = odom*scan_fix
world_idx = np.array(world_idx[0:2,:]).transpose()
map_idx = costmap.world_map(world_idx)
scan_fix = scan_fix.transpose()[:,0:2]
return map_idx, scan_fix
#bagreader = BagReader('h1.bag', 'scan', 'odom', 0, 800)
bagreader = BagReader(bagfile, scan_topic, odom_topic, start_time, end_time)
#bagreader = BagReader('/home/liu/bag/test_range.bag', '/Rulo/laser_scan', '/Rulo/odom', 3, 800)
#bagreader = BagReader('/home/liu/bag/h1.bag', '/Rulo/laser_scan', '/Rulo/odom', 45, 800)
costmap = CostMap(size, original_point, resolution)
gui = LSLAMGUI()
gui.start()
lslam = LSLAM(bagreader.data, costmap, gui)
start = time.time()
lslam.run()
import Image
a = np.flip(costmap.prob_data,0)
img = Image.fromarray(np.uint8(255-a*255))
img.save(image_file_name)
elapsed_time = time.time() - start
print ("elapsed_time:{0}".format(elapsed_time)) + "[sec]"