Code generation requires variable to be fully assigned variable before subscripting it error in Simulink
显示 更早的评论
Dear all,
I am implementing the forward and inverse kinematics of a 6 dof robot arm. I was able to run the code in MATLAB alone and it worked well but when I tried to do the same in SIMULINK, I get an error to define 'theta' and 'dtheta' (the variables in my program) before subscripting it.
Can anyone help me resolve this error?
Appreciate any advice or help
Regards
10 个评论
Walter Roberson
2019-7-8
Undefined function or variable 'Trial'.
(in Path Planning)
Gautami Golani
2019-7-8
编辑:Gautami Golani
2019-7-8
Walter Roberson
2019-7-8
tvals = 0:tao:T;
numt = length(tvals);
theta = zeros(6, numt);
for i = 1 : numt
t = tvals(i);
f_theta = Trial(theta(:,i));
J = Jacobi(theta(:,i));
PJ = pinv(J);
theta(:,i+1) = theta(:,i) + ((tao*PJ)*(dr -(k*(f_theta - radd))));
theta_dot(:,i)=((tao*PJ)*(dr -(k*(f_theta - radd))));
end
Gautami Golani
2019-7-8
Walter Roberson
2019-7-8
Your current code has
i = 1;
for t=0:tao:T
stuff
theta(:,i+1) = theta(:,i) + ((tao*PJ)*(dr -(k*(f_theta - radd))));
theta_dot(:,i)=((tao*PJ)*(dr -(k*(f_theta - radd))));
i = i + 1;
end
Now 0:tao:T is 1001 entries, so up to theta(:,1002) and theta_dot(:,1001) will be stored into, and i will be left as 1002. Then you have
f_theta = Trial(theta(:,i));
J = Jacobi(theta(:,i));
PJ = pinv(J);
theta_dot(:,i)=((tao*PJ)*(dr -(k*(f_theta - radd))));
so i there will be 1002 and theta_dot(:,1002) will be stored into, which completes the symmetry of theta(:,1002) already having been stored into.
Then you have
k10 = T/tao+1;
which is k10 = 10/0.01 + 1 so k10 = 1001 .
theta1(1:k10)=theta(1,1:k10);
with theta1 undefined at that point, that would extract the first 1001 columns from theta row 1 and store them into theta1, and likewise for the theta2 to theta6 variables and the dtheta1 to dtheta6 variables. Then
theta = [theta1;theta2;theta3;theta4;theta5;theta6];
dtheta = [dtheta1;dtheta2;dtheta3;dtheta4;dtheta5;dtheta6];
this appears to be reconstructing theta and dtheta, just without the column 1002 that was stored into.
... and then the code ends, without having stored the theta(:,1002) and dtheta(:,1002) into anything. It is not clear as to why you bother to compute dtheta(:,1002) ?
Also, your function appears to be calculating kinematics for time 0 to 10. That would not normally be done in Simulink: normally you would have a current time and a current state and be calculating the next step. The next step could potentially involve forecasting paths, but the output would normally be the single next step, not all of the steps.
Gautami Golani
2019-7-9
Walter Roberson
2019-7-9
编辑:Walter Roberson
2019-7-9
From Workspace block. You will want to use a timeseries or else use a 2d array in which each row corresponds to a time and the first column indicates the time the row is for.
Gautami Golani
2019-7-9
Walter Roberson
2019-7-9
You were talking about running the code at the MATLAB level, before starting the Simulink model. You can create the timeseries at the MATLAB level using timeseries()
Gautami Golani
2019-7-18
回答(1 个)
Srivardhan Gadila
2019-7-18
0 个投票
Hi Gautami,
“Callbacks” in Simulink can be used to initialize any variables required for the model simulation.
You might initialize the variables “theta” and “dtheta” in the model callbacks (In Simulink: View->Property Inspector->Callbacks).
You can refer to https://www.mathworks.com/help/simulink/ug/model-callbacks.html
类别
在 帮助中心 和 File Exchange 中查找有关 Programmatic Model Editing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!