1 简介

移动机器人路径规划一直是一个比较热门的话题,A星算法以及其扩展性算法被广范地应用于求解移动机器人的最优路径.该文在研究机器人路径规划算法中,详细阐述了传统A星算法的基本原理,并通过栅格法分割了机器人路径规划区域,利用MATLAB仿真平台生成了机器人二维路径仿真地图对其进行仿真实验,并对结果进行分析和研究,为今后进一步的研究提供经验.


【二维路径规划】基于A星算法求解机器人路径规划matlab代码_路径规划

2 部分代码

function varargout = test(varargin)
% TEST MATLAB code for test.fig
,obj2_currentPos,'Curvature',[1 1],'FaceColor',[0 0 1]);
   end 
   if obj3 == 1
       rectangle('Position',obj3_currentPos,'Curvature',[1 1],'FaceColor',[0 0 1]);
   end
   str = strcat(str,sprintf('\nReach the goal point...'));
   set(handles.result,'String',str);
end




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

% Get the handles to the objects of interest:
hObj1X = handles.obj1X;
hObj1Y = handles.obj1Y;

% Get the string value
stringObj1X = get(hObj1X,'String');
stringObj1Y = get(hObj1Y,'String');

obj1X = str2num(stringObj1X);
obj1Y = str2num(stringObj1Y);

setappdata( 0, 'obj1', 1 );

mapResize = evalin( 'base', 'mapResize' );
if(length(obj1X)>0 && length(obj1Y)>0)
  [obj1_currentPos,obj1_centerPoint] = createObj(mapResize,obj1X,obj1Y);
else        
  [obj1_currentPos,obj1_centerPoint] = createObj(mapResize);
end

rectangle('Position',obj1_currentPos,'Curvature',[1 1],'FaceColor',[0 0 1]);

% save route into basic workspace
assignin('base', 'obj1_centerPoint', obj1_centerPoint);
assignin('base', 'obj1_currentPos', obj1_currentPos);

global str;
tmp = sprintf('\nobject 1 start point is: [');
tmp = strcat(tmp,num2str(obj1_centerPoint(1)));
tmp = strcat(tmp,',');
tmp = strcat(tmp,num2str(obj1_centerPoint(2)));
tmp = strcat(tmp,']');
str = strcat(str,tmp);
set(handles.result,'String',str);




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

% Get the handles to the objects of interest:
hObj2X = handles.obj2X;
hObj2Y = handles.obj2Y;

% Get the string value
stringObj2X = get(hObj2X,'String');
stringObj2Y = get(hObj2Y,'String');

obj2X = str2num(stringObj2X);
obj2Y = str2num(stringObj2Y);

% save route into basic workspace
assignin('base', 'obj2X', obj2X);
assignin('base', 'obj2Y', obj2Y);

setappdata( 0, 'obj2', 1 );
mapResize = evalin( 'base', 'mapResize' );
if(length(obj2X)>0 && length(obj2Y)>0)
  [obj2_currentPos,obj2_centerPoint] = createObj(mapResize,obj2X,obj2Y);
else        
  [obj2_currentPos,obj2_centerPoint] = createObj(mapResize);
end

rectangle('Position',obj2_currentPos,'Curvature',[1 1],'FaceColor',[0 0 1]);

% save route into basic workspace
assignin('base', 'obj2_centerPoint', obj2_centerPoint);
assignin('base', 'obj2_currentPos', obj2_currentPos);

global str;
tmp = sprintf('\nobject 2 start point is: [');
tmp = strcat(tmp,num2str(obj2_centerPoint(1)));
tmp = strcat(tmp,',');
tmp = strcat(tmp,num2str(obj2_centerPoint(2)));
tmp = strcat(tmp,']');
str = strcat(str,tmp);
set(handles.result,'String',str);


% --- Executes on button press in obj3.
function obj3_Callback(hObject, eventdata, handles)
% hObject   handle to obj3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)
set(handles.result,'String','obj3');
% Get the handles to the objects of interest:
hObj3X = handles.obj3X;
hObj3Y = handles.obj3Y;

% Get the string value
stringObj3X = get(hObj3X,'String');
stringObj3Y = get(hObj3Y,'String');

obj3X = str2num(stringObj3X);
obj3Y = str2num(stringObj3Y);

% save route into basic workspace
assignin('base', 'obj3X', obj3X);
assignin('base', 'obj3Y', obj3Y);

setappdata( 0, 'obj3', 1 );
mapResize = evalin( 'base', 'mapResize' );
if(length(obj3X)>0 && length(obj3Y)>0)
  [obj3_currentPos,obj3_centerPoint] = createObj(mapResize,obj3X,obj3Y);
else        
  [obj3_currentPos,obj3_centerPoint] = createObj(mapResize);
end

