Effacer les filtres
Effacer les filtres

I'd like to do various things, including controlling the dimensions of the plot box. What I do here with xlim, ylim, and zlim doens't work.

3 vues (au cours des 30 derniers jours)
I put the following together. It runs, but it doesn't produce an image that moves on the screen. Instead, the screen moves around the image. I'd prefer the former. I try to expand the scope of the plot box using xlim, ylim, and zlim, but it doesn't work. I also have a question regarding what is being done with XYZ and XYZ'. I don't understand what is being done with the single quote mark. Can anyone help me or explain the meaning of the single quote mark? Thank you in advance.
pn=20;
pe=0;
pd=-5;
phi=0;
theta=0;
psi=0;
handle=[];
%Simulation parameters
dpn=-0.2; %Change in position.
dpsi=0.05; %Change in yaw angle.
simlength=100;%Number of Sim Steps
%Draw and update the plane's position.
for k = 1:simlength
pn = pn+dpn;
psi = psi+dpsi;
%Draw or update the plane.
handle = drawPlaneBody(pn,pe,pd,phi,theta,psi,handle);
pause(0.1); %Pause to visualize the movement
end
function handle = drawPlaneBody(pn,pe,pd,phi,theta,psi,handle)
%define points on plane in local NED coordintates
NED = airplanepoints;
%rotate plane by (phi; theta; psi)
NED = rotate(NED, phi, theta, psi);
%translate plane to [pn; pe; pd]
NED = translate(NED, pn, pe, pd);
%transform vertices from NED to XYZ
R = [...
0, 1, 0;...
1, 0 , 0;...
0, 0, -1;...
];
XYZ = R* NED';
%plot plane
if isempty(handle)
handle = plot3(XYZ(1,:),XYZ(2,:),XYZ(3,:),'b');
xlim = ([-100 100]);
ylim = ([-100 100]);
zlim = ([0 100]);
grid on;%show grid
xlabel('X');ylabel('Y');zlabel('Z');%label axes
axis equal;%Maintain aspect ratio
else
set(handle, 'XData', XYZ(1,:), 'YData',XYZ(2,:), 'ZData',XYZ(3,:));
drawnow
end
end
function XYZ = airplanepoints
%define points on the aircraft in local NED
XYZ = [...
0 0 0;%point1
-2 1 1;%point2
-2 1 -1;%point3
0 0 0;%point1
-2 -1 1;%point4
-2 -1 -1;%point5
0 0 0;%point1
-2 1 1;%point2
-2 -1 1;%point4
0 0 0;%point1
-2 1 -1;%point3
-2 -1 -1;%point5
0 0 0;%point1
-2 1 1;%point2
-18 0 0;%point6
-2 1 -1;%point3
-18 0 0;%point6
-2 -1 -1;%point5
-18 0 0;%point6
-2 -1 1;%point4
-18 0 0;%point6
-2 1 1;%point2
0 0 0;%point1
-5 0 0;%point7
-5 -10 0;%point8
-8 -10 0;%point9
-8 10 0;%point10
-5 10 0;%point11
-5 0 0;%point7
-15.5 0 0;%point12
-15.5 2 0;%point13
-17.5 2 0;%point14
-17.5 -2 0;%point15
-15.5 -2 0;%point16
-15.5 0 0;%point12
-18 0 0;%point6
-18 0 -3;%point17
-15.5 0 0;%point15
-18 0 0;%point16
];
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';
XYZ = XYZ';
end
function XYZ = translate(XYZ, pn, pe, pd)
XYZ = XYZ' + repmat([pn;pe;pd],1,size(XYZ,1));
XYZ=XYZ';
end
  3 commentaires
Steven Lord
Steven Lord le 31 Mai 2024
Déplacé(e) : Cris LaPierre le 31 Mai 2024
What does "It craps out" mean in this context?
  • Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
  • Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
  • Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
DJ V
DJ V le 31 Mai 2024
Thank you very much. I will be posting this again because of other problems I have encountered. It isn't fair to award a solution based on several different questions on the same code.

Connectez-vous pour commenter.

Réponse acceptée

