Plot lines with curves from csv file

조회 수: 4 (최근 30일)
Konvictus177
Konvictus177 2025년 1월 27일
편집: Mathieu NOE 2025년 2월 5일
Hi,
I have a csv/txt file that contains x,y,z and also the rotation center x and rotation center y data. The data are basically the coordinates of a path that a robot follows. The robot can make his path in a straight line and also in curves. How can I use the rotation center x and y coordinates in my data to plot the curves.
My current plot has only straight lines. But as you can see in the image attached, the actual path inlcudes curves around corners based on the rotation center x and y data. The following code is obviously not enough.
figure(1)
plot(Data.PosX,Data.PosY)
  댓글 수: 2
Konvictus177
Konvictus177 2025년 1월 28일
이동: Star Strider 2025년 1월 28일
The path follows a circle (curve) around the center points in x and y.
The rotation centers x and y are the coordinates in x and y of the circle center relative to the current position (current position = the row before the row with entries in rotation center x and y coordinates).
So for example in the data the move from the 2nd to 3rd row would be a circular movement from x(2) to x(3) and from y(2) to y(3) but in a cirle with center point at x(2)+RotCenterX(3) and y(2)+RotCenterY(3).
dpb
dpb 2025년 1월 28일
You'll have to compute and draw line segments short enough between the two end points that they appear to be circular. One real weakness in MATLAB is and has always been there is only Rectangle that draws a circular object as one (certainly nonintuitive) option of its use.
To see the "how" to draw a circular arc see the FEX submission <circle>. It doesn't have the ability as written to do other than draw the whole circle with a given number of points, but you can modify it such that you set the rotation angle and start position to only draw the portion needed. That, of course is easy if you know the lines two points are connectin are at right angles, but may not be so simple for the little detours along the route. You might there have to figure that out as well by finding where the arc actually intersects the next point which will not necessarily be an exact match given you have to calculate in a discrete number of points, not continuously.
All in all, while it would be doable, it's going to take a fair amount of code to manage it.
I didn't do an exhaustive search on FEX to see if anybody has done something similar already, maybe. It does seem as though it might be a relatively common need in robotics mapping...

댓글을 달려면 로그인하십시오.

채택된 답변

Mathieu NOE
Mathieu NOE 2025년 2월 3일
maybe this ? I'm surprised to be lucky on monday morning
rounded corners appears now as red arcs of circle , but there's a bit of work if you don't want to see the original linear segments (at the corners)
Data = readtable('Test.csv');
indR = find((abs(Data.RotCenterX) + abs(Data.RotCenterY))>eps); % find occurences when we have to create a rounded corner
% create a full circle with 1 deg resolution
n = 360;
th = (0:n-1)/n*2*pi;
figure(1)
plot(Data.PosX,Data.PosY,'-*')
hold on
for k = 1: numel(indR)
indRyk = indR(k);
xc = Data.PosX(indRyk-1) + Data.RotCenterX(indRyk);
yc = Data.PosY(indRyk-1) + Data.RotCenterY(indRyk);
R = sqrt(Data.RotCenterX(indRyk)^2 + Data.RotCenterY(indRyk)^2);
% create circle
xCir = R*cos(th) + xc;
yCir = R*sin(th) + yc;
% limit circle to valid segment (within range of X,Y from start to end point)
indX = (xCir>=min(Data.PosX(indRyk-1),Data.PosX(indRyk))) & (xCir<=max(Data.PosX(indRyk-1),Data.PosX(indRyk)));
indY = (yCir>=min(Data.PosY(indRyk-1),Data.PosY(indRyk))) & (yCir<=max(Data.PosY(indRyk-1),Data.PosY(indRyk)));
indXY = indX & indY;
xCir = xCir(indXY);
yCir = yCir(indXY);
plot(xCir,yCir,'r.');
end
hold off
  댓글 수: 15
Konvictus177
Konvictus177 2025년 2월 5일
편집: Konvictus177 2025년 2월 5일
Yes, thats correct. After generating the .csv file I had to edit it manually and I made a mistake there. The Test3.csv file should be correct. Thanks again!
Mathieu NOE
Mathieu NOE 2025년 2월 5일
편집: Mathieu NOE 2025년 2월 5일
as always, my pleasure !
glad we found the bug ! (and it's not the code :))
and the quiver plot is a good idea too
fyi I made a few clean up ops in the code , so there are some stuff I removed / changed a bit
%% load data
Data = readtable('Test3.csv');
indR = find((abs(Data.RotCenterX) + abs(Data.RotCenterY))>eps); % find occurences when we have to create a rounded corner
indN = 1:size(Data,1);
%% init
Np = 100; % number of points for each arc segment
indRk_old = 0;
x = [];
y = [];
%% main loop
for k = 1:numel(indR)
indRk = indR(k);% current index
% first extract x,y data of the linear segment
ind = find(indN<indRk & indN>=indRk_old);
x = [x; Data.PosX(ind)];
y = [y; Data.PosY(ind)];
% then create the turn (arc of circle)
% circle center (xc,yc) and radius R
xc = Data.PosX(indRk-1) + Data.RotCenterX(indRk);
yc = Data.PosY(indRk-1) + Data.RotCenterY(indRk);
R = sqrt(Data.RotCenterX(indRk)^2 + Data.RotCenterY(indRk)^2);
% get the polar coordinates of starting and ending point of the arc
[th1,r1] = cart2pol(Data.PosX(indRk-1)-xc,Data.PosY(indRk-1)-yc); % starting point
[th2,r2] = cart2pol(Data.PosX(indRk)-xc,Data.PosY(indRk)-yc); % ending point
% NB : R = r1 = r2
% create first a straigth line (x,y array) between starting and ending point of the arc
xx = linspace(Data.PosX(indRk-1),Data.PosX(indRk),Np);
yy = linspace(Data.PosY(indRk-1),Data.PosY(indRk),Np);
% now convert this line into an arc of circle
[th,r] = cart2pol(xx-xc,yy-yc); %
[xCir,yCir] = pol2cart(th,r2); % convert back with correct radius = r2 (or r1 or R if you prefer)
xCir = xCir+xc;
yCir = yCir+yc;
x = [x; xCir(:)];
y = [y; yCir(:)];
% update indRyk_old
indRk_old = indRk;
end
% there is maybe a last straight portion, so don't forget it
if indN(end)>indRk
x = [x; Data.PosX(indRk+1:end)];
y = [y; Data.PosY(indRk+1:end)];
end
% plot new data with round corners
figure(2)
q=quiver(x(1:end-1),y(1:end-1), diff(x), diff(y), 0, 'r');
hold on
plot(x,y,'b-','linewidth',1.5)
q.MaxHeadSize=0.1;
axis equal

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by