路径规划——RRT算法实现
生活随笔
收集整理的這篇文章主要介紹了
路径规划——RRT算法实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
RRT——rapidly exploring random tree,即快速擴展隨機樹,是基于隨機采樣的路徑規劃算法,以起點為根節點,在搜索空間中隨機產生節點,通過進行步長計算、碰撞檢測等操作來產生新的子節點,直到采樣至終點為止。偽代碼如圖所示:
圖源: https://www.cnblogs.com/long5683/p/11795987.html
matlab代碼實現:
main函數:
Near函數產生新的隨機采樣點,同時還要計算樹上與該點距離最近的節點,并返回其在樹上的索引值。
function [index,new_point]=Near(T,x_range,y_range,StepSize)%產生新的節點,并返回樹上距該點最近點的索引 rand_point=[x_range(2)*rand, y_range(2)*rand];%產生隨機點 min_dist=sqrt((rand_point(1)-T.vertex(1).x)^2+(rand_point(2)-T.vertex(1).y)^2); index=1; for i=1:size(T.vertex,2)dist=sqrt((rand_point(1)-T.vertex(i).x)^2+(rand_point(2)-T.vertex(i).y)^2);if dist<min_distmin_dist=dist;index=i;end end if min_dist<=StepSize%在步長范圍內直接使用該點new_point(1)=rand_point(1);new_point(2)=rand_point(2); elsetheta = atan2(rand_point(1)-T.vertex(index).x,rand_point(2)-T.vertex(index).y);new_point(1)=T.vertex(index).x+StepSize*sin(theta);new_point(2)=T.vertex(index).y+StepSize*cos(theta); endcheck_Collision函數用于對新生成的節點進行碰撞檢測,方法是在新生成的節點與樹上最近點之間進行定步長采樣,計算采樣點是否與障礙物發生碰撞,因此采樣步長需要足夠小,防止誤判。
function feasible=check_Collision(obs,new_point,x,y) min_dist=sqrt((new_point(1)-x)^2+(new_point(2)-y)^2); theta = atan2(new_point(1)-x,new_point(2)-y); feasible=false; for k=1:size(obs,2)for step=0:0.001:min_distcheck_point.x=x+step*sin(theta);check_point.y=y+step*cos(theta);feasible=isinPolygon(check_point,obs(k));if feasiblereturn;endend end判斷指定點與凸多邊形的關系可見以下文章,此處不再贅述:
https://blog.csdn.net/qq_36250209/article/details/123763849
function in_out=isinPolygon(Q,obs)%判斷一個點是否在凸多邊形之外——利用目標點與多邊形各頂點構成的面積與多邊形面積進行計算判斷 %如果面積之和大于多邊形面積,則在外部,否則在內部 %將多邊形面積計算轉化為各個三角形的面積計算,可以簡化計算過程 %輸入:目標點的坐標,凸多邊形的頂點坐標 最大邊數為五邊形 輸入的多邊形頂點按逆時針或順時針順序輸入 %輸出:判斷結果 S1=calc_triangle(Q.x,Q.y,obs.x(1),obs.y(1),obs.x(2),obs.y(2)); S2=calc_triangle(Q.x,Q.y,obs.x(2),obs.y(2),obs.x(3),obs.y(3)); S3=calc_triangle(Q.x,Q.y,obs.x(3),obs.y(3),obs.x(4),obs.y(4)); S4=calc_triangle(Q.x,Q.y,obs.x(4),obs.y(4),obs.x(5),obs.y(5)); S = calc_triangle(obs.x(1),obs.y(1),obs.x(2),obs.y(2),obs.x(3),obs.y(3))+ ...calc_triangle(obs.x(1),obs.y(1),obs.x(3),obs.y(3),obs.x(4),obs.y(4)); if S==S1+S2+S3+S4in_out=true; elsein_out=false; end end function S=calc_triangle(x0,y0,x1,y1,x2,y2)%計算三角形面積——利用三角形各點與橫坐標垂直連線構成的梯形面積相加減計算三角形面積S=0.5*abs(x0*y1+x1*y2+x2*y0-x0*y2-x1*y0-x2*y1); end運行結果如下圖所示:
終點處的圓表示的是終止搜索范圍,當采樣點進入該范圍內,即可直接連接至終點,不過,程序中并未對此步驟進行碰撞檢測,可以根據自己的需要進行修改完善。
總結
以上是生活随笔為你收集整理的路径规划——RRT算法实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MacOS壁纸文件夹如何查找
- 下一篇: 唯一解的数独题目生成器——理解回溯法