Skip to content

Commit 78d82c1

Browse files
committed
choosing when to update dt.
choosing when to update particle update function. choosing when to update resampling function. tuning model covariance parameters.
1 parent ba1510f commit 78d82c1

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

package.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<package format="2">
3-
<name>slam_mcmc</name>
3+
<name>MCL_PI</name>
44
<version>0.0.0</version>
55
<description>The slam_mcmc package</description>
66

particle_filter/particle_filter.py

+30-25
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,42 @@
1717

1818
class ParticleFilter(object):
1919

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+
2323
self.pub_particlecloud = rospy.Publisher('/particlecloud', PoseArray, queue_size = 60)
2424
self.pub_particlecloud2fusion = rospy.Publisher('/particlecloud2fuse_out', PoseArray, queue_size = 60)
2525
self.scan = MapClientLaserScanSubscriber ()
26-
self.time_temp = 0.0
26+
self.last_time = rospy.Time.now().to_sec()
2727
self.Np = Np
2828
self.init()
2929
self.i_TH = 0.0
30-
self.dt = 0.0
3130
self.nbrs = KNN(n_neighbors=1, algorithm='ball_tree').fit(self.scan.obs())
3231
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+
3335
#print self.M_idxs
3436
#print self.scan.obs()
3537
#print self.nbrs, "print"
3638

3739
def get_odom(self, msg): # callback function for odom topic
3840
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+
3956

4057
def init_pose (self,msg): # callback function for /initialpose topic
4158
X = np.zeros(3)
@@ -64,18 +81,15 @@ def prediction (self): #Odometry = Odometry massege. donot forget to initialize
6481
dot[:,1] = self.odom.twist.twist.linear.y
6582
dot[:,2] = self.odom.twist.twist.angular.z
6683

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
7087

7188
#self.x_pose_cov = self.odom.pose.covariance[0] ############
7289
#self.y_pose_cov = self.odom.pose.covariance[7] ###########
7390
#self.theta_pose_cov = self.odom.pose.covariance[35] #########
7491

75-
current_time = self.odom.header.stamp.secs
7692
delta = np.zeros((self.Np,3))
77-
78-
self.dt = int(current_time) - int(self.time_temp)
7993

8094
delta[:,2] = dot[:,2] * self.dt + sigma_theta * np.random.randn(self.Np)
8195

@@ -85,13 +99,11 @@ def prediction (self): #Odometry = Odometry massege. donot forget to initialize
8599
delta[:,1] = (dot[:,0] + sigma_y * np.random.randn(self.Np)) * self.dt * np.sin(self.particles[:,2])
86100

87101
self.particles[:,0] += delta[:,0]
88-
self.particles[:,1] += delta[:,1]
89-
90-
self.time_temp = current_time
102+
self.particles[:,1] += delta[:,1]
91103

92104
def update_TH(self):
93105
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)
95107

96108
def likelihood_fild(self):
97109
print "likelihood"
@@ -111,7 +123,7 @@ def likelihood_fild(self):
111123
#print np.abs(ob-z_star[jj,:])
112124
# z[jj,:]= ob[np.argmin(np.abs(ob-z_star[jj,:])),:]
113125
#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))
115127
self.weights = self.weights / np.sum(self.weights)
116128

117129

@@ -133,7 +145,7 @@ def resampling(self):
133145
self.weights = np.ones (self.Np) / self.Np
134146
self.particles[:,0] += 0.01 * np.random.randn(self.Np)
135147
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)
137149

138150
def pub (self):
139151
particle_pose = PoseArray()
@@ -182,14 +194,7 @@ def pub2fuse (self):
182194
while not rospy.is_shutdown():
183195
r.sleep()
184196

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+
193198
PF_l.pub()
194199
#mean = np.zeros(3)
195200
#mean = np.mean(PF_l.particles,axis=0)

0 commit comments

Comments
 (0)