Nhat

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 14

function varargout = Bangdieukhien(varargin)

% BANGDIEUKHIEN MATLAB code for Bangdieukhien.fig


% BANGDIEUKHIEN, by itself, creates a new BANGDIEUKHIEN or raises the existing
% singleton*.
%
% H = BANGDIEUKHIEN returns the handle to a new BANGDIEUKHIEN or the handle to
% the existing singleton*.
%
% BANGDIEUKHIEN('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in BANGDIEUKHIEN.M with the given input arguments.
%
% BANGDIEUKHIEN('Property','Value',...) creates a new BANGDIEUKHIEN or raises
the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Bangdieukhien_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Bangdieukhien_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Bangdieukhien

% Last Modified by GUIDE v2.5 04-Sep-2023 21:46:11

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Bangdieukhien_OpeningFcn, ...
'gui_OutputFcn', @Bangdieukhien_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before Bangdieukhien is made visible.


function Bangdieukhien_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Bangdieukhien (see VARARGIN)

% Choose default command line output for Bangdieukhien


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

% UIWAIT makes Bangdieukhien wait for user response (see UIRESUME)


% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = Bangdieukhien_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure


varargout{1} = handles.output;

% --- Executes on button press in pushbutton1.


function pushbutton1_Callback(hObject, eventdata, handles)
ModelName = 'robot_4dof';

% Opens the Simulink model


open_system(ModelName);

set_param(ModelName,'BlockReduction','off');
set_param(ModelName,'StopTime','inf');
set_param(ModelName,'simulationMode','normal');

set_param(ModelName,'StartFcn','1');

set_param(ModelName, 'SimulationCommand', 'start');


% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --- Executes on slider movement.


function slider1_Callback(hObject, eventdata, handles)
ModelName = 'robot_4dof';

% l1 = 130;
l2 = 350;
l3 = 295;
l4 = 167;
%get the angle
theta1=get(handles.slider1,'value');
set(handles.edit7,'string',num2str(theta1));
theta2=get(handles.slider2,'value');
set(handles.edit8,'string',num2str(theta2));
theta3=get(handles.slider3,'value');
set(handles.edit9,'string',num2str(theta3));
theta4=get(handles.slider4,'value');
set(handles.edit10,'string',num2str(theta4));

%xoay canh tay ben simulink


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));

T1 = [ cosd(theta1) 0 sind(theta1) 0;
sind(theta1) 0 -cosd(theta1) 0;
0 1 0 130;
0 0 0 1];

T2 = [ cosd(theta2) -sind(theta2) 0 l2*cosd(theta2);


sind(theta2) cosd(theta2) 0 l2*sind(theta2);
0 0 1 0;
0 0 0 1];

T3 = [ cosd(theta3) -sind(theta3) 0 l3*cosd(theta3);


sind(theta3) cosd(theta3) 0 l3*sind(theta3);
0 0 1 0;
0 0 0 1];

T4 = [ cosd(theta4) -sind(theta4) 0 l4*cosd(theta4);


sind(theta4) cosd(theta4) 0 l4*sind(theta4);
0 0 1 0;
0 0 0 1];
T = T1*T2*T3*T4;

px=T(1,4);
py=T(2,4);
pz=T(3,4);
set(handles.edit4,'string',num2str(px));
set(handles.edit5,'string',num2str(py));
set(handles.edit6,'string',num2str(pz));
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider


% get(hObject,'Min') and get(hObject,'Max') to determine range of slider

% --- Executes during object creation, after setting all properties.


function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.


if isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

function edit1_Callback(hObject, eventdata, handles)


% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text


% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in pushbutton2.


function pushbutton2_Callback(hObject, eventdata, handles)
close;
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --- Executes on slider movement.


function slider2_Callback(hObject, eventdata, handles)
ModelName = 'robot_4dof';

