

function PUMA560_OpeningFcn(hObject, eventdata, handles, varargin)
clchandles.output = hObject;
%% Run Time
h=timer; % Timer
handles.T=h; % Place the timer in the global variable
%set(handles.T,'ExecutionMode','singleShot'); % The timer executes only once, set once.
set(handles.T,'ExecutionMode','fixedRate'); % Timer, executes in a loop, cyclic timing.
set(handles.T,'Period',1); % Timer, set time interval 1 second
set(handles.T,'TimerFcn',{@disptime,handles}); % Timer triggers the TimerFcn function
% Timer function (TimerFcn) triggers the user-defined function (disptime function)
start(handles.T); % Start the timer
%% Run immediately display toolbox robot model
axes(handles.plot)% Specify display area
mdl_puma560 % Import robot model
assignin('base','Robot',p560);% Assign p560 to Robot variable in the base workspace
Robot= evalin('base','Robot');% Specify the variable from the base workspace to the GUI for data sharing
view(3) % 3D view
Robot.plot([0 0 0 0 0 0]);% Display robot configuration as [0 0 0 0 0 0]
guidata(hObject, handles);
function disptime(hObject, eventdata, handles)
% Custom function to change the content of the edit control to the current time.
set(handles.Time,'String',datestr(now)); % Change the content of the edit control to the current time.

<span>axes(handles.plot)% Specify display area<span>。</span></span>
function Joint1_Callback(hObject, eventdata, handles)
ModelName = 'TestPuma560';% Get the angle
theta1=get(handles.Joint1,'value');
set(handles.edit1,'string',num2str(theta1)); set_param([ModelName '/Slider Gain'],'Gain',num2str(theta1));
theta2=get(handles.Joint2,'value');set(handles.edit2,'string',num2str(theta2));
theta3=get(handles.Joint3,'value');set(handles.edit3,'string',num2str(theta3));
theta4=get(handles.Joint4,'value');set(handles.edit4,'string',num2str(theta4));
theta5=get(handles.Joint5,'value');set(handles.edit5,'string',num2str(theta5));
theta6=get(handles.Joint6,'value');set(handles.edit6,'string',num2str(theta6));% Data transmission to Simulink
set_param([ModelName '/Slider Gain1'],'Gain',num2str(theta2));
set_param([ModelName '/Slider Gain2'],'Gain',num2str(theta3));
set_param([ModelName '/Slider Gain3'],'Gain',num2str(theta4));
set_param([ModelName '/Slider Gain5'],'Gain',num2str(theta5));
set_param([ModelName '/Slider Gain6'],'Gain',num2str(theta6));% Forward kinematics calculation
rpy=zeros(1,3); a2 = 0.4318; a3 = 0.0203; d3 = 0.1501; d4 = 0.4318;
t1=theta1;t2=theta2; t3=theta3; t4=theta4; t5=theta5; t6=theta6; T_01 = tmat(0, 0, 0, t1);
T_12 = tmat(90, 0, 0, t2);
T_23 = tmat(0, a2, d3, t3);
T_34 = tmat(-90, a3, d4, t4);
T_45 = tmat(90, 0, 0, t5);
T_56 = tmat(-90, 0, 0, t6); % Each link frame to base frame transformation T_02 = T_01*T_12;
T_03 = T_02*T_23;
T_04 = T_03*T_34;
T_05 = T_04*T_45;
T_06 = T_05*T_56;
X=T_06(1,4);Y=T_06(2,4);Z=T_06(3,4);%position
R=T_06;if abs(abs(R(1,3)) - 1) < eps % when |R13| == 1 % singularity
rpy(1) = 0; % roll is zero
if R(1,3) > 0
rpy(3) = atan2( R(3,2), R(2,2)); % R+Y
else
rpy(3) = -atan2( R(2,1), R(3,1)); % R-Y
end
rpy(2) = asin(R(1,3));
else rpy(1) = -atan2(R(1,2), R(1,1));
rpy(3) = -atan2(R(2,3), R(3,3));
rpy(2) = atan(R(1,3)*cos(rpy(1))/R(1,1)); end
RPY=rpy*180/pi;
Rall=round(RPY(1));Pitch=round(RPY(2));Yaw=round(RPY(3));
set(handles.X,'string',num2str(X));
set(handles.Y,'string',num2str(Y));
set(handles.Z,'string',num2str(Z));
set(handles.R,'string',num2str(Rall));
set(handles.P,'string',num2str(Pitch));
set(handles.Yr,'string',num2str(Yaw));
q=[theta1 theta2 theta3 theta4 theta5 theta6]; Robot= evalin('base','Robot'); handles.plot;Robot.plot(q*pi/180);% Robot.plot3d(q*pi/180, 'view',[-138 8]); T=Robot.fkine(q*pi/180);hold on
plot3(T.t(1,1),T.t(2,1),T.t(3,1),'.','LineWidth',3,'color','r');
function T = tmat(alpha, a, d, theta)% Improved DH method, coordinate transformation process as seen in Craig
alpha = alpha*pi/180; %Note: alpha is in radians.
theta = theta*pi/180; %Note: theta is in radians.
c = cos(theta);s = sin(theta);ca = cos(alpha);sa = sin(alpha);
T = [c -s 0 a; s*ca c*ca -sa -sa*d; s*sa c*sa ca ca*d; 0 0 0 1];

