Getting an error of 'Reference to non-existent field'
    2 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Orpheus
 el 18 de Sept. de 2019
  
The function when invoked gives the error 'Reference to non-existent field 'vel'.'
function [ desired_state ] = traj_generator(t, state, waypoints)
    % This function is called with variable number of input arguments.
    % During initialization, it is 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 that is used for computing desired_state
    % waypoints: The 3xP matrix listing all the points needed in order
    % to generate the trajectory
    % desired_state: Contains all the information that is passed as output
    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[C]=pols_coef_retrn(n,wayp)
        A = zeros(8*n,8*n);
        B = zeros(1,8*n);
        p=1;
        l=0;
        for i=1:n
            k=1;
            for j=1:8
                if j==1
                    A(p,p)=1;
                    B(p)=wayp(i);
                    A(p+1,p:p+7)=[1 1 1 1 1 1 1 1];
                    B(p+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,theta)= 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
        l=l+8;
        end
        C=inv(A)*B';
    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)-0.0001;
        end
        t_index = find(traj_time>t,1) - 1;
        t_index = max(t_index,1);
        scale = (t-traj_time(t_index)) / d0(t_index);
        if(t == 0)
            desired_state.pos = waypoints0(:,1);
        else
            index = ((t_index-1)*8+1:t_index*8);
            nn=size(waypoints0(1,:));
            n=nn(2)-1;
            coeff_x = pols_coef_retrn(n,waypoints0(1,:)');
            coeff_y = pols_coef_retrn(n,waypoints0(2,:)');
            coeff_z = pols_coef_retrn(n,waypoints0(3,:)');
            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);].*(1/d0(t_index));
            desired_state.acc = [coeff_x(index)'*t_mat3(scale);coeff_y(index)'*t_mat3(scale);coeff_z(index)'*t_mat3(scale);].*((1/d0(t_index))^2);
        end
        desired_state.yaw = 0;
        desired_state.yawdot = 0;
    end
end
the error message is as follows
Reference to non-existent field 'vel'.
Error in simulation_3d (line 70)
        QP.UpdateQuadPlot(x, [desired_state.pos; desired_state.vel], time);
Error in runsim (line 28)
[t, state] = simulation_3d(trajhandle, controlhandle);
where runsim and in simulation_3d invoke the function
5 comentarios
Respuesta aceptada
  Guillaume
      
      
 el 18 de Sept. de 2019
        The way traj_generator is coded, when t is 0, it returns a structure with only 3 fields: pos, yaw and yawdot (these last two are always 0). It's only when t is not 0 that it creates the fields vel and acc.
So, indeed if t is 0 and you try to use the vel field you'll get your error. Either you mustn't use vel or acc if t is 0, or you must modify traj_generator so that it creates these fields even when t is 0:
%original code in traj_generator
        if(t == 0)
            desired_state.pos = waypoints0(:,1);
        else
replace with
        if t == 0   %brackets were completely unnecessary
            desired_state.pos = waypoints0(:,1);
            desired_state.vel = ????;  %no idea what you want there
            desired_state.acc = ????;  %no idea what you want there
        else
            %...
0 comentarios
Más respuestas (0)
Ver también
Categorías
				Más información sobre Marine and Underwater Vehicles en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
