Getting an 'Array indices must be positive integers or logical values' error

조회 수: 1 (최근 30일)
In the given code, waypoints is a 3x3 matrix. The error comes in line 8.
persistent waypoints0 traj_time d0
if nargin > 2
d = waypoints(:,2:end) - waypoints(:,1:end-1);
d0 = 2 * sqrt(d(1,:).^2 + d(2,:).^2 + d(3,:).^2);
traj_time = [0, cumsum(d0)];
waypoints0 = waypoints;
else
if t >= traj_time(end)
t = traj_time(end);
end
t_index = find(traj_time > t,1) - 1;
if (t_index == 0)
t_index = 1;
end
if(t == 0)
desired_state.pos = waypoints0(:,1);
else
scale = (t-traj_time(t_index)) / d0(t_index);
index = [(t_index-1)*8+1:t_index*8];
end
end
  댓글 수: 4
Guillaume
Guillaume 2019년 9월 17일
We really need the function line that this code goes into. The full text of the error message would be extremely useful as well. Also, tell us if it's when the persistent variables are initialised (i.e. nargin > 2) or when it's using them (nargin <= 2).
It's also not clear what the 3rd input would be. I can see just one input needed for initialisation (waypoints) and one input needed afterward (t).
Orpheus
Orpheus 2019년 9월 17일
Here is the original function
function [ desired_state ] = traj_generator(t, state, waypoints)
% This function is called with variable number of input arguments.
% During initialization, it will be called with arguments
% trajectory_generator([], [], waypoints) and later, while testing, it will be
% called with only t and state as arguments.
%
% t,state: time and current state (same variable as "state" in controller)
% that is used for computing desired_state
%
% waypoints: The 3xP matrix listing all the points that have to be used in order
% to generate the trajectory.
function[t_m] =t_mat1(t)
t_m=[1 t^1 t^2 t^3 t^4 t^5 t^6 t^7]';
end
function[t_m_dot] =t_mat2(t)
t_m_dot = [0 1 2*t 3*t^2 4*t^3 5*t^4 6*t^5 7*t^6]';
end
function[t_m_ddot] =t_mat3(t)
t_m_ddot = [0 0 2 6*t 12*t^2 20*t^3 30*t^4 42*t^5]';
end
function[A,B]=pols_coef_retrn(n,wayp)
A = zeros(8*n,8*n);
B = zeros(8*n,1);
p=1;
l=1;
for i=1:n
k=1;
l=p;
for j =1:8
if j==1
A(p,p)=1;B(p,1)=wayp(i);
A(p+1,p:p+7)=[1 1 1 1 1 1 1];B(p+1,1)=wayp(i+1);
end
p=p+1;
if j>=3
for theta = l+k:l+6
if i==1 && k<=4
A(j,k+1)=1;
elseif i==n
if k<=4
A(l+j,:)= factorial(theta-l)/(factorial(k)^2);
elseif k==5
A(l+j,l+7)=1;
A(l+j,8)=-1;
elseif k==6
A(l+j,l+6)=1;
A(l+j,l-3)=-1;
A(l+j,l+j)=-1;
end
else
A(l+j,theta)=-(factorial(theta-l)/(factorial(k)^2));
if theta==(l+6)
A(l+j,theta+2+k)=1;
end
end
end
k=k+1;
end
end
end
end
persistent waypoints0 traj_time d0
if nargin > 2
d = waypoints(:,2:end) - waypoints(:,1:end-1);
d0 = 2 * sqrt(d(1,:).^2 + d(2,:).^2 + d(3,:).^2);
traj_time = [0, cumsum(d0)];
waypoints0 = waypoints;
else
if t >= traj_time(end)
t = traj_time(end);
end
t_index = find(traj_time > t,1) - 1;
if (t_index == 0)
t_index = 1;
end
if(t == 0)
desired_state.pos = waypoints0(:,1);
else
scale = (t-traj_time(t_index)) / d0(t_index);
index = [(t_index-1)*8+1:t_index*8];
nn=size(waypoints0(1,:))
n=nn(2)
Coeffmat_x,Value_x = pols_coef_retrn(n,waypoints0(1,:)');
Coeffmat_y,Value_y = pols_coef_retrn(n,waypoints0(2,:)');
Coeffmat_z,Value_z = pols_coef_retrn(n,waypoints0(3,:)');
coeff_x=Value_x/Coeffmat_x;
coeff_y=Value_y/Coeffmat_y;
coeff_z=Value_z/Coeffmat_z;
desired_state.pos = [coeff_x(index)'*t_mat1(scale);coeff_y(index)'*t_mat1(scale);coeff_z(index)'*t_mat1(scale);]
desired_state.vel = [coeff_x(index)'*t_mat2(scale);coeff_y(index)'*t_mat2(scale);coeff_z(index)'*t_mat2(scale);]
desired_state.acc = [coeff_x(index)'*t_mat3(scale);coeff_y(index)'*t_mat3(scale);coeff_z(index)'*t_mat3(scale);]
end
desired_state.yaw = 0;
desired_state.yawdot = 0;
end
end
Here is the error message
Array indices must be positive integers or logical values.
Error in traj_generator (line 110)
if t >= traj_time(end)
Error in runsim (line 20)
trajhandle(waypoints);

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

채택된 답변

Guillaume
Guillaume 2019년 9월 17일
편집: Guillaume 2019년 9월 17일
You should read the comments at the top of the function and understand them.
In particular, the function must be initialised first by calling it with
trajectory_generator([], [], waypoints);
where waypoints is a 3xN matrix.
Only then can you use it with
desired_state = traj_generator(t, state);
The error you see occurs when traj_time is empty (end is indeed an invalid index in that case). And traj_time can only be empty if the function has not been initialised.
Granted, the function should check that it's been initialised before using the persistent variables. You should add than check.
...and take out the nested functions which should be local functions
...and indent the code properly to make it easier to read and debug
...and fix all the missing semicolons and any other mlint warning

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by