I have a problem involving textbook MATLAB and use of handles and functions

1 visualización (últimos 30 días)
DJ V
DJ V el 30 de Mayo de 2024
Comentada: DJ V el 31 de Mayo de 2024
I'm having problems figuring out how to get some MATLAB code from a textbook to run. The code follows. The only original code statements are the first seven lines. I was trying to provide some basic data to get the code to draw a plane. I did have to add some "end" statements to the code, including the end at the bottom. I would like to get this code to draw an image and move it around a screen, but this code is supposed to do that. I need to learn how to get the code to run. I have a number of additional (or perhaps core) questions.
Is "fred" an appropriate handle reference?
Do I need to define a handle reference?
How do I call drawPlaneBody?
Can you help me to get this code (from a textbook) to work?
Thank you most sincerely.
pn = 20;
pe=0;
pd=0;
phi =0;
theta=0;
psi=0;
handle="fred";
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,:),'Erasemode', mode);
else
set(handle, 'XData', XYZ(1,:), 'YData',XYZ(2,:), 'ZData',XYZ(3,:));
drawnow
end
function XYZ = airplanepoints
%define points on the aircraft in local NED
coordinates
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 2;%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;
end
function XYZ = translate(XYZ, pn, pe, pd)
XYZ = XYZ + repmat([pn;pe;pd],1,size(XYZ,2));
end
end

Respuestas (1)

Milan Bansal
Milan Bansal el 30 de Mayo de 2024
Hi DJ V,
Here are the answer to your doubts.
Is "fred" an appropriate handle reference?
In MATLAB, a handle is typically an object or a variable that references graphical objects (among other uses). Using "fred" as a string for a handle is not appropriate for directly referencing graphical objects. Instead, the handle should be the output of a graphical function, like plot3, which returns a handle to the plotted object.
Do I need to define a handle reference?
You need a handle reference when you want to update and modify a plot without recreating it from scratch.
How do I call drawPlaneBody?
To call drawPlaneBody, provide it with the necessary parameters, including the initial position, orientation, and an empty or a valid graphical handle. If you're calling it for the first time, you would need to pass an empty handle and then use the returned handle for subsequent updates.
Here is how you can modify your code to draw a plane.
% Initial parameters
pn = 20;
pe = 0;
pd = 0;
phi = 0;
theta = 0;
psi = 0;
handle = []; % Initialize as empty
% Draw the plane for the first time and get the handle
handle = drawPlaneBody(pn, pe, pd, phi, theta, psi, handle);
function handle = drawPlaneBody(pn, pe, pd, phi, theta, psi, handle)
% Define points on plane in local NED coordinates
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');
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
% Define the airplanepoints function
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 2; % point17
-15.5 0 0; % point15
-18 0 0; % point16
];
end
function XYZ = rotate(XYZ, phi, theta, psi)
% Define rotation matrices
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 (transposing for correct dimensions)
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
If you wish to show the movement of the plane, run a loop and update pn, pe, pd, phi, theta, psi.Please refer to below code snippet to show movement of the plane.
% % Initial parameters
% pn = 20; pe = 0; pd = -5; % Starting position
% phi = 0; theta = 0; psi = 0; % Initial orientation angles in radians
% handle = []; % Initialize as empty
%
% % Simulation parameters
% simLength = 100; % Number of simulation steps
% dpn = -0.2; % Change in position along N (to simulate forward movement)
% dpsi = 0.05; % Change in yaw angle (to simulate turning)
%
% % Draw and update the plane's position and orientation
% for k = 1:simLength
% % Update position and orientation
% 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
Please refer to the following code snippet to learn more about figure handles.
Hope this helps
  2 comentarios
DJ V
DJ V el 31 de Mayo de 2024
Thank you! If this works, I'll be happy!
DJ V
DJ V el 31 de Mayo de 2024
I do have a few questions on what you did. Why, for example, did you do the following with the single quote mark?
XYZ = R*XYZ'
XYZ=XYZ';
Aren't you setting XYZ equal to R*XYZ'
then setting it equal to:
XYZ'?

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by