【路径规划】基于A星算法实现机器人追踪动态目标路径规划问题Matlab代码

98 阅读4分钟

1 简介

移动机器人路径规划一直是一个比较热门的话题,A星算法以及其扩展性算法被广范地应用于求解移动机器人的最优路径。

img

img

img

img

2 部分代码

function varargout = Astar(varargin)
% ASTAR MATLAB code for Astar.fig
%     ASTAR, by itself, creates a new ASTAR or raises the existing
%     singleton*.
%
%     H = ASTAR returns the handle to a new ASTAR or the handle to
%     the existing singleton*.
%
%     ASTAR('CALLBACK',hObject,eventData,handles,...) calls the local
%     function named CALLBACK in ASTAR.M with the given input arguments.
%
%     ASTAR('Property','Value',...) creates a new ASTAR or raises the
%     existing singleton*. Starting from the left, property value pairs are
%     applied to the GUI before Astar_OpeningFcn gets called. An
%     unrecognized property name or invalid value makes property application
%     stop. All inputs are passed to Astar_OpeningFcn via varargin.
%
%     *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
%     instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Astar

% Last Modified by GUIDE v2.5 30-Oct-2020 10:58:46

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                  'gui_Singleton',  gui_Singleton, ...
                  'gui_OpeningFcn', @Astar_OpeningFcn, ...
                  'gui_OutputFcn',  @Astar_OutputFcn, ...
                  'gui_LayoutFcn', [] , ...
                  'gui_Callback',   []);
if nargin && ischar(varargin{1})
   gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
  [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
   gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Astar is made visible.
function Astar_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject   handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Astar (see VARARGIN)

% Choose default command line output for Astar
handles.output = hObject;

% Update handles structure
g
 Y= parent(step,2);
 delete(car);   
 car=plot(X,Y,'sb','markerfacecolor','b');
 delete(object);
 object=plot(A,B,'om','markerfacecolor','m');
 pause(0.05end;%End of for Loop
% hObject   handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
uiresume;
% hObject   handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
uiwait;
% hObject   handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
uiresume;
% hObject   handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
%初始化参数
Xo=[0 0];%起点位置
longth=1;%步长
J=2000;%循环迭代次数
Xj=Xo;%j=1循环初始,将车的起始坐标赋给Xj
%DEFINE THE 2-D MAP ARRAY
MAX_X=100;
MAX_Y=100;
%MAX_VAL=100;
%This array stores the coordinates of the map and the 
%Objects in each coordinate
MAP=2*(ones(MAX_X,MAX_Y));
% Obtain Obstacle, Target and Robot Position
% Initialize the MAP with input values
% Obstacle=-1,Target = 0,Robot=1,Space=2
j=0;
x_val = 1;
y_val = 1;
axis([-20 120 -20 120])
%grid on;
%hold on;
axis equal;
hold on;
axis off;
%set(gcf,'color','y')
%title ('A*算法路径规划');
fill([-20,120,120,-20],[-20 -20 120 120],'y')
fill([95,120,120,95],[-20 -20 10 10],'w')
text(100,5,'Notes:','FontSize',12)
plot(101,-5,'sb','markerfacecolor','b');
text(101,-5,' Robot','FontSize',12);
plot(101,-15,'om','markerfacecolor','m');
text(101,-15,' Ball','FontSize',12);
plot(1,1,'bs')

car=plot(1,1,'sb','markerfacecolor','b');
%car_name=text(0,0,' ','FontSize',12);
object=plot(0,100,'om','markerfacecolor','m');
%object_name=text(0,100,' Ball','FontSize',12);
but=1;
text(-4,-4,' Start','FontSize',12);
n=0;%Number of Obstacles
% BEGIN Interactive Obstacle, Target, Start Location selection
xTarget=0;%X Coordinate of the Target
yTarget=100;%Y Coordinate of the Target
m_Target=[xTarget,yTarget];
%object=plot(1,100,'om','markerfacecolor','m');
%object_name=text(1,100,' Ball','FontSize',12);
%选择小车起点位置
xval=1;
yval=1;
xStart=xval;
yStart=yval;
MAP(xval,yval)=1;
% hObject   handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function help_Callback(hObject, eventdata, handles)
% hObject   handle to help (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function out_Callback(hObject, eventdata, handles)
selection=questdlg(['是否退出演示窗口?'], ... 
['退出'],'Yes','No','No');%当选择退出按钮时,得出一个问是否确定关闭的框
if strcmp(selection,'No')
   return;                                    
else
   if strcmp(selection,'Yes')
   clc;    %当选择关闭时,清空所有matla输入面上的所有错误信息,同时关闭图像窗口
   clear all;
   delete(gcf);
   else
       return;
   end
end
% hObject   handle to out (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function state_Callback(hObject, eventdata, handles)
aa=['1.进入仿真界面,单击 <绘制障碍> 功能键在工作窗口绘制障碍' char(10)...
   '(注:当绘制障碍超出有效区后,程序会自动停止绘制,此时需要重置)' char(10)...
   '2.绘制障碍结束,单击右键完成绘制,十字光标消失表示完成' char(10)...
   '3.点击 <演示> 功能键,将按预订算法自动演示' char(10) ...
   '4.点击 <暂停> 功能键,演示过程将暂停运行,点击 <继续> 功能键后,将从暂停位置继续演示' char(10)...
   '5.点击 <重置> 功能键,仿真界面将回到初始状态,重新等待操作'];
msgbox(aa,'使用说明');
% hObject   handle to state (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function about_Callback(hObject, eventdata, handles)
msgbox('               北航自动化','关于');




% hObject   handle to about (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

3 仿真结果

4 参考文献

[1]周宇杭, 王文明, 李泽彬, 代宇浩, 徐宇豪, & 柳晨阳. (2020). 基于a星算法的移动机器人路径规划应用研究. 电脑知识与技术:学术版, 16(13), 4.​

部分理论引用网络文献,若有侵权联系博主删除。

图片