How can I plot a UAV in 3D while controlling translation and rotation in Simulink?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am trying to create a 3D plot of a UAV that I will be able to control in rotation and translation. I am trying to put the Matlab function into Simulink but it keeps giving me the following error:
Error due to multiple causes.
Undefined function 'drawSpacecraft' for input arguments of type 'double'.
Error in 'New_Simulation/Interpreted MATLAB Function' while evaluating expression.
I have attached a picture of the Simulink diagram and it has constants that go into slider gains which then go into a mux and into the interpreted MATLAB function where I entered drawSpacecraft(uu)

uu = ones(1,13);
function drawSpacecraft(uu)
% process inputs to function
pn = uu(1); % inertial North position
pe = uu(2); % inertial East position
pd = uu(3);
u = uu(4);
v = uu(5);
w = uu(6);
phi = uu(7); % roll angle
theta = uu(8); % pitch angle
psi = uu(9); % yaw angle
p = uu(10); % roll rate
q = uu(11); % pitch rate
r = uu(12); % yaw rate
t = uu(13); % time
% define persistent variables
persistent spacecraft_handle;
persistent Vertices
persistent Faces
persistent facecolors
% first time function is called, initialize plot and persistent vars
if t==0
figure(1), clf
[Vertices, Faces, facecolors] = defineSpacecraftBody;
spacecraft_handle = drawSpacecraftBody(Vertices,Faces,facecolors,...
pn,pe,pd,phi,theta,psi,...
[],'normal');
title('Spacecraft')
xlabel('East')
ylabel('North')
zlabel('-Down')
view(32,47) % set the vieew angle for figure
axis([-10,10,-10,10,-10,10]);
hold on
% at every other time step, redraw base and rod
else
drawSpacecraftBody(Vertices,Faces,facecolors,...
pn,pe,pd,phi,theta,psi,...
spacecraft_handle);
end
end
%=======================================================================
%
% return handle if 3rd argument is empty, otherwise use 3rd arg as handle
%=======================================================================
%
function handle = drawSpacecraftBody(V,F,patchcolors,...
pn,pe,pd,phi,theta,psi,...
handle,mode)
V = rotate(V', phi, theta, psi)'; % rotate spacecraft
V = translate(V', pn, pe, pd)'; % translate spacecraft
% transform vertices from NED to XYZ (for matlab rendering)
R = [...
0, 1, 0;...
1, 0, 0;...
0, 0, -1;...
];
V = V*R;
if isempty(handle)
handle = patch('Vertices', V, 'Faces', F,...
'FaceVertexCData',patchcolors,...
'FaceColor','flat',...
'EraseMode', mode);
else
set(handle,'Vertices',V,'Faces',F);
drawnow
end
end
%%%%%%%%%%%%%%%%%%%%%%%
function XYZ=rotate(XYZ,phi,theta,psi)
% define rotation matrix
R_roll = [...
1, 0, 0;...
0, cos(phi), -sin(phi);...
0, sin(phi), cos(phi)];
R_pitch = [...
cos(theta), 0, sin(theta);...
0, 1, 0;...
-sin(theta), 0, cos(theta)];
R_yaw = [...
cos(psi), -sin(psi), 0;...
sin(psi), cos(psi), 0;...
0, 0, 1];
R = R_roll*R_pitch*R_yaw;
% rotate vertices
XYZ = R*XYZ;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% translate vertices by pn, pe, pd
function XYZ = translate(XYZ,pn,pe,pd)
XYZ = XYZ + repmat([pn;pe;pd],1,size(XYZ,2));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define spacecraft vertices and faces
function [V,F,colors] = defineSpacecraftBody()
% Define the vertices (physical location of vertices
V = [...
1 1 0;... % point 1
1 -1 0;... % point 2
-1 -1 0;... % point 3
-1 1 0;... % point 4
1 1 -2;... % point 5
1 -1 -2;... % point 6
-1 -1 -2;... % point 7
-1 1 -2;... % point 8
1.5 1.5 0;... % point 9
1.5 -1.5 0;... % point 10
-1.5 -1.5 0;... % point 11
-1.5 1.5 0;... % point 12
];
% define faces as a list of vertices numbered above
F = [...
1, 2, 6, 5;... % front
4, 3, 7, 8;... % back
1, 5, 8, 4;... % right
2, 6, 7, 3;... % left
5, 6, 7, 8;... % top
9, 10, 11, 12;... % bottom
];
% define colors for each face
myred = [1, 0, 0];
mygreen = [0, 1, 0];
myblue = [0, 0, 1];
myyellow = [1, 1, 0];
mycyan = [0, 1, 1];
colors = [...
myred;... % front
mygreen;... % back
myblue;... % right
myyellow;... % left
mycyan;... % top
mycyan;... % bottom
];
end
0 Kommentare
Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!