% l1 = 130;
l2 = 350;
l3 = 295;
l4 = 167;
%get the angle
theta1=get(handles.slider1,'value');
set(handles.edit7,'string',num2str(theta1));
theta2=get(handles.slider2,'value');
set(handles.edit8,'string',num2str(theta2));
theta3=get(handles.slider3,'value');
set(handles.edit9,'string',num2str(theta3));
theta4=get(handles.slider4,'value');
set(handles.edit10,'string',num2str(theta4));

%xoay canh tay ben simulink


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));

T1 = [ cosd(theta1) 0 sind(theta1) 0;
sind(theta1) 0 -cosd(theta1) 0;
0 1 0 130;
0 0 0 1];

T2 = [ cosd(theta2) -sind(theta2) 0 l2*cosd(theta2);


sind(theta2) cosd(theta2) 0 l2*sind(theta2);
0 0 1 0;
0 0 0 1];

T3 = [ cosd(theta3) -sind(theta3) 0 l3*cosd(theta3);


sind(theta3) cosd(theta3) 0 l3*sind(theta3);
0 0 1 0;
0 0 0 1];

T4 = [ cosd(theta4) -sind(theta4) 0 l4*cosd(theta4);


sind(theta4) cosd(theta4) 0 l4*sind(theta4);
0 0 1 0;
0 0 0 1];
T = T1*T2*T3*T4;

px=T(1,4);
py=T(2,4);
pz=T(3,4);
set(handles.edit4,'string',num2str(px));
set(handles.edit5,'string',num2str(py));
set(handles.edit6,'string',num2str(pz));
% hObject handle to slider2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider


% get(hObject,'Min') and get(hObject,'Max') to determine range of slider

% --- Executes during object creation, after setting all properties.


function slider2_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.


if isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

function edit2_Callback(hObject, eventdata, handles)


% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text


% str2double(get(hObject,'String')) returns contents of edit2 as a double

% --- Executes during object creation, after setting all properties.


function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on slider movement.
function slider3_Callback(hObject, eventdata, handles)
ModelName = 'robot_4dof';

% l1 = 130;
l2 = 350;
l3 = 295;
l4 = 167;
%get the angle
theta1=get(handles.slider1,'value');
set(handles.edit7,'string',num2str(theta1));
theta2=get(handles.slider2,'value');
set(handles.edit8,'string',num2str(theta2));
theta3=get(handles.slider3,'value');
set(handles.edit9,'string',num2str(theta3));
theta4=get(handles.slider4,'value');
set(handles.edit10,'string',num2str(theta4));

%xoay canh tay ben simulink


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));

T1 = [ cosd(theta1) 0 sind(theta1) 0;
sind(theta1) 0 -cosd(theta1) 0;
0 1 0 130;
0 0 0 1];

T2 = [ cosd(theta2) -sind(theta2) 0 l2*cosd(theta2);


sind(theta2) cosd(theta2) 0 l2*sind(theta2);
0 0 1 0;
0 0 0 1];

T3 = [ cosd(theta3) -sind(theta3) 0 l3*cosd(theta3);


sind(theta3) cosd(theta3) 0 l3*sind(theta3);
0 0 1 0;
0 0 0 1];

T4 = [ cosd(theta4) -sind(theta4) 0 l4*cosd(theta4);


sind(theta4) cosd(theta4) 0 l4*sind(theta4);
0 0 1 0;
0 0 0 1];
T = T1*T2*T3*T4;

px=T(1,4);
py=T(2,4);
pz=T(3,4);
set(handles.edit4,'string',num2str(px));
set(handles.edit5,'string',num2str(py));
set(handles.edit6,'string',num2str(pz));
% hObject handle to slider3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider


% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties.
function slider3_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.


if isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

function edit3_Callback(hObject, eventdata, handles)


% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text


% str2double(get(hObject,'String')) returns contents of edit3 as a double

% --- Executes during object creation, after setting all properties.


function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

% --- Executes on slider movement.


function slider4_Callback(hObject, eventdata, handles)
ModelName = 'robot_4dof';