Cris LaPierre
Cris LaPierre le 31 Mai 2024
Modifié(e) : Cris LaPierre le 31 Mai 2024
You are creating varlables named xlim, ylim, and zlim rather than setting the corresponding axis properties. Also, axis equal changes the axis limits, so is currently undoing what you are trying to do. Try this instead.
if isempty(handle)
handle = plot3(XYZ(1,:),XYZ(2,:),XYZ(3,:),'b');
xlim([-100 100]);
ylim([-100 100]);
zlim([0 100]);
grid on;%show grid
xlabel('X');ylabel('Y');zlabel('Z');%label axes
else
  3 commentaires
Cris LaPierre
Cris LaPierre le 31 Mai 2024
First, a single quote tranposes your data.
A = 1:3
A = 1x3
1 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A'
ans = 3x1
1 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Your code has the same words, but your syntax is producing a completely different result. Making the recommended change to your code will fix the axis limits so that it is the plane that moves, not the background.
pn=20;
pe=0;
pd=-5;
phi=0;
theta=0;
psi=0;
handle=[];
%Simulation parameters
dpn=-0.2; %Change in position.
dpsi=0.05; %Change in yaw angle.
simlength=100;%Number of Sim Steps
%Draw and update the plane's position.
for k = 1:simlength
pn = pn+dpn;
psi = psi+dpsi;
%Draw or update the plane.
handle = drawPlaneBody(pn,pe,pd,phi,theta,psi,handle);
pause(0.1); %Pause to visualize the movement
end
function handle = drawPlaneBody(pn,pe,pd,phi,theta,psi,handle)
%define points on plane in local NED coordintates
NED = airplanepoints;
%rotate plane by (phi; theta; psi)
NED = rotate(NED, phi, theta, psi);
%translate plane to [pn; pe; pd]
NED = translate(NED, pn, pe, pd);
%transform vertices from NED to XYZ
R = [...
0, 1, 0;...
1, 0 , 0;...
0, 0, -1;...
];
XYZ = R* NED';
%plot plane
if isempty(handle)
handle = plot3(XYZ(1,:),XYZ(2,:),XYZ(3,:),'b');
xlim([-100 100]);
ylim([-100 100]);
zlim([0 100]);
grid on;%show grid
xlabel('X');ylabel('Y');zlabel('Z');%label axes
else
set(handle, 'XData', XYZ(1,:), 'YData',XYZ(2,:), 'ZData',XYZ(3,:));
drawnow
end
end
function XYZ = airplanepoints
%define points on the aircraft in local NED
XYZ = [...
0 0 0;%point1
-2 1 1;%point2
-2 1 -1;%point3
0 0 0;%point1
-2 -1 1;%point4
-2 -1 -1;%point5
0 0 0;%point1
-2 1 1;%point2
-2 -1 1;%point4
0 0 0;%point1
-2 1 -1;%point3
-2 -1 -1;%point5
0 0 0;%point1
-2 1 1;%point2
-18 0 0;%point6
-2 1 -1;%point3
-18 0 0;%point6
-2 -1 -1;%point5
-18 0 0;%point6
-2 -1 1;%point4
-18 0 0;%point6
-2 1 1;%point2
0 0 0;%point1
-5 0 0;%point7
-5 -10 0;%point8
-8 -10 0;%point9
-8 10 0;%point10
-5 10 0;%point11
-5 0 0;%point7
-15.5 0 0;%point12
-15.5 2 0;%point13
-17.5 2 0;%point14
-17.5 -2 0;%point15
-15.5 -2 0;%point16
-15.5 0 0;%point12
-18 0 0;%point6
-18 0 -3;%point17
-15.5 0 0;%point15
-18 0 0;%point16
];
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';
XYZ = XYZ';
end
function XYZ = translate(XYZ, pn, pe, pd)
XYZ = XYZ' + repmat([pn;pe;pd],1,size(XYZ,1));
XYZ=XYZ';
end
DJ V
DJ V le 31 Mai 2024
Thank you very much. I will be posting this again because of other problems I have encountered. It isn't fair to award a solution based on several different questions on the same code.

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 31 Mai 2024
Instead of manually modifying the coordinates, I'd consider using the hgtransform function in conjunction with makehgtform.
  1 commentaire
DJ V
DJ V le 31 Mai 2024
Thank you very much. I will be posting this again because of other problems I have encountered. It isn't fair to award a solution based on several different questions on the same code.

Connectez-vous pour commenter.

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by