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 Comments

Undefined function or variable 'Trial'.
(in Path Planning)
Dear Sir,
Sorry about that.
I just attached all the required files.
Regards
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
Dear Sir,
Thank you for this.
But I still get an error for theta1...theta6, thetadot and dtheta1....dtheta6 when running the SIMULINK program
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.
Dear Sir,
Thank you for this explanation.
I have removed the computation for dtheta(:,1002) that you had mentioned about.
I just had one doubt. If I keep this code as the main code and run it as the MATLAB script, how can I load the theta and dtheta values in SIMULINK?
Should I define it in a MATLAB function block?
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.
Sir, could you explain how i could create the timeseries with the From Workspace block or give any example?
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()
Thank you for your help sir.
I was succsesfully able to resolve the error.

Sign in to comment.

Answers (1)

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).

Categories

Asked:

on 8 Jul 2019

Commented:

on 18 Jul 2019

Community Treasure Hunt

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

Start Hunting!