66
77"""
88
9+ # MAX_RANGE=20;%最大観測距離
10+ # NP=100;%パーティクル数
11+ # NTh=NP/2.0;%リサンプリングを実施する有効パーティクル数
12+
13+ # px=repmat(xEst,1,NP);%パーティクル格納変数
14+ # pw=zeros(1,NP)+1/NP;%重み変数
15+
16+ # tic;
17+ # %movcount=0;
18+ # % Main loop
19+ # for i=1 : nSteps
20+ # time = time + dt;
21+ # % Input
22+ # u=doControl(time);
23+ # % Observation
24+ # [z,xTrue,xd,u]=Observation(xTrue, xd, u, RFID, MAX_RANGE);
25+
26+ # % ------ Particle Filter --------
27+ # for ip=1:NP
28+ # x=px(:,ip);
29+ # w=pw(ip);
30+
31+ # % Dead Reckoning and random sampling
32+ # x=f(x, u)+sqrt(Q)*randn(3,1);
33+
34+ # % Calc Inportance Weight
35+ # for iz=1:length(z(:,1))
36+ # pz=norm(x(1:2)'-z(iz,2:3));
37+ # dz=pz-z(iz,1);
38+ # w=w*Gauss(dz,0,sqrt(R));
39+ # end
40+ # px(:,ip)=x;%格納
41+ # pw(ip)=w;
42+ # end
43+
44+ # pw=Normalize(pw,NP);%正規化
45+ # [px,pw]=Resampling(px,pw,NTh,NP);%リサンプリング
46+ # xEst=px*pw';%最終推定値は期待値
47+
48+ # % Simulation Result
49+ # result.time=[result.time; time];
50+ # result.xTrue=[result.xTrue; xTrue'];
51+ # result.xd=[result.xd; xd'];
52+ # result.xEst=[result.xEst;xEst'];
53+ # result.u=[result.u; u'];
54+
55+ # %Animation (remove some flames)
56+ # if rem(i,5)==0
57+ # hold off;
58+ # arrow=0.5;
59+ # %パーティクル表示
60+ # for ip=1:NP
61+ # quiver(px(1,ip),px(2,ip),arrow*cos(px(3,ip)),arrow*sin(px(3,ip)),'ok');hold on;
62+ # end
63+ # plot(result.xTrue(:,1),result.xTrue(:,2),'.b');hold on;
64+ # plot(RFID(:,1),RFID(:,2),'pk','MarkerSize',10);hold on;
65+ # %観測線の表示
66+ # if~isempty(z)
67+ # for iz=1:length(z(:,1))
68+ # ray=[xTrue(1:2)';z(iz,2:3)];
69+ # plot(ray(:,1),ray(:,2),'-r');hold on;
70+ # end
71+ # end
72+ # plot(result.xd(:,1),result.xd(:,2),'.k');hold on;
73+ # plot(result.xEst(:,1),result.xEst(:,2),'.r');hold on;
74+ # axis equal;
75+ # grid on;
76+ # drawnow;
77+ #
78+ # function [px,pw]=Resampling(px,pw,NTh,NP)
79+ # %リサンプリングを実施する関数
80+ # %アルゴリズムはLow Variance Sampling
81+ # Neff=1.0/(pw*pw');
82+ # if Neff<NTh %リサンプリング
83+ # wcum=cumsum(pw);
84+ # base=cumsum(pw*0+1/NP)-1/NP;%乱数を加える前のbase
85+ # resampleID=base+rand/NP;%ルーレットを乱数分増やす
86+ # ppx=px;%データ格納用
87+ # ind=1;%新しいID
88+ # for ip=1:NP
89+ # while(resampleID(ip)>wcum(ind))
90+ # ind=ind+1;
91+ # end
92+ # px(:,ip)=ppx(:,ind);%LVSで選ばれたパーティクルに置き換え
93+ # pw(ip)=1/NP;%尤度は初期化
94+ # end
95+ # end
96+
97+ # function pw=Normalize(pw,NP)
98+ # %重みベクトルを正規化する関数
99+ # sumw=sum(pw);
100+ # if sumw~=0
101+ # pw=pw/sum(pw);%正規化
102+ # else
103+ # pw=zeros(1,NP)+1/NP;
104+ # end
105+
106+
107+ # function p=Gauss(x,u,sigma)
108+ # %ガウス分布の確率密度を計算する関数
109+ # p=1/sqrt(2*pi*sigma^2)*exp(-(x-u)^2/(2*sigma^2));
110+
111+ # %Calc Observation from noise prameter
112+ # function [z, x, xd, u] = Observation(x, xd, u, RFID,MAX_RANGE)
113+ # global Qsigma;
114+ # global Rsigma;
115+
116+ # x=f(x, u);% Ground Truth
117+ # u=u+sqrt(Qsigma)*randn(2,1);%add Process Noise
118+ # xd=f(xd, u);% Dead Reckoning
119+ # %Simulate Observation
120+ # z=[];
121+ # for iz=1:length(RFID(:,1))
122+ # d=norm(RFID(iz,:)-x(1:2)');
123+ # if d<MAX_RANGE %観測範囲内
124+ # z=[z;[d+sqrt(Rsigma)*randn(1,1) RFID(iz,:)]];
125+ # end
126+
9127import numpy as np
10128import math
11129import matplotlib .pyplot as plt
@@ -102,7 +220,7 @@ def jacobH(x):
102220 return jH
103221
104222
105- def ekf_estimation (xEst , PEst , z , u ):
223+ def pf_estimation (xEst , PEst , z , u ):
106224
107225 # Predict
108226 xPred = motion_model (xEst , u )
@@ -151,6 +269,12 @@ def main():
151269
152270 time = 0.0
153271
272+ # RFID positions [x, y]
273+ RFID = np .array ([[10.0 , 0.0 ],
274+ [10.0 , 10.0 ],
275+ [0.0 , 15.0 ],
276+ [- 5.0 , 20.0 ]])
277+
154278 # State Vector [x y yaw v]'
155279 xEst = np .matrix (np .zeros ((4 , 1 )))
156280 xTrue = np .matrix (np .zeros ((4 , 1 )))
@@ -170,7 +294,7 @@ def main():
170294
171295 xTrue , z , xDR , ud = observation (xTrue , xDR , u )
172296
173- xEst , PEst = ekf_estimation (xEst , PEst , z , ud )
297+ xEst , PEst = pf_estimation (xEst , PEst , z , ud )
174298
175299 # store data history
176300 hxEst = np .hstack ((hxEst , xEst ))
@@ -181,6 +305,7 @@ def main():
181305 if show_animation :
182306 plt .cla ()
183307 plt .plot (hz [:, 0 ], hz [:, 1 ], ".g" )
308+ plt .plot (RFID [:, 0 ], RFID [:, 1 ], "*k" )
184309 plt .plot (np .array (hxTrue [0 , :]).flatten (),
185310 np .array (hxTrue [1 , :]).flatten (), "-b" )
186311 plt .plot (np .array (hxDR [0 , :]).flatten (),
0 commit comments