自动驾驶-预瞄-Pure pursuit纯跟踪算法-MATLAB实现

打印 上一主题 下一主题

主题 957|帖子 957|积分 2871

有空把引入、逻辑、原理介绍给写了,目前先给大家看看代码。
将来写大概会分成这么几块:

  • 汽车运动学自行车模型
  • 跟踪算法主流模型及特点
  • 纯跟踪算法原理推导
  • 代码介绍
代码原创,来之不易,请勿不注明转载。
喜欢点个赞吧!网上许多代码都跑不起来hh
  1. clc;
  2. clear;
  3. % ------------form road------------
  4. cx = 0:0.1:50;
  5. cx = cx';
  6. for i = 1:length(cx)
  7.     cy(i) = sin(cx(i)/5)*cx(i)/2;
  8. end
  9. %--------form initial state---------
  10. L = 2.9  ;% [m] wheel base of vehicle
  11. i = 1;
  12. target_speed = 2.5;
  13. T = 80;
  14. x = 0; y = -3; yaw = 0; v = 0;
  15. time = 0;
  16. %--------how to update--------------
  17. lastIndex = length(cx)-1;
  18. dt = 0.1  ;% [s]
  19. Lfc = 1.0;  % look-ahead distance
  20. k = 0.1;  % look forward gain
  21. Kp = 1.0 ; % speed propotional gain
  22. Lf = k * v + Lfc;
  23. %--------show initial state---------
  24. target_ind= calc_target_index(x,y,cx,cy,Lf)
  25. %--------Update now keep on track-----
  26. while T > time && target_ind < lastIndex-10
  27.     target_ind= calc_target_index(x,y,cx,cy,Lf)
  28.     ai = PIDcontrol(target_speed, v,Kp);
  29.     di = pure_pursuit_control(x,y,yaw,v,cx,cy,target_ind,k,Lfc,L,Lf);
  30.    
  31.     [x,y,yaw,v] = update(x,y,yaw,v, ai, di,dt,L)
  32.    
  33.     time = time + dt;
  34.     pause(0.1)
  35.     subplot(1,2,1)
  36.     plot(cx,cy,'b',x,y,'r-*')
  37.     title(['time=' num2str(time), 'v=' num2str(v),'ind=' num2str(target_ind)])
  38.     drawnow
  39.     hold on
  40.     box off
  41. % should also show errors
  42. end
  43. %-----growing speed-----------------
  44. function [a] = PIDcontrol(target_v, current_v, Kp)
  45.     a = Kp * (target_v - current_v);
  46. end
  47. %-----pute pursuit-------------------
  48. function [delta] = pure_pursuit_control(x,y,yaw,v,cx,cy,ind,k,Lfc,L,Lf)
  49.     tx = cx(ind);
  50.     ty = cy(ind);
  51.    
  52.     alpha = atan((ty-y)/(tx-x))-yaw;
  53. %--------should also show lateral error---------
  54.     latError =abs((ty-y)*cos(yaw) - (tx-x)*sin(yaw))     
  55.     subplot(1,2,2)
  56.     plot(x,latError,'b-*')
  57.     title(['latError=' num2str(latError)])
  58.     drawnow
  59.     hold on
  60.     box off
  61. %------------------------------------------------
  62.     Lf = k * v + Lfc;
  63.     delta = atan(2*L * sin(alpha)/Lf)  ;
  64. end
  65. %-----update state-------------------
  66. function [x, y, yaw, v] = update(x, y, yaw, v, a, delta,dt,L)
  67.     x = x + v * cos(yaw) * dt;
  68.     y = y + v * sin(yaw) * dt;
  69.     yaw = yaw + v / L * tan(delta) * dt;
  70.     v = v + a * dt;
  71. end
  72. %-----find the nearest point---------
  73. function [ind] = calc_target_index(x,y, cx,cy,Lf)
  74.     N =  length(cx);
  75.     %N =  length(cx)-11;
  76.     Distance = zeros(N,1);
  77.     for i = 1:N
  78.         Distance(i) =  sqrt((cx(i)-x)^2 + (cy(i)-y)^2);
  79.     end
  80.     [~, location]= min(Distance);
  81.     ind = location;
  82.     ind = ind + 10;
  83. end
复制代码
结果图是实时的,包含一个路径跟踪展示和横向误差图,大概如下(图形并不精美,展示原理所用):

凑字数凑字数使得可以投稿!不用理会
凑字数凑字数使得可以投稿!不用理会
凑字数凑字数使得可以投稿!不用理会

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

欢乐狗

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表