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);
path=AStar(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;
if length(path)>=1
plot(path(:,1),path(:,2),'-r');hold on;
end
grid on;
function path=AStar(obstacle,p)
path=[];
open=[p.start(1) p.start(2) h(p.start,p.goal) p.start(1) p.start(2)];
close=[];
next=MotionModel();
findFlag=false;%目标标志
while ~findFlag
if isempty(open(:,1)) disp('No path to goal!!');
return;
end
[Y,I] = sort(open(:,3))
open=open(I,:);
if isSamePosi(open(1,1:2),p.goal)
disp('Find Goal!!');
close=[open(1,:);close]
open(1,:)=[];
findFlag=true;
break;
end
for in=1:length(next(:,1))
m=[open(1,1)+next(in,1) open(1,2)+next(in,2) open(1,3)];
%m(3)=m(3)+next(in,3)+h(m(1:2),p.goal)-h(open(1,1:2),p.goal);
m(3)=next(in,3)+h(m(1:2),p.goal);
if isObstacle(m,obstacle) continue; end
[flag, targetInd]=FindList(m,open,close)
if flag==1
disp("cao")
continue;
elseif flag==2
continue;
else
open=[open;[m open(1,1) open(1,2)]]
end
end
if findFlag==false
close=[close; open(1,:)]
open(1,:)=[];
end
end
path=GetPath(close,p.start);
结果展示
其他代码下载见链接:
咨询链接:matlab正逆运动学分析与轨迹规划