手把手教用matlab做無人駕駛(二)-路徑規劃A*演算法
對於路徑規劃演算法-A*演算法在matlab中模擬,首先我們在matlab中構建地圖:
先給出matlab主函式程式:
% editor: Robert.Cao % 2018.9.1 clc clear all close all disp('A Star Path Planing start!!') p.start=[1,1]; %起始點 p.goal=[10,3]; %目標點 p.XYMAX=11; obstacle=GetBoundary(p);%得到邊界資料 nObstacle=20; obstacle=GetObstacle(nObstacle,obstacle,p); figure(1) if length(obstacle)>=1 plot(obstacle(:,1),obstacle(:,2),'om');hold on; end plot(p.start(1),p.start(2),'*r');hold on; plot(p.goal(1),p.goal(2),'*b');hold on;
註釋:程式碼可能直接複製放到matlab中執行可能會出錯,程式碼本身是沒錯的,可能複製上去格式不對,我運行了一下因為空格的原因,所有你一條一條複製到matlab中肯定不會出錯。
現在解釋一下上面程式碼:
p.start=[1,1]; %起始點就是代表機器人的起始點座標
p.goal=[10,3]; %機器人目標點座標
p.XYMAX=11; %代表我們要畫一個地圖的長和寬
obstacle=GetBoundary(p);%這裡呼叫了function 函式GetBoundary,這個函式就是通過我們設定 p.XYMAX=11函式來取得地圖的邊界
nObstacle=20;%這個函式就是我們在地圖中隨機加入二十個障礙物
obstacle=GetObstacle(nObstacle,obstacle,p);%這個obstacle包含了障礙物和地圖邊界資料 if length(obstacle)>=1 plot(obstacle(:,1),obstacle(:,2),'om');hold on; end plot(p.start(1),p.start(2),'*r');hold on; plot(p.goal(1),p.goal(2),'*b');hold on; %這段程式沒什麼解釋的,就是畫圖,包含了機器人座標的起始和終點位置。
下面,我們給出GetBoundary 函式與GetObstacle函式:
function :GetBoundary
function boundary=GetBoundary(p)
boundary=[];
for i1=0:(p.XYMAX+1)
boundary=[boundary;[0 i1]];
end
for i2=0:(p.XYMAX+1)
boundary=[boundary;[i2 0]];
end
for i3=0:(p.XYMAX+1)
boundary=[boundary;[p.XYMAX+1 i3]];
end
for i4=0:(p.XYMAX+1)
boundary=[boundary;[i4 p.XYMAX+1]];
end
boundary=[boundary;[11 11]];
boundary=[boundary;[9 1]];
boundary=[boundary;[10 2]];
boundary=[boundary;[11 3]];
boundary=[boundary;[10 1]];
boundary=[boundary;[11 2]];
boundary=[boundary;[11 1]];
end
function :GetObstacle
function obstacle=GetObstacle(nob,obstacle,p)
ob=round(rand([nob,2])*p.XYMAX);
removeInd=[];
for io=1:length(ob(:,1))
if(isSamePosi(ob(io,:),p.start) || isSamePosi(ob(io,:),p.goal))
removeInd=[removeInd;io];
end
end
ob(removeInd,:)=[];
obstacle=[obstacle;ob];
現在看看matlab中的效果:
圖中可以看出邊界點,障礙物點,機器人起始座標,終點座標,下一篇將在這個地圖中做A*演算法。