Robotics Lab Lecture #3: Purpose of This Lecture
Robotics Lab Lecture #3: Purpose of This Lecture
Lecture #3
- Learn how to use this code in order to make a good software that will eventually be used to
animate a robot-like form.
Programs Practice:
Part 1: Programs
The following function is a simple function used to draw and control the cylinder appearance
and its position.
for i = 1:m
cylin =R*[x(i,:); y(i,:); z(i,:); ones(1,face+1)];
x1 = [x1; cylin(1,:)];
y1 = [y1; cylin(2,:)];
z1 = [z1; cylin(3,:)];
end
surf(x1,y1,z1,'FaceColor','r','Facealpha',0.6, ...
'EdgeColor','black','AmbientStrength',0.4);
end
……………………………
Notes :
1- The following experiments will be built upon this function.
2- Most of the code’s logic will be explained later.
1
Program number 1:
function exp3()
rad=[0.6 0.2 0.4 0.3];
face=10;
scale=[0.4 0.4 0.4];
hold on;
R=eye(4);
drawingCy( R,rad,face,scale);
R=[1 0 0 0;0 cosd(90) -sind(90) 0;
0 sind(90) cosd(90) 0;0 0 0 1];
drawingCy( R,rad,face,scale);
R=[1 0 0 0.5;0 1 0 0.5;0 0 1 0.5;0 0 0 1];
drawingCy( R,rad,face,scale);
axis([-0.5 1 -0.5 1 -0.5 1]),grid;
xlabel('X');
ylabel('Y');
zlabel('Z');
end
Testing program 1:
clear; clc;
exp3(); % Or exp4(); when testing exp4()…
Program number 2:
function exp4()
R=[1 0 0 0;
0 cosd(90) -sind(90) 0.2;
0 sind(90) cosd(90) 0;
0 0 0 1];
%R=[1 0 0 0;0 1 0 0;0 0 1 0; 0 0 0 1];
rad=[0 0.2 0.3 0.3 0.3 0.3 0.3 0.2 0];
face=30;
scale=[0.4 0.4 0.4];
hold on;
drawingCy( R,rad,face,scale);
axis([-0.8 0.8 -0.8 0.8 0 1.2]),grid;
xlabel('X');
ylabel('Y');
zlabel('Z');
end
2
drawing the robot in MATLAB
This phase consists of 3 stages:
The principles of drawing any 3D object easy to implement in MATLAB. The function used
to draw the robotic arm is (cylinder).
[X,Y,Z] = cylinder(r,n)
Cylinder generates x-, y-, and z-coordinates of a unit cylinder. You can draw the cylindrical
object using surf or mesh, or draw it immediately by not providing output arguments.
[X,Y,Z] = cylinder(r,n) returns the x-, y-, and z-coordinates of a cylinder based on the profile
curve defined by vector r. The cylinder has n equally spaced points around its circumference.
You can do that by scaling the input ([x,y,z]T ) that’s used to draw the arm, then multiplying
the resulting input with a rotational matrix that’s created to rotate the 3-D object into the right
orientation based on their frame and then move the 3-D object to the right position.
After the second stage, you can manipulate the 3-D object in the same way, by multiplying
the resulting input [x y z]T with after adjusting the input by making a 4th row in the
bottom of the [x y z]T matrix and fill it with ones .
The result can be easily printed in a 3D plotting tool in MATLAB using surf command.
The following example shows how the second joint is printed as an output of this procedure:
function exp6()
d1=0.6;
d3=1;%longest destance!
A10=DHhomomatrix(90,d1,90,0);
A21=DHhomomatrix(90,0,90,0);
H10=A10;
H20=A10*A21;
R=[1 0 0 0;0 cosd(90) -sind(90) 0.2;
0 sind(90) cosd(90) 0;0 0 0 1];
[x1,y1,z1]=cylinder([0 0.2 0.3 0.3 0.3 0.3 0.3 0.2 0],30);
3
x1=x1*0.4; y1=y1*0.4; z1=z1*0.4;
x2 = []; y2 = []; z2 = [];
for i = 1:9
cylin =H20*R*[x1(i,:); y1(i,:); z1(i,:); ones(1,31)];
x2 = [x2; cylin(1,:)];
y2 = [y2; cylin(2,:)];
z2 = [z2; cylin(3,:)];
end
surf(x2,y2,z2,'FaceColor','c',...
'EdgeColor','r','AmbientStrength',0.4),
grid, axis([-0.8 0.8 -0.8 0.8 0 1.2]),
daspect([1 1 1]), grid;
hold on;
end
function H=DHhomomatrix(thetazd,jointoff,thetaxd,linklen)
cz=cos(thetazd);cx=cosd(thetaxd);
sz=sin(thetazd);sx=sind(thetaxd);
d=jointoff;a=linklen;
H =[cz,-cx*sz,sx*sz,a*cz;sz,cx*cz,-cz*sx,a*sz;
0,sx,cx,d;0,0,0,1];
end