机器人学习--粒子滤波定位-MATLAB仿真1
生活随笔
收集整理的這篇文章主要介紹了
机器人学习--粒子滤波定位-MATLAB仿真1
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
坐標系下,給定幾個路標 landmarks,機器人行走路線是一個圓,60秒的行走時間,每秒更新一次結果,MATLAB仿真運動更新過程,以及結果的誤差圖顯示。
代碼:
function [] = particle_filter_localization() %PARTICLE_FILTER_LOCALIZATION Summary of this function goes here % Detailed explanation goes here% ------------------------------------------------------------------------- % TASK for particle filter localization % for robotic class in 2018 of ZJU% Preparartion: % 1. you need to know how to code and debug in matlab % 2. understand the theory of Monte Carlo% Then complete the code by YOURSELF! % -------------------------------------------------------------------------close all; clear all;disp('Particle Filter program start!!')%% initialization tic; time = 0; endTime = 60; % second global dt; dt = 0.1; % secondnSteps = ceil((endTime - time)/dt);localizer.time = []; localizer.xEst = []; localizer.xGnd = []; localizer.xOdom = []; localizer.z = []; localizer.PEst=[]; localizer.u=[];% Estimated State [x y yaw]' xEst=[0 0 0]'; % GroundTruth State xGnd = xEst; % Odometry-only = Dead Reckoning xOdom = xGnd;% Covariance Matrix for predict Q=diag([0.1 0.1 toRadian(3)]).^2; % Covariance Matrix for observation R=diag([1]).^2;% range:meter% Simulation parameter global Qsigma Qsigma=diag([0.1 toRadian(5)]).^2; global Rsigma Rsigma=diag([0.1]).^2;% landmark position landMarks=[10 0; 10 10; 0 15; -5 20];% longest observation confined MAX_RANGE=20; % Num of particles, initialized NP=100; % Used in Resampling Step, a threshold NTh=NP/2.0;% particles produced px=repmat(xEst,1,NP); % weights of particles produced pw=zeros(1,NP)+1/NP;errs=[]; % sum of error %% Main Loop for i=1 : nStepstime = time + dt;u=doControl(time);% do observation[z,xGnd,xOdom,u]=doObservation(xGnd, xOdom, u, landMarks, MAX_RANGE);for ip=1:NP% process every particlex=px(:,ip);w=pw(ip);% do motion model and random samplingx=doMotion(x, u)+sqrt(Q)*randn(3,1);% calculate inportance weightfor iz=1:length(z(:,1))pz=norm(x(1:2)'-z(iz,2:3));dz=pz-z(iz,1);w=w*Gaussian(dz,0,sqrt(R));endpx(:,ip)=x;pw(ip)=w;endpw=Normalization(pw,NP);[px,pw]=ResamplingStep(px,pw,NTh,NP);xEst=px*pw';errs=[errs, norm(xGnd(1:2)'-xEst(1:2)')];% Simulation Resultlocalizer.time=[localizer.time; time];localizer.xGnd=[localizer.xGnd; xGnd'];localizer.xOdom=[localizer.xOdom; xOdom'];localizer.xEst=[localizer.xEst;xEst'];localizer.u=[localizer.u; u'];% Animation (remove some flames)if rem(i,10)==0 hold off;arrow=0.5;for ip=1:NPquiver(px(1,ip),px(2,ip),arrow*cos(px(3,ip)),arrow*sin(px(3,ip)),'ok');hold on;endplot(localizer.xGnd(:,1),localizer.xGnd(:,2),'.b');hold on;plot(landMarks(:,1),landMarks(:,2),'pk','MarkerSize',10);hold on;if~isempty(z)for iz=1:length(z(:,1))ray=[xGnd(1:2)';z(iz,2:3)];plot(ray(:,1),ray(:,2),'-r');hold on;endendplot(localizer.xOdom(:,1),localizer.xOdom(:,2),'.k');hold on;plot(localizer.xEst(:,1),localizer.xEst(:,2),'.r');hold on;axis equal;grid on;drawnow;endend% draw the final results of localizer, compared to odometry & ground truth drawResults(localizer);toc num2str(sum(errs.^2)) figure(2) plot(0.1:0.1:60,errs) xlabel("time") ylabel("error square") end%% Other functions% degree to radian function radian = toRadian(degree)radian = degree/180*pi; endfunction []=drawResults(localizer) %Plot Resultfigure(1);hold off;x=[ localizer.xGnd(:,1:2) localizer.xEst(:,1:2)];set(gca, 'fontsize', 12, 'fontname', 'times');plot(x(:,1), x(:,2),'-.b','linewidth', 4); hold on;plot(x(:,3), x(:,4),'r','linewidth', 4); hold on;plot(localizer.xOdom(:,1), localizer.xOdom(:,2),'--k','linewidth', 4); hold on;title('Localization Result', 'fontsize', 12, 'fontname', 'times');xlabel('X (m)', 'fontsize', 12, 'fontname', 'times');ylabel('Y (m)', 'fontsize', 12, 'fontname', 'times');legend('Ground Truth','Particle Filter','Odometry Only');grid on;axis equal;endfunction [ u ] = doControl( time ) %DOCONTROL Summary of this function goes here % Detailed explanation goes here%Calc Input ParameterT=10; % [sec]% [V yawrate]V=1.0; % [m/s]yawrate = 5; % [deg/s]u =[ V*(1-exp(-time/T)) toRadian(yawrate)*(1-exp(-time/T))]';end%% you need to complete% do Observation model function [z, xGnd, xOdom, u] = doObservation(xGnd, xOdom, u, landMarks, MAX_RANGE)global Qsigma;global Rsigma;% Gnd Truth and OdometryxGnd=doMotion(xGnd, u);% Ground Truthu=u+sqrt(Qsigma)*randn(2,1); % add noise randomlyxOdom=doMotion(xOdom, u); % odometry only%Simulate Observationz=[];for iz=1:length(landMarks(:,1))% d = ?d = norm(xGnd(1:2)'-landMarks(iz,:));if d<MAX_RANGE z=[z;[d+sqrt(Rsigma)*randn(1,1) landMarks(iz,:)]]; % add observation noise randomlyendend end% do Motion Model function [ x ] = doMotion( x, u)global dt;Delta = [ dt*cos(x(3)) 0dt*sin(x(3)) 00 dt];% x = ?x = x + Delta*u; end% Gauss function function g = Gaussian(x,u,sigma)g = (1/sqrt(2*pi*sigma))*exp(-x^2/(2*sigma)); end% Normalization function pw=Normalization(pw,NP)pw = pw./sum(pw); end% Resampling function [px,pw]=ResamplingStep(px,pw,NTh,NP)Neff = 1/sum(pw.*pw);if Neff<NThpx_ = px;for i=1:NPu = rand;tempsum = 0;for j=1:NPtempsum = tempsum + pw(j);if tempsum >= upx_(:,i) = px(:,j);break;endendendpx = px_;pw=zeros(1,NP)+1/NP;end end參考:Matlab:粒子濾波定位仿真__朝聞道_的博客-CSDN博客_粒子濾波定位matlab我一定要吐槽一下機器人學,秋學期課死多,上課么全是高大上算法,基本聽不懂,居然還有期中考試。跟前面那個ROS大作業(yè)一起要在12.2前交,然而11.18才考完,我好怕啊。...https://blog.csdn.net/weixin_42231070/article/details/83795438
另一個版本:https://github.com/hujunhan/Particle-Filter-Localizationhttps://github.com/hujunhan/Particle-Filter-Localization
?
?
總結
以上是生活随笔為你收集整理的机器人学习--粒子滤波定位-MATLAB仿真1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人工智能学习--文本检测和识别综述(20
- 下一篇: SCI论文写作--IEEE的期刊和杂志区