"Index in position 1 exceeds array bounds (must not exceed 1)." error using spline.

1 visualizzazione (ultimi 30 giorni)
function [E,B,cmd,vx,vy] = trajplanner(a,x,N,T)
% trajplanner function creates a path and calculates the velocity of the
% end effector along path at various times
% inputs:
% a = column vector containing sections' lengths a1 & a2
% x = 2-by-N matrix containing target positions of end-effector;
% each column is one point (xi,yi)
% N = integer that equals # of intermediate configurations
% needing interpolation
% T = total duration of the trip from first to last point
% outputs:
% E = 2-by-N matrix containing path end-effector will follow
% B = 2-by-N matrix containing path joint B will follow
% cmd = 2-by-N matrix containing joint angle commands
% vx = array of length N containing x-component of velocity
% vy = array of length N containing y-component of velocity
eP=length(x); % # of effector points
xq=linspace(x(1,1),x(1,eP),N);
E=[xq;zeros(1,N)]; % create matrix for E
E(2,:)=spline(x(1,:),x(2,:),xq);
B=zeros(2,eP); % create matrix for B
cmd=zeros(2,eP);
% For loop to move through xqyq
for ii=1:N
cmd(:,ii)=ikFS(a,E(:,ii),[1;1]);
B(:,ii)=[a(1,1)*cos(cmd(1,ii));a(1,1)*sin(cmd(1,ii))];
end
% snapshots at intermediate positions
plot([zeros(1,N);B(1,:);E(1,:)],[zeros(1,N);B(2,:);E(2,:)],'k')
hold on
axis equal
plot(E(1,:),E(2,:),'g','linewidth',2) % trajectory at intermediate positions
pt_size = 1000; % mark user-defined points
scatter(x(1,:),x(2,:),pt_size,'.')
xpos=E(1,1:N);
ypos=E(2,1:N);
vx = (gradient(xpos)/steps)'
vy = (gradient(ypos)/steps)'
xE = cumtrapz(linspace(0,T,N),vx)+x(1,2);
yE = cumtrapz(linspace(0,T,N),vy)+x(1,2);
plot(xE,yE)
end
Running the program with a = [2,1], N = 50, T = 1, and x = [a(1) a(1)/2 0 -a(1) -a(1)-a(2) 0 a(1)/2 a(1) a(1) 0] returns:
"Index in position 1 exceeds array bounds (must not exceed 1).
Error in trajplanner (line 23)
E(2,:)=spline(x(1,:),x(2,:),xq);"
Any suggestions on how to fix this?

Risposta accettata

Cris LaPierre
Cris LaPierre il 30 Ott 2020
I suspect the issue is your indexing of your variable x is the issue. I don't think it has a 2nd row, so your call to x(2,:) results in an error.
a = [2,1];
x = [a(1) a(1)/2 0 -a(1) -a(1)-a(2) 0 a(1)/2 a(1) a(1) 0]
x = 1×10
2 1 0 -2 -3 0 1 2 2 0
x(2,:)
Index in position 1 exceeds array bounds (must not exceed 1).

Più risposte (0)

Categorie

Scopri di più su Splines in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by