rectangle('Position',obj3_currentPos,'Curvature',[1 1],'FaceColor',[0 0 1]);

% save route into basic workspace
assignin('base', 'obj3_centerPoint', obj3_centerPoint);
assignin('base', 'obj3_currentPos', obj3_currentPos);


global str;
tmp = sprintf('\nobject 3 start point is: [');
tmp = strcat(tmp,num2str(obj3_centerPoint(1)));
tmp = strcat(tmp,',');
tmp = strcat(tmp,num2str(obj3_centerPoint(2)));
tmp = strcat(tmp,']');
str = strcat(str,tmp);
set(handles.result,'String',str);





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

set(handles.result,'String','stop!');
smoothPathX = evalin( 'base', 'smoothPathX' );

global str;
str = 'stop!';
set(handles.result,'String',str);

global stop;
stop = true;


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

set(handles.result,'String','restart !');

global stop;
stop = true;
axes(handles.findPathAxes);
imshow(imread('empty.png'));
drawnow;
axes(handles.pathFollowingAxes);
imshow(imread('empty.png'));
drawnow;

% Get the string value
set(handles.startX, 'String', '');
set(handles.startY, 'String', '');
set(handles.goalX, 'String', '');
set(handles.goalY, 'String', '');
set(handles.obj1X, 'String', '');
set(handles.obj1Y, 'String', '');
set(handles.obj2X, 'String', '');
set(handles.obj2Y, 'String', '');
set(handles.obj3X, 'String', '');
set(handles.obj3Y, 'String', '');

set(handles.result,'String','restart!');






% ------------------------ default function -----------------------------



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

% Hints: get(hObject,'String') returns contents of startX as text
%       str2double(get(hObject,'String')) returns contents of startX as a double


% --- Executes during object creation, after setting all properties.
function startX_CreateFcn(hObject, eventdata, handles)
% hObject   handle to startX (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
   set(hObject,'BackgroundColor','white');
end



function startY_Callback(hObject, eventdata, handles)

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

% Hints: get(hObject,'String') returns contents of obj1X as text
%       str2double(get(hObject,'String')) returns contents of obj1X as a double


% --- Executes during object creation, after setting all properties.
function obj1X_CreateFcn(hObject, eventdata, handles)
% hObject   handle to obj1X (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
   set(hObject,'BackgroundColor','white');
end



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

% Hints: get(hObject,'String') returns contents of obj1Y as text
%       str2double(get(hObject,'String')) returns contents of obj1Y as a double


% --- Executes during object creation, after setting all properties.
function obj1Y_CreateFcn(hObject, eventdata, handles)
% hObject   handle to obj1Y (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
   set(hObject,'BackgroundColor','white');
end



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

% Hints: get(hObject,'String') returns contents of obj2X as text
%       str2double(get(hObject,'String')) returns contents of obj2X as a double


% --- Executes during object creation, after setting all properties.
function obj2X_CreateFcn(hObject, eventdata, handles)
% hObject   handle to obj2X (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
   set(hObject,'BackgroundColor','white');
end



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

% Hints: get(hObject,'String') returns contents of obj2Y as text
%       str2double(get(hObject,'String')) returns contents of obj2Y as a double


% --- Executes during object creation, after setting all properties.
function obj2Y_CreateFcn(hObject, eventdata, handles)
% hObject   handle to obj2Y (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
   set(hObject,'BackgroundColor','white');
end




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

% Hints: get(hObject,'String') returns contents of obj3X as text
%       str2double(get(hObject,'String')) returns contents of obj3X as a double


% --- Executes during object creation, after setting all properties.
function obj3X_CreateFcn(hObject, eventdata, handles)
% hObject   handle to obj3X (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
   set(hObject,'BackgroundColor','white');
end



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

% Hints: get(hObject,'String') returns contents of obj3Y as text
%       str2double(get(hObject,'String')) returns contents of obj3Y as a double


% --- Executes during object creation, after setting all properties.
function obj3Y_CreateFcn(hObject, eventdata, handles)
% hObject   handle to obj3Y (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
   set(hObject,'BackgroundColor','white');
end


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

% Hints: get(hObject,'String') returns contents of result as text
%       str2double(get(hObject,'String')) returns contents of result as a double


% --- Executes during object creation, after setting all properties.
function result_CreateFcn(hObject, eventdata, handles)
% hObject   handle to result (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
   set(hObject,'BackgroundColor','white');
end

3 仿真结果

【二维路径规划】基于A星算法求解机器人路径规划matlab代码_路径规划_02

4 参考文献

[1]周宇杭等. "基于A星算法的移动机器人路径规划应用研究." 电脑知识与技术 v.16.13(2020):7-9+16.

博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,有科研问题可私信交流。

【二维路径规划】基于A星算法求解机器人路径规划matlab代码_2d_03