PRM路径规划算法仿真使用

摘要

小虎想分享一个简单的路径规划算法,因为仿真起来很直观。

结果

在这里插入图片描述
在这里插入图片描述

完整代码

clc,clear,close all 

robot = differentialDriveKinematics("TrackWidth", 1.5, "VehicleInputs", "VehicleSpeedHeadingRate");

%加载地图
load exampleMaps
map = binaryOccupancyMap(simpleMap);
figure(1)
show(map)
% PRM路径规划算法
mapInflated = copy(map);
inflate(mapInflated, robot.TrackWidth/2);
prm = robotics.PRM(mapInflated);
% way1
prm.NumNodes = 800;
prm.ConnectionDistance = 20;
%  prm.NumNodes = 500;
%  prm.ConnectionDistance = 10;

% prm.NumNodes = 3000;
% prm.ConnectionDistance = 30;
% way1
startLocation = [5 6];
endLocation = [22 20];
% % endLocation = [22 3.5];

path = findpath(prm, startLocation, endLocation);
show(prm);
% 
controller = controllerPurePursuit;
controller.Waypoints = path;%路径
controller.DesiredLinearVelocity = 3;%线速度
controller.MaxAngularVelocity = 2;%角速度
controller.LookaheadDistance = 0.3;

robotInitialLocation = path(1,:);
robotGoal = path(end,:);

initialOrientation = 0;

robotCurrentPose = [robotInitialLocation initialOrientation]';

distanceToGoal = norm(robotInitialLocation - robotGoal);

goalRadius = 0.1;

sampleTime = 0.1;
vizRate = rateControl(1/sampleTime);
frameSize = robot.TrackWidth/0.8;

reset(vizRate);

% Initialize the figure
figure(2)

while( distanceToGoal > goalRadius )
    
    % Compute the controller outputs, i.e., the inputs to the robot
    [v, omega] = controller(robotCurrentPose);
    
    % 速度计算
    vel = derivative(robot, robotCurrentPose, [v omega]);
    
    %更新位置
    robotCurrentPose = robotCurrentPose + vel*sampleTime; 
    
    % 目标距离
    distanceToGoal = norm(robotCurrentPose(1:2) - robotGoal(:));
    
    % Update the plot
    hold off
    show(map);
    hold all

    % Plot path each instance so that it stays persistent while robot mesh
    % moves
    plot(path(:,1), path(:,2),"k--d")
    
    % Plot the path of the robot as a set of transforms
    plotTrVec = [robotCurrentPose(1:2); 0];
    plotRot = axang2quat([0 0 1 robotCurrentPose(3)]);
    plotTransforms(plotTrVec', plotRot, 'MeshFilePath', 'groundvehicle.stl', 'Parent', gca, "View","2D", "FrameSize", frameSize);
    light;
%     xlim([0 27])
%     ylim([0 26])
    
    waitfor(vizRate);
end
posted @   狂小虎  阅读(119)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示