17
17
18
18
class ParticleFilter (object ):
19
19
20
- def __init__ (self ,Np = 100 ):
21
- rospy . Subscriber ( '/odom' , Odometry , self .get_odom )
22
- rospy . Subscriber ( '/initialpose' , PoseWithCovarianceStamped , self . init_pose )
20
+ def __init__ (self ,Np = 200 ):
21
+ self .ctr = 1
22
+
23
23
self .pub_particlecloud = rospy .Publisher ('/particlecloud' , PoseArray , queue_size = 60 )
24
24
self .pub_particlecloud2fusion = rospy .Publisher ('/particlecloud2fuse_out' , PoseArray , queue_size = 60 )
25
25
self .scan = MapClientLaserScanSubscriber ()
26
- self .time_temp = 0.0
26
+ self .last_time = rospy . Time . now (). to_sec ()
27
27
self .Np = Np
28
28
self .init ()
29
29
self .i_TH = 0.0
30
- self .dt = 0.0
31
30
self .nbrs = KNN (n_neighbors = 1 , algorithm = 'ball_tree' ).fit (self .scan .obs ())
32
31
self .M_idxs = (np .linspace (0 ,len (self .scan .z .ranges )- 1 ,20 )).astype (np .int32 )
32
+ rospy .Subscriber ('/odom' , Odometry , self .get_odom )
33
+ rospy .Subscriber ('/initialpose' , PoseWithCovarianceStamped , self .init_pose )
34
+
33
35
#print self.M_idxs
34
36
#print self.scan.obs()
35
37
#print self.nbrs, "print"
36
38
37
39
def get_odom (self , msg ): # callback function for odom topic
38
40
self .odom = msg
41
+ current_time = msg .header .stamp .secs
42
+ self .dt = current_time - self .last_time
43
+ self .last_time = current_time
44
+ self .prediction ()
45
+ if self .update_TH () > 0.1 and self .ctr % 2 == 0 :
46
+ self .likelihood_fild ()
47
+ self .i_TH = 0.0
48
+ self .ctr = 1
49
+ if 1 / np .sum (self .weights ** 2 ) < self .Np / 10 :
50
+ self .resampling ()
51
+
52
+ self .ctr += 1
53
+
54
+
55
+
39
56
40
57
def init_pose (self ,msg ): # callback function for /initialpose topic
41
58
X = np .zeros (3 )
@@ -64,18 +81,15 @@ def prediction (self): #Odometry = Odometry massege. donot forget to initialize
64
81
dot [:,1 ] = self .odom .twist .twist .linear .y
65
82
dot [:,2 ] = self .odom .twist .twist .angular .z
66
83
67
- sigma_x = np .sqrt (self .odom .twist .covariance [0 ])
68
- sigma_y = np .sqrt (self .odom .twist .covariance [7 ])
69
- sigma_theta = np .sqrt (self .odom .twist .covariance [35 ])
84
+ sigma_x = np .sqrt (self .odom .twist .covariance [0 ]) + 0.01
85
+ sigma_y = np .sqrt (self .odom .twist .covariance [7 ]) + 0.01
86
+ sigma_theta = np .sqrt (self .odom .twist .covariance [35 ]) + 0.01
70
87
71
88
#self.x_pose_cov = self.odom.pose.covariance[0] ############
72
89
#self.y_pose_cov = self.odom.pose.covariance[7] ###########
73
90
#self.theta_pose_cov = self.odom.pose.covariance[35] #########
74
91
75
- current_time = self .odom .header .stamp .secs
76
92
delta = np .zeros ((self .Np ,3 ))
77
-
78
- self .dt = int (current_time ) - int (self .time_temp )
79
93
80
94
delta [:,2 ] = dot [:,2 ] * self .dt + sigma_theta * np .random .randn (self .Np )
81
95
@@ -85,13 +99,11 @@ def prediction (self): #Odometry = Odometry massege. donot forget to initialize
85
99
delta [:,1 ] = (dot [:,0 ] + sigma_y * np .random .randn (self .Np )) * self .dt * np .sin (self .particles [:,2 ])
86
100
87
101
self .particles [:,0 ] += delta [:,0 ]
88
- self .particles [:,1 ] += delta [:,1 ]
89
-
90
- self .time_temp = current_time
102
+ self .particles [:,1 ] += delta [:,1 ]
91
103
92
104
def update_TH (self ):
93
105
self .i_TH += (self .odom .twist .twist .linear .x + self .odom .twist .twist .angular .z ) * self .dt
94
- return self .i_TH
106
+ return np . abs ( self .i_TH )
95
107
96
108
def likelihood_fild (self ):
97
109
print "likelihood"
@@ -111,7 +123,7 @@ def likelihood_fild(self):
111
123
#print np.abs(ob-z_star[jj,:])
112
124
# z[jj,:]= ob[np.argmin(np.abs(ob-z_star[jj,:])),:]
113
125
#print z.shape,z_star.shape
114
- self .weights [ii ] = np .prod (np .exp (- (0.2 )* np .linalg .norm (z_star - z ,axis = 1 )** 2 ))
126
+ self .weights [ii ] = self . weights [ ii ] * np .prod (np .exp (- (0.4 )* np .linalg .norm (z_star - z ,axis = 1 )** 2 ))
115
127
self .weights = self .weights / np .sum (self .weights )
116
128
117
129
@@ -133,7 +145,7 @@ def resampling(self):
133
145
self .weights = np .ones (self .Np ) / self .Np
134
146
self .particles [:,0 ] += 0.01 * np .random .randn (self .Np )
135
147
self .particles [:,1 ] += 0.01 * np .random .randn (self .Np )
136
- self .particles [:,2 ] += 0.02 * np .random .randn (self .Np )
148
+ self .particles [:,2 ] += 0.03 * np .random .randn (self .Np )
137
149
138
150
def pub (self ):
139
151
particle_pose = PoseArray ()
@@ -182,14 +194,7 @@ def pub2fuse (self):
182
194
while not rospy .is_shutdown ():
183
195
r .sleep ()
184
196
185
- PF_l .prediction ()
186
- PF_l .likelihood_fild ()
187
- if np .abs (PF_l .update_TH ()) > 0.1 :
188
-
189
- #PF_l.simpel_likelihood()
190
- PF_l .resampling ()
191
- PF_l .i_TH = 0.0
192
- print 'Updating particles...'
197
+
193
198
PF_l .pub ()
194
199
#mean = np.zeros(3)
195
200
#mean = np.mean(PF_l.particles,axis=0)
0 commit comments