clear all;
close all;
clc;
tic;
robot = rigidBodyTree('DataFormat','column','MaxNumBodies',5);
L1 = 28.45;
L2 = 75.41;
L3 = 70.02;
L4 = 87.14;
G = 100;
body = rigidBody('link1');
joint1 = rigidBodyJoint('joint1', 'revolute');
setFixedTransform(joint1,trvec2tform([0 0 G]));
joint1.JointAxis = [0 0 1];
joint1.HomePosition = pi/6;
body.Joint = joint1;
addBody(robot, body, 'base');
body = rigidBody('link2');
joint2 = rigidBodyJoint('joint2','revolute');
setFixedTransform(joint2, trvec2tform([L1,0,0]));
joint2.JointAxis = [0 1 0];
joint2.HomePosition = pi/6;
body.Joint = joint2;
addBody(robot, body, 'link1');
body = rigidBody('link3');
joint3 = rigidBodyJoint('joint3','revolute');
setFixedTransform(joint3, trvec2tform([L2,0,0]));
joint3.JointAxis = [0 1 0];
joint3.HomePosition = pi/6;
body.Joint = joint3;
addBody(robot, body, 'link2');
body = rigidBody('link4');
joint4 = rigidBodyJoint('joint4','revolute');
setFixedTransform(joint4, trvec2tform([L3,0,0]));
joint4.JointAxis = [0 1 0];
joint4.HomePosition = pi/6;
body.Joint = joint4;
addBody(robot, body, 'link3');
body = rigidBody('tool');
joint = rigidBodyJoint('fix1','fixed');
setFixedTransform(joint, trvec2tform([L4, 0, 0]));
body.Joint = joint;
addBody(robot, body, 'link4');
showdetails(robot);
xCenter = 0;
yCenter = 120;
zCenter = 0;
xRadius = 70;
yRadius = 20;
zRadius = 20;
theta = linspace(30, 150, 100);
x = (xRadius * cosd(theta) + xCenter)';
y = (yRadius * sind(theta) + yCenter)';
z = (zRadius * sind(theta) + zCenter)';
points = [x y z];
count = length(x);
plot3(x,y,z)
hold on
plot3(points(:,1),points(:,2),points(:,3),'k')
xlabel('x')
ylabel('y')
zlabel('z')
q0 = homeConfiguration(robot);
ndof = length(q0);
qs = zeros(count, ndof);
ik = inverseKinematics('RigidBodyTree', robot);
weights = [0.1, 0.1, 0, 1, 1, 1];
endEffector = 'tool';
qInitial = q0;
for i = 1:count
point = points(i,:);
qSol = ik(endEffector,trvec2tform(point),weights,qInitial);
qs(i,:) = qSol;
qInitial = qSol;
end
figure
show(robot,qs(1,:)');
ax = gca;
ax.Projection = 'orthographic';
hold on
plot3(points(:,1),points(:,2),points(:,3),'k')
view([-37.5 30])
framesPerSecond = 7;
r = rateControl(framesPerSecond);
for i = 1:count
show(robot,qs(i,:)','PreservePlot',false);
drawnow
waitfor(r);
end
toc