Code generation requires variable to be fully assigned variable before subscripting it error in Simulink
Show older comments
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 Comments
Walter Roberson
on 8 Jul 2019
Undefined function or variable 'Trial'.
(in Path Planning)
Gautami Golani
on 8 Jul 2019
Edited: Gautami Golani
on 8 Jul 2019
Walter Roberson
on 8 Jul 2019
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
on 8 Jul 2019
Walter Roberson
on 8 Jul 2019
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
on 9 Jul 2019
Walter Roberson
on 9 Jul 2019
Edited: Walter Roberson
on 9 Jul 2019
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
on 9 Jul 2019
Walter Roberson
on 9 Jul 2019
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
on 18 Jul 2019
Answers (1)
Srivardhan Gadila
on 18 Jul 2019
0 votes
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
Categories
Find more on Programmatic Model Editing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!