matlab实现纯跟踪算法

对《Pure Pursuit纯跟踪算法Python/Matlab算法实现》该博客matlab算法提升:

该博客链接:https://blog.csdn.net/gophae/article/details/102761138

该博客算法跑的效果:

  • 路径没有完整跟下来,可以看出尾部少一块
    在这里插入图片描述

我在该博客提供算法的基础上做了改进:

下面展示一些 内联代码片

// An highlighted block
k = 0.1;  % look forward gain
Lfc = 1;  % look-ahead distance
Kp = 1.0 ; % speed propotional gain
dt = 0.1  ;% [s]
L = 2.9  ;% [m] wheel base of vehicle
cx = 0:0.1:50;
cx = cx';
for i = 1:length(cx)
cy(i) = sin(cx(i)/5)*cx(i)/2;                                               %曲线函数
% cy(i)=(3.75/(3.^5))*(6*i.^5-15*3*i.^4+10*3^2*i.^3);
% cy(i) = i;
end

i = 1;
target_speed = 3;
T = 80;
lastIndex = length(cx);                                                     %lastIndex=501
x = 0; y = -3; yaw = 0; v = 2;                                               %初始状态
time = 0;
Lf = k * v + Lfc;                                                           %相当于预瞄距离
figure
 
while T > time   
    [target_ind,~]= calc_target_index(x,y,cx,cy,Lf)                         %找到预瞄点
    ai = PIDcontrol(target_speed, v,Kp);                                    %pid控速度
    [~,After_Lf]= calc_target_index(x,y,cx,cy,Lf)  
    di = pure_pursuit_control(x,y,yaw,v,cx,cy,target_ind,k,Lfc,L,After_Lf);
    
    [x,y,yaw,v] = update(x,y,yaw,v, ai, di,dt,L)
    time = time + dt;
    pause(0.05)
    plot(cx,cy,'b',x,y,'r-*')
    drawnow
    hold on
end


function [x, y, yaw, v] = update(x, y, yaw, v, a, delta,dt,L)                 %更新状态
    x = x + v * cos(yaw) * dt;
    y = y + v * sin(yaw) * dt;
    yaw = yaw + v / L * tan(delta) * dt;
   v = v + a * dt;
end

function [a] = PIDcontrol(target_v, current_v, Kp)
a = Kp * (target_v - current_v);
end

function [delta] = pure_pursuit_control(x,y,yaw,v,cx,cy,ind,k,Lfc,L,Lf)        %纯跟踪
    tx = cx(ind);
    ty = cy(ind);
    
    %alpha = atan((ty-y)/(tx-x))-yaw;                                           %实际点的夹角-航向角
    alpha=atan2((ty-y),(tx-x))-yaw;
                                                                               %预瞄距离
    delta = atan(2*L * sin(alpha)/Lf)  ;
end

function [ind,After_Lf] = calc_target_index(x,y, cx,cy,Lf)                     %找到最近的那个
N =  length(cx); %N=501
Distance = zeros(N,1);
for i = 1:N
Distance(i) =  sqrt((cx(i)-x)^2 + (cy(i)-y)^2);
end
[distance, location]= min(Distance);
ind = location;                                                                %第几个数
Relative_distance=distance;                                                    %实际的数
% LL = 0;
%     while Lf > LL && (ind + 1) < length(cx)
%         dx = cx(ind + 1 )- cx(ind);
%         dy = cx(ind + 1) - cx(ind);
%         LL = LL + sqrt(dx * 2 + dy * 2);
%         ind  = ind + 1;
%     end
%     
h=sqrt(Lf^2-Relative_distance^2);
sum=0;
z=1;
 while z<(length(cx)-ind-1)
  sum=sum+sqrt((cx(ind+z+1)-cx(ind+z))^2+(cy(ind+z+1)-cy(ind+z))^2);
      z=z+1;
   if (sum>=h)
      break;
   end
 end 
 After_Lf=sqrt(sum^2+Relative_distance^2);
%  if After_Lf>=3
%      After_Lf=3
%  else
%      After_Lf=After_Lf
%  end
 ind=z+ind;
end

实现效果:
在这里插入图片描述


  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是一个简单的跟踪算法MATLAB实现: ```matlab % 初始化 video = VideoReader('video.mp4'); numFrames = video.NumFrames; frameSize = [video.Height, video.Width]; trackingPoints = [100, 100; 200, 200]; % 跟踪点的初始位置 numPoints = size(trackingPoints, 1); trackedPoints = zeros(numFrames, numPoints, 2); % 用于存储每个跟踪点在每一帧中的位置 % 循环处理每一帧 for frameIdx = 1:numFrames % 读取当前帧 frame = read(video, frameIdx); % 对于每个跟踪点,计算在当前帧中的位置 for pointIdx = 1:numPoints prevPoint = trackingPoints(pointIdx, :); patchSize = 15; % 用于计算光流的小区域大小 patch = frame(prevPoint(2)-patchSize/2:prevPoint(2)+patchSize/2, ... prevPoint(1)-patchSize/2:prevPoint(1)+patchSize/2); if frameIdx == 1 % 如果是第一帧,则直接将跟踪点的位置作为当前位置 currPoint = prevPoint; else % 否则,计算跟踪点在当前帧中的位置 flow = estimateFlow(opticalFlowLK, patch); currPoint = prevPoint + flow.Magnitude * flow.Orientation; end % 保存跟踪点在当前帧中的位置 trackedPoints(frameIdx, pointIdx, :) = currPoint; end end % 绘制跟踪结果 figure; imshow(read(video, 1)); % 显示第一帧 hold on; for pointIdx = 1:numPoints plot(squeeze(trackedPoints(:, pointIdx, 1)), ... squeeze(trackedPoints(:, pointIdx, 2)), '-o'); end ``` 这个算法假设跟踪点在相邻帧之间的运动是光流。在每个跟踪点周围提取一个小的图像区域(称为“patch”),然后使用LK光流算法计算该区域在相邻帧之间的位移。最后,将该位移应用于跟踪点的当前位置以获得其在当前帧中的位置。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_43796045

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值