function pushbutton3_Callback(hObject, eventdata, handles)
Robot= evalin('base','Robot');
theta1=get(handles.Joint1,'value');
theta2=get(handles.Joint2,'value');
theta3=get(handles.Joint3,'value');
theta4=get(handles.Joint4,'value');
theta5=get(handles.Joint5,'value');
theta6=get(handles.Joint6,'value');
q0=[theta1 theta2 theta3 theta4 theta5 theta6];
T0=Robot.fkine(q0*pi/180);
R=[T0.n T0.o T0.a];% Directly call the forward kinematics function
numMats = size(R,3);
H = zeros(4,4,numMats,'like',R);
H(1:3,1:3,:) = R;
H(4,4,:) = ones(1,1,numMats,'like',R);
H(1:3,4)=T0.t;% SE3 xyzMovement To Homogeneous matrix
Hinit=H;
offset=str2double(get(handles.MoveAccuracy,'string'));% Movement step length, precision
H(1:1 ,4:4)=Hinit(13)+offset;% Take the current end as a reference for end movement
%Hinit(13) is the number in the 13th position of the matrix, which is the X coordinate in the homogeneous matrix.
Hmove=H;
Q=Robot.ikine(Hmove);% Call the inverse kinematics function to obtain joint values
handles.plot;Robot.plot(Q);% Display end movement
theta1=Q(1)*180/pi;
theta2=Q(2)*180/pi;
theta3=Q(3)*180/pi;
theta4=Q(4)*180/pi;
theta5=Q(5)*180/pi;
theta6=Q(6)*180/pi;
% Set slider movement and corresponding angle display
set(handles.Joint1,'value',theta1);
set(handles.Joint2,'value',theta2);
set(handles.Joint3,'value',theta3);
set(handles.Joint4,'value',theta4);
set(handles.Joint5,'value',theta5);
set(handles.Joint6,'value',theta6);
set(handles.edit1,'string',num2str(theta1));
set(handles.edit2,'string',num2str(theta2));
set(handles.edit3,'string',num2str(theta3));
set(handles.edit4,'string',num2str(theta4));
set(handles.edit5,'string',num2str(theta5));
set(handles.edit6,'string',num2str(theta6));
% Operation for Simulink
ModelName = 'TestPuma560';
set_param([ModelName '/Slider Gain'],'Gain',num2str(theta1));
set_param([ModelName '/Slider Gain1'],'Gain',num2str(theta2));
set_param([ModelName '/Slider Gain2'],'Gain',num2str(theta3));
set_param([ModelName '/Slider Gain3'],'Gain',num2str(theta4));
set_param([ModelName '/Slider Gain5'],'Gain',num2str(theta5));
set_param([ModelName '/Slider Gain6'],'Gain',num2str(theta6));
% Display end movement trajectory, X is red
T=Robot.fkine(Q);
hold on
plot3(T.t(1,1),T.t(2,1),T.t(3,1),'.','LineWidth',3,'color','r');
X=T.t(1,1);
Y=T.t(2,1);
Z=T.t(3,1);
set(handles.X,'string',num2str(X));
set(handles.Y,'string',num2str(Y));
set(handles.Z,'string',num2str(Z));










Benefits
Please reply “Member Exchange” in the background of the GuYueJu public account for those who participated in the GuYueJu discount event last week to receive 3 months of GuYue Academy membership.