% l1 = 130;
l2 = 350;
l3 = 295;
l4 = 167;
%get the angle
theta1=get(handles.slider1,'value');
set(handles.edit7,'string',num2str(theta1));
theta2=get(handles.slider2,'value');
set(handles.edit8,'string',num2str(theta2));
theta3=get(handles.slider3,'value');
set(handles.edit9,'string',num2str(theta3));
theta4=get(handles.slider4,'value');
set(handles.edit10,'string',num2str(theta4));

%xoay canh tay ben simulink


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));

T1 = [ cosd(theta1) 0 sind(theta1) 0;
sind(theta1) 0 -cosd(theta1) 0;
0 1 0 130;
0 0 0 1];

T2 = [ cosd(theta2) -sind(theta2) 0 l2*cosd(theta2);


sind(theta2) cosd(theta2) 0 l2*sind(theta2);
0 0 1 0;
0 0 0 1];

T3 = [ cosd(theta3) -sind(theta3) 0 l3*cosd(theta3);


sind(theta3) cosd(theta3) 0 l3*sind(theta3);
0 0 1 0;
0 0 0 1];

T4 = [ cosd(theta4) -sind(theta4) 0 l4*cosd(theta4);


sind(theta4) cosd(theta4) 0 l4*sind(theta4);
0 0 1 0;
0 0 0 1];
T = T1*T2*T3*T4;

px=T(1,4);
py=T(2,4);
pz=T(3,4);
set(handles.edit4,'string',num2str(px));
set(handles.edit5,'string',num2str(py));
set(handles.edit6,'string',num2str(pz));
% hObject handle to slider4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider


% get(hObject,'Min') and get(hObject,'Max') to determine range of slider

% --- Executes during object creation, after setting all properties.


function slider4_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.


if isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

function edit4_Callback(hObject, eventdata, handles)


% hObject handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4 as text


% str2double(get(hObject,'String')) returns contents of edit4 as a double
% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

% --- Executes on slider movement.


function slider5_Callback(hObject, eventdata, handles)
px=get(handles.slider5,'value');
set(handles.edit1,'string',num2str(px));
% hObject handle to slider5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider


% get(hObject,'Min') and get(hObject,'Max') to determine range of slider

% --- Executes during object creation, after setting all properties.


function slider5_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.


if isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

% --- Executes on slider movement.


function slider6_Callback(hObject, eventdata, handles)
py=get(handles.slider6,'value');
set(handles.edit2,'string',num2str(py));
% hObject handle to slider6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider


% get(hObject,'Min') and get(hObject,'Max') to determine range of slider

% --- Executes during object creation, after setting all properties.


function slider6_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

% --- Executes on slider movement.


function slider7_Callback(hObject, eventdata, handles)
pz=get(handles.slider7,'value');
set(handles.edit3,'string',num2str(pz));
% hObject handle to slider7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider


% get(hObject,'Min') and get(hObject,'Max') to determine range of slider

% --- Executes during object creation, after setting all properties.


function slider7_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.


if isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

function edit5_Callback(hObject, eventdata, handles)


% hObject handle to edit5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit5 as text


% str2double(get(hObject,'String')) returns contents of edit5 as a double

% --- Executes during object creation, after setting all properties.


function edit5_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function edit6_Callback(hObject, eventdata, handles)


% hObject handle to edit6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit6 as text


% str2double(get(hObject,'String')) returns contents of edit6 as a double

% --- Executes during object creation, after setting all properties.


function edit6_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function edit7_Callback(hObject, eventdata, handles)


% hObject handle to edit7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit7 as text


% str2double(get(hObject,'String')) returns contents of edit7 as a double

% --- Executes during object creation, after setting all properties.


function edit7_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in pushbutton3.


function pushbutton3_Callback(hObject, eventdata, handles)
ModelName = 'robot_4dof';
% a1 = 130;
a2 = 350;
a3 = 295;
a4 = 167;

theta1 = 0;
theta2 = 0;
theta3 = 0;
theta4 = 0;

