From Workspace Simulink dimension error

Hi, I have a problem with the From Workspace block. I have 4 vars in the workspace of MATLAB and I need to export Simulink with that block. How I correct that error?

Risposte (1)

Ameer Hamza
Ameer Hamza il 2 Mag 2020
These variables are constant. You can use the constant block to get their value in Simulink: https://www.mathworks.com/help/releases/R2020a/simulink/slref/constant.html and in the value field, write the name of the variable. The Simulink constant block is able to read the value from the base workspace.

18 Commenti

Not is a constant, this change respect to time
What is the name of these variables in the image you shared?
TORQUEM1,TORQUEM2,TORQUEM3,TORQUEM4 but in the image already finish the simulation time but really are variables
To use with from workspace block. The matrix must have two columns. The first column is the time values, and the second column is data. You need to define these four variables using the format I described.
Yeah, but How I do that?, a example please
Something like this
T_stop = % wtite the stop time
TORQUEM1 = % column vector of values TORQUEM1
TORQUEM2 = % column vector of values TORQUEM2
TORQUEM3 = % column vector of values TORQUEM3
TORQUEM4 = % column vector of values TORQUEM4
t = linspace(0, T_stop, numel(TORQUEM1)).';
TORQUEM1 = [t TORQUEM1];
TORQUEM2 = [t TORQUEM2];
TORQUEM3 = [t TORQUEM3];
TORQUEM4 = [t TORQUEM4];
Yes, but I do not save the all values of the variable, my variable torque is 1x1 double that change with the time. See the image with my code and the workspace
I do not understand the simulink error, I do not understand why the From Workspace do not work and give me that error.
How does TORQUEM1 change with time? You said that you want to input the value of TORQUEM1 in your model, but if it just a single value, then From Workspace cannot read it. From Workspace block needs all the values in advance, it cannot read a changing variable.
That vars are not inputs, are a vars calculate with the Rotor hub (D) and rotor thrust (T) inside of a function that change with the time. See the equation of the vars.
Why are you using asignin() to write it base workspace? Simply output the variable from the MATLAB function block and use it in your Simulink model.
How????? I attach the code. The interest lines are in the forces and torques section.
function [sys,x0,str,ts] = quadrotor_dynamics(t,x,u,flag, quad, x0, groundflag)
% Flyer2dynamics lovingly coded by Paul Pounds, first coded 12/4/04
% A simulation of idealised X-4 Flyer II flight dynamics.
% version 2.0 2005 modified to be compatible with latest version of Matlab
% version 3.0 2006 fixed rotation matrix problem
% version 4.0 4/2/10, fixed rotor flapping rotation matrix bug, mirroring
% version 5.0 8/8/11, simplified and restructured
% version 6.0 25/10/13, fixed rotation matrix/inverse wronskian definitions, flapping cross-product bug
warning off MATLAB:divideByZero
global groundFlag;
% New in version 2:
% - Generalised rotor thrust model
% - Rotor flapping model
% - Frame aerodynamic drag model
% - Frame aerodynamic surfaces model
% - Internal motor model
% - Much coolage
% Version 1.3
% - Rigid body dynamic model
% - Rotor gyroscopic model
% - External motor model
%ARGUMENTS
% u Reference inputs 1x4
% tele Enable telemetry (1 or 0) 1x1
% crash Enable crash detection (1 or 0) 1x1
% init Initial conditions 1x12
%INPUTS
% u = [N S E W]
% NSEW motor commands 1x4
%CONTINUOUS STATES
% z Position 3x1 (x,y,z)
% v Velocity 3x1 (xd,yd,zd)
% n Attitude 3x1 (Y,P,R)
% o Angular velocity 3x1 (wx,wy,wz)
% w Rotor angular velocity 4x1
%
% Notes: z-axis downward so altitude is -z(3)
%CONTINUOUS STATE MATRIX MAPPING
% x = [z1 z2 z3 n1 n2 n3 z1 z2 z3 o1 o2 o3 w1 w2 w3 w4]
%INITIAL CONDITIONS
n0 = [0 0 0]; % n0 Ang. position initial conditions 1x3
v0 = [0 0 0]; % v0 Velocity Initial conditions 1x3
o0 = [0 0 0]; % o0 Ang. velocity initial conditions 1x3
init = [x0 n0 v0 o0]; % x0 is the passed initial position 1x3
groundFlag = groundflag;
%CONTINUOUS STATE EQUATIONS
% z` = v
% v` = g*e3 - (1/m)*T*R*e3
% I*o` = -o X I*o + G + torq
% R = f(n)
% n` = inv(W)*o
% Dispatch the flag.
%
switch flag
case 0
[sys,x0,str,ts]=mdlInitializeSizes(init, quad); % Initialization
case 1
sys = mdlDerivatives(t,x,u, quad); % Calculate derivatives
case 3
sys = mdlOutputs(t,x, quad); % Calculate outputs
case { 2, 4, 9 } % Unused flags
sys = [];
otherwise
error(['Unhandled flag = ',num2str(flag)]); % Error handling
end
end % End of flyer2dynamics
%==============================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the
% S-function.
%==============================================================
%
function [sys,x0,str,ts] = mdlInitializeSizes(init, quad)
%
% Call simsizes for a sizes structure, fill it in and convert it
% to a sizes array.
%
sizes = simsizes;
sizes.NumContStates = 12;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 12;
sizes.NumInputs = 4;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
%
% Initialize the initial conditions.
x0 = init;
%
% str is an empty matrix.
str = [];
%
% Generic timesample
ts = [0 0];
if quad.verbose
disp(sprintf('t\t\tz1\t\tz2\t\tz3\t\tn1\t\tn2\t\tn3\t\tv1\t\tv2\t\tv3\t\to1\t\to2\t\to3\t\tw1\t\tw2\t\tw3\t\tw4\t\tu1\t\tu2\t\tu3\t\tu4'))
end
end % End of mdlInitializeSizes.
%==============================================================
% mdlDerivatives
% Calculate the state derivatives for the next timestep
%==============================================================
%
function sys = mdlDerivatives(t,x,u, quad)
global a1s b1s groundFlag
%CONSTANTS
%Cardinal Direction Indicies
N = 1; % N 'North' 1x1
E = 2; % S 'South' 1x1
S = 3; % E 'East' 1x1
W = 4; % W 'West' 1x1
D(:,1) = [quad.d;0;quad.h]; % Di Rotor hub displacements 1x3
D(:,2) = [0;quad.d;quad.h];
D(:,3) = [-quad.d;0;quad.h];
D(:,4) = [0;-quad.d;quad.h];
%Body-fixed frame references
e1 = [1;0;0]; % ei Body fixed frame references 3x1
e2 = [0;1;0];
e3 = [0;0;1];
%EXTRACT ROTOR SPEEDS FROM U
w = u(1:4);
%EXTRACT STATES FROM X
z = x(1:3); % position in {W}
n = x(4:6); % RPY angles {W}
v = x(7:9); % velocity in {W}
o = x(10:12); % angular velocity in {W}
%PREPROCESS ROTATION AND WRONSKIAN MATRICIES
phi = n(1); % yaw
the = n(2); % pitch
psi = n(3); % roll
% rotz(phi)*roty(the)*rotx(psi)
R = [cos(the)*cos(phi) sin(psi)*sin(the)*cos(phi)-cos(psi)*sin(phi) cos(psi)*sin(the)*cos(phi)+sin(psi)*sin(phi); %BBF > Inertial rotation matrix
cos(the)*sin(phi) sin(psi)*sin(the)*sin(phi)+cos(psi)*cos(phi) cos(psi)*sin(the)*sin(phi)-sin(psi)*cos(phi);
-sin(the) sin(psi)*cos(the) cos(psi)*cos(the)];
%Manual Construction
% Q3 = [cos(phi) -sin(phi) 0;sin(phi) cos(phi) 0;0 0 1]; % RZ %Rotation mappings
% Q2 = [cos(the) 0 sin(the);0 1 0;-sin(the) 0 cos(the)]; % RY
% Q1 = [1 0 0;0 cos(psi) -sin(psi);0 sin(psi) cos(psi)]; % RX
% R = Q3*Q2*Q1 %Rotation matrix
%
% RZ * RY * RX
iW = [0 sin(psi) cos(psi); %inverted Wronskian
0 cos(psi)*cos(the) -sin(psi)*cos(the);
cos(the) sin(psi)*sin(the) cos(psi)*sin(the)] / cos(the);
if any(w == 0)
% might need to fix this, preculudes aerobatics :(
% mu becomes NaN due to 0/0
error('quadrotor_dynamics: not defined for zero rotor speed');
end
%ROTOR MODEL
for i=[N E S W] %for each rotor
%Relative motion
Vr = cross(o,D(:,i)) + v;
mu = sqrt(sum(Vr(1:2).^2)) / (abs(w(i))*quad.r); %Magnitude of mu, planar components
lc = Vr(3) / (abs(w(i))*quad.r); %Non-dimensionalised normal inflow
li = mu; %Non-dimensionalised induced velocity approximation
alphas = atan2(lc,mu);
j = atan2(Vr(2),Vr(1)); %Sideslip azimuth relative to e1 (zero over nose)
J = [cos(j) -sin(j);
sin(j) cos(j)]; %BBF > mu sideslip rotation matrix
%Flapping
beta = [((8/3*quad.theta0 + 2*quad.theta1)*mu - 2*(lc)*mu)/(1-mu^2/2); %Longitudinal flapping
0;];%sign(w) * (4/3)*((Ct/sigma)*(2*mu*gamma/3/a)/(1+3*e/2/r) + li)/(1+mu^2/2)]; %Lattitudinal flapping (note sign)
beta = J'*beta; %Rotate the beta flapping angles to longitudinal and lateral coordinates.
a1s(i) = beta(1) - 16/quad.gamma/abs(w(i)) * o(2);
b1s(i) = beta(2) - 16/quad.gamma/abs(w(i)) * o(1);
%Forces and torques
T(:,i) = quad.Ct*quad.rho*quad.A*quad.r^2*w(i)^2 * [-cos(b1s(i))*sin(a1s(i)); sin(b1s(i));-cos(a1s(i))*cos(b1s(i))]; %Rotor thrust, linearised angle approximations
Q(:,i) = -quad.Cq*quad.rho*quad.A*quad.r^3*w(i)*abs(w(i)) * e3; %Rotor drag torque - note that this preserves w(i) direction sign
tau(:,i) = cross(T(:,i),D(:,i)); %Torque due to rotor thrust
if i==1
torque1=tau(1);
assignin('base','TORQUEM1',torque1);
elseif i==2
torque2=tau(2);
assignin('base','TORQUEM2',torque2);
elseif i==3
torque3=tau(3);
assignin('base','TORQUEM3',torque3);
else
torque4=tau(4);
assignin('base','TORQUEM4',torque4);
end
end
%RIGID BODY DYNAMIC MODEL
dz = v;
dn = iW*o;
dv = quad.g*e3 + R*(1/quad.M)*sum(T,2);
% vehicle can't fall below ground
if groundFlag && (z(3) > 0)
z(3) = 0;
dz(3) = 0;
end
do = inv(quad.J)*(cross(-o,quad.J*o) + sum(tau,2) + sum(Q,2)); %row sum of torques
sys = [dz;dn;dv;do]; %This is the state derivative vector
end % End of mdlDerivatives.
%==============================================================
% mdlOutputs
% Calculate the output vector for this timestep
%==============================================================
%
function sys = mdlOutputs(t,x, quad)
%TELEMETRY
if quad.verbose
disp(sprintf('%0.3f\t',t,x))
end
% compute output vector as a function of state vector
% z Position 3x1 (x,y,z)
% v Velocity 3x1 (xd,yd,zd)
% n Attitude 3x1 (Y,P,R)
% o Angular velocity 3x1 (Yd,Pd,Rd)
n = x(4:6); % RPY angles
phi = n(1); % yaw
the = n(2); % pitch
psi = n(3); % roll
% rotz(phi)*roty(the)*rotx(psi)
R = [cos(the)*cos(phi) sin(psi)*sin(the)*cos(phi)-cos(psi)*sin(phi) cos(psi)*sin(the)*cos(phi)+sin(psi)*sin(phi); %BBF > Inertial rotation matrix
cos(the)*sin(phi) sin(psi)*sin(the)*sin(phi)+cos(psi)*cos(phi) cos(psi)*sin(the)*sin(phi)-sin(psi)*cos(phi);
-sin(the) sin(psi)*cos(the) cos(psi)*cos(the)];
iW = [0 sin(psi) cos(psi); %inverted Wronskian
0 cos(psi)*cos(the) -sin(psi)*cos(the);
cos(the) sin(psi)*sin(the) cos(psi)*sin(the)] / cos(the);
% return velocity in the body frame
sys = [ x(1:6);
inv(R)*x(7:9); % translational velocity mapped to body frame
iW*x(10:12)]; % RPY rates mapped to body frame
%sys = [x(1:6); iW*x(7:9); iW*x(10:12)];
%sys = x;
end
% End of mdlOutputs.
Output another variable like this
function [sys,x0,str,ts,torque] = quadrotor_dynamics(t,x,u,flag, quad, x0, groundflag)
and change the block with assignin like this
if i==1
torque=tau(1);
elseif i==2
torque=tau(2);
elseif i==3
torque=tau(3);
else
torque=tau(4);
end
ok and how I call the variable in the Simulink??
You will see another output port on the block.
Do not run. See this.
It shows the lines containing torque=... does not even run in each iteration. You can add the line
torque = 0
at the beginning of the function.

Accedi per commentare.

Categorie

Prodotti

Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by