%xoay canh tay ben simulink


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));

T1 = [ cosd(theta1) 0 sind(theta1) 0;
sind(theta1) 0 -cosd(theta1) 0;
0 1 0 130;
0 0 0 1];

T2 = [ cosd(theta2) -sind(theta2) 0 a2*cosd(theta2);


sind(theta2) cosd(theta2) 0 a2*sind(theta2);
0 0 1 0;
0 0 0 1];

T3 = [ cosd(theta3) -sind(theta3) 0 a3*cosd(theta3);


sind(theta3) cosd(theta3) 0 a3*sind(theta3);
0 0 1 0;
0 0 0 1];

T4 = [ cosd(theta4) -sind(theta4) 0 a4*cosd(theta4);


sind(theta4) cosd(theta4) 0 a4*sind(theta4);
0 0 1 0;
0 0 0 1];
T = T1*T2*T3*T4;

px=T(1,4);
py=T(2,4);
pz=T(3,4);
set(handles.slider1,'value',theta1);
set(handles.slider2,'value',theta2);
set(handles.slider3,'value',theta3);
set(handles.slider4,'value',theta4);
set(handles.edit7,'string',num2str(0));
set(handles.edit8,'string',num2str(0));
set(handles.edit9,'string',num2str(0));
set(handles.edit10,'string',num2str(0));
set(handles.edit1,'string',num2str(px));
set(handles.edit2,'string',num2str(py));
set(handles.edit3,'string',num2str(pz));
set(handles.edit4,'string',num2str(px));
set(handles.edit5,'string',num2str(py));
set(handles.edit6,'string',num2str(pz));
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --- Executes on button press in pushbutton4.


function pushbutton4_Callback(hObject, eventdata, handles)
ModelName = 'robot_4dof';
global var;
px=get(handles.slider5,'value');
set(handles.edit1,'string',num2str(px));
py=get(handles.slider6,'value');
set(handles.edit2,'string',num2str(py));
pz=get(handles.slider7,'value');
set(handles.edit3,'string',num2str(pz));
set(handles.edit4,'string',num2str(px));
set(handles.edit5,'string',num2str(py));
set(handles.edit6,'string',num2str(pz));
l1 = 0;
d1 = 245;
l2 = 350;
l3 = 295;
l4 = 167;

theta1 = atan2d(py,px);
theta234= 0;
c3 = (((l1+sqrt(px^2 +py^2) - l4 *cosd(theta234))^2 +(pz - d1 + l4
*sin(theta234))^2 - (l2^2 +l3^2))/(2*l2*l3));
s3 = sqrt(1-(c3^2));
theta3 = atan2d(s3 ,c3 );
alpha =atan2d(l3*sind(theta3) , l2+l3*cosd(theta3));
beta = atan2d(pz -d1 +l4*sind(theta234), l1 + sqrt(px^2+py^2) - l4*cosd(theta234));
theta2 = alpha + beta;
theta4 = theta234 - (theta2+theta3);
guidata(hObject,handles);

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(handles.edit7,'string',num2str(theta1));

set(handles.edit8,'string',num2str(theta2));

set(handles.edit9,'string',num2str(theta3));

set(handles.edit10,'string',num2str(theta4));
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

function edit8_Callback(hObject, eventdata, handles)


% hObject handle to edit8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit8 as text


% str2double(get(hObject,'String')) returns contents of edit8 as a double

% --- Executes during object creation, after setting all properties.


function edit8_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function edit9_Callback(hObject, eventdata, handles)


% hObject handle to edit9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit9 as text


% str2double(get(hObject,'String')) returns contents of edit9 as a double

% --- Executes during object creation, after setting all properties.


function edit9_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function edit10_Callback(hObject, eventdata, handles)


% hObject handle to edit10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit10 as text


% str2double(get(hObject,'String')) returns contents of edit10 as a double

% --- Executes during object creation, after setting all properties.


function edit10_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

You might also like